Primiano Tucci | 0b651b8 | 2019-06-03 17:16:23 +0100 | [diff] [blame] | 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 | #include "perfetto/tracing.h" |
| 18 | |
Primiano Tucci | dd5ebc9 | 2019-07-25 01:09:37 +0100 | [diff] [blame] | 19 | #include <thread> |
| 20 | |
Primiano Tucci | 355b8c8 | 2019-08-29 08:37:51 +0200 | [diff] [blame^] | 21 | #include "protos/perfetto/trace/test_event.pbzero.h" |
| 22 | #include "protos/perfetto/trace/trace_packet.pbzero.h" |
Primiano Tucci | 0b651b8 | 2019-06-03 17:16:23 +0100 | [diff] [blame] | 23 | |
| 24 | // Deliberately not pulling any non-public perfetto header to spot accidental |
| 25 | // header public -> non-public dependency while building this file. |
| 26 | |
Primiano Tucci | dd5ebc9 | 2019-07-25 01:09:37 +0100 | [diff] [blame] | 27 | namespace { |
| 28 | |
Primiano Tucci | 0b651b8 | 2019-06-03 17:16:23 +0100 | [diff] [blame] | 29 | class MyDataSource : public perfetto::DataSource<MyDataSource> { |
| 30 | public: |
| 31 | void OnSetup(const SetupArgs& args) override { |
| 32 | // This can be used to access the domain-specific DataSourceConfig, via |
| 33 | // args.config->xxx_config_raw(). |
| 34 | PERFETTO_ILOG("OnSetup called, name: %s", args.config->name().c_str()); |
| 35 | } |
| 36 | |
| 37 | void OnStart(const StartArgs&) override { PERFETTO_ILOG("OnStart called"); } |
| 38 | |
Primiano Tucci | dd5ebc9 | 2019-07-25 01:09:37 +0100 | [diff] [blame] | 39 | void OnStop(const StopArgs& args) override { |
| 40 | PERFETTO_ILOG("OnStop called"); |
| 41 | |
| 42 | // Demonstrates the ability to defer stop and handle it asynchronously, |
| 43 | // writing data at the very end of the trace. |
| 44 | auto stop_closure = args.HandleStopAsynchronously(); |
| 45 | std::thread another_thread([stop_closure] { |
| 46 | sleep(2); |
| 47 | MyDataSource::Trace([](MyDataSource::TraceContext ctx) { |
| 48 | PERFETTO_LOG("Tracing lambda called while stopping"); |
| 49 | auto packet = ctx.NewTracePacket(); |
| 50 | packet->set_for_testing()->set_str("event recorded while stopping"); |
| 51 | packet->Finalize(); // Required because of the Flush below. |
| 52 | |
| 53 | // This explicit Flush() is required because the service doesn't issue |
| 54 | // any other flush requests after the Stop() signal. |
| 55 | ctx.Flush(); |
| 56 | }); |
| 57 | stop_closure(); |
| 58 | }); |
| 59 | another_thread.detach(); |
| 60 | } |
Primiano Tucci | 0b651b8 | 2019-06-03 17:16:23 +0100 | [diff] [blame] | 61 | }; |
| 62 | |
Primiano Tucci | dd5ebc9 | 2019-07-25 01:09:37 +0100 | [diff] [blame] | 63 | } // namespace |
| 64 | |
Primiano Tucci | 0b651b8 | 2019-06-03 17:16:23 +0100 | [diff] [blame] | 65 | PERFETTO_DEFINE_DATA_SOURCE_STATIC_MEMBERS(MyDataSource); |
| 66 | |
| 67 | int main() { |
| 68 | perfetto::TracingInitArgs args; |
| 69 | args.backends = perfetto::kSystemBackend; |
| 70 | perfetto::Tracing::Initialize(args); |
| 71 | |
| 72 | // DataSourceDescriptor can be used to advertise domain-specific features. |
| 73 | perfetto::DataSourceDescriptor dsd; |
| 74 | dsd.set_name("com.example.mytrace"); |
| 75 | MyDataSource::Register(dsd); |
| 76 | |
| 77 | for (;;) { |
| 78 | MyDataSource::Trace([](MyDataSource::TraceContext ctx) { |
| 79 | PERFETTO_LOG("Tracing lambda called"); |
| 80 | auto packet = ctx.NewTracePacket(); |
| 81 | packet->set_timestamp(42); |
| 82 | packet->set_for_testing()->set_str("event 1"); |
| 83 | }); |
| 84 | sleep(1); |
| 85 | } |
| 86 | } |