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.3.5
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, rocshmem_api, "rocSHMEM API function")
104ROCPROFILER_DEFINE_CATEGORY(category, hipfile_api, "hipFILE API function")
105ROCPROFILER_DEFINE_CATEGORY(category, counter_collection, "Counter Collection")
106ROCPROFILER_DEFINE_CATEGORY(category, kfd_events, "KFD events collection")
107ROCPROFILER_DEFINE_CATEGORY(category, scratch_memory, "Scratch Memory Allocation")
108ROCPROFILER_DEFINE_CATEGORY(category, none, "Unknown category")
109
110#define ROCPROFILER_PERFETTO_CATEGORIES \
111 ROCPROFILER_PERFETTO_CATEGORY(category::hsa_api), \
112 ROCPROFILER_PERFETTO_CATEGORY(category::hip_api), \
113 ROCPROFILER_PERFETTO_CATEGORY(category::marker_api), \
114 ROCPROFILER_PERFETTO_CATEGORY(category::rccl_api), \
115 ROCPROFILER_PERFETTO_CATEGORY(category::openmp), \
116 ROCPROFILER_PERFETTO_CATEGORY(category::kernel_dispatch), \
117 ROCPROFILER_PERFETTO_CATEGORY(category::memory_copy), \
118 ROCPROFILER_PERFETTO_CATEGORY(category::counter_collection), \
119 ROCPROFILER_PERFETTO_CATEGORY(category::memory_allocation), \
120 ROCPROFILER_PERFETTO_CATEGORY(category::rocdecode_api), \
121 ROCPROFILER_PERFETTO_CATEGORY(category::rocjpeg_api), \
122 ROCPROFILER_PERFETTO_CATEGORY(category::rocshmem_api), \
123 ROCPROFILER_PERFETTO_CATEGORY(category::hipfile_api), \
124 ROCPROFILER_PERFETTO_CATEGORY(category::scratch_memory), \
125 ROCPROFILER_PERFETTO_CATEGORY(category::none)
126
127#include <perfetto.h>
128
129PERFETTO_DEFINE_CATEGORIES(ROCPROFILER_PERFETTO_CATEGORIES);
130
131namespace rocprofiler
132{
133namespace sdk
134{
135using perfetto_event_context_t = ::perfetto::EventContext;
136
137template <typename Np, typename Tp>
138auto
139add_perfetto_annotation(perfetto_event_context_t& ctx, Np&& _name, Tp&& _val)
140{
141 namespace mpl = ::rocprofiler::sdk::mpl;
142
143 using named_type = mpl::unqualified_identity_t<Np>;
144 using value_type = mpl::unqualified_identity_t<Tp>;
145
146 static_assert(mpl::is_string_type<named_type>::value, "Error! name is not a string type");
147
148 auto _get_dbg = [&]() {
149 auto* _dbg = ctx.event()->add_debug_annotations();
150 _dbg->set_name(std::string_view{std::forward<Np>(_name)}.data());
151 return _dbg;
152 };
153
154 if constexpr(std::is_same<value_type, std::string_view>::value)
155 {
156 _get_dbg()->set_string_value(_val.data());
157 }
158 else if constexpr(mpl::is_string_type<value_type>::value)
159 {
160 _get_dbg()->set_string_value(std::forward<Tp>(_val));
161 }
162 else if constexpr(std::is_same<value_type, bool>::value)
163 {
164 _get_dbg()->set_bool_value(_val);
165 }
166 else if constexpr(std::is_enum<value_type>::value)
167 {
168 _get_dbg()->set_int_value(static_cast<int64_t>(_val));
169 }
170 else if constexpr(std::is_floating_point<value_type>::value)
171 {
172 _get_dbg()->set_double_value(static_cast<double>(_val));
173 }
174 else if constexpr(std::is_integral<value_type>::value)
175 {
176 if constexpr(std::is_unsigned<value_type>::value)
177 {
178 _get_dbg()->set_uint_value(_val);
179 }
180 else
181 {
182 _get_dbg()->set_int_value(_val);
183 }
184 }
185 else if constexpr(std::is_pointer<value_type>::value)
186 {
187 _get_dbg()->set_pointer_value(reinterpret_cast<uint64_t>(_val));
188 }
189 else if constexpr(mpl::can_stringify<value_type>::value)
190 {
191 auto _ss = std::stringstream{};
192 _ss << std::forward<Tp>(_val);
193 _get_dbg()->set_string_value(_ss.str());
194 }
195 else
196 {
197 static_assert(std::is_empty<value_type>::value, "Error! unsupported data type");
198 }
199}
200} // namespace sdk
201} // namespace rocprofiler
202
203#include <rocprofiler-sdk/fwd.h>
204
205namespace rocprofiler
206{
207namespace sdk
208{
209template <typename KindT, size_t Idx>
210struct rocprofiler_tracing_perfetto_category;
211
212ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(NONE, none)
213ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(HSA_CORE_API, hsa_api)
214ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(HSA_AMD_EXT_API, hsa_api)
215ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(HSA_IMAGE_EXT_API, hsa_api)
216ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(HSA_FINALIZE_EXT_API, hsa_api)
217ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(HIP_RUNTIME_API, hip_api)
218ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(HIP_COMPILER_API, hip_api)
219ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(MARKER_CORE_API, marker_api)
220ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(MARKER_CORE_RANGE_API, marker_api)
221ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(MARKER_CONTROL_API, marker_api)
222ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(MARKER_NAME_API, marker_api)
223ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(MEMORY_COPY, memory_copy)
224ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(MEMORY_ALLOCATION, memory_allocation)
225ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(KERNEL_DISPATCH, kernel_dispatch)
226ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(SCRATCH_MEMORY, scratch_memory)
227ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(CORRELATION_ID_RETIREMENT, none)
228ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(RCCL_API, rccl_api)
229ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(OMPT, openmp)
230ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(RUNTIME_INITIALIZATION, none)
231ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(ROCDECODE_API, rocdecode_api)
232ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(ROCJPEG_API, rocjpeg_api)
233ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(HIP_STREAM, hip_api)
234ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(HIP_RUNTIME_API_EXT, hip_api)
235ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(HIP_COMPILER_API_EXT, hip_api)
236ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(ROCDECODE_API_EXT, rocdecode_api)
237ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(KFD_EVENT_PAGE_MIGRATE, kfd_events)
238ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(KFD_EVENT_PAGE_FAULT, kfd_events)
239ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(KFD_EVENT_QUEUE, kfd_events)
240ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(KFD_EVENT_UNMAP_FROM_GPU, kfd_events)
241ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(KFD_EVENT_DROPPED_EVENTS, kfd_events)
242ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(KFD_PAGE_MIGRATE, kfd_events)
243ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(KFD_PAGE_FAULT, kfd_events)
244ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(KFD_QUEUE, kfd_events)
245ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(HIP_GRAPH, hip_api)
246ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(ROCSHMEM_API, rocshmem_api)
247ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(ROCSHMEM_API_EXT, rocshmem_api)
248ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY(HIPFILE_API_EXT, hipfile_api)
249
250ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(NONE, none)
251ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(HSA_CORE_API, hsa_api)
252ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(HSA_AMD_EXT_API, hsa_api)
253ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(HSA_IMAGE_EXT_API, hsa_api)
254ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(HSA_FINALIZE_EXT_API, hsa_api)
255ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(HIP_RUNTIME_API, hip_api)
256ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(HIP_COMPILER_API, hip_api)
257ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(MARKER_CORE_API, marker_api)
258ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(MARKER_CORE_RANGE_API, marker_api)
259ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(MARKER_CONTROL_API, marker_api)
260ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(MARKER_NAME_API, marker_api)
261ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(CODE_OBJECT, none)
262ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(SCRATCH_MEMORY, scratch_memory)
263ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(KERNEL_DISPATCH, kernel_dispatch)
264ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(MEMORY_COPY, memory_copy)
265ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(MEMORY_ALLOCATION, memory_allocation)
266ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(RCCL_API, rccl_api)
267ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(OMPT, openmp)
268ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(RUNTIME_INITIALIZATION, none)
269ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(ROCDECODE_API, rocdecode_api)
270ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(ROCJPEG_API, rocjpeg_api)
271ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(HIP_STREAM, hip_api)
272ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(HIP_GRAPH, hip_api)
273ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY(ROCSHMEM_API, rocshmem_api)
274
275template <>
276struct rocprofiler_tracing_perfetto_category<rocprofiler_buffer_tracing_kind_t,
278{
279 static constexpr auto name =
280 ::rocprofiler::sdk::perfetto_category<::rocprofiler::sdk::category::hipfile_api>::name;
281};
282
283template <>
284struct rocprofiler_tracing_perfetto_category<rocprofiler_callback_tracing_kind_t,
286{
287 static constexpr auto name =
288 ::rocprofiler::sdk::perfetto_category<::rocprofiler::sdk::category::hipfile_api>::name;
289};
290
291template <typename KindT, size_t Idx, size_t... Tail>
292const char*
293get_perfetto_category(KindT kind, std::index_sequence<Idx, Tail...>)
294{
295 if(kind == Idx) return rocprofiler_tracing_perfetto_category<KindT, Idx>::name;
296 // recursion until tail empty
297 if constexpr(sizeof...(Tail) > 0)
298 return get_perfetto_category(kind, std::index_sequence<Tail...>{});
299 return nullptr;
300}
301
302template <typename KindT>
303const char*
304get_perfetto_category(KindT kind)
305{
306 if constexpr(std::is_same<KindT, rocprofiler_buffer_tracing_kind_t>::value)
307 {
308 return get_perfetto_category<KindT>(
309 kind, std::make_index_sequence<ROCPROFILER_BUFFER_TRACING_LAST>{});
310 }
311 else if constexpr(std::is_same<KindT, rocprofiler_callback_tracing_kind_t>::value)
312 {
313 return get_perfetto_category<KindT>(
314 kind, std::make_index_sequence<ROCPROFILER_CALLBACK_TRACING_LAST>{});
315 }
316 else
317 {
318 static_assert(rocprofiler::sdk::mpl::assert_false<KindT>::value, "Unsupported data type");
319 }
320
321 return nullptr;
322}
323} // namespace sdk
324} // namespace rocprofiler
325
326#undef ROCPROFILER_DEFINE_PERFETTO_CATEGORY
327#undef ROCPROFILER_DEFINE_CATEGORY
328#undef ROCPROFILER_PERFETTO_CATEGORY
329#undef ROCPROFILER_PERFETTO_CATEGORIES
330#undef ROCPROFILER_PERFETTO_TRACING_CATEGORY
331#undef ROCPROFILER_PERFETTO_BUFFER_TRACING_CATEGORY
332#undef ROCPROFILER_PERFETTO_CALLBACK_TRACING_CATEGORY
rocprofiler_buffer_tracing_kind_t
Service Buffer Tracing Kind.
Definition fwd.h:196
rocprofiler_callback_tracing_kind_t
Service Callback Tracing Kind.
Definition fwd.h:158
@ ROCPROFILER_BUFFER_TRACING_HIPFILE_API
hipFILE tracing
Definition fwd.h:243
@ ROCPROFILER_CALLBACK_TRACING_HIPFILE_API
hipFILE API Tracing
Definition fwd.h:188