64 if (raw ==
nullptr || *raw ==
'\0')
return std::nullopt;
65 std::uint64_t value = 0;
66 for (
const char* c = raw; *c !=
'\0'; ++c) {
67 if (*c <
'0' || *c >
'9')
return std::nullopt;
68 value = value * 10u +
static_cast<std::uint64_t
>(*c -
'0');
72 return static_cast<std::uint32_t
>(value);
98 while (!text.empty()) {
99 const auto end = text.find(
'\n');
100 auto line = text.substr(0, end);
101 text = end == std::string_view::npos ? std::string_view{} : text.substr(end + 1);
102 while (!line.empty() && (line.back() ==
'\r' || line.back() ==
' ' || line.back() ==
'\t'))
103 line.remove_suffix(1);
104 while (!line.empty() && (line.front() ==
' ' || line.front() ==
'\t')) line.remove_prefix(1);
105 if (line.empty() || line.front() ==
'#')
continue;
106 const auto eq = line.find(
'=');
107 if (eq == std::string_view::npos)
continue;
108 const auto key = line.substr(0, eq);
109 const auto value = std::string(line.substr(eq + 1));
110 if (key ==
"PULP_TRACE_PATH") config.path = value;
111 else if (key ==
"PULP_TRACE_SECONDS") config.seconds = value;
112 else if (key ==
"PULP_TRACE_RING_KB") config.ring_kb = value;
121 constexpr std::string_view token =
"$TMPDIR";
122 if (path.compare(0, token.size(), token) != 0)
return path;
123 std::string base = temp_dir;
124 if (!base.empty() && base.back() ==
'/') base.pop_back();
125 return base + path.substr(token.size());
131 std::string_view process_name,
long pid) {
132 if (path.empty() || path.back() !=
'/')
return path;
133 std::string name(process_name.empty() ? std::string_view{
"pulp"} : process_name);
135 if (c ==
'/' || c ==
' ') c =
'_';
136 return path + name +
"-" + std::to_string(pid) +
".pftrace";
162 : generation_(std::exchange(
other.generation_, 0)),
163 token_(std::exchange(
other.token_, 0)) {}
165 if (
this != &
other) {
166 generation_ = std::exchange(
other.generation_, 0);
167 token_ = std::exchange(
other.token_, 0);
177 std::uint64_t token) noexcept
178 : generation_(generation), token_(token) {}
180 std::uint64_t generation_ = 0;
181 std::uint64_t token_ = 0;
226 static bool start(
const std::vector<std::string>& categories = {},
227 const std::string& out_path = {},
228 std::uint32_t ring_kb = 80u * 1024u);
233 const std::vector<std::string>& categories = {},
234 const std::string& out_path = {},
235 std::uint32_t ring_kb = 80u * 1024u);
282 : attached_(
other.attached_) {
283 other.attached_ =
false;
286 if (
this != &
other) {
288 attached_ =
other.attached_;
289 other.attached_ =
false;
297 if (!attached_)
return;
302 bool attached() const noexcept {
return attached_; }
305 bool attached_ =
false;
ScopedTracingAttachment & operator=(ScopedTracingAttachment &&other) noexcept
Definition trace_session.hpp:285
~ScopedTracingAttachment()
Definition trace_session.hpp:293
ScopedTracingAttachment & operator=(const ScopedTracingAttachment &)=delete
void reset() noexcept
Detach early. Idempotent.
Definition trace_session.hpp:296
ScopedTracingAttachment(const ScopedTracingAttachment &)=delete
bool attached() const noexcept
Definition trace_session.hpp:302
ScopedTracingAttachment()
Definition trace_session.hpp:278
ScopedTracingAttachment(ScopedTracingAttachment &&other) noexcept
Definition trace_session.hpp:281
Opaque authority over one exact process-global trace generation.
Definition trace_session.hpp:159
TraceOwnership(TraceOwnership &&other) noexcept
Definition trace_session.hpp:161
TraceOwnership & operator=(const TraceOwnership &)=delete
TraceOwnership & operator=(TraceOwnership &&other) noexcept
Definition trace_session.hpp:164
friend class Tracing
Definition trace_session.hpp:175
TraceOwnership(const TraceOwnership &)=delete
Process-global tracing controller.
Definition trace_session.hpp:219
static TraceStartResult start_exclusive(const std::vector< std::string > &categories={}, const std::string &out_path={}, std::uint32_t ring_kb=80u *1024u)
Start only when no process session exists, reporting whether this call actually applied the requested...
static TraceStopResult stop()
Stop the active session, flush, and write the .pftrace. No-op → ok=false.
static bool active()
Whether a process session is currently active.
static int attachment_count()
Current attachment count.
static std::uint64_t session_generation()
Generation of the session currently active, or 0 when none is.
static TraceStopResult stop_owned(const TraceOwnership &ownership)
Stop only when both opaque ownership fields match the active session.
static bool start(const std::vector< std::string > &categories={}, const std::string &out_path={}, std::uint32_t ring_kb=80u *1024u)
Start the single process session if none is active.
static void attach()
Reference-counted attachment for multi-instance hosts.
static TraceOwnershipStatus ownership_status(const TraceOwnership *ownership)
Read active and ownership state under one runtime lock.
constexpr std::uint32_t max_autostart_ring_kb
Definition trace_session.hpp:48
std::optional< std::uint32_t > parse_autostart_ring_kb(const char *raw)
Parse a $PULP_TRACE_RING_KB value.
Definition trace_session.hpp:63
std::string trace_autostart_expand(const std::string &path, const std::string &temp_dir)
Expand a leading $TMPDIR in an autostart path to temp_dir (the process's own temporary directory: a s...
Definition trace_session.hpp:120
constexpr std::uint32_t default_autostart_ring_kb
Ring size the env-driven autostart uses when $PULP_TRACE_RING_KB is unset.
Definition trace_session.hpp:45
constexpr std::uint32_t min_autostart_ring_kb
Accepted bounds for an override: 1 MB to 4 GB, expressed in KB.
Definition trace_session.hpp:47
TraceAutostartConfig parse_trace_autostart(std::string_view text)
Parse the autostart file's text.
Definition trace_session.hpp:96
std::string trace_autostart_output_path(const std::string &path, std::string_view process_name, long pid)
The file a process writes for path: path itself, or, for a directory (ends with '/'),...
Definition trace_session.hpp:130
Definition generated_event_source.hpp:33
void log_tracing_reminder()
Emit — at most once for the whole process — a single dev-build reminder that Perfetto tracing is comp...
TraceStartStatus
Outcome of an exclusive attempt to start the process-global trace session.
Definition trace_session.hpp:152
@ AlreadyActive
Definition trace_session.hpp:154
@ Unavailable
Definition trace_session.hpp:155
@ Started
Definition trace_session.hpp:153
@ other
Definition udev_monitor.hpp:26
bool tracing_reminder_first_time(std::atomic< bool > &already_emitted)
One-shot gate for the "tracing compiled in" reminder.
Atomic view of the process-global session relative to one ownership token.
Definition trace_session.hpp:191
bool owned
Definition trace_session.hpp:193
bool active
Definition trace_session.hpp:192
Definition trace_session.hpp:184
TraceStartStatus status
Definition trace_session.hpp:185
std::optional< TraceOwnership > ownership
Capability returned only to the caller that started this generation.
Definition trace_session.hpp:187
Result of stopping a session: the flushed trace path + basic loss accounting so a caller can tell whe...
Definition trace_session.hpp:143
std::uint64_t trace_bytes
Definition trace_session.hpp:146
std::string path
Absolute path to the flushed .pftrace.
Definition trace_session.hpp:145
bool ok
A session was active and a trace was written.
Definition trace_session.hpp:144
A traced plug-in records when PULP_TRACE_PATH is set in its host's environment, but an out-of-process...
Definition trace_session.hpp:88
std::string ring_kb
Definition trace_session.hpp:91
std::string seconds
Definition trace_session.hpp:90
std::string path
Definition trace_session.hpp:89