timemory 3.3.0
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
perfetto.hpp
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2020, The Regents of the University of California,
4// through Lawrence Berkeley National Laboratory (subject to receipt of any
5// required approvals from the U.S. Dept. of Energy). All rights reserved.
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in all
15// copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23// SOFTWARE.
24
25#pragma once
26
27#include "timemory/components/base.hpp" // for base
28#include "timemory/components/perfetto/backends.hpp"
30#include "timemory/mpl/concepts.hpp" // for concepts::is_component
31#include "timemory/mpl/types.hpp" // for derivation_types
32
33#include <iostream>
34#include <sstream>
35#include <string>
36#include <unordered_set>
37
38namespace tim
39{
40//--------------------------------------------------------------------------------------//
41//
42// Component declaration
43//
44//--------------------------------------------------------------------------------------//
45
46namespace component
47{
48/// \struct tim::component::perfetto_trace
49/// \brief Component providing perfetto implementation
50//
51struct perfetto_trace : base<perfetto_trace, void>
52{
53 using TracingInitArgs = backend::perfetto::tracing_init_args;
54 using TracingSession = backend::perfetto::tracing_session;
55
56 using value_type = void;
58 using strset_t = std::unordered_set<std::string>;
59 using strset_iterator = typename strset_t::iterator;
60
61 constexpr perfetto_trace() = default;
62
63 struct config;
64
65 static std::string label();
66 static std::string description();
67 static config& get_config();
68 static void global_init();
69 static void global_finalize();
70
71 template <typename Tp>
72 void store(const char*, Tp, enable_if_t<std::is_integral<Tp>::value, int> = 0);
73
74 template <typename Tp>
75 void store(Tp, enable_if_t<std::is_integral<Tp>::value, int> = 0);
76
77 void start(const char*);
78 void start();
79 void stop();
80 void set_prefix(const char*);
81
82 struct config
83 {
84 friend struct perfetto_trace;
85
86 using session_t = std::unique_ptr<TracingSession>;
87 bool in_process = true;
88 bool system_backend = false;
90
91 private:
92 session_t session{ nullptr };
93 };
94
95private:
96 static TracingInitArgs& get_tracing_init_args();
97 const char* m_prefix = nullptr;
98};
99
100} // namespace component
101} // namespace tim
102
103//--------------------------------------------------------------------------------------//
104//
105// perfetto_trace component function defs
106//
107//--------------------------------------------------------------------------------------//
108
109template <typename Tp>
110void
112 enable_if_t<std::is_integral<Tp>::value, int>)
113{
114 if(m_prefix)
115 backend::perfetto::trace_counter(m_prefix, _val);
116}
117
118template <typename Tp>
119void
120tim::component::perfetto_trace::store(const char* _label, Tp _val,
121 enable_if_t<std::is_integral<Tp>::value, int>)
122{
123 backend::perfetto::trace_counter<TIMEMORY_PERFETTO_API>(_label, _val);
124}
125
126//--------------------------------------------------------------------------------------//
127
128#if defined(TIMEMORY_COMPONENT_PERFETTO_HEADER_ONLY_MODE) && \
129 TIMEMORY_COMPONENT_PERFETTO_HEADER_ONLY_MODE > 0
131#endif
typename std::enable_if< B, T >::type enable_if_t
Definition: types.hpp:58
Definition: kokkosp.cpp:39
tim::mpl::apply< std::string > string
Definition: macros.hpp:53
std::unique_ptr< TracingSession > session_t
Definition: perfetto.hpp:86
Component providing perfetto implementation.
Definition: perfetto.hpp:52
static std::string description()
Definition: perfetto.cpp:59
static config & get_config()
Definition: perfetto.cpp:67
constexpr perfetto_trace()=default
typename strset_t::iterator strset_iterator
Definition: perfetto.hpp:59
void set_prefix(const char *)
Definition: perfetto.cpp:208
static std::string label()
Definition: perfetto.cpp:52
void store(const char *, Tp, enable_if_t< std::is_integral< Tp >::value, int >=0)
Definition: perfetto.hpp:120
std::unordered_set< std::string > strset_t
Definition: perfetto.hpp:58
backend::perfetto::tracing_init_args TracingInitArgs
Definition: perfetto.hpp:53
backend::perfetto::tracing_session TracingSession
Definition: perfetto.hpp:54