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