From e1116fc62d5737295d9048727cc7b85427594717 Mon Sep 17 00:00:00 2001 From: Pete Stevenson Date: Tue, 25 Apr 2023 16:05:36 -0700 Subject: [PATCH] In `stirling/core/connector_context.h`: add `EverythingLocalContext` to trace short lived processes. (#1157) Summary: We add `EverythingLocalContext` and a new method `UPIDIsInContext`. The new context type will always return true (for `UPIDIsInContext`) while other pre-existing context types will check the UPIDs list/map/set. Use of this new context type enables tracing short lived processes. In a subsequent PR, we will switch the `UnitConnector` to use this context type when now pid filtering is desired, which will enable the profiler binary to trace all processes and symbolize all stack traces. Type of change: /kind feature Test Plan: Existing tests pass. And existing tests continue to pass in subsequent PRs that switch-over `UnitConnector` and the profiler binary. Signed-off-by: Pete Stevenson --- src/stirling/core/connector_context.cc | 5 +++++ src/stirling/core/connector_context.h | 22 +++++++++++++++++++ .../perf_profiler/perf_profile_connector.cc | 4 +--- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/stirling/core/connector_context.cc b/src/stirling/core/connector_context.cc index 6fff0cf719e..8d6aba4acf5 100644 --- a/src/stirling/core/connector_context.cc +++ b/src/stirling/core/connector_context.cc @@ -98,5 +98,10 @@ absl::flat_hash_set ListUPIDs(const std::filesystem::path& proc_path, SystemWideStandaloneContext::SystemWideStandaloneContext(const std::filesystem::path& proc_path) : StandaloneContext(ListUPIDs(proc_path, /*asid*/ 0), proc_path) {} +const absl::flat_hash_set& EverythingLocalContext::GetUPIDs() const { + upids_ = ListUPIDs(::px::system::ProcPath(), GetASID()); + return upids_; +} + } // namespace stirling } // namespace px diff --git a/src/stirling/core/connector_context.h b/src/stirling/core/connector_context.h index ea4812ccd43..f420352106d 100644 --- a/src/stirling/core/connector_context.h +++ b/src/stirling/core/connector_context.h @@ -45,6 +45,11 @@ class ConnectorContext { */ virtual uint32_t GetASID() const = 0; + /** + * Return true if the UPID is in context. + */ + virtual bool UPIDIsInContext(const md::UPID& upid) const = 0; + /** * Return current set of active UPIDs. */ @@ -86,6 +91,10 @@ class AgentContext : public ConnectorContext { uint32_t GetASID() const override { return agent_metadata_state_->asid(); } + bool UPIDIsInContext(const md::UPID& upid) const override { + return agent_metadata_state_->upids().contains(upid); + } + const absl::flat_hash_set& GetUPIDs() const override { return agent_metadata_state_->upids(); } @@ -115,6 +124,8 @@ class StandaloneContext : public ConnectorContext { uint32_t GetASID() const override { return 0; } + bool UPIDIsInContext(const md::UPID& upid) const override { return upids_.contains(upid); } + const absl::flat_hash_set& GetUPIDs() const override { return upids_; } const absl::flat_hash_map& GetPIDInfoMap() const override { @@ -147,5 +158,16 @@ class SystemWideStandaloneContext : public StandaloneContext { const std::filesystem::path& proc_path = ::px::system::ProcPath()); }; +/** + * Trace everything. Does NOT filter by whether or not the PID was found in /proc. + * Useful to trace short lived processes (which SystemWideStandaloneContext may not). + */ +class EverythingLocalContext : public SystemWideStandaloneContext { + public: + bool UPIDIsInContext(const md::UPID& /*upid*/) const override { return true; } + const absl::flat_hash_set& GetUPIDs() const override; + mutable absl::flat_hash_set upids_; +}; + } // namespace stirling } // namespace px diff --git a/src/stirling/source_connectors/perf_profiler/perf_profile_connector.cc b/src/stirling/source_connectors/perf_profiler/perf_profile_connector.cc index 1a25a35f2af..95910dc29f0 100644 --- a/src/stirling/source_connectors/perf_profiler/perf_profile_connector.cc +++ b/src/stirling/source_connectors/perf_profiler/perf_profile_connector.cc @@ -234,7 +234,6 @@ PerfProfileConnector::StackTraceHisto PerfProfileConnector::AggregateStackTraces uint64_t cum_sum_count = 0; const uint32_t asid = ctx->GetASID(); - const absl::flat_hash_set& upids_for_symbolization = ctx->GetUPIDs(); // Cause symbolizers to perform any necessary updates before we put them to work. u_symbolizer_->IterationPreTick(); @@ -249,9 +248,8 @@ PerfProfileConnector::StackTraceHisto PerfProfileConnector::AggregateStackTraces std::string stack_trace_str; const md::UPID upid(asid, stack_trace_key.upid.pid, stack_trace_key.upid.start_time_ticks); - const bool symbolize = upids_for_symbolization.contains(upid); - if (symbolize) { + if (ctx->UPIDIsInContext(upid)) { // The stringifier clears stack-ids out of the stack traces table when it // first encounters them. If a stack-id is reused by a different stack-trace-key, // the stringifier returns its memoized stack trace string. Because the stack-ids