26#ifndef TIMEMORY_COMPONENTS_PERFETTO_PERFETTO_CPP_
27#define TIMEMORY_COMPONENTS_PERFETTO_PERFETTO_CPP_ 1
31#if !defined(TIMEMORY_COMPONENT_PERFETTO_HEADER_ONLY_MODE) || \
32 TIMEMORY_COMPONENT_PERFETTO_HEADER_ONLY_MODE < 1
34# define TIMEMORY_COMPONENT_PERFETTO_INLINE
36# define TIMEMORY_COMPONENT_PERFETTO_INLINE inline
54 return "perfetto_trace";
61 return "Provides Perfetto Tracing SDK: system profiling, app tracing and trace "
77#if defined(TIMEMORY_USE_PERFETTO)
82 auto& args = get_tracing_init_args();
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);
88 ::perfetto::TraceConfig cfg{};
89 ::perfetto::protos::gen::TrackEventConfig track_event_cfg{};
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());
96 args.shmem_size_hint_kb = shmem_size_hint;
99 args.backends |= ::perfetto::kInProcessBackend;
102 args.backends |= ::perfetto::kSystemBackend;
104 ::perfetto::Tracing::Initialize(args);
105 ::perfetto::TrackEvent::Register();
107 _session = ::perfetto::Tracing::NewTrace();
108 _session->Setup(cfg);
109 _session->StartBlocking();
138#if defined(TIMEMORY_USE_PERFETTO)
143 ::perfetto::TrackEvent::Flush();
150 _session->StopBlocking();
151 std::vector<char> trace_data(_session->ReadTraceBlocking());
153 auto _rank = dmp::rank();
157 TIMEMORY_JOIN(
'_', _label, _category),
"pftrace", dmp::is_initialized(), _rank);
159 printf(
"[%s]|%i> Outputting '%s'...\n", _label.c_str(), (
int) _rank, _fname.c_str());
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());
178perfetto_trace::get_tracing_init_args()
189 backend::perfetto::trace_event_start<TIMEMORY_PERFETTO_API>(m_prefix);
196 backend::perfetto::trace_event_start<TIMEMORY_PERFETTO_API>(_label);
203 backend::perfetto::trace_event_stop<TIMEMORY_PERFETTO_API>();
static pointer_t instance()
Get a shared pointer to the instance for the current thread.
std::array< char *, 4 > _args
char const std::string & _prefix
tim::mpl::apply< std::string > string
#define TIMEMORY_COMPONENT_PERFETTO_INLINE
Definition for various functions for storage_initializer in operations.
static std::string description()
static void global_init()
static config & get_config()
void set_prefix(const char *)
static void global_finalize()
static std::string label()
backend::perfetto::tracing_init_args TracingInitArgs
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={})
Provides the static category for perfetto traces.
#define DEBUG_PRINT_HERE(...)
#define TIMEMORY_JOIN(delim,...)