Add explicit MIT license and copyright
[deliverable/exatracer.git] / src / lttng-ust-exatracer.cpp
CommitLineData
c38914e1
MJ
1// SPDX-FileCopyrightText: 2024 Advanced Micro Devices, Inc. All rights reserved.
2// SPDX-FileCopyrightText: 2024 EfficiOS, Inc.
3//
4// SPDX-License-Identifier: MIT
74c0b9b3
OD
5
6#include <cinttypes>
7
8#include <hip/hip_runtime.h>
9#include <hip/hip_gl_interop.h>
10
11/*
12 * Do not re-order! hip_api_trace.h header referenced some R0000 types which
13 * are only defined in the hip_deprecated.h header.
14 */
15#include <hip/hip_deprecated.h>
16#include <hip/amd_detail/hip_api_trace.hpp>
17
18/*
19 * Without AMD_INTERNAL_BUILD, hsa_api_trace.h tries to include
20 * "inc/*", which is not installed.
21 */
22#define AMD_INTERNAL_BUILD
23#include <hsa/hsa.h>
24#include <hsa/hsa_api_trace.h>
25
26/*
27 * Rocprofiler integration.
28 */
29#include <rocprofiler-sdk/registration.h>
30#include <rocprofiler-sdk/rocprofiler.h>
31#include <rocprofiler-sdk-roctx/api_trace.h>
32
33/*
34 * This is the table with LTTng tracepoints for HIP.
35 */
36static struct HipDispatchTable lttng_hip_table;
37
38/*
39 * This is an internal copy of the orignal table used by the LTTng wrappers to
40 * call the next function for HIP.
41 */
42static struct HipDispatchTable next_hip_table;
43
44/* Auto-generated by gen-hip-wrappers. */
45#include "lttng-ust-hip-wrappers.cpp"
46
47
48/*
49 * This is the table with LTTng tracepoints for HSA.
50 */
51static struct CoreApiTable lttng_hsa_core_table;
52
53/*
54 * This is an internal copy of the orignal table used by the LTTng wrappers to
55 * call the next function for HSA.
56 */
57static struct CoreApiTable next_hsa_core_table;
58
59/* Auto-generated by gen-hsa-wrappers. */
60#include "lttng-ust-hsa-wrappers.cpp"
61
62
63/*
64 * This is the table with LTTng tracepoints for roctx.
65 */
66static roctxCoreApiTable_t lttng_roctx_core_table;
67
68/*
69 * This is an internal copy of the orignal table used by the LTTng wrappers to
70 * call the next function for roctx.
71 */
72static roctxCoreApiTable_t next_roctx_core_table;
73
74/* LTTng events for roctx. */
75#include "lttng-roctx.c"
76
77#define die(FMT, ...) \
78 do { \
79 fprintf(stderr, FMT "\n" __VA_OPT__(,) __VA_ARGS__); \
80 exit(EXIT_FAILURE); \
81 } while(0)
82
83#define error(FMT, ...) \
84 fprintf(stderr, FMT "\n" __VA_OPT__(,) __VA_ARGS__)
85
86static void
87register_hip_table(uint64_t lib_instance, uint64_t num_tables, void **tables)
88{
89 /* Add instrumentation only to first library instance. */
90 if (0 != lib_instance) {
91 return;
92 }
93
94 /* Ensure that there is at least one table. */
95 if (num_tables < 1) {
96 return;
97 }
c75d6f3f 98
74c0b9b3
OD
99 /*
100 * This could be done at compile time in some way if C++ could support
101 * designated initializers.
102 */
103 lttng_hip_install_wrappers();
104
105 auto original_hip_table = static_cast<HipDispatchTable*>(tables[0]);
106
107 /* Swap tables. */
108 if (original_hip_table) {
109 auto size = std::min(sizeof(next_hip_table), original_hip_table->size);
110 memcpy(&next_hip_table, original_hip_table, size);
111 memcpy(original_hip_table, &lttng_hip_table, size);
112 }
113}
114
115static void
116register_hsa_core_table(uint64_t lib_instance, uint64_t num_tables, void **tables)
117{
118 /* Add instrumentation only to first library instance. */
119 if (0 != lib_instance) {
120 return;
121 }
122
123 /* Ensure that there is at least one table. */
124 if (num_tables < 1) {
125 return;
126 }
c75d6f3f 127
74c0b9b3
OD
128 /*
129 * This could be done at compile time in some way if C++ could support
130 * designated initializers.
131 */
132 lttng_hsa_install_wrappers();
133
134 auto original_hsa_table = static_cast<HsaApiTable*>(tables[0]);
135
136 /* Swap tables. */
137 if (original_hsa_table) {
138 auto core = original_hsa_table->core_;
139 auto size = sizeof(*core);
140 memcpy(&next_hsa_core_table, core, size);
141 memcpy(core, &lttng_hsa_core_table, size);
142 }
143}
144
145static void
146register_roctx_core_table(uint64_t lib_instance, uint64_t num_tables, void **tables)
147{
148 /* Add instrumentation only to first library instance. */
149 if (0 != lib_instance) {
150 return;
151 }
152
153 /* Ensure that there is at least one table. */
154 if (num_tables < 1) {
155 return;
156 }
157
158 /*
159 * This could be done at compile time in some way if C++ could support
160 * designated initializers.
161 */
162 lttng_roctx_install_wrappers();
c75d6f3f 163
74c0b9b3
OD
164 auto original_roctx_core_table = static_cast<roctxCoreApiTable_t*>(tables[0]);
165
166 /* Swap tables. */
167 if (original_roctx_core_table) {
168 auto size = std::min(original_roctx_core_table->size, sizeof(roctxCoreApiTable_t));
169 memcpy(&next_roctx_core_table, original_roctx_core_table, size);
170 memcpy(original_roctx_core_table, &lttng_roctx_core_table, size);
171 }
172}
173
174static void
175api_registration_callback(rocprofiler_intercept_table_t type,
176 uint64_t lib_version,
177 uint64_t lib_instance,
178 void** tables,
179 uint64_t num_tables,
180 void* user_data)
181{
182 (void) user_data;
183 (void) lib_version;
184
185 /*
c75d6f3f
OD
186 * We only want HIP, HSA or ROC-TX tables. If we get something else, there
187 * is a bug somewhere.
74c0b9b3
OD
188 */
189 switch (type) {
190 case ROCPROFILER_HIP_RUNTIME_TABLE:
191 register_hip_table(lib_instance, num_tables, tables);
192 break;
193 case ROCPROFILER_HSA_TABLE:
194 register_hsa_core_table(lib_instance, num_tables, tables);
195 break;
196 case ROCPROFILER_MARKER_CORE_TABLE:
197 register_roctx_core_table(lib_instance, num_tables, tables);
198 break;
199 defualt:
200 die("unexpected library type: %d", type);
201 }
202}
203
204extern "C"
205__attribute__((visibility("default")))
206rocprofiler_tool_configure_result_t *rocprofiler_configure(uint32_t version,
207 const char *runtime_version,
208 uint32_t priority,
209 rocprofiler_client_id_t *id)
210{
211 (void) priority;
212 (void) runtime_version;
213 (void) version;
214
215 /*
216 * Maybe Exatracer instead?
217 */
218 id->name = "LTTng-UST";
219
220 if (ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED ==
221 rocprofiler_at_intercept_table_registration(api_registration_callback,
222 ROCPROFILER_HIP_RUNTIME_TABLE |
223 ROCPROFILER_HSA_TABLE |
224 ROCPROFILER_MARKER_CORE_TABLE,
225 nullptr)) {
c75d6f3f 226 die("Trying to register API interception : " "NOT IMPLEMENTED");
74c0b9b3
OD
227 }
228
229 static auto cfg = rocprofiler_tool_configure_result_t {
230 sizeof(rocprofiler_tool_configure_result_t),
231 nullptr,
232 nullptr,
233 nullptr
234 };
235
236 return &cfg;
237}
This page took 0.055055 seconds and 4 git commands to generate.