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