• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_TRACED_PROBES_PROBES_PRODUCER_H_
18 #define SRC_TRACED_PROBES_PROBES_PRODUCER_H_
19 
20 #include <memory>
21 #include <unordered_map>
22 #include <utility>
23 
24 #include "perfetto/base/task_runner.h"
25 #include "perfetto/ext/base/watchdog.h"
26 #include "perfetto/ext/base/weak_ptr.h"
27 #include "perfetto/ext/tracing/core/producer.h"
28 #include "perfetto/ext/tracing/core/trace_writer.h"
29 #include "perfetto/ext/tracing/core/tracing_service.h"
30 #include "src/traced/probes/filesystem/inode_file_data_source.h"
31 #include "src/traced/probes/ftrace/ftrace_controller.h"
32 #include "src/traced/probes/ftrace/ftrace_metadata.h"
33 
34 #include "protos/perfetto/trace/filesystem/inode_file_map.pbzero.h"
35 
36 namespace perfetto {
37 
38 class ProbesDataSource;
39 
40 const uint64_t kLRUInodeCacheSize = 1000;
41 
42 class ProbesProducer : public Producer, public FtraceController::Observer {
43  public:
44   ProbesProducer();
45   ~ProbesProducer() override;
46 
47   static ProbesProducer* GetInstance();
48 
49   // Producer Impl:
50   void OnConnect() override;
51   void OnDisconnect() override;
52   void SetupDataSource(DataSourceInstanceID, const DataSourceConfig&) override;
53   void StartDataSource(DataSourceInstanceID, const DataSourceConfig&) override;
54   void StopDataSource(DataSourceInstanceID) override;
55   void OnTracingSetup() override;
56   void Flush(FlushRequestID,
57              const DataSourceInstanceID* data_source_ids,
58              size_t num_data_sources) override;
59   void ClearIncrementalState(const DataSourceInstanceID* data_source_ids,
60                              size_t num_data_sources) override;
61 
62   // FtraceController::Observer implementation.
63   void OnFtraceDataWrittenIntoDataSourceBuffers() override;
64 
65   // Our Impl
66   void ConnectWithRetries(const char* socket_name,
67                           base::TaskRunner* task_runner);
68   std::unique_ptr<ProbesDataSource> CreateFtraceDataSource(
69       TracingSessionID session_id,
70       const DataSourceConfig& config);
71   std::unique_ptr<ProbesDataSource> CreateProcessStatsDataSource(
72       TracingSessionID session_id,
73       const DataSourceConfig& config);
74   std::unique_ptr<ProbesDataSource> CreateInodeFileDataSource(
75       TracingSessionID session_id,
76       DataSourceConfig config);
77   std::unique_ptr<ProbesDataSource> CreateSysStatsDataSource(
78       TracingSessionID session_id,
79       const DataSourceConfig& config);
80   std::unique_ptr<ProbesDataSource> CreateAndroidPowerDataSource(
81       TracingSessionID session_id,
82       const DataSourceConfig& config);
83   std::unique_ptr<ProbesDataSource> CreateAndroidPowerStatsDataSource(
84       TracingSessionID session_id,
85       const DataSourceConfig& config);
86   std::unique_ptr<ProbesDataSource> CreateAndroidLogDataSource(
87       TracingSessionID session_id,
88       const DataSourceConfig& config);
89   std::unique_ptr<ProbesDataSource> CreatePackagesListDataSource(
90       TracingSessionID session_id,
91       const DataSourceConfig& config);
92   std::unique_ptr<ProbesDataSource> CreateMetatraceDataSource(
93       TracingSessionID session_id,
94       const DataSourceConfig& config);
95   std::unique_ptr<ProbesDataSource> CreateSystemInfoDataSource(
96       TracingSessionID session_id,
97       const DataSourceConfig& config);
98   std::unique_ptr<ProbesDataSource> CreateInitialDisplayStateDataSource(
99       TracingSessionID session_id,
100       const DataSourceConfig& config);
101   void ActivateTrigger(std::string trigger);
102 
103  private:
104   static ProbesProducer* instance_;
105 
106   enum State {
107     kNotStarted = 0,
108     kNotConnected,
109     kConnecting,
110     kConnected,
111   };
112 
113   ProbesProducer(const ProbesProducer&) = delete;
114   ProbesProducer& operator=(const ProbesProducer&) = delete;
115 
116   void Connect();
117   void Restart();
118   void ResetConnectionBackoff();
119   void IncreaseConnectionBackoff();
120   void OnDataSourceFlushComplete(FlushRequestID, DataSourceInstanceID);
121   void OnFlushTimeout(FlushRequestID);
122 
123   State state_ = kNotStarted;
124   base::TaskRunner* task_runner_ = nullptr;
125   std::unique_ptr<TracingService::ProducerEndpoint> endpoint_;
126   std::unique_ptr<FtraceController> ftrace_;
127   bool ftrace_creation_failed_ = false;
128   uint32_t connection_backoff_ms_ = 0;
129   const char* socket_name_ = nullptr;
130 
131   // Owning map for all active data sources.
132   std::unordered_map<DataSourceInstanceID, std::unique_ptr<ProbesDataSource>>
133       data_sources_;
134 
135   // Keeps (pointers to) data sources ordered by session id.
136   std::unordered_multimap<TracingSessionID, ProbesDataSource*>
137       session_data_sources_;
138 
139   std::unordered_multimap<FlushRequestID, DataSourceInstanceID>
140       pending_flushes_;
141 
142   std::unordered_map<DataSourceInstanceID, base::Watchdog::Timer> watchdogs_;
143   LRUInodeCache cache_{kLRUInodeCacheSize};
144   std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>
145       system_inodes_;
146 
147   base::WeakPtrFactory<ProbesProducer> weak_factory_;  // Keep last.
148 };
149 
150 }  // namespace perfetto
151 
152 #endif  // SRC_TRACED_PROBES_PROBES_PRODUCER_H_
153