Skip to content
File

Blob: src/workerd/util/perfetto-tracing.h

cpp73 lines
1#pragma once
2 
3#ifdef WORKERD_USE_PERFETTO
4#include <kj/memory.h>
5#define PERFETTO_ENABLE_LEGACY_TRACE_EVENTS 1
6// Only include a smaller header here to keep include size under control. This approach is
7// recommended in the full perfetto header (perfetto/tracing.h).
8#include "perfetto/tracing/track_event.h"
9 
10PERFETTO_DEFINE_CATEGORIES_IN_NAMESPACE(workerd::traces, perfetto::Category("workerd"));
11 
12namespace kj {
13class StringPtr;
14class String;
15} // namespace kj
16 
17namespace workerd {
18 
19// The PerfettoSession initializes a Perfetto tracing session, initializing the
20// Perfetto subsystem on first initialization and writing events to the given
21// file path.
22class PerfettoSession {
23 public:
24 explicit PerfettoSession(kj::StringPtr path, kj::StringPtr categories);
25 
26 // Create a PerfettoSession on an existing fd (the constructor will handle
27 // wrapping the fd in kj::AutocloseFd)
28 explicit PerfettoSession(int fd, kj::StringPtr categories);
29 PerfettoSession(PerfettoSession&&) = default;
30 PerfettoSession& operator=(PerfettoSession&&) = default;
31 KJ_DISALLOW_COPY(PerfettoSession);
32 ~PerfettoSession() noexcept(false);
33 
34 void flush();
35 
36 // Receives a comma-separated list of trace categories and returns an array.
37 static kj::Array<kj::ArrayPtr<const char>> parseCategories(kj::StringPtr categories);
38 
39 // Called by embedders at most once to register the workerd track events.
40 static void registerWorkerdTracks();
41 
42 private:
43 struct Impl;
44 kj::Own<Impl> impl;
45 
46 friend constexpr bool _kj_internal_isPolymorphic(PerfettoSession::Impl*);
47};
48 
49#define PERFETTO_FLOW_FROM_POINTER(ptr) perfetto::Flow::FromPointer(ptr)
50#define PERFETTO_TERMINATING_FLOW_FROM_POINTER(ptr) perfetto::TerminatingFlow::FromPointer(ptr)
51#define PERFETTO_TRACK_FROM_POINTER(ptr) perfetto::Track::FromPointer(ptr)
52 
53KJ_DECLARE_NON_POLYMORPHIC(PerfettoSession::Impl);
54} // namespace workerd
55 
56#else // defined(WORKERD_USE_PERFETTO)
57struct PerfettoNoop {};
58// We define non-op versions of the instrumentation macros here so that we can
59// still instrument and build when perfetto is not enabled.
60#define TRACE_EVENT(...)
61#define TRACE_EVENT_BEGIN(...)
62#define TRACE_EVENT_END(...)
63#define TRACE_EVENT_INSTANT(...)
64#define TRACE_COUNTER(...)
65#define TRACE_EVENT_CATEGORY_ENABLED(...) false
66#define PERFETTO_FLOW_FROM_POINTER(ptr) \
67 PerfettoNoop {}
68#define PERFETTO_TERMINATING_FLOW_FROM_POINTER(ptr) \
69 PerfettoNoop {}
70#define PERFETTO_TRACK_FROM_POINTER(ptr) \
71 PerfettoNoop {}
72#endif // defined(WORKERD_USE_PERFETTO)