rocprofiler-sdk-roctx/roctx.h Source File

rocprofiler-sdk-roctx/roctx.h Source File#

ROCTx Developer API: rocprofiler-sdk-roctx/roctx.h Source File
ROCTx Developer API 0.6.0
ROCm Profiling API and tools
roctx.h
Go to the documentation of this file.
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/**
26 * \file roctx.h
27 * \brief ROCTx API interface for AMD code annotation and profiling
28 *
29 * \mainpage ROCTx API Specification
30 *
31 * \section introduction Introduction
32 * ROCTx is a comprehensive library that implements the AMD code annotation API. It provides
33 * essential functionality for:
34 * - Event annotation and marking
35 * - Code range tracking and management
36 * - Profiler control and customization
37 * - Thread and device naming capabilities
38 *
39 * Key features:
40 * - Nested range tracking with push/pop functionality
41 * - Process-wide range management
42 * - Thread-specific and global profiler control
43 * - Device and stream naming support
44 * - HSA agent and HIP device management
45 *
46 * The API is divided into several main components:
47 * 1. Markers - For single event annotations
48 * 2. Ranges - For tracking code execution spans
49 * 3. Profiler Control - For managing profiling tool behavior
50 * 4. Naming Utilities - For labeling threads, devices, and streams
51 *
52 * Thread Safety:
53 * - Range operations are thread-local and thread-safe
54 * - Marking operations are thread-safe
55 * - Profiler control operations are process-wide
56 *
57 * Integration:
58 * - Compatible with HIP runtime
59 * - Supports HSA (Heterogeneous System Architecture)
60 * - Provides both C and C++ interfaces
61 *
62 * Performance Considerations:
63 * - Minimal overhead for marking and range operations
64 * - Thread-local storage for efficient range stacking
65 * - Lightweight profiler control mechanisms
66 *
67 * \note All string parameters must be null-terminated
68 * \warning Proper nesting of range push/pop operations is user's responsibility
69 */
70
71#include <stddef.h>
72#include <stdint.h>
73
77
79
80/** \defgroup marker_group ROCTx Markers
81 *
82 * Marker annotations are used to describe events in a ROCm application.
83 *
84 * @{
85 */
86
87/**
88 * Mark an event.
89 *
90 * \param[in] message The message associated with the event.
91 */
92void
93roctxMarkA(const char* message) ROCTX_API ROCTX_NONNULL(1);
94
95/** @} */
96
97/** \defgroup range_group ROCTx Ranges
98 *
99 * Range annotations are used to describe events in a ROCm application.
100 *
101 * @{
102 */
103
104/**
105 * Start a new nested range.
106 *
107 * Nested ranges are stacked and local to the current CPU thread.
108 *
109 * \param[in] message The message associated with this range.
110 *
111 * \return Returns the level this nested range is started at. Nested range
112 * levels are 0 based.
113 */
114int
116
117/**
118 * Stop the current nested range.
119 *
120 * Stop the current nested range, and pop it from the stack. If a nested range
121 * was active before the last one was started, it becomes again the current
122 * nested range.
123 *
124 * \return Returns the level the stopped nested range was started at, or a
125 * negative value if there was no nested range active.
126 */
127int
129
130/**
131 * @brief Starts a process range.
132 *
133 * Start/stop ranges can be started and stopped in different threads. Each
134 * timespan is assigned a unique range ID.
135 *
136 * @param [in] message The message associated with this range.
137 *
138 * @return Returns the ID of the new range.
139 */
142
143/**
144 * Stop a process range.
145 */
146void
148
149/** @} */
150
151/** \defgroup PROFILER_COMM ROCTx Application control/customization of profiling tools
152 *
153 * Applications can invoke these functions to control/customize profiling tool behavior.
154 *
155 * @{
156 */
157
158/**
159 * @brief Request any currently running profiling tool that is should stop collecting data.
160 *
161 * Within a profiling tool, it is recommended that the tool cache all active contexts at the time of
162 * the request and then stop them. By convention, the application should pass zero to indicate a
163 * global pause of the profiler in the current process. If the application wishes to pause only the
164 * current thread, the application should obtain the thread ID via @ref roctxGetThreadId.
165 *
166 * @param [in] tid Zero for all threads in current process or non-zero for a specific thread
167 *
168 * @return int A profiling tool may choose to set this value to a non-zero value to indicate a
169 * failure while executing the request or lack of support. If the profiling tool supports pausing
170 * but is already paused, the tool should ignore the request and return zero.
171 */
172int
174
175/**
176 * @brief Request any currently running profiling tool that is should resume collecting data.
177 *
178 * Within a profiling tool, it is recommended that the tool re-activated the active contexts which
179 * were cached when the pause request was issued. By convention, the application should pass zero to
180 * indicate a global pause of the profiler in the current process. If the application wishes to
181 * pause only the current thread, the application should obtain the thread ID via @ref
182 * roctxGetThreadId.
183 *
184 * @param [in] tid Zero for all threads in current process or non-zero for a specific thread
185 *
186 * @return int A profiling tool may choose to set this value to a non-zero value to indicate a
187 * failure while executing the request or lack of support. If the profiling tool is supports
188 * resuming but is already active, the tool should ignore the request and return zero.
189 */
190int
192
193/**
194 * @brief Indicate to a profiling tool that, where possible, you would like the current CPU OS
195 * thread to be labeled by the provided name in the output of the profiling tool.
196 *
197 * Rocprofiler does not provide explicit support for how profiling tools handle this request:
198 * support for this capability is tool specific. ROCTx does NOT rename the thread via
199 * `pthread_setname_np`.
200 *
201 * @param [in] name Name for the current OS thread
202 *
203 * @return int A profiling tool may choose to set this value to a non-zero value to indicate a
204 * failure while executing the request or lack of support
205 */
206int
208
209/**
210 * @brief Indicate to a profiling tool that, where possible, you would like the given HSA agent
211 * to be labeled by the provided name in the output of the profiling tool.
212 *
213 * Rocprofiler does not provide any explicit support for how profiling tools handle this request:
214 * support for this capability is tool specific.
215 *
216 * @param [in] name Name for the specified agent
217 * @param [in] stream Pointer to a HSA agent identifier
218 *
219 * @return int A profiling tool may choose to set this value to a non-zero value to indicate a
220 * failure while executing the request or lack of support
221 */
222int
223roctxNameHsaAgent(const char* name, const struct hsa_agent_s*) ROCTX_API ROCTX_NONNULL(1, 2);
224
225/**
226 * @brief Indicate to a profiling tool that, where possible, you would like the given HIP device id
227 * to be labeled by the provided name in the output of the profiling tool.
228 *
229 * Rocprofiler does not provide any explicit support for how profiling tools handle this request:
230 * support for this capability is tool specific.
231 *
232 * @param [in] name Name for the specified device
233 * @param [in] device_id HIP device ordinal
234 *
235 * @return int A profiling tool may choose to set this value to a non-zero value to indicate a
236 * failure while executing the request or lack of support
237 */
238int
239roctxNameHipDevice(const char* name, int device_id) ROCTX_API ROCTX_NONNULL(1);
240
241/**
242 * @brief Indicate to a profiling tool that, where possible, you would like the given HIP stream
243 * to be labeled by the provided name in the output of the profiling tool.
244 *
245 * Rocprofiler does not provide any explicit support for how profiling tools handle this request:
246 * support for this capability is tool specific.
247 *
248 * @param [in] name Name for the specified stream
249 * @param [in] stream A `hipStream_t` value (hipStream_t == ihipStream_t*)
250 *
251 * @return int A profiling tool may choose to set this value to a non-zero value to indicate a
252 * failure while executing the request or lack of support
253 */
254int
255roctxNameHipStream(const char* name, const struct ihipStream_t* stream) ROCTX_API ROCTX_NONNULL(1);
256
257/** @} */
258
259/** \defgroup UTILITIES ROCTx Utility functions
260 *
261 * @{
262 */
263
264/**
265 * @brief Retrieve a id value for the current thread which will be identical to the id value a
266 * profiling tool gets via `rocprofiler_get_thread_id(rocprofiler_thread_id_t*)`
267 *
268 * @param tid [out] Pointer to where the value should be placed
269 *
270 * @return int A profiling tool may choose to set this value to a non-zero value to indicate a
271 * failure while executing the request or lack of support
272 */
273int
275
276/** @} */
277
279
280#if !defined(roctxRangeStart)
281# define roctxRangeStart(message) roctxRangeStartA(message)
282#endif
283
284#if !defined(roctxMark)
285# define roctxMark(message) roctxMarkA(message)
286#endif
287
288#if !defined(roctxRangePush)
289# define roctxRangePush(message) roctxRangePushA(message)
290#endif
uint64_t roctx_range_id_t
Definition api_trace.h:47
#define ROCTX_EXTERN_C_INIT
Definition defines.h:114
#define ROCTX_NONNULL(...)
Definition defines.h:106
#define ROCTX_API
Definition defines.h:92
#define ROCTX_EXTERN_C_FINI
Definition defines.h:115
uint64_t roctx_thread_id_t
ROCTx thread ID.
Definition types.h:54
int roctxProfilerResume(roctx_thread_id_t tid) ROCTX_API
Request any currently running profiling tool that is should resume collecting data.
int roctxProfilerPause(roctx_thread_id_t tid) ROCTX_API
Request any currently running profiling tool that is should stop collecting data.
int roctxNameHsaAgent(const char *name, const struct hsa_agent_s *) ROCTX_API ROCTX_NONNULL(1
Indicate to a profiling tool that, where possible, you would like the given HSA agent to be labeled b...
int roctxNameHipStream(const char *name, const struct ihipStream_t *stream) ROCTX_API ROCTX_NONNULL(1)
Indicate to a profiling tool that, where possible, you would like the given HIP stream to be labeled ...
int roctxNameOsThread(const char *) ROCTX_API ROCTX_NONNULL(1)
Indicate to a profiling tool that, where possible, you would like the current CPU OS thread to be lab...
int int roctxNameHipDevice(const char *name, int device_id) ROCTX_API ROCTX_NONNULL(1)
Indicate to a profiling tool that, where possible, you would like the given HIP device id to be label...
int roctxGetThreadId(roctx_thread_id_t *tid) ROCTX_API ROCTX_NONNULL(1)
Retrieve a id value for the current thread which will be identical to the id value a profiling tool g...
void roctxMarkA(const char *message) ROCTX_API ROCTX_NONNULL(1)
int roctxRangePushA(const char *message) ROCTX_API ROCTX_NONNULL(1)
void roctxRangeStop(roctx_range_id_t id) ROCTX_API
roctx_range_id_t roctxRangeStartA(const char *message) ROCTX_API ROCTX_NONNULL(1)
Starts a process range.
int roctxRangePop() ROCTX_API