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 | |
Primiano Tucci | 0b651b8 | 2019-06-03 17:16:23 +0100 | [diff] [blame] | 17 | |
Primiano Tucci | c64b05f | 2019-12-05 10:30:52 +0000 | [diff] [blame] | 18 | #include <chrono> |
Primiano Tucci | dd5ebc9 | 2019-07-25 01:09:37 +0100 | [diff] [blame] | 19 | #include <thread> |
| 20 | |
Primiano Tucci | 93f45b2 | 2019-12-05 22:37:05 +0000 | [diff] [blame] | 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" |
Primiano Tucci | 2cfc7fd | 2019-11-28 16:07:27 +0000 | [diff] [blame] | 29 | #include "protos/perfetto/config/gpu/gpu_counter_config.pbzero.h" |
| 30 | #include "protos/perfetto/trace/gpu/gpu_counter_event.pbzero.h" |
Primiano Tucci | 355b8c8 | 2019-08-29 08:37:51 +0200 | [diff] [blame] | 31 | #include "protos/perfetto/trace/test_event.pbzero.h" |
| 32 | #include "protos/perfetto/trace/trace_packet.pbzero.h" |
Primiano Tucci | 93f45b2 | 2019-12-05 22:37:05 +0000 | [diff] [blame] | 33 | #endif |
Primiano Tucci | 0b651b8 | 2019-06-03 17:16:23 +0100 | [diff] [blame] | 34 | |
| 35 | // Deliberately not pulling any non-public perfetto header to spot accidental |
| 36 | // header public -> non-public dependency while building this file. |
Primiano Tucci | dd5ebc9 | 2019-07-25 01:09:37 +0100 | [diff] [blame] | 37 | namespace { |
Primiano Tucci | 0b651b8 | 2019-06-03 17:16:23 +0100 | [diff] [blame] | 38 | class 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 Tucci | 2cfc7fd | 2019-11-28 16:07:27 +0000 | [diff] [blame] | 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()); |
Primiano Tucci | 0b651b8 | 2019-06-03 17:16:23 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void OnStart(const StartArgs&) override { PERFETTO_ILOG("OnStart called"); } |
| 67 | |
Primiano Tucci | 2cfc7fd | 2019-11-28 16:07:27 +0000 | [diff] [blame] | 68 | void OnStop(const StopArgs& stop_args) override { |
Primiano Tucci | dd5ebc9 | 2019-07-25 01:09:37 +0100 | [diff] [blame] | 69 | PERFETTO_ILOG("OnStop called"); |
| 70 | |
Primiano Tucci | 2cfc7fd | 2019-11-28 16:07:27 +0000 | [diff] [blame] | 71 | auto stop_closure = stop_args.HandleStopAsynchronously(); |
Primiano Tucci | dd5ebc9 | 2019-07-25 01:09:37 +0100 | [diff] [blame] | 72 | |
Primiano Tucci | 2cfc7fd | 2019-11-28 16:07:27 +0000 | [diff] [blame] | 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(); |
Primiano Tucci | dd5ebc9 | 2019-07-25 01:09:37 +0100 | [diff] [blame] | 83 | }); |
Primiano Tucci | 2cfc7fd | 2019-11-28 16:07:27 +0000 | [diff] [blame] | 84 | |
| 85 | stop_closure(); |
Primiano Tucci | dd5ebc9 | 2019-07-25 01:09:37 +0100 | [diff] [blame] | 86 | } |
Primiano Tucci | 0b651b8 | 2019-06-03 17:16:23 +0100 | [diff] [blame] | 87 | }; |
| 88 | |
Primiano Tucci | dd5ebc9 | 2019-07-25 01:09:37 +0100 | [diff] [blame] | 89 | } // namespace |
| 90 | |
Sami Kyostila | f43e650 | 2021-01-06 14:18:49 +0000 | [diff] [blame] | 91 | PERFETTO_DECLARE_DATA_SOURCE_STATIC_MEMBERS(MyDataSource); |
Primiano Tucci | 0b651b8 | 2019-06-03 17:16:23 +0100 | [diff] [blame] | 92 | PERFETTO_DEFINE_DATA_SOURCE_STATIC_MEMBERS(MyDataSource); |
| 93 | |
| 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); |
Primiano Tucci | 2cfc7fd | 2019-11-28 16:07:27 +0000 | [diff] [blame] | 109 | auto* gpu_packet = packet->set_gpu_counter_event(); |
| 110 | auto* cnt = gpu_packet->add_counters(); |
| 111 | cnt->set_counter_id(1); |
Primiano Tucci | 0b651b8 | 2019-06-03 17:16:23 +0100 | [diff] [blame] | 112 | }); |
Primiano Tucci | c64b05f | 2019-12-05 10:30:52 +0000 | [diff] [blame] | 113 | std::this_thread::sleep_for(std::chrono::seconds(1)); |
Primiano Tucci | 0b651b8 | 2019-06-03 17:16:23 +0100 | [diff] [blame] | 114 | } |
| 115 | } |