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/base/watchdog.h" 26 #include "perfetto/base/weak_ptr.h" 27 #include "perfetto/tracing/core/producer.h" 28 #include "perfetto/tracing/core/trace_writer.h" 29 #include "perfetto/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 "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 // Producer Impl: 48 void OnConnect() override; 49 void OnDisconnect() override; 50 void SetupDataSource(DataSourceInstanceID, const DataSourceConfig&) override; 51 void StartDataSource(DataSourceInstanceID, const DataSourceConfig&) override; 52 void StopDataSource(DataSourceInstanceID) override; 53 void OnTracingSetup() override; 54 void Flush(FlushRequestID, 55 const DataSourceInstanceID* data_source_ids, 56 size_t num_data_sources) override; 57 void ClearIncrementalState(const DataSourceInstanceID* data_source_ids, 58 size_t num_data_sources) override; 59 60 // FtraceController::Observer implementation. 61 void OnFtraceDataWrittenIntoDataSourceBuffers() override; 62 63 // Our Impl 64 void ConnectWithRetries(const char* socket_name, 65 base::TaskRunner* task_runner); 66 std::unique_ptr<ProbesDataSource> CreateFtraceDataSource( 67 TracingSessionID session_id, 68 const DataSourceConfig& config); 69 std::unique_ptr<ProbesDataSource> CreateProcessStatsDataSource( 70 TracingSessionID session_id, 71 const DataSourceConfig& config); 72 std::unique_ptr<ProbesDataSource> CreateInodeFileDataSource( 73 TracingSessionID session_id, 74 DataSourceConfig config); 75 std::unique_ptr<ProbesDataSource> CreateSysStatsDataSource( 76 TracingSessionID session_id, 77 const DataSourceConfig& config); 78 std::unique_ptr<ProbesDataSource> CreateAndroidPowerDataSource( 79 TracingSessionID session_id, 80 const DataSourceConfig& config); 81 std::unique_ptr<ProbesDataSource> CreateAndroidLogDataSource( 82 TracingSessionID session_id, 83 const DataSourceConfig& config); 84 std::unique_ptr<ProbesDataSource> CreatePackagesListDataSource( 85 TracingSessionID session_id, 86 const DataSourceConfig& config); 87 88 private: 89 enum State { 90 kNotStarted = 0, 91 kNotConnected, 92 kConnecting, 93 kConnected, 94 }; 95 96 ProbesProducer(const ProbesProducer&) = delete; 97 ProbesProducer& operator=(const ProbesProducer&) = delete; 98 99 void Connect(); 100 void Restart(); 101 void ResetConnectionBackoff(); 102 void IncreaseConnectionBackoff(); 103 void OnDataSourceFlushComplete(FlushRequestID, DataSourceInstanceID); 104 void OnFlushTimeout(FlushRequestID); 105 106 State state_ = kNotStarted; 107 base::TaskRunner* task_runner_ = nullptr; 108 std::unique_ptr<TracingService::ProducerEndpoint> endpoint_; 109 std::unique_ptr<FtraceController> ftrace_; 110 bool ftrace_creation_failed_ = false; 111 uint32_t connection_backoff_ms_ = 0; 112 const char* socket_name_ = nullptr; 113 114 // Owning map for all active data sources. 115 std::unordered_map<DataSourceInstanceID, std::unique_ptr<ProbesDataSource>> 116 data_sources_; 117 118 // Keeps (pointers to) data sources ordered by session id. 119 std::unordered_multimap<TracingSessionID, ProbesDataSource*> 120 session_data_sources_; 121 122 std::unordered_multimap<FlushRequestID, DataSourceInstanceID> 123 pending_flushes_; 124 125 std::unordered_map<DataSourceInstanceID, base::Watchdog::Timer> watchdogs_; 126 LRUInodeCache cache_{kLRUInodeCacheSize}; 127 std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>> 128 system_inodes_; 129 130 base::WeakPtrFactory<ProbesProducer> weak_factory_; // Keep last. 131 }; 132 133 } // namespace perfetto 134 135 #endif // SRC_TRACED_PROBES_PROBES_PRODUCER_H_ 136