Lalit Maganti | 0b65b91 | 2018-01-12 18:04:31 +0000 | [diff] [blame] | 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 | |
Hector Dearman | ebf07c7 | 2018-03-13 10:31:05 +0000 | [diff] [blame] | 17 | #ifndef SRC_TRACED_PROBES_PROBES_PRODUCER_H_ |
| 18 | #define SRC_TRACED_PROBES_PROBES_PRODUCER_H_ |
| 19 | |
Florian Mayer | ec4b10e | 2018-01-17 10:38:23 +0000 | [diff] [blame] | 20 | #include <map> |
| 21 | #include <memory> |
| 22 | #include <utility> |
| 23 | |
Lalit Maganti | 79f2d7b | 2018-01-23 18:27:33 +0000 | [diff] [blame] | 24 | #include "perfetto/base/task_runner.h" |
Lalit Maganti | 0b65b91 | 2018-01-12 18:04:31 +0000 | [diff] [blame] | 25 | #include "perfetto/ftrace_reader/ftrace_controller.h" |
| 26 | #include "perfetto/tracing/core/producer.h" |
| 27 | #include "perfetto/tracing/core/trace_writer.h" |
| 28 | #include "perfetto/tracing/ipc/producer_ipc_client.h" |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 29 | #include "src/traced/probes/filesystem/inode_file_data_source.h" |
Hector Dearman | ebf07c7 | 2018-03-13 10:31:05 +0000 | [diff] [blame] | 30 | #include "src/traced/probes/process_stats_data_source.h" |
Lalit Maganti | 0b65b91 | 2018-01-12 18:04:31 +0000 | [diff] [blame] | 31 | |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 32 | #include "perfetto/trace/filesystem/inode_file_map.pbzero.h" |
| 33 | |
Lalit Maganti | 0b65b91 | 2018-01-12 18:04:31 +0000 | [diff] [blame] | 34 | namespace perfetto { |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 35 | |
Anna Zappone | 3be7b67 | 2018-03-23 17:26:10 +0000 | [diff] [blame] | 36 | const uint64_t kLRUInodeCacheSize = 1000; |
| 37 | |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 38 | class ProbesProducer : public Producer { |
Lalit Maganti | 0b65b91 | 2018-01-12 18:04:31 +0000 | [diff] [blame] | 39 | public: |
Hector Dearman | c848803 | 2018-03-02 13:12:01 +0000 | [diff] [blame] | 40 | ProbesProducer(); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 41 | ~ProbesProducer() override; |
Lalit Maganti | 0b65b91 | 2018-01-12 18:04:31 +0000 | [diff] [blame] | 42 | |
| 43 | // Producer Impl: |
| 44 | void OnConnect() override; |
| 45 | void OnDisconnect() override; |
| 46 | void CreateDataSourceInstance(DataSourceInstanceID, |
| 47 | const DataSourceConfig&) override; |
| 48 | void TearDownDataSourceInstance(DataSourceInstanceID) override; |
Isabelle Taylor | 69faa90 | 2018-03-21 15:42:03 +0000 | [diff] [blame] | 49 | void OnTracingStart() override; |
| 50 | void OnTracingStop() override; |
Lalit Maganti | 0b65b91 | 2018-01-12 18:04:31 +0000 | [diff] [blame] | 51 | |
| 52 | // Our Impl |
Hector Dearman | e50e453 | 2018-01-25 14:52:09 +0000 | [diff] [blame] | 53 | void ConnectWithRetries(const char* socket_name, |
| 54 | base::TaskRunner* task_runner); |
Hector Dearman | bcb56fb | 2018-03-26 13:59:24 +0100 | [diff] [blame] | 55 | bool CreateFtraceDataSourceInstance(TracingSessionID session_id, |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 56 | DataSourceInstanceID id, |
| 57 | const DataSourceConfig& config); |
| 58 | void CreateProcessStatsDataSourceInstance(TracingSessionID session_id, |
| 59 | DataSourceInstanceID id, |
| 60 | const DataSourceConfig& config); |
| 61 | void CreateInodeFileDataSourceInstance(TracingSessionID session_id, |
| 62 | DataSourceInstanceID id, |
Primiano Tucci | dae3565 | 2018-03-29 18:32:02 +0100 | [diff] [blame^] | 63 | DataSourceConfig config); |
Lalit Maganti | 0b65b91 | 2018-01-12 18:04:31 +0000 | [diff] [blame] | 64 | |
Hector Dearman | c848803 | 2018-03-02 13:12:01 +0000 | [diff] [blame] | 65 | void OnMetadata(const FtraceMetadata& metadata); |
| 66 | |
Lalit Maganti | 0b65b91 | 2018-01-12 18:04:31 +0000 | [diff] [blame] | 67 | private: |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 68 | using FtraceBundleHandle = |
Hector Dearman | aaa4c19 | 2018-02-19 11:57:35 +0000 | [diff] [blame] | 69 | protozero::MessageHandle<protos::pbzero::FtraceEventBundle>; |
Lalit Maganti | 0b65b91 | 2018-01-12 18:04:31 +0000 | [diff] [blame] | 70 | |
| 71 | class SinkDelegate : public FtraceSink::Delegate { |
| 72 | public: |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 73 | SinkDelegate(TracingSessionID, |
| 74 | base::TaskRunner*, |
| 75 | std::unique_ptr<TraceWriter>); |
Lalit Maganti | 0b65b91 | 2018-01-12 18:04:31 +0000 | [diff] [blame] | 76 | ~SinkDelegate() override; |
| 77 | |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 78 | TracingSessionID session_id() const { return session_id_; } |
| 79 | |
Lalit Maganti | 0b65b91 | 2018-01-12 18:04:31 +0000 | [diff] [blame] | 80 | // FtraceDelegateImpl |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 81 | FtraceBundleHandle GetBundleForCpu(size_t cpu) override; |
Hector Dearman | c848803 | 2018-03-02 13:12:01 +0000 | [diff] [blame] | 82 | void OnBundleComplete(size_t cpu, |
| 83 | FtraceBundleHandle bundle, |
| 84 | const FtraceMetadata& metadata) override; |
Lalit Maganti | 0b65b91 | 2018-01-12 18:04:31 +0000 | [diff] [blame] | 85 | |
Hector Dearman | 210dc6f | 2018-03-12 10:51:07 +0000 | [diff] [blame] | 86 | void set_sink(std::unique_ptr<FtraceSink> sink) { sink_ = std::move(sink); } |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 87 | |
| 88 | void set_ps_source(base::WeakPtr<ProcessStatsDataSource> ptr) { |
| 89 | ps_source_ = std::move(ptr); |
| 90 | } |
| 91 | const base::WeakPtr<ProcessStatsDataSource>& ps_source() const { |
| 92 | return ps_source_; |
| 93 | } |
| 94 | |
| 95 | void set_file_source(base::WeakPtr<InodeFileDataSource> ptr) { |
| 96 | file_source_ = std::move(ptr); |
| 97 | } |
| 98 | const base::WeakPtr<InodeFileDataSource>& file_source() const { |
| 99 | return file_source_; |
| 100 | } |
Lalit Maganti | 0b65b91 | 2018-01-12 18:04:31 +0000 | [diff] [blame] | 101 | |
| 102 | private: |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 103 | const TracingSessionID session_id_; |
Hector Dearman | c848803 | 2018-03-02 13:12:01 +0000 | [diff] [blame] | 104 | base::TaskRunner* task_runner_; |
Lalit Maganti | 0b65b91 | 2018-01-12 18:04:31 +0000 | [diff] [blame] | 105 | std::unique_ptr<FtraceSink> sink_ = nullptr; |
Lalit Maganti | 0b65b91 | 2018-01-12 18:04:31 +0000 | [diff] [blame] | 106 | std::unique_ptr<TraceWriter> writer_; |
Lalit Maganti | 6157e6a | 2018-02-13 11:18:16 +0000 | [diff] [blame] | 107 | |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 108 | base::WeakPtr<ProcessStatsDataSource> ps_source_; |
| 109 | base::WeakPtr<InodeFileDataSource> file_source_; |
| 110 | |
Lalit Maganti | 6157e6a | 2018-02-13 11:18:16 +0000 | [diff] [blame] | 111 | // Keep this after the TraceWriter because TracePackets must not outlive |
| 112 | // their originating writer. |
| 113 | TraceWriter::TracePacketHandle trace_packet_; |
Hector Dearman | c848803 | 2018-03-02 13:12:01 +0000 | [diff] [blame] | 114 | // Keep this last. |
| 115 | base::WeakPtrFactory<SinkDelegate> weak_factory_; |
Lalit Maganti | 0b65b91 | 2018-01-12 18:04:31 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
Hector Dearman | e50e453 | 2018-01-25 14:52:09 +0000 | [diff] [blame] | 118 | enum State { |
| 119 | kNotStarted = 0, |
| 120 | kNotConnected, |
| 121 | kConnecting, |
| 122 | kConnected, |
| 123 | }; |
| 124 | |
Hector Dearman | ebf07c7 | 2018-03-13 10:31:05 +0000 | [diff] [blame] | 125 | ProbesProducer(const ProbesProducer&) = delete; |
| 126 | ProbesProducer& operator=(const ProbesProducer&) = delete; |
| 127 | |
Hector Dearman | e50e453 | 2018-01-25 14:52:09 +0000 | [diff] [blame] | 128 | void Connect(); |
| 129 | void ResetConnectionBackoff(); |
| 130 | void IncreaseConnectionBackoff(); |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 131 | void AddWatchdogsTimer(DataSourceInstanceID id, |
| 132 | const DataSourceConfig& source_config); |
Hector Dearman | e50e453 | 2018-01-25 14:52:09 +0000 | [diff] [blame] | 133 | |
| 134 | State state_ = kNotStarted; |
| 135 | base::TaskRunner* task_runner_; |
Lalit Maganti | 0b65b91 | 2018-01-12 18:04:31 +0000 | [diff] [blame] | 136 | std::unique_ptr<Service::ProducerEndpoint> endpoint_ = nullptr; |
| 137 | std::unique_ptr<FtraceController> ftrace_ = nullptr; |
Primiano Tucci | 2c5cd3b | 2018-02-06 19:27:02 +0000 | [diff] [blame] | 138 | bool ftrace_creation_failed_ = false; |
Hector Dearman | e50e453 | 2018-01-25 14:52:09 +0000 | [diff] [blame] | 139 | uint64_t connection_backoff_ms_ = 0; |
| 140 | const char* socket_name_ = nullptr; |
Hector Dearman | bcb56fb | 2018-03-26 13:59:24 +0100 | [diff] [blame] | 141 | std::set<DataSourceInstanceID> failed_sources_; |
Hector Dearman | ebf07c7 | 2018-03-13 10:31:05 +0000 | [diff] [blame] | 142 | std::map<DataSourceInstanceID, std::unique_ptr<ProcessStatsDataSource>> |
| 143 | process_stats_sources_; |
Lalit Maganti | 0b65b91 | 2018-01-12 18:04:31 +0000 | [diff] [blame] | 144 | std::map<DataSourceInstanceID, std::unique_ptr<SinkDelegate>> delegates_; |
Lalit Maganti | 44ff2a7 | 2018-02-27 16:12:24 +0000 | [diff] [blame] | 145 | std::map<DataSourceInstanceID, base::Watchdog::Timer> watchdogs_; |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 146 | std::map<DataSourceInstanceID, std::unique_ptr<InodeFileDataSource>> |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 147 | file_map_sources_; |
Anna Zappone | 3be7b67 | 2018-03-23 17:26:10 +0000 | [diff] [blame] | 148 | LRUInodeCache cache_{kLRUInodeCacheSize}; |
Anna Zappone | 9ab7960 | 2018-03-27 11:57:44 +0100 | [diff] [blame] | 149 | std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>> |
| 150 | system_inodes_; |
Lalit Maganti | 0b65b91 | 2018-01-12 18:04:31 +0000 | [diff] [blame] | 151 | }; |
Hector Dearman | ebf07c7 | 2018-03-13 10:31:05 +0000 | [diff] [blame] | 152 | |
Lalit Maganti | 0b65b91 | 2018-01-12 18:04:31 +0000 | [diff] [blame] | 153 | } // namespace perfetto |
| 154 | |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 155 | #endif // SRC_TRACED_PROBES_PROBES_PRODUCER_H_ |