rocprofiler-sdk/internal_threading.h Source File

rocprofiler-sdk/internal_threading.h Source File#

ROCprofiler-SDK developer API: rocprofiler-sdk/internal_threading.h Source File
ROCprofiler-SDK developer API 1.0.0
ROCm Profiling API and tools
internal_threading.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 INTERNAL_THREADING Internal Thread Handling
32 * @brief Callbacks before and after threads created internally by libraries
33 *
34 * @{
35 * @example api_buffered_tracing/client.cpp
36 * Example demonstrating @ref BUFFER_TRACING_SERVICE that includes usage of
37 * ::rocprofiler_at_internal_thread_create, ::rocprofiler_create_callback_thread, and
38 * ::rocprofiler_assign_callback_thread.
39 */
40
41/**
42 * @brief (experimental) Callback type before and after internal thread creation. @see
43 * rocprofiler_at_internal_thread_create
44 *
45 */
46ROCPROFILER_SDK_EXPERIMENTAL
48
49/**
50 * @brief (experimental) Invoke this function to receive callbacks before and after the creation of
51 * an internal thread by a library which as invoked on the thread which is creating the internal
52 * thread(s).
53 *
54 * Use the ::rocprofiler_runtime_library_t enumeration for specifying which libraries you want
55 * callbacks before and after the library creates an internal thread. These callbacks will be
56 * invoked on the thread that is about to create the new thread (not on the newly created thread).
57 * In thread-aware tools that wrap pthread_create, this can be used to disable the wrapper before
58 * the pthread_create invocation and re-enable the wrapper afterwards. In many cases, tools will
59 * want to ignore the thread(s) created by rocprofiler since these threads do not exist in the
60 * normal application execution, whereas the internal threads for HSA, HIP, etc. are created in
61 * normal application execution; however, the HIP, HSA, etc. internal threads are typically
62 * background threads which just monitor kernel completion and are unlikely to contribute to any
63 * performance issues. Please note that the postcreate callback is guaranteed to be invoked after
64 * the underlying system call to create a new thread but it does not guarantee that the new thread
65 * has been started. Please note, that once these callbacks are registered, they cannot be removed
66 * so the caller is responsible for ignoring these callbacks if they want to ignore them beyond a
67 * certain point in the application.
68 *
69 * @param [in] precreate Callback invoked immediately before a new internal thread is created
70 * @param [in] postcreate Callback invoked immediately after a new internal thread is created
71 * @param [in] libs Bitwise-or of libraries, e.g. `ROCPROFILER_LIBRARY | ROCPROFILER_MARKER_LIBRARY`
72 * means the callbacks will be invoked whenever rocprofiler and/or the marker library create
73 * internal threads but not when the HSA or HIP libraries create internal threads.
74 * @param [in] data Data shared between callbacks
75 * @return ::rocprofiler_status_t
76 * @retval ::ROCPROFILER_STATUS_SUCCESS There are currently no conditions which result in any other
77 * value, even if internal threads have already been created
78 */
79ROCPROFILER_SDK_EXPERIMENTAL
83 int libs,
84 void* data) ROCPROFILER_API;
85
86/**
87 * @brief (experimental) opaque handle to an internal thread identifier which delivers callbacks for
88 * buffers
89 * @see rocprofiler_create_callback_thread
90 */
91typedef struct ROCPROFILER_SDK_EXPERIMENTAL rocprofiler_callback_thread_t
92{
93 uint64_t handle;
95
96/**
97 * @brief (experimental) Create a handle to a unique thread (created by rocprofiler) which, when
98 * associated with a particular buffer, will guarantee those buffered results always get delivered
99 * on the same thread. This is useful to prevent/control thread-safety issues and/or enable
100 * multithreaded processing of buffers with non-overlapping data
101 *
102 * @param [in] cb_thread_id User-provided pointer to a ::rocprofiler_callback_thread_t
103 * @return ::rocprofiler_status_t
104 * @retval ::ROCPROFILER_STATUS_SUCCESS Successful thread creation
105 * @retval ::ROCPROFILER_STATUS_ERROR_CONFIGURATION_LOCKED Thread creation is no longer available
106 * post-initialization
107 * @retval ::ROCPROFILER_STATUS_ERROR Failed to create thread
108 */
109ROCPROFILER_SDK_EXPERIMENTAL
112 ROCPROFILER_NONNULL(1);
113
114/**
115 * @brief (experimental) By default, all buffered results are delivered on the same thread. Using
116 * ::rocprofiler_create_callback_thread, one or more buffers can be assigned to deliever their
117 * results on a unique, dedicated thread.
118 *
119 * @param [in] buffer_id Buffer identifier
120 * @param [in] cb_thread_id Callback thread identifier via ::rocprofiler_create_callback_thread
121 * @return ::rocprofiler_status_t
122 * @retval ::ROCPROFILER_STATUS_SUCCESS Successful assignment of the delivery thread for the given
123 * buffer
124 * @retval ::ROCPROFILER_STATUS_ERROR_CONFIGURATION_LOCKED Thread assignment is no longer available
125 * post-initialization
126 * @retval ::ROCPROFILER_STATUS_ERROR_THREAD_NOT_FOUND Thread identifier did not match any of the
127 * threads created by rocprofiler
128 * @retval ::ROCPROFILER_STATUS_ERROR_BUFFER_NOT_FOUND Buffer identifier did not match any of the
129 * buffers registered with rocprofiler
130 */
131ROCPROFILER_SDK_EXPERIMENTAL
134 rocprofiler_callback_thread_t cb_thread_id) ROCPROFILER_API;
135
136/** @} */
137
138ROCPROFILER_EXTERN_C_FINI
rocprofiler_status_t
Status codes.
Definition fwd.h:49
rocprofiler_runtime_library_t
Enumeration for specifying runtime libraries supported by rocprofiler. This enumeration is used for t...
Definition fwd.h:397
void(* rocprofiler_internal_thread_library_cb_t)(rocprofiler_runtime_library_t, void *)
(experimental) Callback type before and after internal thread creation.
rocprofiler_status_t rocprofiler_assign_callback_thread(rocprofiler_buffer_id_t buffer_id, rocprofiler_callback_thread_t cb_thread_id)
(experimental) By default, all buffered results are delivered on the same thread. Using rocprofiler_c...
rocprofiler_status_t rocprofiler_at_internal_thread_create(rocprofiler_internal_thread_library_cb_t precreate, rocprofiler_internal_thread_library_cb_t postcreate, int libs, void *data)
(experimental) Invoke this function to receive callbacks before and after the creation of an internal...
rocprofiler_status_t rocprofiler_create_callback_thread(rocprofiler_callback_thread_t *cb_thread_id)
(experimental) Create a handle to a unique thread (created by rocprofiler) which, when associated wit...
(experimental) opaque handle to an internal thread identifier which delivers callbacks for buffers