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