Bring the HSA_EVT callbacks back to the roctracer
Change-Id: I26080b264d7989880ba7e9f00502cc680b2256d7
This commit is contained in:
+31
-47
@@ -18,68 +18,52 @@
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE. */
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "inc/roctracer.h"
|
||||
#include "inc/roctracer_hsa.h"
|
||||
#include <rocprofiler/rocprofiler.h>
|
||||
|
||||
#define PUBLIC_API __attribute__((visibility("default")))
|
||||
#define CONSTRUCTOR_API __attribute__((constructor))
|
||||
#define DESTRUCTOR_API __attribute__((destructor))
|
||||
|
||||
namespace {
|
||||
// Check returned HSA API status
|
||||
void check_status(roctracer_status_t status) {
|
||||
inline void CHECK(roctracer_status_t status) {
|
||||
if (status != ROCTRACER_STATUS_SUCCESS) {
|
||||
const char* error_string = roctracer_error_string();
|
||||
fprintf(stderr, "ERROR: %s\n", error_string);
|
||||
fprintf(stderr, "ERROR: %s\n", roctracer_error_string());
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
// codeobj callback
|
||||
void codeobj_callback(uint32_t domain, uint32_t cid, const void* data, void* arg) {
|
||||
void CodeObjectCallback(uint32_t domain, uint32_t cid, const void* data, void* arg) {
|
||||
const hsa_evt_data_t* evt_data = reinterpret_cast<const hsa_evt_data_t*>(data);
|
||||
const char* uri = evt_data->codeobj.uri;
|
||||
printf(
|
||||
"codeobj_callback domain(%u) cid(%u): load_base(0x%lx) load_size(0x%lx) load_delta(0x%lx) "
|
||||
"uri(\"%s\")\n",
|
||||
domain, cid, evt_data->codeobj.load_base, evt_data->codeobj.load_size,
|
||||
evt_data->codeobj.load_delta, uri);
|
||||
free((void*)uri);
|
||||
fflush(stdout);
|
||||
fprintf(stdout,
|
||||
"codeobj_callback domain(%u) cid(%u): load_base(0x%lx) load_size(0x%lx) "
|
||||
"load_delta(0x%lx) uri(\"%s\") unload(%d)\n",
|
||||
domain, cid, evt_data->codeobj.load_base, evt_data->codeobj.load_size,
|
||||
evt_data->codeobj.load_delta, evt_data->codeobj.uri, evt_data->codeobj.unload);
|
||||
}
|
||||
|
||||
void initialize() {
|
||||
roctracer_status_t status = roctracer_enable_op_callback(
|
||||
ACTIVITY_DOMAIN_HSA_EVT, HSA_EVT_ID_CODEOBJ, codeobj_callback, NULL);
|
||||
check_status(status);
|
||||
} // namespace
|
||||
|
||||
#include <hsa/hsa_api_trace.h>
|
||||
|
||||
extern "C" {
|
||||
// The HSA_AMD_TOOL_PRIORITY variable must be a constant value type initialized by the loader
|
||||
// itself, not by code during _init. 'extern const' seems to do that although that is not a
|
||||
// guarantee.
|
||||
ROCTRACER_EXPORT extern const uint32_t HSA_AMD_TOOL_PRIORITY = 1050;
|
||||
|
||||
// HSA-runtime tool on-load method
|
||||
ROCTRACER_EXPORT bool OnLoad(HsaApiTable* table, uint64_t runtime_version,
|
||||
uint64_t failed_tool_count, const char* const* failed_tool_names) {
|
||||
CHECK(roctracer_enable_op_callback(ACTIVITY_DOMAIN_HSA_EVT, HSA_EVT_ID_CODEOBJ,
|
||||
CodeObjectCallback, nullptr));
|
||||
return true;
|
||||
}
|
||||
|
||||
void cleanup() {
|
||||
roctracer_status_t status = roctracer_disable_domain_callback(ACTIVITY_DOMAIN_HSA_EVT);
|
||||
check_status(status);
|
||||
ROCTRACER_EXPORT void OnUnload() {
|
||||
CHECK(roctracer_disable_domain_callback(ACTIVITY_DOMAIN_HSA_EVT));
|
||||
}
|
||||
|
||||
// Tool constructor
|
||||
extern "C" PUBLIC_API void OnLoadToolProp(rocprofiler_settings_t* settings) {
|
||||
// Enable HSA events intercepting
|
||||
settings->hsa_intercepting = 1;
|
||||
// Initialize profiling
|
||||
initialize();
|
||||
}
|
||||
|
||||
// Tool destructor
|
||||
extern "C" PUBLIC_API void OnUnloadTool() {
|
||||
// Final resources cleanup
|
||||
cleanup();
|
||||
}
|
||||
|
||||
extern "C" CONSTRUCTOR_API void constructor() {
|
||||
printf("constructor\n");
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
extern "C" DESTRUCTOR_API void destructor() { OnUnloadTool(); }
|
||||
} // extern "C"
|
||||
@@ -1,127 +0,0 @@
|
||||
/* 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. */
|
||||
|
||||
#include <hsa/hsa.h>
|
||||
#include <hsa/hsa_api_trace.h>
|
||||
#include <hsa/hsa_ven_amd_loader.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define PUBLIC_API __attribute__((visibility("default")))
|
||||
#define CONSTRUCTOR_API __attribute__((constructor))
|
||||
#define DESTRUCTOR_API __attribute__((destructor))
|
||||
|
||||
#define HSA_RT(call) \
|
||||
do { \
|
||||
const hsa_status_t status = call; \
|
||||
if (status != HSA_STATUS_SUCCESS) { \
|
||||
printf("error \"%s\"\n", #call); \
|
||||
fflush(stdout); \
|
||||
abort(); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
// HSA API intercepting primitives
|
||||
decltype(hsa_executable_freeze)* hsa_executable_freeze_fn;
|
||||
hsa_ven_amd_loader_1_01_pfn_t loader_api_table{};
|
||||
|
||||
hsa_status_t code_object_callback(hsa_executable_t executable,
|
||||
hsa_loaded_code_object_t loaded_code_object, void* arg) {
|
||||
printf("code_object_callback\n");
|
||||
fflush(stdout);
|
||||
|
||||
uint64_t load_base = 0;
|
||||
uint64_t load_size = 0;
|
||||
uint64_t load_delta = 0;
|
||||
uint32_t uri_len = 0;
|
||||
char* uri_str = NULL;
|
||||
|
||||
HSA_RT(loader_api_table.hsa_ven_amd_loader_loaded_code_object_get_info(
|
||||
loaded_code_object, HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_INFO_LOAD_BASE, &load_base));
|
||||
HSA_RT(loader_api_table.hsa_ven_amd_loader_loaded_code_object_get_info(
|
||||
loaded_code_object, HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_INFO_LOAD_SIZE, &load_size));
|
||||
HSA_RT(loader_api_table.hsa_ven_amd_loader_loaded_code_object_get_info(
|
||||
loaded_code_object, HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_INFO_LOAD_DELTA, &load_delta));
|
||||
HSA_RT(loader_api_table.hsa_ven_amd_loader_loaded_code_object_get_info(
|
||||
loaded_code_object, HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_INFO_URI_LENGTH, &uri_len));
|
||||
|
||||
uri_str = (char*)calloc(uri_len + 1, sizeof(char));
|
||||
if (!uri_str) {
|
||||
perror("calloc");
|
||||
abort();
|
||||
}
|
||||
|
||||
HSA_RT(loader_api_table.hsa_ven_amd_loader_loaded_code_object_get_info(
|
||||
loaded_code_object, HSA_VEN_AMD_LOADER_LOADED_CODE_OBJECT_INFO_URI, uri_str));
|
||||
|
||||
printf("load_base(0x%lx)\n", load_base);
|
||||
fflush(stdout);
|
||||
printf("load_size(0x%lx)\n", load_size);
|
||||
fflush(stdout);
|
||||
printf("load_delta(0x%lx)\n", load_delta);
|
||||
fflush(stdout);
|
||||
printf("uri_len(%u)\n", uri_len);
|
||||
fflush(stdout);
|
||||
printf("uri_str(\"%s\")\n", uri_str);
|
||||
fflush(stdout);
|
||||
|
||||
free(uri_str);
|
||||
|
||||
return HSA_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
hsa_status_t hsa_executable_freeze_interceptor(hsa_executable_t executable, const char* options) {
|
||||
HSA_RT(loader_api_table.hsa_ven_amd_loader_executable_iterate_loaded_code_objects(
|
||||
executable, code_object_callback, NULL));
|
||||
HSA_RT(hsa_executable_freeze_fn(executable, options));
|
||||
return HSA_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
// HSA-runtime tool on-load method
|
||||
extern "C" PUBLIC_API bool OnLoad(HsaApiTable* table, uint64_t runtime_version,
|
||||
uint64_t failed_tool_count,
|
||||
const char* const* failed_tool_names) {
|
||||
printf("OnLoad: begin\n");
|
||||
fflush(stdout);
|
||||
// intercepting hsa_executable_freeze API
|
||||
hsa_executable_freeze_fn = table->core_->hsa_executable_freeze_fn;
|
||||
table->core_->hsa_executable_freeze_fn = hsa_executable_freeze_interceptor;
|
||||
// Fetching AMD Loader HSA extension API
|
||||
HSA_RT(hsa_system_get_major_extension_table(
|
||||
HSA_EXTENSION_AMD_LOADER, 1, sizeof(hsa_ven_amd_loader_1_01_pfn_t), &loader_api_table));
|
||||
printf("OnLoad: end\n");
|
||||
fflush(stdout);
|
||||
return true;
|
||||
}
|
||||
|
||||
extern "C" PUBLIC_API void OnUnload() {
|
||||
printf("OnUnload\n");
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
extern "C" CONSTRUCTOR_API void constructor() {
|
||||
printf("constructor\n");
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
extern "C" DESTRUCTOR_API void destructor() {
|
||||
printf("destructor\n");
|
||||
fflush(stdout);
|
||||
}
|
||||
Reference in New Issue
Block a user