• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/perfetto_cmd/trigger_producer.h"
18 
19 #include <memory>
20 
21 #include "perfetto/base/logging.h"
22 #include "perfetto/tracing/core/producer.h"
23 #include "perfetto/tracing/ipc/producer_ipc_client.h"
24 #include "src/tracing/ipc/default_socket.h"
25 
26 namespace perfetto {
27 
28 class DataSourceConfig;
29 
TriggerProducer(base::TaskRunner * task_runner,std::function<void (bool)> callback,const std::vector<std::string> * const triggers)30 TriggerProducer::TriggerProducer(base::TaskRunner* task_runner,
31                                  std::function<void(bool)> callback,
32                                  const std::vector<std::string>* const triggers)
33     : task_runner_(task_runner),
34       callback_(std::move(callback)),
35       triggers_(triggers),
36       producer_endpoint_(ProducerIPCClient::Connect(GetProducerSocket(),
37                                                     this,
38                                                     "perfetto_cmd_producer",
39                                                     task_runner)),
40       weak_factory_(this) {
41   // Give the socket up to 10 seconds to attach and send the triggers before
42   // reporting a failure.
43   auto weak_this = weak_factory_.GetWeakPtr();
44   task_runner_->PostDelayedTask(
45       [weak_this]() {
46         if (!weak_this || weak_this->issued_callback_)
47           return;
48         weak_this->issued_callback_ = true;
49         weak_this->callback_(false);
50       },
51       10000);
52 }
53 
~TriggerProducer()54 TriggerProducer::~TriggerProducer() {}
55 
OnConnect()56 void TriggerProducer::OnConnect() {
57   PERFETTO_DLOG("Producer connected, sending triggers.");
58   // Send activation signal.
59   producer_endpoint_->ActivateTriggers(*triggers_);
60   auto weak_this = weak_factory_.GetWeakPtr();
61   task_runner_->PostTask([weak_this]() {
62     if (!weak_this || weak_this->issued_callback_)
63       return;
64     weak_this->issued_callback_ = true;
65     weak_this->callback_(true);
66   });
67 }
68 
OnDisconnect()69 void TriggerProducer::OnDisconnect() {}
70 
OnTracingSetup()71 void TriggerProducer::OnTracingSetup() {}
72 
SetupDataSource(DataSourceInstanceID,const DataSourceConfig &)73 void TriggerProducer::SetupDataSource(DataSourceInstanceID,
74                                       const DataSourceConfig&) {
75   PERFETTO_DFATAL("Attempted to SetupDataSource() on commandline producer");
76 }
StartDataSource(DataSourceInstanceID,const DataSourceConfig &)77 void TriggerProducer::StartDataSource(DataSourceInstanceID,
78                                       const DataSourceConfig&) {
79   PERFETTO_DFATAL("Attempted to StartDataSource() on commandline producer");
80 }
StopDataSource(DataSourceInstanceID)81 void TriggerProducer::StopDataSource(DataSourceInstanceID) {
82   PERFETTO_DFATAL("Attempted to StopDataSource() on commandline producer");
83 }
Flush(FlushRequestID,const DataSourceInstanceID *,size_t)84 void TriggerProducer::Flush(FlushRequestID,
85                             const DataSourceInstanceID*,
86                             size_t) {
87   PERFETTO_DFATAL("Attempted to Flush() on commandline producer");
88 }
89 
ClearIncrementalState(const DataSourceInstanceID *,size_t)90 void TriggerProducer::ClearIncrementalState(const DataSourceInstanceID*,
91                                             size_t) {
92   PERFETTO_DFATAL(
93       "Attempted to ClearIncrementalState() on commandline producer");
94 }
95 
96 }  // namespace perfetto
97