rocprofiler-sdk/intercept_table.h Source File

rocprofiler-sdk/intercept_table.h Source File#

ROCprofiler-SDK developer API: rocprofiler-sdk/intercept_table.h Source File
ROCprofiler-SDK developer API 1.0.0
ROCm Profiling API and tools
intercept_table.h
1// MIT License
2//
3// Copyright (c) 2023-2025 Advanced Micro Devices, Inc. All rights reserved.
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23#pragma once
24
25#include <rocprofiler-sdk/defines.h>
26#include <rocprofiler-sdk/fwd.h>
27
28ROCPROFILER_EXTERN_C_INIT
29
30/**
31 * @defgroup INTERCEPT_TABLE Intercept table for runtime libraries
32 * @brief Enable tools to wrap the runtime API function calls of HIP, HSA, and ROCTx before and
33 * after the "real" implementation is called.
34 *
35 * When an application invokes the public function from the HIP, HSA, and ROCTx libraries, these
36 * functions invoke a function pointer which, when properly chained, allow tools to wrap these
37 * function calls to collect information. When this capability is used alongside the rocprofiler API
38 * tracing, tools will wrap the rocprofiler wrappers of the API function, e.g. if the tool installs
39 * a wrapper around the `hsa_init` function called `tool_hsa_init`, and rocprofiler installs a
40 * wrapper around the `hsa_init` function called `rocp_hsa_init`, and within the HSA runtime
41 * library, the "real" implementation of the `hsa_init` invokes a function called `real_hsa_init`,
42 * the invocation chain (starting from within the user application) will be: `<application>` ->
43 * `hsa_init` -> `tool_hsa_init` -> `rocp_hsa_init` -> `real_hsa_init`. The return sequence will be
44 * the inverse of invocation chain: `real_hsa_init` -> `rocp_hsa_init` -> `tool_hsa_init` ->
45 * `<application>`. Thus, it is important for tools that use this feature to (A) call the next
46 * function in the chain and (B) properly handle the return value.
47 *
48 * @{
49 */
50
51/**
52 * @brief (experimental) Callback type when a new runtime library is loaded. @see
53 * rocprofiler_at_intercept_table_registration
54 * @param [in] type Type of API table
55 * @param [in] lib_version Major, minor, and patch version of library encoded into single number
56 * similar to ::ROCPROFILER_VERSION
57 * @param [in] lib_instance The number of times this runtime library has been registered previously
58 * @param [in] tables An array of pointers to the API tables
59 * @param [in] num_tables The size of the array of pointers to the API tables
60 * @param [in] user_data The pointer to the data provided to
61 * ::rocprofiler_at_intercept_table_registration
62 */
63ROCPROFILER_SDK_EXPERIMENTAL
65 uint64_t lib_version,
66 uint64_t lib_instance,
67 void** tables,
68 uint64_t num_tables,
69 void* user_data);
70
71/**
72 * @brief (experimental) Query the name of the intercept table. The name retrieved from this
73 * function is a string literal that is encoded in the read-only section of the binary (i.e. it is
74 * always "allocated" and never "deallocated").
75 *
76 * @param [in] kind Intercept table kind
77 * @param [out] name If non-null and the name is a constant string that does not require dynamic
78 * allocation, this paramter will be set to the address of the string literal, otherwise it will
79 * be set to nullptr
80 * @param [out] name_len If non-null, this will be assigned the length of the name (regardless of
81 * the name is a constant string or requires dynamic allocation)
82 * @return ::rocprofiler_status_t
83 * @retval ::ROCPROFILER_STATUS_ERROR_KIND_NOT_FOUND Returned if the domain id is not valid
84 * @retval ::ROCPROFILER_STATUS_SUCCESS Returned if a valid domain, regardless if there is a
85 * constant string or not.
86 */
87ROCPROFILER_SDK_EXPERIMENTAL
90 const char** name,
91 uint64_t* name_len) ROCPROFILER_API;
92
93/**
94 * @brief (experimental) Invoke this function to receive callbacks when a ROCm library registers its
95 * API intercept table with rocprofiler. Use the ::rocprofiler_intercept_table_t enumeration for
96 * specifying which raw API tables the tool would like to have access to. E.g. including
97 * ::ROCPROFILER_HSA_TABLE in the ::rocprofiler_at_intercept_table_registration function call
98 * communicates to rocprofiler that, when rocprofiler receives a `HsaApiTable` instance, the tool
99 * would like rocprofiler to provide it access too.
100 *
101 * When the HIP, HSA, and ROCTx libraries are initialized (either explicitly or on the first
102 * invocation of one of their public API functions), these runtimes will provide a table of function
103 * pointers to the rocprofiler library via the rocprofiler-register library if the
104 * `rocprofiler_configure` symbol is visible in the application's symbol table. The vast majority of
105 * tools will want to use the @ref CALLBACK_TRACING_SERVICE to trace these runtime APIs, however,
106 * some tools may want or require installing their own intercept functions in lieu of receiving
107 * these callbacks and those tools should use the ::rocprofiler_at_intercept_table_registration
108 * to install their intercept functions. There are no restrictions to where or how early this
109 * function can be invoked but it will return ::ROCPROFILER_STATUS_ERROR_CONFIGURATION_LOCKED if it
110 * is invoked after rocprofiler has requested all the tool configurations. Thus, it is highly
111 * recommended to invoke this function within the ::rocprofiler_configure function or the
112 * callback passed to the ::rocprofiler_force_configure function -- the reason for this
113 * recommendation is that if ::rocprofiler_at_intercept_table_registration is invoked in one of
114 * these locations, rocprofiler can guarantee that the tool will be passed the API table because, at
115 * the first instance of a runtime registering it's API table, rocprofiler will ensure that, in the
116 * case of the former, rocprofiler will invoke all of the ::rocprofiler_configure symbols that
117 * are visible before checking the list of tools which want to receive the API tables and, in the
118 * case of the latter, ::rocprofiler_force_configure will fail with error code
119 * ::ROCPROFILER_STATUS_ERROR_CONFIGURATION_LOCKED if a runtime has already been registered (and,
120 * therefore, already scanned and invoked the visible ::rocprofiler_configure symbols and
121 * completed the tool initialization). If ::rocprofiler_at_intercept_table_registration is
122 * invoked outside of these recommended places, even if it is done before the `main` function starts
123 * (e.g. in a library init/constructor function), it is possible that another library, such as
124 * ROCm-aware MPI, caused the HIP and HSA runtime libraries to be initialized when that library was
125 * loaded. In this aforementioned scenario, if the ROCm-aware MPI library library init/constructor
126 * function runs before your library init/constructor function, rocprofiler will have already
127 * processed the API table and will not provide the API table to the tool due to the fact that the
128 * API may already be in use and, thus, any modifications to the table might result in thread-safety
129 * violations or more disastrous consequences.
130 *
131 * @param [in] callback Callback to tool invoked when a runtime registers their API table with
132 * rocprofiler
133 * @param [in] libs Bitwise-or of libraries, e.g. `ROCPROFILER_HSA_TABLE |
134 * ROCPROFILER_HIP_RUNTIME_TABLE | ROCPROFILER_MARKER_CORE_TABLE` means the callbacks will be
135 * invoked whenever the HSA, HIP runtime, and ROCTx core API tables register their intercept
136 * table(s).
137 * @param [in] data Data to provide to callback(s)
138 * @return ::rocprofiler_status_t
139 * @retval ::ROCPROFILER_STATUS_SUCCESS Callback was registered for specified runtime(s)
140 * @retval ::ROCPROFILER_STATUS_ERROR_CONFIGURATION_LOCKED rocprofiler has already initialized
141 * @retval ::ROCPROFILER_STATUS_ERROR_INVALID_ARGUMENT this error code is returned if
142 * `ROCPROFILER_TABLE` is included in bitwise-or of the libs
143 * @retval ::ROCPROFILER_STATUS_ERROR_NOT_IMPLEMENTED this error code is returned if one of the
144 * specified libraries does not have support for API intercept tables (which should not be the case
145 * by the time this code is publicly released)
146 *
147 * @code{.cpp}
148 * namespace
149 * {
150 * // this function generates a wrapper around the original function that
151 * // prints out the function name and then invokes the original function
152 * template <size_t Idx, typename RetT, typename... Args>
153 * auto
154 * generate_wrapper(const char* name, RetT (*func)(Args...))
155 * {
156 * using functor_type = RetT (*)(Args...);
157 *
158 * // save function name, "real function"
159 * static const auto* func_name = name;
160 * static functor_type underlying_func = func;
161 * static functor_type wrapped_func = [](Args... args) -> RetT {
162 * std::cout << "Wrapping " << func_name << "..." << std::endl;
163 * if(underlying_func) return underlying_func(args...);
164 * if constexpr(!std::is_void<RetT>::value) return RetT{};
165 * };
166 *
167 * return wrapped_func;
168 * }
169 *
170 *
171 * // this macro installs the wrapper in place of the original function
172 * #define GENERATE_WRAPPER(TABLE, FUNC) \
173 * TABLE->FUNC##_fn = generate_wrapper<__COUNTER__>(#FUNC, TABLE->FUNC##_fn)
174 *
175 *
176 * // this is the function that gets called when the HSA runtime
177 * // intercept table is registered with rocprofiler
178 * void
179 * api_registration_callback(rocprofiler_intercept_table_t type,
180 * uint64_t lib_version,
181 * uint64_t lib_instance,
182 * void** tables,
183 * uint64_t num_tables,
184 * void* user_data)
185 * {
186 * if(type != ROCPROFILER_HSA_TABLE)
187 * throw std::runtime_error{"unexpected library type: " +
188 * std::to_string(static_cast<int>(type))};
189 * if(lib_instance != 0) throw std::runtime_error{"multiple instances of HSA runtime library"};
190 * if(num_tables != 1) throw std::runtime_error{"expected only one table of type HsaApiTable"};
191 *
192 * auto* hsa_api_table = static_cast<HsaApiTable*>(tables[0]);
193 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_agent_get_info);
194 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_agent_iterate_isas);
195 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_code_object_reader_create_from_memory);
196 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_executable_create_alt);
197 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_executable_freeze);
198 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_executable_get_symbol_by_name);
199 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_executable_iterate_symbols);
200 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_executable_load_agent_code_object);
201 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_executable_symbol_get_info);
202 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_isa_get_info_alt);
203 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_iterate_agents);
204 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_queue_add_write_index_screlease);
205 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_queue_create);
206 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_queue_load_read_index_relaxed);
207 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_queue_load_read_index_scacquire);
208 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_signal_create);
209 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_signal_destroy);
210 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_signal_load_relaxed);
211 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_signal_silent_store_relaxed);
212 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_signal_store_screlease);
213 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_signal_wait_scacquire);
214 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_system_get_info);
215 * GENERATE_WRAPPER(hsa_api_table->core_, hsa_system_get_major_extension_table);
216 * }
217 * } // namespace
218 *
219 *
220 * extern "C" rocprofiler_tool_configure_result_t*
221 * rocprofiler_configure(uint32_t version,
222 * const char* runtime_version,
223 * uint32_t priority,
224 * rocprofiler_client_id_t* id)
225 * {
226 * // set the client name
227 * id->name = "ExampleTool";
228 *
229 * // specify that we only want to intercept the HSA library
230 * rocprofiler_at_intercept_table_registration(api_registration_callback,
231 * ROCPROFILER_HSA_TABLE, nullptr);
232 *
233 * return nullptr;
234 * }
235 * @endcode
236 *
237 * @example intercept_table/client.cpp
238 * Example demonstrating ::rocprofiler_at_intercept_table_registration usage
239 */
240ROCPROFILER_SDK_EXPERIMENTAL
242rocprofiler_at_intercept_table_registration(rocprofiler_intercept_library_cb_t callback,
243 int libs,
244 void* data) ROCPROFILER_API;
245
246/** @} */
247
248ROCPROFILER_EXTERN_C_FINI
rocprofiler_intercept_table_t
Enumeration for specifying intercept tables supported by rocprofiler. This enumeration is used for in...
Definition fwd.h:413
rocprofiler_status_t
Status codes.
Definition fwd.h:49
void(* rocprofiler_intercept_library_cb_t)(rocprofiler_intercept_table_t type, uint64_t lib_version, uint64_t lib_instance, void **tables, uint64_t num_tables, void *user_data)
(experimental) Callback type when a new runtime library is loaded.
rocprofiler_status_t rocprofiler_query_intercept_table_name(rocprofiler_intercept_table_t kind, const char **name, uint64_t *name_len)
(experimental) Query the name of the intercept table. The name retrieved from this function is a stri...