• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #include <chrono>
19 #include <thread>
20 
21 // This source file is built in two ways:
22 // 1. As part of the regular GN build, against standard includes.
23 // 2. To test that the amalgmated SDK works, against the perfetto.h source.
24 
25 #ifdef PERFETTO_AMALGAMATED_SDK_TEST
26 #include "perfetto.h"
27 #else
28 #include "perfetto/tracing.h"
29 #include "protos/perfetto/config/gpu/gpu_counter_config.pbzero.h"
30 #include "protos/perfetto/trace/gpu/gpu_counter_event.pbzero.h"
31 #include "protos/perfetto/trace/test_event.pbzero.h"
32 #include "protos/perfetto/trace/trace_packet.pbzero.h"
33 #endif
34 
35 // Deliberately not pulling any non-public perfetto header to spot accidental
36 // header public -> non-public dependency while building this file.
37 namespace {
38 class MyDataSource : public perfetto::DataSource<MyDataSource> {
39  public:
OnSetup(const SetupArgs & args)40   void OnSetup(const SetupArgs& args) override {
41     // This can be used to access the domain-specific DataSourceConfig, via
42     // args.config->xxx_config_raw().
43     const std::string& config_raw = args.config->gpu_counter_config_raw();
44     perfetto::protos::pbzero::GpuCounterConfig::Decoder config(config_raw);
45 
46     int sample_period = 0;
47     if (config.has_counter_period_ns())
48       sample_period = static_cast<int>(config.counter_period_ns());
49 
50     std::vector<uint32_t> counter_ids;
51     for (auto it = config.counter_ids(); it; ++it) {
52       uint32_t counter_id = it->as_uint32();
53       if (counter_id > 0) {
54         counter_ids.push_back(counter_id);
55       }
56     }
57 
58     PERFETTO_ILOG(
59         "OnSetup called, name: %s,"
60         "counter_period_ms: %d, tracing_session_id: %" PRIu64
61         " num counters: %zu",
62         args.config->name().c_str(), int(sample_period / 1000000),
63         args.config->tracing_session_id(), counter_ids.size());
64   }
65 
OnStart(const StartArgs &)66   void OnStart(const StartArgs&) override { PERFETTO_ILOG("OnStart called"); }
67 
OnStop(const StopArgs & stop_args)68   void OnStop(const StopArgs& stop_args) override {
69     PERFETTO_ILOG("OnStop called");
70 
71     auto stop_closure = stop_args.HandleStopAsynchronously();
72 
73     // It is possible to trace on stop as well, but doing so requires manually
74     // calling Flush() at the end.
75     MyDataSource::Trace([](MyDataSource::TraceContext ctx) {
76       PERFETTO_LOG("Tracing lambda called while stopping");
77       // This block here is to auto-finalize the packet before calling Flush.
78       {
79         auto packet = ctx.NewTracePacket();
80         packet->set_timestamp(999);
81       }
82       ctx.Flush();
83     });
84 
85     stop_closure();
86   }
87 };
88 
89 }  // namespace
90 
91 PERFETTO_DECLARE_DATA_SOURCE_STATIC_MEMBERS(MyDataSource);
92 PERFETTO_DEFINE_DATA_SOURCE_STATIC_MEMBERS(MyDataSource);
93 
main()94 int main() {
95   perfetto::TracingInitArgs args;
96   args.backends = perfetto::kSystemBackend;
97   perfetto::Tracing::Initialize(args);
98 
99   // DataSourceDescriptor can be used to advertise domain-specific features.
100   perfetto::DataSourceDescriptor dsd;
101   dsd.set_name("com.example.mytrace");
102   MyDataSource::Register(dsd);
103 
104   for (;;) {
105     MyDataSource::Trace([](MyDataSource::TraceContext ctx) {
106       PERFETTO_LOG("Tracing lambda called");
107       auto packet = ctx.NewTracePacket();
108       packet->set_timestamp(42);
109       auto* gpu_packet = packet->set_gpu_counter_event();
110       auto* cnt = gpu_packet->add_counters();
111       cnt->set_counter_id(1);
112     });
113     std::this_thread::sleep_for(std::chrono::seconds(1));
114   }
115 }
116