-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample.cc
79 lines (66 loc) · 2.63 KB
/
example.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "perfetto.h"
// Deliberately not pulling any non-public perfetto header to spot accidental
// header public -> non-public dependency while building this file.
class MyDataSource : public perfetto::DataSource<MyDataSource> {
public:
void OnSetup(const SetupArgs& args) override {
// This can be used to access the domain-specific DataSourceConfig, via
// args.config->xxx_config_raw().
PERFETTO_ILOG("OnSetup called, name: %s", args.config->name().c_str());
const std::string& config_raw = args.config->gpu_counter_config_raw();
perfetto::protos::pbzero::GpuCounterConfig::Decoder config(config_raw);
}
void OnStart(const StartArgs&) override { PERFETTO_ILOG("OnStart called"); }
void OnStop(const StopArgs& stop_args) override {
PERFETTO_ILOG("OnStop called");
auto stop_closure = stop_args.HandleStopAsynchronously();
// It is possible to trace on stop as well, but doing so requires manually
// calling Flush() at the end.
MyDataSource::Trace([](MyDataSource::TraceContext ctx) {
PERFETTO_LOG("Tracing lambda called while stopping");
// This block here is to auto-finalize the packet before calling Flush.
{
auto packet = ctx.NewTracePacket();
packet->set_timestamp(999);
}
ctx.Flush();
});
stop_closure();
}
};
PERFETTO_DEFINE_DATA_SOURCE_STATIC_MEMBERS(MyDataSource);
int main() {
perfetto::TracingInitArgs args;
args.backends = perfetto::kSystemBackend;
perfetto::Tracing::Initialize(args);
// DataSourceDescriptor can be used to advertise domain-specific features.
perfetto::DataSourceDescriptor dsd;
dsd.set_name("com.example.mytrace");
MyDataSource::Register(dsd);
for (;;) {
MyDataSource::Trace([](MyDataSource::TraceContext ctx) {
PERFETTO_LOG("Tracing lambda called");
auto packet = ctx.NewTracePacket();
packet->set_timestamp(42);
auto* gpu_packet = packet->set_gpu_counter_event();
auto* cnt = gpu_packet->add_counters();
cnt->set_counter_id(1);
});
sleep(1);
}
}