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 0.6.0
ROCm Profiling API and tools
perfetto.hpp
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
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
68namespace rocprofiler
69{
70namespace sdk
71{
72template <typename Tp>
74} // namespace sdk
75} // namespace rocprofiler
76
77ROCPROFILER_DEFINE_CATEGORY(category, hsa_api, "HSA API function")
78ROCPROFILER_DEFINE_CATEGORY(category, hip_api, "HIP API function")
79ROCPROFILER_DEFINE_CATEGORY(category, marker_api, "Marker API region")
80ROCPROFILER_DEFINE_CATEGORY(category, rccl_api, "RCCL API function")
82ROCPROFILER_DEFINE_CATEGORY(category, kernel_dispatch, "GPU kernel dispatch")
83ROCPROFILER_DEFINE_CATEGORY(category, memory_copy, "Async memory copy")
84ROCPROFILER_DEFINE_CATEGORY(category, memory_allocation, "Memory Allocation")
85
86#define ROCPROFILER_PERFETTO_CATEGORIES \
87 ROCPROFILER_PERFETTO_CATEGORY(category::hsa_api), \
88 ROCPROFILER_PERFETTO_CATEGORY(category::hip_api), \
89 ROCPROFILER_PERFETTO_CATEGORY(category::marker_api), \
90 ROCPROFILER_PERFETTO_CATEGORY(category::rccl_api), \
91 ROCPROFILER_PERFETTO_CATEGORY(category::openmp), \
92 ROCPROFILER_PERFETTO_CATEGORY(category::kernel_dispatch), \
93 ROCPROFILER_PERFETTO_CATEGORY(category::memory_copy), \
94 ROCPROFILER_PERFETTO_CATEGORY(category::memory_allocation)
95
96#include <perfetto.h>
97
99
100namespace rocprofiler
101{
102namespace sdk
103{
104using perfetto_event_context_t = ::perfetto::EventContext;
105
106template <typename Np, typename Tp>
107auto
109{
110 namespace mpl = ::rocprofiler::sdk::mpl;
111
112 using named_type = mpl::unqualified_identity_t<Np>;
113 using value_type = mpl::unqualified_identity_t<Tp>;
114
115 static_assert(mpl::is_string_type<named_type>::value, "Error! name is not a string type");
116
117 auto _get_dbg = [&]() {
118 auto* _dbg = ctx.event()->add_debug_annotations();
119 _dbg->set_name(std::string_view{std::forward<Np>(_name)}.data());
120 return _dbg;
121 };
122
123 if constexpr(std::is_same<value_type, std::string_view>::value)
124 {
125 _get_dbg()->set_string_value(_val.data());
126 }
128 {
129 _get_dbg()->set_string_value(std::forward<Tp>(_val));
130 }
131 else if constexpr(std::is_same<value_type, bool>::value)
132 {
133 _get_dbg()->set_bool_value(_val);
134 }
135 else if constexpr(std::is_enum<value_type>::value)
136 {
137 _get_dbg()->set_int_value(static_cast<int64_t>(_val));
138 }
139 else if constexpr(std::is_floating_point<value_type>::value)
140 {
141 _get_dbg()->set_double_value(static_cast<double>(_val));
142 }
143 else if constexpr(std::is_integral<value_type>::value)
144 {
145 if constexpr(std::is_unsigned<value_type>::value)
146 {
147 _get_dbg()->set_uint_value(_val);
148 }
149 else
150 {
151 _get_dbg()->set_int_value(_val);
152 }
153 }
154 else if constexpr(std::is_pointer<value_type>::value)
155 {
156 _get_dbg()->set_pointer_value(reinterpret_cast<uint64_t>(_val));
157 }
159 {
160 auto _ss = std::stringstream{};
161 _ss << std::forward<Tp>(_val);
162 _get_dbg()->set_string_value(_ss.str());
163 }
164 else
165 {
166 static_assert(std::is_empty<value_type>::value, "Error! unsupported data type");
167 }
168}
169} // namespace sdk
170} // namespace rocprofiler
171
172#undef ROCPROFILER_DEFINE_PERFETTO_CATEGORY
173#undef ROCPROFILER_DEFINE_CATEGORY
174#undef ROCPROFILER_PERFETTO_CATEGORY
175#undef ROCPROFILER_PERFETTO_CATEGORIES
typename unqualified_identity< Tp >::type unqualified_identity_t
Definition mpl.hpp:74
::perfetto::EventContext perfetto_event_context_t
Definition perfetto.hpp:104
auto add_perfetto_annotation(perfetto_event_context_t &ctx, Np &&_name, Tp &&_val)
Definition perfetto.hpp:108
#define ROCPROFILER_PERFETTO_CATEGORIES
Definition perfetto.hpp:86
#define ROCPROFILER_DEFINE_CATEGORY(NS, VALUE, DESC)
Definition perfetto.hpp:49
PERFETTO_DEFINE_CATEGORIES(::perfetto::Category(::rocprofiler::sdk::perfetto_category<::rocprofiler::sdk::category::hsa_api >::name) .SetDescription(::rocprofiler::sdk::perfetto_category<::rocprofiler::sdk::category::hsa_api >::description), ::perfetto::Category(::rocprofiler::sdk::perfetto_category<::rocprofiler::sdk::category::hip_api >::name) .SetDescription(::rocprofiler::sdk::perfetto_category<::rocprofiler::sdk::category::hip_api >::description), ::perfetto::Category(::rocprofiler::sdk::perfetto_category<::rocprofiler::sdk::category::marker_api >::name) .SetDescription(::rocprofiler::sdk::perfetto_category<::rocprofiler::sdk::category::marker_api >::description), ::perfetto::Category(::rocprofiler::sdk::perfetto_category<::rocprofiler::sdk::category::rccl_api >::name) .SetDescription(::rocprofiler::sdk::perfetto_category<::rocprofiler::sdk::category::rccl_api >::description), ::perfetto::Category(::rocprofiler::sdk::perfetto_category<::rocprofiler::sdk::category::openmp >::name) .SetDescription(::rocprofiler::sdk::perfetto_category<::rocprofiler::sdk::category::openmp >::description), ::perfetto::Category(::rocprofiler::sdk::perfetto_category<::rocprofiler::sdk::category::kernel_dispatch >::name) .SetDescription(::rocprofiler::sdk::perfetto_category<::rocprofiler::sdk::category::kernel_dispatch >::description), ::perfetto::Category(::rocprofiler::sdk::perfetto_category<::rocprofiler::sdk::category::memory_copy >::name) .SetDescription(::rocprofiler::sdk::perfetto_category<::rocprofiler::sdk::category::memory_copy >::description), ::perfetto::Category(::rocprofiler::sdk::perfetto_category<::rocprofiler::sdk::category::memory_allocation >::name) .SetDescription(::rocprofiler::sdk::perfetto_category<::rocprofiler::sdk::category::memory_allocation >::description))