2022-04-19 07:18:05 -07:00
|
|
|
/* Copyright (c) 2018-2022 Advanced Micro Devices, Inc.
|
|
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
THE SOFTWARE. */
|
2019-08-30 08:53:34 -05:00
|
|
|
|
2022-05-17 20:59:56 -07:00
|
|
|
#include "roctx.h"
|
|
|
|
|
#include "roctracer_roctx.h"
|
|
|
|
|
#include "ext/prof_protocol.h"
|
2019-08-30 08:53:34 -05:00
|
|
|
|
2022-09-02 12:40:15 -07:00
|
|
|
#include <atomic>
|
|
|
|
|
#include <cassert>
|
2022-05-20 20:19:32 -07:00
|
|
|
|
2022-05-06 12:29:50 -07:00
|
|
|
namespace {
|
2022-04-18 16:13:08 -07:00
|
|
|
|
2022-09-02 12:40:15 -07:00
|
|
|
std::atomic<int (*)(activity_domain_t domain, uint32_t operation_id, void* data)> report_activity;
|
|
|
|
|
thread_local int nested_range_level{0};
|
|
|
|
|
|
|
|
|
|
void ReportActivity(roctx_api_id_t operation_id, const char* message = nullptr,
|
|
|
|
|
roctx_range_id_t id = {}) {
|
|
|
|
|
auto function = report_activity.load(std::memory_order_relaxed);
|
|
|
|
|
if (!function) return;
|
|
|
|
|
|
|
|
|
|
roctx_api_data_t api_data{};
|
|
|
|
|
switch (operation_id) {
|
|
|
|
|
case ROCTX_API_ID_roctxMarkA:
|
|
|
|
|
api_data.args.roctxMarkA.message = message;
|
|
|
|
|
break;
|
|
|
|
|
case ROCTX_API_ID_roctxRangePushA:
|
|
|
|
|
api_data.args.roctxRangePushA.message = message;
|
|
|
|
|
break;
|
|
|
|
|
case ROCTX_API_ID_roctxRangePop:
|
|
|
|
|
break;
|
|
|
|
|
case ROCTX_API_ID_roctxRangeStartA:
|
|
|
|
|
api_data.args.roctxRangeStartA.message = message;
|
|
|
|
|
api_data.args.roctxRangeStartA.id = id;
|
|
|
|
|
break;
|
|
|
|
|
case ROCTX_API_ID_roctxRangeStop:
|
|
|
|
|
api_data.args.roctxRangeStop.id = id;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
assert(!"should not reach here");
|
|
|
|
|
}
|
|
|
|
|
function(ACTIVITY_DOMAIN_ROCTX, operation_id, &api_data);
|
|
|
|
|
}
|
2022-04-18 16:13:08 -07:00
|
|
|
|
2022-05-06 12:29:50 -07:00
|
|
|
} // namespace
|
2019-08-30 08:53:34 -05:00
|
|
|
|
2022-05-23 20:58:53 -07:00
|
|
|
ROCTX_API uint32_t roctx_version_major() { return ROCTX_VERSION_MAJOR; }
|
|
|
|
|
ROCTX_API uint32_t roctx_version_minor() { return ROCTX_VERSION_MINOR; }
|
2019-08-30 08:53:34 -05:00
|
|
|
|
2022-09-02 12:40:15 -07:00
|
|
|
ROCTX_API void roctxMarkA(const char* message) { ReportActivity(ROCTX_API_ID_roctxMarkA, message); }
|
2019-08-30 08:53:34 -05:00
|
|
|
|
2022-05-23 20:58:53 -07:00
|
|
|
ROCTX_API int roctxRangePushA(const char* message) {
|
2022-09-02 12:40:15 -07:00
|
|
|
ReportActivity(ROCTX_API_ID_roctxRangePushA, message);
|
2022-05-20 20:19:32 -07:00
|
|
|
return nested_range_level++;
|
2019-08-30 08:53:34 -05:00
|
|
|
}
|
|
|
|
|
|
2022-05-23 20:58:53 -07:00
|
|
|
ROCTX_API int roctxRangePop() {
|
2022-09-02 12:40:15 -07:00
|
|
|
ReportActivity(ROCTX_API_ID_roctxRangePop);
|
2022-05-20 20:19:32 -07:00
|
|
|
if (nested_range_level == 0) return -1;
|
|
|
|
|
return --nested_range_level;
|
2019-10-21 21:18:54 -05:00
|
|
|
}
|
|
|
|
|
|
2022-05-23 20:58:53 -07:00
|
|
|
ROCTX_API roctx_range_id_t roctxRangeStartA(const char* message) {
|
2022-05-20 20:19:32 -07:00
|
|
|
static std::atomic<roctx_range_id_t> start_stop_range_id(1);
|
2022-09-02 12:40:15 -07:00
|
|
|
auto range_id = start_stop_range_id++;
|
|
|
|
|
ReportActivity(ROCTX_API_ID_roctxRangeStartA, message, range_id);
|
|
|
|
|
return range_id;
|
2020-07-29 18:28:51 -04:00
|
|
|
}
|
|
|
|
|
|
2022-09-02 12:40:15 -07:00
|
|
|
ROCTX_API void roctxRangeStop(roctx_range_id_t range_id) {
|
|
|
|
|
ReportActivity(ROCTX_API_ID_roctxRangeStop, nullptr, range_id);
|
2020-07-29 18:28:51 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-18 18:19:07 -07:00
|
|
|
extern "C" ROCTX_EXPORT void roctxRegisterTracerCallback(int (*function)(activity_domain_t domain,
|
|
|
|
|
uint32_t operation_id,
|
|
|
|
|
void* data)) {
|
|
|
|
|
report_activity.store(function, std::memory_order_relaxed);
|
2022-04-18 16:13:08 -07:00
|
|
|
}
|