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, 60 FlushFlags) override; 61 void ClearIncrementalState(const DataSourceInstanceID* data_source_ids, 62 size_t num_data_sources) override; 63 64 // FtraceController::Observer implementation. 65 void OnFtraceDataWrittenIntoDataSourceBuffers() override; 66 67 // Our Impl 68 void ConnectWithRetries(const char* socket_name, 69 base::TaskRunner* task_runner); 70 71 // Constructs an instance of a data source of type T. 72 template <typename T> 73 std::unique_ptr<ProbesDataSource> CreateDSInstance( 74 TracingSessionID session_id, 75 const DataSourceConfig& config); 76 77 void ActivateTrigger(std::string trigger); 78 79 // Calls `cb` when all data sources have been registered. SetAllDataSourcesRegisteredCb(std::function<void ()> cb)80 void SetAllDataSourcesRegisteredCb(std::function<void()> cb) { 81 all_data_sources_registered_cb_ = cb; 82 } 83 84 private: 85 static ProbesProducer* instance_; 86 87 enum State { 88 kNotStarted = 0, 89 kNotConnected, 90 kConnecting, 91 kConnected, 92 }; 93 94 ProbesProducer(const ProbesProducer&) = delete; 95 ProbesProducer& operator=(const ProbesProducer&) = delete; 96 97 void Connect(); 98 void Restart(); 99 void ResetConnectionBackoff(); 100 void IncreaseConnectionBackoff(); 101 void OnDataSourceFlushComplete(FlushRequestID, DataSourceInstanceID); 102 void OnFlushTimeout(FlushRequestID); 103 104 State state_ = kNotStarted; 105 base::TaskRunner* task_runner_ = nullptr; 106 std::unique_ptr<TracingService::ProducerEndpoint> endpoint_; 107 std::unique_ptr<FtraceController> ftrace_; 108 bool ftrace_creation_failed_ = false; 109 uint32_t connection_backoff_ms_ = 0; 110 const char* socket_name_ = nullptr; 111 112 // Owning map for all active data sources. 113 std::unordered_map<DataSourceInstanceID, std::unique_ptr<ProbesDataSource>> 114 data_sources_; 115 116 // Keeps (pointers to) data sources grouped by session id and data source 117 // type. The pointers do not own the data sources (they're owned by 118 // data_sources_). 119 // 120 // const ProbesDataSource::Descriptor* identifies the type. 121 // 122 // Used by OnFtraceDataWrittenIntoDataSourceBuffers(). 123 std::unordered_map< 124 TracingSessionID, 125 std::unordered_multimap<const ProbesDataSource::Descriptor*, 126 ProbesDataSource*>> 127 session_data_sources_; 128 129 std::unordered_multimap<FlushRequestID, DataSourceInstanceID> 130 pending_flushes_; 131 132 std::function<void()> all_data_sources_registered_cb_; 133 134 std::unordered_map<DataSourceInstanceID, base::Watchdog::Timer> watchdogs_; 135 LRUInodeCache cache_{kLRUInodeCacheSize}; 136 std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>> 137 system_inodes_; 138 139 base::WeakPtrFactory<ProbesProducer> weak_factory_; // Keep last. 140 }; 141 142 } // namespace perfetto 143 144 #endif // SRC_TRACED_PROBES_PROBES_PRODUCER_H_ 145