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 1.0.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
74#include "rocprofiler-sdk-roctx/defines.h"
75#include "rocprofiler-sdk-roctx/types.h"
76#include "rocprofiler-sdk-roctx/version.h"
77
78ROCTX_EXTERN_C_INIT
79
80/** @defgroup marker_group ROCTx Markers
81 *
82 * @brief Markers are used to annotate specific events in the code execution.
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 * @brief Ranges are used to describe a span of code execution in a ROCm application.
100 *
101 * Ranges can be nested, and the API provides functions to start and stop ranges.
102 * Ranges are thread-local, meaning that each thread can have its own stack of
103 * ranges. The API provides functions to push and pop ranges from the stack.
104 * The API also provides functions to start and stop ranges, which are
105 * process-wide. Each range is assigned a unique ID, which can be used to
106 * identify the range when stopping it.
107 * @{
108 */
109
110/**
111 * Start a new nested range.
112 *
113 * Nested ranges are stacked and local to the current CPU thread.
114 *
115 * @param[in] message The message associated with this range.
116 *
117 * @return Returns the level this nested range is started at. Nested range
118 * levels are 0 based.
119 */
120int
121roctxRangePushA(const char* message) ROCTX_API ROCTX_NONNULL(1);
122
123/**
124 * Stop the current nested range.
125 *
126 * Stop the current nested range, and pop it from the stack. If a nested range
127 * was active before the last one was started, it becomes again the current
128 * nested range.
129 *
130 * @return Returns the level the stopped nested range was started at, or a
131 * negative value if there was no nested range active.
132 */
133int
134roctxRangePop() ROCTX_API;
135
136/**
137 * @brief Starts a process range.
138 *
139 * Start/stop ranges can be started and stopped in different threads. Each
140 * timespan is assigned a unique range ID.
141 *
142 * @param [in] message The message associated with this range.
143 *
144 * @return Returns the ID of the new range.
145 */
147roctxRangeStartA(const char* message) ROCTX_API ROCTX_NONNULL(1);
148
149/**
150 * Stop a process range.
151 *
152 * @param [in] id ::roctx_range_id_t returned from ::roctxRangeStartA to stop
153 */
154void
156
157/** @} */
158
159/** @defgroup PROFILER_COMM ROCTx Application control/customization of profiling tools
160 *
161 * @brief Applications can invoke these functions to control/customize profiling tool behavior.
162 *
163 * @{
164 */
165
166/**
167 * @brief Request any currently running profiling tool that is should stop collecting data.
168 *
169 * Within a profiling tool, it is recommended that the tool cache all active contexts at the time of
170 * the request and then stop them. By convention, the application should pass zero to indicate a
171 * global pause of the profiler in the current process. If the application wishes to pause only the
172 * current thread, the application should obtain the thread ID via @ref roctxGetThreadId.
173 *
174 * @param [in] tid Zero for all threads in current process or non-zero for a specific thread
175 *
176 * @return int A profiling tool may choose to set this value to a non-zero value to indicate a
177 * failure while executing the request or lack of support. If the profiling tool supports pausing
178 * but is already paused, the tool should ignore the request and return zero.
179 */
180int
182
183/**
184 * @brief Request any currently running profiling tool that is should resume collecting data.
185 *
186 * Within a profiling tool, it is recommended that the tool re-activated the active contexts which
187 * were cached when the pause request was issued. By convention, the application should pass zero to
188 * indicate a global pause of the profiler in the current process. If the application wishes to
189 * pause only the current thread, the application should obtain the thread ID via @ref
190 * roctxGetThreadId.
191 *
192 * @param [in] tid Zero for all threads in current process or non-zero for a specific thread
193 *
194 * @return int A profiling tool may choose to set this value to a non-zero value to indicate a
195 * failure while executing the request or lack of support. If the profiling tool is supports
196 * resuming but is already active, the tool should ignore the request and return zero.
197 */
198int
200
201/** @} */
202
203/** \defgroup UTILITIES ROCTx Utility functions
204 *
205 * @brief Utility functions for profiling tools to customize their behavior.
206 *
207 * @{
208 */
209
210/**
211 * @brief Indicate to a profiling tool that, where possible, you would like the current CPU OS
212 * thread to be labeled by the provided name in the output of the profiling tool.
213 *
214 * Rocprofiler does not provide explicit support for how profiling tools handle this request:
215 * support for this capability is tool specific. ROCTx does NOT rename the thread via
216 * `pthread_setname_np`.
217 *
218 * @param [in] name Name for the current OS thread
219 *
220 * @return int A profiling tool may choose to set this value to a non-zero value to indicate a
221 * failure while executing the request or lack of support
222 */
223int
224roctxNameOsThread(const char* name) ROCTX_API ROCTX_NONNULL(1);
225
226/**
227 * @brief Indicate to a profiling tool that, where possible, you would like the given HSA agent
228 * to be labeled by the provided name in the output of the profiling tool.
229 *
230 * Rocprofiler does not provide any explicit support for how profiling tools handle this request:
231 * support for this capability is tool specific.
232 *
233 * @param [in] name Name for the specified agent
234 * @param [in] agent Pointer to a HSA agent identifier
235 *
236 * @return int A profiling tool may choose to set this value to a non-zero value to indicate a
237 * failure while executing the request or lack of support
238 */
239int
240roctxNameHsaAgent(const char* name, const struct hsa_agent_s* agent) ROCTX_API ROCTX_NONNULL(1, 2);
241
242/**
243 * @brief Indicate to a profiling tool that, where possible, you would like the given HIP device id
244 * to be labeled by the provided name in the output of the profiling tool.
245 *
246 * Rocprofiler does not provide any explicit support for how profiling tools handle this request:
247 * support for this capability is tool specific.
248 *
249 * @param [in] name Name for the specified device
250 * @param [in] device_id HIP device ordinal
251 *
252 * @return int A profiling tool may choose to set this value to a non-zero value to indicate a
253 * failure while executing the request or lack of support
254 */
255int
256roctxNameHipDevice(const char* name, int device_id) ROCTX_API ROCTX_NONNULL(1);
257
258/**
259 * @brief Indicate to a profiling tool that, where possible, you would like the given HIP stream
260 * to be labeled by the provided name in the output of the profiling tool.
261 *
262 * Rocprofiler does not provide any explicit support for how profiling tools handle this request:
263 * support for this capability is tool specific.
264 *
265 * @param [in] name Name for the specified stream
266 * @param [in] stream A `hipStream_t` value (hipStream_t == ihipStream_t*)
267 *
268 * @return int A profiling tool may choose to set this value to a non-zero value to indicate a
269 * failure while executing the request or lack of support
270 */
271int
272roctxNameHipStream(const char* name, const struct ihipStream_t* stream) ROCTX_API ROCTX_NONNULL(1);
273
274/** @} */
275
276/** @defgroup UTILITIES ROCTx Utility functions
277 *
278 * @{
279 */
280
281/**
282 * @brief Retrieve a id value for the current thread which will be identical to the id value a
283 * profiling tool gets via `rocprofiler_get_thread_id(rocprofiler_thread_id_t*)`
284 *
285 * @param tid [out] Pointer to where the value should be placed
286 *
287 * @return int A profiling tool may choose to set this value to a non-zero value to indicate a
288 * failure while executing the request or lack of support
289 */
290int
291roctxGetThreadId(roctx_thread_id_t* tid) ROCTX_API ROCTX_NONNULL(1);
292
293/** @} */
294
295ROCTX_EXTERN_C_FINI
296
297#if !defined(roctxRangeStart)
298/** @brief For future compatibility */
299# define roctxRangeStart(message) roctxRangeStartA(message)
300#endif
301
302#if !defined(roctxMark)
303/** @brief For future compatibility */
304# define roctxMark(message) roctxMarkA(message)
305#endif
306
307#if !defined(roctxRangePush)
308/** @brief For future compatibility */
309# define roctxRangePush(message) roctxRangePushA(message)
310#endif
uint64_t roctx_thread_id_t
ROCTx thread ID.
Definition types.h:54
uint64_t roctx_range_id_t
ROCTx range ID.
Definition types.h:47
int roctxProfilerResume(roctx_thread_id_t tid)
Request any currently running profiling tool that is should resume collecting data.
int roctxProfilerPause(roctx_thread_id_t tid)
Request any currently running profiling tool that is should stop collecting data.
int roctxGetThreadId(roctx_thread_id_t *tid)
Retrieve a id value for the current thread which will be identical to the id value a profiling tool g...
int roctxNameOsThread(const char *name)
Indicate to a profiling tool that, where possible, you would like the current CPU OS thread to be lab...
int roctxNameHipDevice(const char *name, int device_id)
Indicate to a profiling tool that, where possible, you would like the given HIP device id to be label...
int roctxNameHipStream(const char *name, const struct ihipStream_t *stream)
Indicate to a profiling tool that, where possible, you would like the given HIP stream to be labeled ...
int roctxNameHsaAgent(const char *name, const struct hsa_agent_s *agent)
Indicate to a profiling tool that, where possible, you would like the given HSA agent to be labeled b...
void roctxMarkA(const char *message)
roctx_range_id_t roctxRangeStartA(const char *message)
Starts a process range.
int roctxRangePop()
void roctxRangeStop(roctx_range_id_t id)
int roctxRangePushA(const char *message)