Primiano Tucci | 3324dfc | 2017-12-20 14:35:58 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2017 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 <inttypes.h> |
| 18 | |
| 19 | #include "perfetto/base/unix_task_runner.h" |
| 20 | #include "perfetto/traced/traced.h" |
| 21 | #include "perfetto/tracing/core/consumer.h" |
| 22 | #include "perfetto/tracing/core/data_source_config.h" |
| 23 | #include "perfetto/tracing/core/data_source_descriptor.h" |
| 24 | #include "perfetto/tracing/core/trace_config.h" |
| 25 | #include "perfetto/tracing/core/trace_packet.h" |
| 26 | #include "perfetto/tracing/ipc/consumer_ipc_client.h" |
| 27 | |
| 28 | #include "protos/trace_packet.pb.h" |
| 29 | |
| 30 | namespace perfetto { |
| 31 | namespace { |
| 32 | |
| 33 | class ConsumerCmd : Consumer { |
| 34 | public: |
| 35 | explicit ConsumerCmd(base::TaskRunner*, TraceConfig); |
| 36 | ~ConsumerCmd() override; |
| 37 | |
| 38 | void OnTraceTimer(); |
| 39 | |
| 40 | // Consumer implementation. |
| 41 | void OnConnect() override; |
| 42 | void OnDisconnect() override; |
| 43 | void OnTraceData(std::vector<TracePacket>, bool has_more) override; |
| 44 | |
| 45 | private: |
| 46 | base::TaskRunner* task_runner_; |
| 47 | TraceConfig trace_config_; |
| 48 | std::unique_ptr<Service::ConsumerEndpoint> consumer_endpoint_; |
| 49 | }; |
| 50 | |
| 51 | ConsumerCmd::ConsumerCmd(base::TaskRunner* task_runner, |
| 52 | TraceConfig trace_config) |
| 53 | : task_runner_(task_runner), |
| 54 | trace_config_(std::move(trace_config)), |
| 55 | consumer_endpoint_(ConsumerIPCClient::Connect(PERFETTO_CONSUMER_SOCK_NAME, |
| 56 | this, |
| 57 | task_runner)) {} |
| 58 | |
| 59 | ConsumerCmd::~ConsumerCmd() = default; |
| 60 | |
| 61 | void ConsumerCmd::OnConnect() { |
| 62 | PERFETTO_ILOG("Connected to tracing service, enabling tracing"); |
| 63 | consumer_endpoint_->EnableTracing(trace_config_); |
| 64 | // TODO(primiano): auto-disabling should be really up to the tracing service, |
| 65 | // move this responsibility there. |
| 66 | task_runner_->PostDelayedTask(std::bind(&ConsumerCmd::OnTraceTimer, this), |
| 67 | trace_config_.duration_ms()); |
| 68 | } |
| 69 | |
| 70 | void ConsumerCmd::OnTraceTimer() { |
| 71 | PERFETTO_ILOG("Timer expired, disabling timer"); |
| 72 | consumer_endpoint_->DisableTracing(); |
| 73 | consumer_endpoint_->ReadBuffers(); |
| 74 | } |
| 75 | |
| 76 | void ConsumerCmd::OnTraceData(std::vector<TracePacket> packets, bool has_more) { |
| 77 | for (TracePacket& packet : packets) { |
| 78 | bool decoded = packet.Decode(); |
| 79 | PERFETTO_ILOG("Received packet decoded: %d size: %zu", decoded, |
| 80 | packet.size()); |
| 81 | } |
| 82 | |
| 83 | if (!has_more) { |
| 84 | consumer_endpoint_->FreeBuffers(); |
| 85 | task_runner_->PostTask([] { _exit(0); }); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void ConsumerCmd::OnDisconnect() { |
| 90 | PERFETTO_ILOG("Disconnected from tracing service"); |
| 91 | } |
| 92 | |
| 93 | } // namespace |
| 94 | } // namespace perfetto |
| 95 | |
| 96 | int main() { |
| 97 | // Prepare trace config. |
| 98 | // TODO: this should read the text-version protobuf from stdin using |
| 99 | // libprotobuf_full. |
| 100 | perfetto::TraceConfig trace_config; |
| 101 | trace_config.add_buffers()->set_size_kb(4096 * 10); |
| 102 | trace_config.set_duration_ms(10000); |
| 103 | auto* ds_config = trace_config.add_data_sources()->mutable_config(); |
| 104 | ds_config->set_name("perfetto.test"); |
| 105 | ds_config->set_target_buffer(0); |
| 106 | ds_config->set_trace_category_filters("foo,bar"); |
| 107 | |
| 108 | perfetto::base::UnixTaskRunner task_runner; |
| 109 | perfetto::ConsumerCmd consumer(&task_runner, std::move(trace_config)); |
| 110 | |
| 111 | task_runner.Run(); |
| 112 | return 0; |
| 113 | } |