Skip to content

Commit

Permalink
SWDEV-351980 - Consolidate registration tables in the roctracer
Browse files Browse the repository at this point in the history
Change-Id: I44cd1cc81cf6a529aed89ee8db1377c0aa67f0dc
  • Loading branch information
lmoriche committed Sep 9, 2022
1 parent 57867e4 commit 2673bf5
Show file tree
Hide file tree
Showing 11 changed files with 653 additions and 695 deletions.
7 changes: 6 additions & 1 deletion inc/roctracer_hip.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ inline static std::ostream& operator<<(std::ostream& out, const char& v) {

#include <roctracer.h>

enum { HIP_OP_ID_DISPATCH = 0, HIP_OP_ID_COPY = 1, HIP_OP_ID_BARRIER = 2, HIP_OP_ID_NUMBER = 3 };
typedef enum {
HIP_OP_ID_DISPATCH = 0,
HIP_OP_ID_COPY = 1,
HIP_OP_ID_BARRIER = 2,
HIP_OP_ID_NUMBER = 3
} hip_op_id_t;

#ifdef __cplusplus
extern "C" {
Expand Down
47 changes: 29 additions & 18 deletions script/hsaap.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ def __init__(self, out_h_file, hsa_dir, api_table_h, api_headers, license):
self.cpp_content += "/* Generated by " + os.path.basename(__file__) + " */\n" + license + "\n\n"

self.cpp_content += '#include <hsa/hsa_api_trace.h>\n'
self.cpp_content += '#include \"util/callback_table.h\"\n\n'
self.cpp_content += '#include <atomic>\n'
self.cpp_content += 'namespace roctracer::hsa_support::detail {\n'

Expand Down Expand Up @@ -409,40 +408,52 @@ def gen_arg_struct(self, n, name, call, struct):
def gen_callbacks(self, n, name, call, struct):
content = ''
if n == -1:
content += 'static util::CallbackTable<ACTIVITY_DOMAIN_HSA_API, HSA_API_ID_NUMBER> cb_table;\n'
content += '/* section: Static declarations */\n'
content += '\n'
if call != '-':
call_id = self.api_id[call];
ret_type = struct['ret']
content += 'static ' + ret_type + ' ' + call + '_callback(' + struct['args'] + ') {\n'
content += ' hsa_api_data_t api_data{};\n'

content += ' hsa_trace_data_t trace_data;\n'
content += ' bool enabled{false};\n'
content += '\n'
content += ' if (auto function = report_activity.load(std::memory_order_relaxed); function &&\n'
content += ' (enabled =\n'
content += ' function(ACTIVITY_DOMAIN_HSA_API, ' + call_id + ', &trace_data) == 0)) {\n'
content += ' if (trace_data.phase_enter != nullptr) {\n'

for var in struct['alst']:
item = struct['astr'][var];
if re.search(r'char\* ', item):
content += ' api_data.args.' + call + '.' + var + ' = ' + '(' + var + ' != NULL) ? strdup(' + var + ')' + ' : NULL;\n'
# FIXME: we should not strdup the char* arguments here, as the callback will not outlive the scope of this function. Instead, we
# should generate a helper function to capture the content of the arguments similar to hipApiArgsInit for HIP. We also need a
# helper to free the memory that is allocated to capture the content.
content += ' trace_data.api_data.args.' + call + '.' + var + ' = ' + '(' + var + ' != NULL) ? strdup(' + var + ')' + ' : NULL;\n'
else:
content += ' api_data.args.' + call + '.' + var + ' = ' + var + ';\n'
content += ' trace_data.api_data.args.' + call + '.' + var + ' = ' + var + ';\n'
if call == 'hsa_amd_memory_async_copy_rect' and var == 'range':
content += ' api_data.args.' + call + '.' + var + '__val = ' + '*(' + var + ');\n'
content += ' auto [ api_callback_fun, api_callback_arg ] = cb_table.Get(' + call_id + ');\n'
content += ' if (api_callback_fun) {\n'
content += ' api_data.phase = ACTIVITY_API_PHASE_ENTER;\n'
content += ' api_data.correlation_id = CorrelationIdPush();\n'
content += ' api_callback_fun(ACTIVITY_DOMAIN_HSA_API, ' + call_id + ', &api_data, api_callback_arg);\n'
content += ' trace_data.api_data.args.' + call + '.' + var + '__val = ' + '*(' + var + ');\n'

content += ' trace_data.phase_enter(' + call_id + ', &trace_data);\n'
content += ' }\n'
content += ' }\n'
content += '\n'

if ret_type != 'void':
# FIXME: we should capture the return value and store it in the api_data
content += ' ' + ret_type + ' ret ='
content += ' ' + name + '_saved_before_cb.' + call + '_fn(' + ', '.join(struct['alst']) + ');\n'
content += ' if (api_callback_fun) {\n'
if ret_type != 'void':
content += ' api_data.' + ret_type + '_retval = ret;\n'
content += ' api_data.phase = ACTIVITY_API_PHASE_EXIT;\n'
content += ' api_callback_fun(ACTIVITY_DOMAIN_HSA_API, ' + call_id + ', &api_data, api_callback_arg);\n'
content += ' CorrelationIdPop();\n'
content += ' }\n'

content += '\n'
content += ' if (enabled && trace_data.phase_exit != nullptr)\n'
content += ' trace_data.phase_exit(' + call_id + ', &trace_data);\n'

if ret_type != 'void':
content += '\n'
content += ' return ret;\n'
content += '}\n'

return content

# generate API intercepting code
Expand Down
Loading

0 comments on commit 2673bf5

Please sign in to comment.