aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniele Di Proietto <ddiproietto@google.com>2022-11-15 13:44:14 +0000
committerMartin Stjernholm <mast@google.com>2023-03-06 18:14:32 +0000
commitb72146354692f2d968d5ef604363996780211771 (patch)
tree2f85111bdbd6ec7423a275a3ad3f0e564b0b7697
parente3a35402007bc82e6ffb595e5d0a70ee5fe8649a (diff)
downloadperfetto-b72146354692f2d968d5ef604363996780211771.tar.gz
tracing: Add ActivateTriggers function.
Instead of shelling out and executing `trigger_perfetto <trigger>`, a process using the perfetto SDK can call `perfetto::Tracing::ActivateTriggers({"<trigger>"})`. Bug: 258465428 Change-Id: Ic8f0458684d87099d05ff18ba87831158bc5be4a (cherry picked from commit 55fd4db8acbfe3742d2505ec5a94acbba3c7d41a) Merged-In: Ic8f0458684d87099d05ff18ba87831158bc5be4a
-rw-r--r--CHANGELOG2
-rw-r--r--include/perfetto/tracing/internal/tracing_muxer.h4
-rw-r--r--include/perfetto/tracing/tracing.h4
-rw-r--r--src/tracing/internal/tracing_muxer_fake.cc4
-rw-r--r--src/tracing/internal/tracing_muxer_fake.h1
-rw-r--r--src/tracing/internal/tracing_muxer_impl.cc10
-rw-r--r--src/tracing/internal/tracing_muxer_impl.h2
-rw-r--r--src/tracing/test/api_integrationtest.cc42
-rw-r--r--src/tracing/tracing.cc5
9 files changed, 74 insertions, 0 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 4b35e3837..6a4cdbd3c 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -10,6 +10,8 @@ Unreleased:
SDK:
* Added TracingInitArgs::enable_system_consumer configuration option, that
allows the linker to discard the consumer IPC, if not required.
+ * Add perfetto::Tracing::ActivateTriggers() function.
+
v25.0 - 2022-04-01:
Tracing service and probes:
diff --git a/include/perfetto/tracing/internal/tracing_muxer.h b/include/perfetto/tracing/internal/tracing_muxer.h
index bef7191f3..02cb758ba 100644
--- a/include/perfetto/tracing/internal/tracing_muxer.h
+++ b/include/perfetto/tracing/internal/tracing_muxer.h
@@ -88,6 +88,10 @@ class PERFETTO_EXPORT TracingMuxer {
InterceptorBase::TLSFactory,
InterceptorBase::TracePacketCallback) = 0;
+ // Informs the tracing services to activate any of these triggers if any
+ // tracing session was waiting for them.
+ virtual void ActivateTriggers(const std::vector<std::string>&) = 0;
+
protected:
explicit TracingMuxer(Platform* platform) : platform_(platform) {}
diff --git a/include/perfetto/tracing/tracing.h b/include/perfetto/tracing/tracing.h
index 01d4851c5..aaec68d1b 100644
--- a/include/perfetto/tracing/tracing.h
+++ b/include/perfetto/tracing/tracing.h
@@ -194,6 +194,10 @@ class PERFETTO_EXPORT Tracing {
// this call is made.
static void ResetForTesting();
+ // Informs the tracing services to activate any of these triggers if any
+ // tracing session was waiting for them.
+ static void ActivateTriggers(const std::vector<std::string>& triggers);
+
private:
static void InitializeInternal(const TracingInitArgs&);
diff --git a/src/tracing/internal/tracing_muxer_fake.cc b/src/tracing/internal/tracing_muxer_fake.cc
index a6700f325..254762e20 100644
--- a/src/tracing/internal/tracing_muxer_fake.cc
+++ b/src/tracing/internal/tracing_muxer_fake.cc
@@ -85,5 +85,9 @@ void TracingMuxerFake::RegisterInterceptor(
FailUninitialized();
}
+void TracingMuxerFake::ActivateTriggers(const std::vector<std::string>&) {
+ FailUninitialized();
+}
+
} // namespace internal
} // namespace perfetto
diff --git a/src/tracing/internal/tracing_muxer_fake.h b/src/tracing/internal/tracing_muxer_fake.h
index 78fc76960..0982a32b1 100644
--- a/src/tracing/internal/tracing_muxer_fake.h
+++ b/src/tracing/internal/tracing_muxer_fake.h
@@ -67,6 +67,7 @@ class TracingMuxerFake : public TracingMuxer {
InterceptorFactory,
InterceptorBase::TLSFactory,
InterceptorBase::TracePacketCallback) override;
+ void ActivateTriggers(const std::vector<std::string>& triggers) override;
private:
static TracingMuxerFake instance;
diff --git a/src/tracing/internal/tracing_muxer_impl.cc b/src/tracing/internal/tracing_muxer_impl.cc
index 876b90b0e..4e4b1ee07 100644
--- a/src/tracing/internal/tracing_muxer_impl.cc
+++ b/src/tracing/internal/tracing_muxer_impl.cc
@@ -894,6 +894,16 @@ void TracingMuxerImpl::RegisterInterceptor(
});
}
+void TracingMuxerImpl::ActivateTriggers(const std::vector<std::string>& triggers) {
+ task_runner_->PostTask([this, triggers] {
+ for (RegisteredBackend& backend : backends_) {
+ if (backend.producer->connected_) {
+ backend.producer->service_->ActivateTriggers(triggers);
+ }
+ }
+ });
+}
+
// Called by the service of one of the backends.
void TracingMuxerImpl::SetupDataSource(TracingBackendId backend_id,
uint32_t backend_connection_id,
diff --git a/src/tracing/internal/tracing_muxer_impl.h b/src/tracing/internal/tracing_muxer_impl.h
index c545a0bc7..dc31e8743 100644
--- a/src/tracing/internal/tracing_muxer_impl.h
+++ b/src/tracing/internal/tracing_muxer_impl.h
@@ -116,6 +116,8 @@ class TracingMuxerImpl : public TracingMuxer {
InterceptorBase::TLSFactory,
InterceptorBase::TracePacketCallback) override;
+ void ActivateTriggers(const std::vector<std::string>& triggers) override;
+
std::unique_ptr<TracingSession> CreateTracingSession(BackendType);
// Producer-side bookkeeping methods.
diff --git a/src/tracing/test/api_integrationtest.cc b/src/tracing/test/api_integrationtest.cc
index 3d16f458b..ba889ea2f 100644
--- a/src/tracing/test/api_integrationtest.cc
+++ b/src/tracing/test/api_integrationtest.cc
@@ -91,6 +91,7 @@
#include "protos/perfetto/trace/track_event/thread_descriptor.pbzero.h"
#include "protos/perfetto/trace/track_event/track_descriptor.gen.h"
#include "protos/perfetto/trace/track_event/track_event.gen.h"
+#include "protos/perfetto/trace/trigger.gen.h"
// Events in categories starting with "dynamic" will use dynamic category
// lookup.
@@ -187,6 +188,7 @@ using perfetto::TracingInitArgs;
using perfetto::internal::TrackEventInternal;
using ::testing::_;
using ::testing::ContainerEq;
+using ::testing::Contains;
using ::testing::ElementsAre;
using ::testing::HasSubstr;
using ::testing::Invoke;
@@ -313,6 +315,8 @@ class MockTracingMuxer : public perfetto::internal::TracingMuxer {
perfetto::InterceptorBase::TLSFactory,
perfetto::InterceptorBase::TracePacketCallback) override {}
+ void ActivateTriggers(const std::vector<std::string>&) override {}
+
std::vector<DataSource> data_sources;
private:
@@ -4955,6 +4959,44 @@ TEST_P(PerfettoApiTest, EmptyEvent) {
EXPECT_EQ(it, trace.packet().end());
}
+TEST_P(PerfettoApiTest, ActivateTriggers) {
+ // Create a new trace session without any data sources configured.
+ perfetto::TraceConfig cfg;
+ cfg.add_buffers()->set_size_kb(1024);
+ perfetto::TraceConfig::TriggerConfig* tr_cfg = cfg.mutable_trigger_config();
+ tr_cfg->set_trigger_mode(perfetto::TraceConfig::TriggerConfig::STOP_TRACING);
+ tr_cfg->set_trigger_timeout_ms(5000);
+ perfetto::TraceConfig::TriggerConfig::Trigger* trigger =
+ tr_cfg->add_triggers();
+ trigger->set_name("trigger1");
+ auto* tracing_session = NewTrace(cfg);
+ tracing_session->get()->StartBlocking();
+
+ perfetto::Tracing::ActivateTriggers({"trigger2", "trigger1"});
+
+ bool done = false;
+ std::mutex mutex;
+ std::condition_variable cv;
+ std::unique_lock<std::mutex> lock(mutex);
+ tracing_session->get()->SetOnStopCallback([&]() {
+ std::lock_guard<std::mutex> inner_lock(mutex);
+ done = true;
+ cv.notify_one();
+ });
+ cv.wait(lock, [&done] { return done; });
+
+ std::vector<char> bytes = tracing_session->get()->ReadTraceBlocking();
+ perfetto::protos::gen::Trace parsed_trace;
+ ASSERT_TRUE(parsed_trace.ParseFromArray(bytes.data(), bytes.size()));
+ EXPECT_THAT(
+ parsed_trace,
+ Property(&perfetto::protos::gen::Trace::packet,
+ Contains(Property(
+ &perfetto::protos::gen::TracePacket::trigger,
+ Property(&perfetto::protos::gen::Trigger::trigger_name,
+ "trigger1")))));
+}
+
TEST(PerfettoApiInitTest, DisableSystemConsumer) {
g_test_tracing_policy->should_allow_consumer_connection = true;
diff --git a/src/tracing/tracing.cc b/src/tracing/tracing.cc
index ab495cfa7..0ae9d168c 100644
--- a/src/tracing/tracing.cc
+++ b/src/tracing/tracing.cc
@@ -84,6 +84,11 @@ std::unique_ptr<TracingSession> Tracing::NewTrace(BackendType backend) {
->CreateTracingSession(backend);
}
+// static
+void Tracing::ActivateTriggers(const std::vector<std::string>& triggers) {
+ internal::TracingMuxer::Get()->ActivateTriggers(triggers);
+}
+
// Can be called from any thread.
bool TracingSession::FlushBlocking(uint32_t timeout_ms) {
std::atomic<bool> flush_result;