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.
tim::component::perfetto_trace Struct Reference

Component providing perfetto implementation. More...

#include "timemory/components/perfetto/perfetto.hpp"

+ Collaboration diagram for tim::component::perfetto_trace:

Classes

struct  config
 

Public Types

using TracingInitArgs = backend::perfetto::tracing_init_args
 
using TracingSession = backend::perfetto::tracing_session
 
using value_type = void
 
using base_type = base< perfetto_trace, value_type >
 
using strset_t = std::unordered_set< std::string >
 
using strset_iterator = typename strset_t::iterator
 
using storage_type = empty_storage
 

Public Member Functions

constexpr perfetto_trace ()=default
 
template<typename Tp >
void store (const char *, Tp, enable_if_t< std::is_integral< Tp >::value, int >=0)
 
template<typename Tp >
void store (Tp, enable_if_t< std::is_integral< Tp >::value, int >=0)
 
void start (const char *)
 
void start ()
 
void stop ()
 
void set_prefix (const char *)
 
void get () const
 

Static Public Member Functions

static std::string label ()
 
static std::string description ()
 
static configget_config ()
 
static void global_init ()
 
static void global_finalize ()
 
template<typename... Args>
static opaque get_opaque (Args &&...)
 

Detailed Description

Component providing perfetto implementation.

Definition at line 51 of file perfetto.hpp.

Member Typedef Documentation

◆ base_type

◆ storage_type

Definition at line 66 of file declaration.hpp.

◆ strset_iterator

Definition at line 59 of file perfetto.hpp.

◆ strset_t

using tim::component::perfetto_trace::strset_t = std::unordered_set<std::string>

Definition at line 58 of file perfetto.hpp.

◆ TracingInitArgs

using tim::component::perfetto_trace::TracingInitArgs = backend::perfetto::tracing_init_args

Definition at line 53 of file perfetto.hpp.

◆ TracingSession

using tim::component::perfetto_trace::TracingSession = backend::perfetto::tracing_session

Definition at line 54 of file perfetto.hpp.

◆ value_type

Definition at line 56 of file perfetto.hpp.

Constructor & Destructor Documentation

◆ perfetto_trace()

constexpr tim::component::perfetto_trace::perfetto_trace ( )
constexprdefault

Member Function Documentation

◆ description()

std::string tim::component::perfetto_trace::description ( )
static

Definition at line 59 of file perfetto.cpp.

60{
61 return "Provides Perfetto Tracing SDK: system profiling, app tracing and trace "
62 "analysis";
63}

◆ get()

void tim::component::empty_base::get ( ) const
inlineinherited

Definition at line 69 of file declaration.hpp.

69{}

◆ get_config()

perfetto_trace::config & tim::component::perfetto_trace::get_config ( )
static

Definition at line 67 of file perfetto.cpp.

68{
69 static auto _config = perfetto_trace::config{};
70 return _config;
71}

Referenced by global_finalize(), and global_init().

◆ get_opaque()

template<typename... Args>
static opaque tim::component::empty_base::get_opaque ( Args &&  ...)
inlinestaticinherited

Definition at line 72 of file declaration.hpp.

73 {
74 return opaque{};
75 }

◆ global_finalize()

void tim::component::perfetto_trace::global_finalize ( )
static

Definition at line 136 of file perfetto.cpp.

137{
138#if defined(TIMEMORY_USE_PERFETTO)
139 DEBUG_PRINT_HERE("Finalizing %s", label().c_str());
140 dmp::barrier();
141
142 // Make sure the last event is closed for this example.
143 ::perfetto::TrackEvent::Flush();
144
145 auto& _session = get_config().session;
146 if(!_session)
147 return;
148
149 // Stop tracing and read the trace data.
150 _session->StopBlocking();
151 std::vector<char> trace_data(_session->ReadTraceBlocking());
152
153 auto _rank = dmp::rank();
154 std::string _label = label();
157 TIMEMORY_JOIN('_', _label, _category), "pftrace", dmp::is_initialized(), _rank);
158 // output to a unique filename per rank if DMP is initialized
159 printf("[%s]|%i> Outputting '%s'...\n", _label.c_str(), (int) _rank, _fname.c_str());
160 manager::instance()->add_file_output("binary", _label, _fname);
161
162 // Write the result into a file.
163 // Note: To save memory with longer traces, you can tell Perfetto to write
164 // directly into a file by passing a file descriptor into Setup() above.
165 std::ofstream output{};
166 output.open(_fname.c_str(), std::ios::out | std::ios::binary);
167 output.write(&trace_data[0], trace_data.size());
168 output.close();
169
170 dmp::barrier();
171 if(_rank == 0)
172 puts("");
173#endif
174}
static pointer_t instance()
Get a shared pointer to the instance for the current thread.
tim::mpl::apply< std::string > string
Definition: macros.hpp:53
static config & get_config()
Definition: perfetto.cpp:67
static std::string label()
Definition: perfetto.cpp:52
static string_t compose_output_filename(string_t _tag, string_t _ext, bool _use_suffix=use_output_suffix(), int32_t _suffix=default_process_suffix(), bool _make_dir=false, std::string _explicit={})
Definition: settings.cpp:322
Provides the static category for perfetto traces.
#define DEBUG_PRINT_HERE(...)
Definition: macros.hpp:168
#define TIMEMORY_JOIN(delim,...)
Definition: macros.hpp:90

References tim::settings::compose_output_filename(), DEBUG_PRINT_HERE, get_config(), tim::manager::instance(), label(), and TIMEMORY_JOIN.

◆ global_init()

void tim::component::perfetto_trace::global_init ( )
static

Definition at line 75 of file perfetto.cpp.

76{
77#if defined(TIMEMORY_USE_PERFETTO)
78 DEBUG_PRINT_HERE("Initializing %s", label().c_str());
79 if(get_config().session)
80 return;
81
82 auto& args = get_tracing_init_args();
83 auto& _session = get_config().session;
84 auto shmem_size_hint =
85 tim::get_env<size_t>("TIMEMORY_PERFETTO_SHMEM_SIZE_HINT_KB", 40960);
86 auto buffer_size = tim::get_env<size_t>("TIMEMORY_PERFETTO_BUFFER_SIZE_KB", 1024000);
87
88 ::perfetto::TraceConfig cfg{};
89 ::perfetto::protos::gen::TrackEventConfig track_event_cfg{};
90
91 cfg.add_buffers()->set_size_kb(buffer_size);
92 auto* ds_cfg = cfg.add_data_sources()->mutable_config();
93 ds_cfg->set_name("track_event");
94 ds_cfg->set_track_event_config_raw(track_event_cfg.SerializeAsString());
95
96 args.shmem_size_hint_kb = shmem_size_hint;
97
98 if(get_config().in_process)
99 args.backends |= ::perfetto::kInProcessBackend;
100
101 if(get_config().system_backend)
102 args.backends |= ::perfetto::kSystemBackend;
103
104 ::perfetto::Tracing::Initialize(args);
105 ::perfetto::TrackEvent::Register();
106
107 _session = ::perfetto::Tracing::NewTrace();
108 _session->Setup(cfg);
109 _session->StartBlocking();
110
111 /*
112 ::perfetto::ProcessTrack _process_track = ::perfetto::ProcessTrack::Current();
113 ::perfetto::protos::gen::TrackDescriptor _desc = _process_track.Serialize();
114 auto& _cmdline = settings::instance()->get_command_line();
115 if(!_cmdline.empty())
116 {
117 auto _name = _cmdline.at(0);
118 auto _pos = _name.find_last_of('/');
119 if(_pos != std::string::npos && _pos + 1 < _name.size())
120 _name = _name.substr(_pos + 1);
121 _desc.mutable_process()->set_process_name(_name);
122 }
123 else
124 {
125 std::string _category =
126 ::tim::trait::perfetto_category<TIMEMORY_PERFETTO_API>::value;
127 _desc.mutable_process()->set_process_name(TIMEMORY_JOIN("-", label(), _category));
128 }
129 ::perfetto::TrackEvent::SetTrackDescriptor(_process_track, _desc);
130 */
131#endif
132}

References DEBUG_PRINT_HERE, get_config(), and label().

◆ label()

std::string tim::component::perfetto_trace::label ( )
static

Definition at line 52 of file perfetto.cpp.

53{
54 return "perfetto_trace";
55}

Referenced by global_finalize(), and global_init().

◆ set_prefix()

void tim::component::perfetto_trace::set_prefix ( const char *  _prefix)

Definition at line 208 of file perfetto.cpp.

209{
210 if(_prefix && m_prefix != _prefix)
211 {
212 m_prefix = _prefix;
213 }
214}
char const std::string & _prefix
Definition: config.cpp:55

References tim::_prefix.

◆ start() [1/2]

void tim::component::perfetto_trace::start ( )

Definition at line 186 of file perfetto.cpp.

187{
188 if(m_prefix)
189 backend::perfetto::trace_event_start<TIMEMORY_PERFETTO_API>(m_prefix);
190}

◆ start() [2/2]

void tim::component::perfetto_trace::start ( const char *  _label)

Definition at line 194 of file perfetto.cpp.

195{
196 backend::perfetto::trace_event_start<TIMEMORY_PERFETTO_API>(_label);
197}

◆ stop()

void tim::component::perfetto_trace::stop ( )

Definition at line 201 of file perfetto.cpp.

202{
203 backend::perfetto::trace_event_stop<TIMEMORY_PERFETTO_API>();
204}

◆ store() [1/2]

template<typename Tp >
void tim::component::perfetto_trace::store ( const char *  _label,
Tp  _val,
enable_if_t< std::is_integral< Tp >::value, int >  = 0 
)

Definition at line 120 of file perfetto.hpp.

122{
123 backend::perfetto::trace_counter<TIMEMORY_PERFETTO_API>(_label, _val);
124}

◆ store() [2/2]

template<typename Tp >
void tim::component::perfetto_trace::store ( Tp  _val,
enable_if_t< std::is_integral< Tp >::value, int >  = 0 
)

Definition at line 111 of file perfetto.hpp.

113{
114 if(m_prefix)
115 backend::perfetto::trace_counter(m_prefix, _val);
116}

The documentation for this struct was generated from the following files: