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