1 /*
2  * Copyright (C) 2018 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 #ifndef SRC_PERFETTO_CMD_PERFETTO_CMD_H_
18 #define SRC_PERFETTO_CMD_PERFETTO_CMD_H_
19 
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include <time.h>
25 
26 #include "perfetto/base/build_config.h"
27 #include "perfetto/base/scoped_file.h"
28 #include "perfetto/base/unix_task_runner.h"
29 #include "perfetto/tracing/core/consumer.h"
30 #include "perfetto/tracing/ipc/consumer_ipc_client.h"
31 #include "src/perfetto_cmd/rate_limiter.h"
32 
33 #include "src/perfetto_cmd/perfetto_cmd_state.pb.h"
34 
35 #if defined(PERFETTO_OS_ANDROID)
36 #include "perfetto/base/android_task_runner.h"
37 #endif  // defined(PERFETTO_OS_ANDROID)
38 
39 
40 namespace perfetto {
41 
42 // Temporary directory for DropBox traces. Note that this is automatically
43 // created by the system by setting setprop persist.traced.enable=1.
44 extern const char* kTempDropBoxTraceDir;
45 
46 #if defined(PERFETTO_OS_ANDROID)
47 using PlatformTaskRunner = base::AndroidTaskRunner;
48 #else
49 using PlatformTaskRunner = base::UnixTaskRunner;
50 #endif
51 
52 class PerfettoCmd : public Consumer {
53  public:
54   int Main(int argc, char** argv);
55 
56   // perfetto::Consumer implementation.
57   void OnConnect() override;
58   void OnDisconnect() override;
59   void OnTracingDisabled() override;
60   void OnTraceData(std::vector<TracePacket>, bool has_more) override;
61 
62   int ctrl_c_pipe_wr() const { return *ctrl_c_pipe_wr_; }
63 
64  private:
65   bool OpenOutputFile();
66   void SetupCtrlCSignalHandler();
67   void FinalizeTraceAndExit();
68   int PrintUsage(const char* argv0);
69   void OnTimeout();
70 
71   PlatformTaskRunner task_runner_;
72   std::unique_ptr<perfetto::Service::ConsumerEndpoint> consumer_endpoint_;
73   std::unique_ptr<TraceConfig> trace_config_;
74   base::ScopedFstream trace_out_stream_;
75   std::string trace_out_path_;
76   base::ScopedFile ctrl_c_pipe_wr_;
77   base::ScopedFile ctrl_c_pipe_rd_;
78   std::string dropbox_tag_;
79   bool did_process_full_trace_ = false;
80   size_t bytes_uploaded_to_dropbox_ = 0;
81 };
82 
83 }  // namespace perfetto
84 
85 #endif  // SRC_PERFETTO_CMD_PERFETTO_CMD_H_
86