1 // Copyright (C) 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <benchmark/benchmark.h>
16
17 #include "perfetto/tracing.h"
18 #include "protos/perfetto/trace/test_event.pbzero.h"
19 #include "protos/perfetto/trace/track_event/log_message.pbzero.h"
20
21 PERFETTO_DEFINE_CATEGORIES(perfetto::Category("benchmark"));
22 PERFETTO_TRACK_EVENT_STATIC_STORAGE();
23
24 namespace {
25
26 class BenchmarkDataSource : public perfetto::DataSource<BenchmarkDataSource> {
27 public:
OnSetup(const SetupArgs &)28 void OnSetup(const SetupArgs&) override {}
OnStart(const StartArgs &)29 void OnStart(const StartArgs&) override {}
OnStop(const StopArgs &)30 void OnStop(const StopArgs&) override {}
31 };
32
BM_TracingDataSourceDisabled(benchmark::State & state)33 static void BM_TracingDataSourceDisabled(benchmark::State& state) {
34 while (state.KeepRunning()) {
35 BenchmarkDataSource::Trace([&](BenchmarkDataSource::TraceContext) {});
36 benchmark::ClobberMemory();
37 }
38 }
39
StartTracing(const std::string & data_source_name)40 std::unique_ptr<perfetto::TracingSession> StartTracing(
41 const std::string& data_source_name) {
42 perfetto::TracingInitArgs args;
43 args.backends = perfetto::kInProcessBackend;
44 perfetto::Tracing::Initialize(args);
45
46 perfetto::DataSourceDescriptor dsd;
47 dsd.set_name("benchmark");
48 BenchmarkDataSource::Register(dsd);
49 perfetto::TrackEvent::Register();
50
51 perfetto::TraceConfig cfg;
52 cfg.add_buffers()->set_size_kb(1024);
53 auto* ds_cfg = cfg.add_data_sources()->mutable_config();
54 ds_cfg->set_name(data_source_name);
55 auto tracing_session =
56 perfetto::Tracing::NewTrace(perfetto::kInProcessBackend);
57 tracing_session->Setup(cfg);
58 tracing_session->StartBlocking();
59 return tracing_session;
60 }
61
BM_TracingDataSourceLambda(benchmark::State & state)62 static void BM_TracingDataSourceLambda(benchmark::State& state) {
63 auto tracing_session = StartTracing("benchmark");
64
65 while (state.KeepRunning()) {
66 BenchmarkDataSource::Trace([&](BenchmarkDataSource::TraceContext ctx) {
67 auto packet = ctx.NewTracePacket();
68 packet->set_timestamp(42);
69 packet->set_for_testing()->set_str("benchmark");
70 });
71 benchmark::ClobberMemory();
72 }
73
74 tracing_session->StopBlocking();
75 PERFETTO_CHECK(!tracing_session->ReadTraceBlocking().empty());
76 }
77
BM_TracingTrackEventDisabled(benchmark::State & state)78 static void BM_TracingTrackEventDisabled(benchmark::State& state) {
79 while (state.KeepRunning()) {
80 TRACE_EVENT_BEGIN("benchmark", "DisabledEvent");
81 benchmark::ClobberMemory();
82 }
83 }
84
BM_TracingTrackEventBasic(benchmark::State & state)85 static void BM_TracingTrackEventBasic(benchmark::State& state) {
86 auto tracing_session = StartTracing("track_event");
87
88 while (state.KeepRunning()) {
89 TRACE_EVENT_BEGIN("benchmark", "Event");
90 benchmark::ClobberMemory();
91 }
92
93 tracing_session->StopBlocking();
94 PERFETTO_CHECK(!tracing_session->ReadTraceBlocking().empty());
95 }
96
BM_TracingTrackEventDebugAnnotations(benchmark::State & state)97 static void BM_TracingTrackEventDebugAnnotations(benchmark::State& state) {
98 auto tracing_session = StartTracing("track_event");
99
100 while (state.KeepRunning()) {
101 TRACE_EVENT_BEGIN("benchmark", "Event", "value", 42);
102 benchmark::ClobberMemory();
103 }
104
105 tracing_session->StopBlocking();
106 PERFETTO_CHECK(!tracing_session->ReadTraceBlocking().empty());
107 }
108
BM_TracingTrackEventLambda(benchmark::State & state)109 static void BM_TracingTrackEventLambda(benchmark::State& state) {
110 auto tracing_session = StartTracing("track_event");
111
112 while (state.KeepRunning()) {
113 TRACE_EVENT_BEGIN("benchmark", "Event", [&](perfetto::EventContext ctx) {
114 auto* log = ctx.event()->set_log_message();
115 log->set_source_location_iid(42);
116 log->set_body_iid(1234);
117 });
118 benchmark::ClobberMemory();
119 }
120
121 tracing_session->StopBlocking();
122 PERFETTO_CHECK(!tracing_session->ReadTraceBlocking().empty());
123 }
124
125 } // namespace
126
127 BENCHMARK(BM_TracingDataSourceDisabled);
128 BENCHMARK(BM_TracingDataSourceLambda);
129 BENCHMARK(BM_TracingTrackEventBasic);
130 BENCHMARK(BM_TracingTrackEventDebugAnnotations);
131 BENCHMARK(BM_TracingTrackEventDisabled);
132 BENCHMARK(BM_TracingTrackEventLambda);
133