blob: f8dfcc49334856a41c33e5e20590494f4df46647 [file] [log] [blame]
Primiano Tucci0b651b82019-06-03 17:16:23 +01001/*
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
Primiano Tucci0b651b82019-06-03 17:16:23 +010017
Primiano Tuccic64b05f2019-12-05 10:30:52 +000018#include <chrono>
Primiano Tuccidd5ebc92019-07-25 01:09:37 +010019#include <thread>
20
Primiano Tucci93f45b22019-12-05 22:37:05 +000021// 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"
Primiano Tucci2cfc7fd2019-11-28 16:07:27 +000029#include "protos/perfetto/config/gpu/gpu_counter_config.pbzero.h"
30#include "protos/perfetto/trace/gpu/gpu_counter_event.pbzero.h"
Primiano Tucci355b8c82019-08-29 08:37:51 +020031#include "protos/perfetto/trace/test_event.pbzero.h"
32#include "protos/perfetto/trace/trace_packet.pbzero.h"
Primiano Tucci93f45b22019-12-05 22:37:05 +000033#endif
Primiano Tucci0b651b82019-06-03 17:16:23 +010034
35// Deliberately not pulling any non-public perfetto header to spot accidental
36// header public -> non-public dependency while building this file.
Primiano Tuccidd5ebc92019-07-25 01:09:37 +010037namespace {
Primiano Tucci0b651b82019-06-03 17:16:23 +010038class MyDataSource : public perfetto::DataSource<MyDataSource> {
39 public:
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().
Primiano Tucci2cfc7fd2019-11-28 16:07:27 +000043 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());
Primiano Tucci0b651b82019-06-03 17:16:23 +010064 }
65
66 void OnStart(const StartArgs&) override { PERFETTO_ILOG("OnStart called"); }
67
Primiano Tucci2cfc7fd2019-11-28 16:07:27 +000068 void OnStop(const StopArgs& stop_args) override {
Primiano Tuccidd5ebc92019-07-25 01:09:37 +010069 PERFETTO_ILOG("OnStop called");
70
Primiano Tucci2cfc7fd2019-11-28 16:07:27 +000071 auto stop_closure = stop_args.HandleStopAsynchronously();
Primiano Tuccidd5ebc92019-07-25 01:09:37 +010072
Primiano Tucci2cfc7fd2019-11-28 16:07:27 +000073 // 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();
Primiano Tuccidd5ebc92019-07-25 01:09:37 +010083 });
Primiano Tucci2cfc7fd2019-11-28 16:07:27 +000084
85 stop_closure();
Primiano Tuccidd5ebc92019-07-25 01:09:37 +010086 }
Primiano Tucci0b651b82019-06-03 17:16:23 +010087};
88
Primiano Tuccidd5ebc92019-07-25 01:09:37 +010089} // namespace
90
Sami Kyostilaf43e6502021-01-06 14:18:49 +000091PERFETTO_DECLARE_DATA_SOURCE_STATIC_MEMBERS(MyDataSource);
Primiano Tucci0b651b82019-06-03 17:16:23 +010092PERFETTO_DEFINE_DATA_SOURCE_STATIC_MEMBERS(MyDataSource);
93
94int 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);
Primiano Tucci2cfc7fd2019-11-28 16:07:27 +0000109 auto* gpu_packet = packet->set_gpu_counter_event();
110 auto* cnt = gpu_packet->add_counters();
111 cnt->set_counter_id(1);
Primiano Tucci0b651b82019-06-03 17:16:23 +0100112 });
Primiano Tuccic64b05f2019-12-05 10:30:52 +0000113 std::this_thread::sleep_for(std::chrono::seconds(1));
Primiano Tucci0b651b82019-06-03 17:16:23 +0100114 }
115}