1 // SPDX-FileCopyrightText: 2024 Advanced Micro Devices, Inc. All rights reserved.
2 // SPDX-FileCopyrightText: 2024 EfficiOS, Inc.
4 // SPDX-License-Identifier: MIT
8 #include <hip/hip_runtime.h>
9 #include <hip/hip_gl_interop.h>
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.
15 #include <hip/hip_deprecated.h>
16 #include <hip/amd_detail/hip_api_trace.hpp>
19 * Without AMD_INTERNAL_BUILD, hsa_api_trace.h tries to include
20 * "inc/*", which is not installed.
22 #define AMD_INTERNAL_BUILD
24 #include <hsa/hsa_api_trace.h>
27 * Rocprofiler integration.
29 #include <rocprofiler-sdk/registration.h>
30 #include <rocprofiler-sdk/rocprofiler.h>
31 #include <rocprofiler-sdk-roctx/api_trace.h>
34 * This is the table with LTTng tracepoints for HIP.
36 static struct HipDispatchTable lttng_hip_table
;
39 * This is an internal copy of the orignal table used by the LTTng wrappers to
40 * call the next function for HIP.
42 static struct HipDispatchTable next_hip_table
;
44 /* Auto-generated by gen-hip-wrappers. */
45 #include "lttng-ust-hip-wrappers.cpp"
49 * This is the table with LTTng tracepoints for HSA.
51 static struct CoreApiTable lttng_hsa_core_table
;
54 * This is an internal copy of the orignal table used by the LTTng wrappers to
55 * call the next function for HSA.
57 static struct CoreApiTable next_hsa_core_table
;
59 /* Auto-generated by gen-hsa-wrappers. */
60 #include "lttng-ust-hsa-wrappers.cpp"
64 * This is the table with LTTng tracepoints for roctx.
66 static roctxCoreApiTable_t lttng_roctx_core_table
;
69 * This is an internal copy of the orignal table used by the LTTng wrappers to
70 * call the next function for roctx.
72 static roctxCoreApiTable_t next_roctx_core_table
;
74 /* LTTng events for roctx. */
75 #include "lttng-roctx.c"
77 #define die(FMT, ...) \
79 fprintf(stderr, FMT "\n" __VA_OPT__(,) __VA_ARGS__); \
83 #define error(FMT, ...) \
84 fprintf(stderr, FMT "\n" __VA_OPT__(,) __VA_ARGS__)
87 register_hip_table(uint64_t lib_instance
, uint64_t num_tables
, void **tables
)
89 /* Add instrumentation only to first library instance. */
90 if (0 != lib_instance
) {
94 /* Ensure that there is at least one table. */
100 * This could be done at compile time in some way if C++ could support
101 * designated initializers.
103 lttng_hip_install_wrappers();
105 auto original_hip_table
= static_cast<HipDispatchTable
*>(tables
[0]);
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
, <tng_hip_table
, size
);
116 register_hsa_core_table(uint64_t lib_instance
, uint64_t num_tables
, void **tables
)
118 /* Add instrumentation only to first library instance. */
119 if (0 != lib_instance
) {
123 /* Ensure that there is at least one table. */
124 if (num_tables
< 1) {
129 * This could be done at compile time in some way if C++ could support
130 * designated initializers.
132 lttng_hsa_install_wrappers();
134 auto original_hsa_table
= static_cast<HsaApiTable
*>(tables
[0]);
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
, <tng_hsa_core_table
, size
);
146 register_roctx_core_table(uint64_t lib_instance
, uint64_t num_tables
, void **tables
)
148 /* Add instrumentation only to first library instance. */
149 if (0 != lib_instance
) {
153 /* Ensure that there is at least one table. */
154 if (num_tables
< 1) {
159 * This could be done at compile time in some way if C++ could support
160 * designated initializers.
162 lttng_roctx_install_wrappers();
164 auto original_roctx_core_table
= static_cast<roctxCoreApiTable_t
*>(tables
[0]);
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
, <tng_roctx_core_table
, size
);
175 api_registration_callback(rocprofiler_intercept_table_t type
,
176 uint64_t lib_version
,
177 uint64_t lib_instance
,
186 * We only want HIP, HSA or ROC-TX tables. If we get something else, there
187 * is a bug somewhere.
190 case ROCPROFILER_HIP_RUNTIME_TABLE
:
191 register_hip_table(lib_instance
, num_tables
, tables
);
193 case ROCPROFILER_HSA_TABLE
:
194 register_hsa_core_table(lib_instance
, num_tables
, tables
);
196 case ROCPROFILER_MARKER_CORE_TABLE
:
197 register_roctx_core_table(lib_instance
, num_tables
, tables
);
200 die("unexpected library type: %d", type
);
205 __attribute__((visibility("default")))
206 rocprofiler_tool_configure_result_t
*rocprofiler_configure(uint32_t version
,
207 const char *runtime_version
,
209 rocprofiler_client_id_t
*id
)
212 (void) runtime_version
;
216 * Maybe Exatracer instead?
218 id
->name
= "LTTng-UST";
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
,
226 die("Trying to register API interception : " "NOT IMPLEMENTED");
229 static auto cfg
= rocprofiler_tool_configure_result_t
{
230 sizeof(rocprofiler_tool_configure_result_t
),