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.5.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")
81ROCPROFILER_DEFINE_CATEGORY(category, kernel_dispatch, "GPU kernel dispatch")
82ROCPROFILER_DEFINE_CATEGORY(category, memory_copy, "Async memory copy")
83
84#define ROCPROFILER_PERFETTO_CATEGORIES \
85 ROCPROFILER_PERFETTO_CATEGORY(category::hsa_api), \
86 ROCPROFILER_PERFETTO_CATEGORY(category::hip_api), \
87 ROCPROFILER_PERFETTO_CATEGORY(category::marker_api), \
88 ROCPROFILER_PERFETTO_CATEGORY(category::rccl_api), \
89 ROCPROFILER_PERFETTO_CATEGORY(category::kernel_dispatch), \
90 ROCPROFILER_PERFETTO_CATEGORY(category::memory_copy)
91
92#include <perfetto.h>
93
95
96namespace rocprofiler
97{
98namespace sdk
99{
100using perfetto_event_context_t = ::perfetto::EventContext;
101
102template <typename Np, typename Tp>
103auto
105{
106 namespace mpl = ::rocprofiler::sdk::mpl;
107
108 using named_type = mpl::unqualified_identity_t<Np>;
109 using value_type = mpl::unqualified_identity_t<Tp>;
110
111 static_assert(mpl::is_string_type<named_type>::value, "Error! name is not a string type");
112
113 auto _get_dbg = [&]() {
114 auto* _dbg = ctx.event()->add_debug_annotations();
115 _dbg->set_name(std::string_view{std::forward<Np>(_name)}.data());
116 return _dbg;
117 };
118
119 if constexpr(std::is_same<value_type, std::string_view>::value)
120 {
121 _get_dbg()->set_string_value(_val.data());
122 }
124 {
125 _get_dbg()->set_string_value(std::forward<Tp>(_val));
126 }
127 else if constexpr(std::is_same<value_type, bool>::value)
128 {
129 _get_dbg()->set_bool_value(_val);
130 }
131 else if constexpr(std::is_enum<value_type>::value)
132 {
133 _get_dbg()->set_int_value(static_cast<int64_t>(_val));
134 }
135 else if constexpr(std::is_floating_point<value_type>::value)
136 {
137 _get_dbg()->set_double_value(static_cast<double>(_val));
138 }
139 else if constexpr(std::is_integral<value_type>::value)
140 {
141 if constexpr(std::is_unsigned<value_type>::value)
142 {
143 _get_dbg()->set_uint_value(_val);
144 }
145 else
146 {
147 _get_dbg()->set_int_value(_val);
148 }
149 }
150 else if constexpr(std::is_pointer<value_type>::value)
151 {
152 _get_dbg()->set_pointer_value(reinterpret_cast<uint64_t>(_val));
153 }
155 {
156 auto _ss = std::stringstream{};
157 _ss << std::forward<Tp>(_val);
158 _get_dbg()->set_string_value(_ss.str());
159 }
160 else
161 {
162 static_assert(std::is_empty<value_type>::value, "Error! unsupported data type");
163 }
164}
165} // namespace sdk
166} // namespace rocprofiler
167
168#undef ROCPROFILER_DEFINE_PERFETTO_CATEGORY
169#undef ROCPROFILER_DEFINE_CATEGORY
170#undef ROCPROFILER_PERFETTO_CATEGORY
171#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:100
auto add_perfetto_annotation(perfetto_event_context_t &ctx, Np &&_name, Tp &&_val)
Definition perfetto.hpp:104
#define ROCPROFILER_PERFETTO_CATEGORIES
Definition perfetto.hpp:84
#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::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))