rocprofiler-sdk/cxx/perfetto.hpp Source File

rocprofiler-sdk/cxx/perfetto.hpp Source File#

ROCprofiler-SDK developer API: rocprofiler-sdk/cxx/perfetto.hpp Source File
ROCprofiler-SDK developer API 1.0.0
ROCm Profiling API and tools
perfetto.hpp
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/cxx/details/mpl.hpp>
26
27#include <cstddef>
28#include <ostream>
29#include <sstream>
30#include <string>
31#include <string_view>
32#include <type_traits>
33#include <utility>
34
35#define ROCPROFILER_DEFINE_PERFETTO_CATEGORY(NAME, DESC, ...) \
36 namespace rocprofiler \
37 { \
38 namespace sdk \
39 { \
40 template <> \
41 struct perfetto_category<__VA_ARGS__> \
42 { \
43 static constexpr auto name = NAME; \
44 static constexpr auto description = DESC; \
45 }; \
46 } \
47 }
48
49#define ROCPROFILER_DEFINE_CATEGORY(NS, VALUE, DESC) \
50 namespace rocprofiler \
51 { \
52 namespace sdk \
53 { \
54 namespace NS \
55 { \
56 struct VALUE \
57 {}; \
58 } \
59 } \
60 } \
61 ROCPROFILER_DEFINE_PERFETTO_CATEGORY(#VALUE, DESC, NS::VALUE)
62
63#define ROCPROFILER_PERFETTO_CATEGORY(TYPE) \
64 ::perfetto::Category(::rocprofiler::sdk::perfetto_category<::rocprofiler::sdk::TYPE>::name) \
65 .SetDescription( \
66 ::rocprofiler::sdk::perfetto_category<::rocprofiler::sdk::TYPE>::description)
67
68#define ROCPROFILER_PERFETTO_TRACING_CATEGORY(KIND, PREFIX, CODE, CATEGORY) \
69 template <> \
70 struct rocprofiler_tracing_perfetto_category<KIND, PREFIX##CODE> \
71 { \
72 static constexpr auto name = \
73 ::rocprofiler::sdk::perfetto_category<::rocprofiler::sdk::category::CATEGORY>::name; \
74 };
75
76#define ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(...) \
77 ROCPROFILER_PERFETTO_TRACING_CATEGORY( \
78 rocprofiler_buffer_tracing_kind_t, ROCPROFILER_BUFFER_TRACING_, __VA_ARGS__)
79
80#define ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(...) \
81 ROCPROFILER_PERFETTO_TRACING_CATEGORY( \
82 rocprofiler_callback_tracing_kind_t, ROCPROFILER_CALLBACK_TRACING_, __VA_ARGS__)
83
84namespace rocprofiler
85{
86namespace sdk
87{
88template <typename Tp>
89struct perfetto_category;
90} // namespace sdk
91} // namespace rocprofiler
92
93ROCPROFILER_DEFINE_CATEGORY(category, hsa_api, "HSA API function")
94ROCPROFILER_DEFINE_CATEGORY(category, hip_api, "HIP API function")
95ROCPROFILER_DEFINE_CATEGORY(category, marker_api, "Marker API region")
96ROCPROFILER_DEFINE_CATEGORY(category, rccl_api, "RCCL API function")
97ROCPROFILER_DEFINE_CATEGORY(category, openmp, "OpenMP")
98ROCPROFILER_DEFINE_CATEGORY(category, kernel_dispatch, "GPU kernel dispatch")
99ROCPROFILER_DEFINE_CATEGORY(category, memory_copy, "Async memory copy")
100ROCPROFILER_DEFINE_CATEGORY(category, memory_allocation, "Memory Allocation")
101ROCPROFILER_DEFINE_CATEGORY(category, rocdecode_api, "rocDecode API function")
102ROCPROFILER_DEFINE_CATEGORY(category, rocjpeg_api, "rocJPEG API function")
103ROCPROFILER_DEFINE_CATEGORY(category, counter_collection, "Counter Collection")
104ROCPROFILER_DEFINE_CATEGORY(category, kfd_events, "KFD events collection")
105ROCPROFILER_DEFINE_CATEGORY(category, scratch_memory, "Scratch Memory Allocation")
106ROCPROFILER_DEFINE_CATEGORY(category, none, "Unknown category")
107
108#define ROCPROFILER_PERFETTO_CATEGORIES \
109 ROCPROFILER_PERFETTO_CATEGORY(category::hsa_api), \
110 ROCPROFILER_PERFETTO_CATEGORY(category::hip_api), \
111 ROCPROFILER_PERFETTO_CATEGORY(category::marker_api), \
112 ROCPROFILER_PERFETTO_CATEGORY(category::rccl_api), \
113 ROCPROFILER_PERFETTO_CATEGORY(category::openmp), \
114 ROCPROFILER_PERFETTO_CATEGORY(category::kernel_dispatch), \
115 ROCPROFILER_PERFETTO_CATEGORY(category::memory_copy), \
116 ROCPROFILER_PERFETTO_CATEGORY(category::counter_collection), \
117 ROCPROFILER_PERFETTO_CATEGORY(category::memory_allocation), \
118 ROCPROFILER_PERFETTO_CATEGORY(category::rocdecode_api), \
119 ROCPROFILER_PERFETTO_CATEGORY(category::rocjpeg_api), \
120 ROCPROFILER_PERFETTO_CATEGORY(category::scratch_memory), \
121 ROCPROFILER_PERFETTO_CATEGORY(category::none)
122
123#include <perfetto.h>
124
125PERFETTO_DEFINE_CATEGORIES(ROCPROFILER_PERFETTO_CATEGORIES);
126
127namespace rocprofiler
128{
129namespace sdk
130{
131using perfetto_event_context_t = ::perfetto::EventContext;
132
133template <typename Np, typename Tp>
134auto
135add_perfetto_annotation(perfetto_event_context_t& ctx, Np&& _name, Tp&& _val)
136{
137 namespace mpl = ::rocprofiler::sdk::mpl;
138
139 using named_type = mpl::unqualified_identity_t<Np>;
140 using value_type = mpl::unqualified_identity_t<Tp>;
141
142 static_assert(mpl::is_string_type<named_type>::value, "Error! name is not a string type");
143
144 auto _get_dbg = [&]() {
145 auto* _dbg = ctx.event()->add_debug_annotations();
146 _dbg->set_name(std::string_view{std::forward<Np>(_name)}.data());
147 return _dbg;
148 };
149
150 if constexpr(std::is_same<value_type, std::string_view>::value)
151 {
152 _get_dbg()->set_string_value(_val.data());
153 }
154 else if constexpr(mpl::is_string_type<value_type>::value)
155 {
156 _get_dbg()->set_string_value(std::forward<Tp>(_val));
157 }
158 else if constexpr(std::is_same<value_type, bool>::value)
159 {
160 _get_dbg()->set_bool_value(_val);
161 }
162 else if constexpr(std::is_enum<value_type>::value)
163 {
164 _get_dbg()->set_int_value(static_cast<int64_t>(_val));
165 }
166 else if constexpr(std::is_floating_point<value_type>::value)
167 {
168 _get_dbg()->set_double_value(static_cast<double>(_val));
169 }
170 else if constexpr(std::is_integral<value_type>::value)
171 {
172 if constexpr(std::is_unsigned<value_type>::value)
173 {
174 _get_dbg()->set_uint_value(_val);
175 }
176 else
177 {
178 _get_dbg()->set_int_value(_val);
179 }
180 }
181 else if constexpr(std::is_pointer<value_type>::value)
182 {
183 _get_dbg()->set_pointer_value(reinterpret_cast<uint64_t>(_val));
184 }
185 else if constexpr(mpl::can_stringify<value_type>::value)
186 {
187 auto _ss = std::stringstream{};
188 _ss << std::forward<Tp>(_val);
189 _get_dbg()->set_string_value(_ss.str());
190 }
191 else
192 {
193 static_assert(std::is_empty<value_type>::value, "Error! unsupported data type");
194 }
195}
196} // namespace sdk
197} // namespace rocprofiler
198
199#include <rocprofiler-sdk/fwd.h>
200
201namespace rocprofiler
202{
203namespace sdk
204{
205template <typename KindT, size_t Idx>
206struct rocprofiler_tracing_perfetto_category;
207
208ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(NONE, none)
209ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(HSA_CORE_API, hsa_api)
210ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(HSA_AMD_EXT_API, hsa_api)
211ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(HSA_IMAGE_EXT_API, hsa_api)
212ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(HSA_FINALIZE_EXT_API, hsa_api)
213ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(HIP_RUNTIME_API, hip_api)
214ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(HIP_COMPILER_API, hip_api)
215ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(MARKER_CORE_API, marker_api)
216ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(MARKER_CORE_RANGE_API, marker_api)
217ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(MARKER_CONTROL_API, marker_api)
218ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(MARKER_NAME_API, marker_api)
219ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(MEMORY_COPY, memory_copy)
220ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(MEMORY_ALLOCATION, memory_allocation)
221ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(KERNEL_DISPATCH, kernel_dispatch)
222ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(SCRATCH_MEMORY, scratch_memory)
223ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(CORRELATION_ID_RETIREMENT, none)
224ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(RCCL_API, rccl_api)
225ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(OMPT, openmp)
226ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(RUNTIME_INITIALIZATION, none)
227ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(ROCDECODE_API, rocdecode_api)
228ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(ROCJPEG_API, rocjpeg_api)
229ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(HIP_STREAM, hip_api)
230ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(HIP_RUNTIME_API_EXT, hip_api)
231ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(HIP_COMPILER_API_EXT, hip_api)
232ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(ROCDECODE_API_EXT, rocdecode_api)
233ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(KFD_EVENT_PAGE_MIGRATE, kfd_events)
234ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(KFD_EVENT_PAGE_FAULT, kfd_events)
235ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(KFD_EVENT_QUEUE, kfd_events)
236ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(KFD_EVENT_UNMAP_FROM_GPU, kfd_events)
237ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(KFD_EVENT_DROPPED_EVENTS, kfd_events)
238ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(KFD_PAGE_MIGRATE, kfd_events)
239ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(KFD_PAGE_FAULT, kfd_events)
240ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(KFD_QUEUE, kfd_events)
241
242ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(NONE, none)
243ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(HSA_CORE_API, hsa_api)
244ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(HSA_AMD_EXT_API, hsa_api)
245ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(HSA_IMAGE_EXT_API, hsa_api)
246ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(HSA_FINALIZE_EXT_API, hsa_api)
247ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(HIP_RUNTIME_API, hip_api)
248ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(HIP_COMPILER_API, hip_api)
249ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(MARKER_CORE_API, marker_api)
250ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(MARKER_CORE_RANGE_API, marker_api)
251ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(MARKER_CONTROL_API, marker_api)
252ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(MARKER_NAME_API, marker_api)
253ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(CODE_OBJECT, none)
254ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(SCRATCH_MEMORY, scratch_memory)
255ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(KERNEL_DISPATCH, kernel_dispatch)
256ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(MEMORY_COPY, memory_copy)
257ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(MEMORY_ALLOCATION, memory_allocation)
258ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(RCCL_API, rccl_api)
259ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(OMPT, openmp)
260ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(RUNTIME_INITIALIZATION, none)
261ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(ROCDECODE_API, rocdecode_api)
262ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(ROCJPEG_API, rocjpeg_api)
263ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(HIP_STREAM, hip_api)
264
265template <typename KindT, size_t Idx, size_t... Tail>
266const char*
267get_perfetto_category(KindT kind, std::index_sequence<Idx, Tail...>)
268{
269 if(kind == Idx) return rocprofiler_tracing_perfetto_category<KindT, Idx>::name;
270 // recursion until tail empty
271 if constexpr(sizeof...(Tail) > 0)
272 return get_perfetto_category(kind, std::index_sequence<Tail...>{});
273 return nullptr;
274}
275
276template <typename KindT>
277const char*
278get_perfetto_category(KindT kind)
279{
280 if constexpr(std::is_same<KindT, rocprofiler_buffer_tracing_kind_t>::value)
281 {
282 return get_perfetto_category<KindT>(
283 kind, std::make_index_sequence<ROCPROFILER_BUFFER_TRACING_LAST>{});
284 }
285 else if constexpr(std::is_same<KindT, rocprofiler_callback_tracing_kind_t>::value)
286 {
287 return get_perfetto_category<KindT>(
288 kind, std::make_index_sequence<ROCPROFILER_CALLBACK_TRACING_LAST>{});
289 }
290 else
291 {
292 static_assert(rocprofiler::sdk::mpl::assert_false<KindT>::value, "Unsupported data type");
293 }
294
295 return nullptr;
296}
297} // namespace sdk
298} // namespace rocprofiler
299
300#undef ROCPROFILER_DEFINE_PERFETTO_CATEGORY
301#undef ROCPROFILER_DEFINE_CATEGORY
302#undef ROCPROFILER_PERFETTO_CATEGORY
303#undef ROCPROFILER_PERFETTO_CATEGORIES
304#undef ROCPROFILER_PERFETTO_TRACING_CATEGORY
305#undef ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY
306#undef ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY