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