blob: eb4593575e6b445b828152e1f2d57c06ab042f95 [file] [log] [blame]
Lalit Maganti0b65b912018-01-12 18:04:31 +00001/*
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 Dearmanebf07c72018-03-13 10:31:05 +000017#ifndef SRC_TRACED_PROBES_PROBES_PRODUCER_H_
18#define SRC_TRACED_PROBES_PROBES_PRODUCER_H_
19
Florian Mayerec4b10e2018-01-17 10:38:23 +000020#include <map>
21#include <memory>
22#include <utility>
23
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000024#include "perfetto/base/task_runner.h"
Lalit Maganti0b65b912018-01-12 18:04:31 +000025#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 Zappone2a6f9042018-03-14 13:26:07 +000029#include "src/traced/probes/filesystem/inode_file_data_source.h"
Hector Dearmanebf07c72018-03-13 10:31:05 +000030#include "src/traced/probes/process_stats_data_source.h"
Lalit Maganti0b65b912018-01-12 18:04:31 +000031
Anna Zappone27ac99c2018-03-06 14:25:35 +000032#include "perfetto/trace/filesystem/inode_file_map.pbzero.h"
33
Lalit Maganti0b65b912018-01-12 18:04:31 +000034namespace perfetto {
Anna Zappone27ac99c2018-03-06 14:25:35 +000035
Anna Zappone3be7b672018-03-23 17:26:10 +000036const uint64_t kLRUInodeCacheSize = 1000;
37
Isabelle Taylord404ea12018-02-19 17:28:01 +000038class ProbesProducer : public Producer {
Lalit Maganti0b65b912018-01-12 18:04:31 +000039 public:
Hector Dearmanc8488032018-03-02 13:12:01 +000040 ProbesProducer();
Isabelle Taylord404ea12018-02-19 17:28:01 +000041 ~ProbesProducer() override;
Lalit Maganti0b65b912018-01-12 18:04:31 +000042
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 Taylor69faa902018-03-21 15:42:03 +000049 void OnTracingStart() override;
50 void OnTracingStop() override;
Lalit Maganti0b65b912018-01-12 18:04:31 +000051
52 // Our Impl
Hector Dearmane50e4532018-01-25 14:52:09 +000053 void ConnectWithRetries(const char* socket_name,
54 base::TaskRunner* task_runner);
Hector Dearmanbcb56fb2018-03-26 13:59:24 +010055 bool CreateFtraceDataSourceInstance(TracingSessionID session_id,
Hector Dearman0ff07c72018-03-15 09:54:46 +000056 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 Tuccidae35652018-03-29 18:32:02 +010063 DataSourceConfig config);
Lalit Maganti0b65b912018-01-12 18:04:31 +000064
Hector Dearmanc8488032018-03-02 13:12:01 +000065 void OnMetadata(const FtraceMetadata& metadata);
66
Lalit Maganti0b65b912018-01-12 18:04:31 +000067 private:
Isabelle Taylord404ea12018-02-19 17:28:01 +000068 using FtraceBundleHandle =
Hector Dearmanaaa4c192018-02-19 11:57:35 +000069 protozero::MessageHandle<protos::pbzero::FtraceEventBundle>;
Lalit Maganti0b65b912018-01-12 18:04:31 +000070
71 class SinkDelegate : public FtraceSink::Delegate {
72 public:
Hector Dearman0ff07c72018-03-15 09:54:46 +000073 SinkDelegate(TracingSessionID,
74 base::TaskRunner*,
75 std::unique_ptr<TraceWriter>);
Lalit Maganti0b65b912018-01-12 18:04:31 +000076 ~SinkDelegate() override;
77
Hector Dearman0ff07c72018-03-15 09:54:46 +000078 TracingSessionID session_id() const { return session_id_; }
79
Lalit Maganti0b65b912018-01-12 18:04:31 +000080 // FtraceDelegateImpl
Isabelle Taylord404ea12018-02-19 17:28:01 +000081 FtraceBundleHandle GetBundleForCpu(size_t cpu) override;
Hector Dearmanc8488032018-03-02 13:12:01 +000082 void OnBundleComplete(size_t cpu,
83 FtraceBundleHandle bundle,
84 const FtraceMetadata& metadata) override;
Lalit Maganti0b65b912018-01-12 18:04:31 +000085
Hector Dearman210dc6f2018-03-12 10:51:07 +000086 void set_sink(std::unique_ptr<FtraceSink> sink) { sink_ = std::move(sink); }
Hector Dearman0ff07c72018-03-15 09:54:46 +000087
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 Maganti0b65b912018-01-12 18:04:31 +0000101
102 private:
Hector Dearman0ff07c72018-03-15 09:54:46 +0000103 const TracingSessionID session_id_;
Hector Dearmanc8488032018-03-02 13:12:01 +0000104 base::TaskRunner* task_runner_;
Lalit Maganti0b65b912018-01-12 18:04:31 +0000105 std::unique_ptr<FtraceSink> sink_ = nullptr;
Lalit Maganti0b65b912018-01-12 18:04:31 +0000106 std::unique_ptr<TraceWriter> writer_;
Lalit Maganti6157e6a2018-02-13 11:18:16 +0000107
Hector Dearman0ff07c72018-03-15 09:54:46 +0000108 base::WeakPtr<ProcessStatsDataSource> ps_source_;
109 base::WeakPtr<InodeFileDataSource> file_source_;
110
Lalit Maganti6157e6a2018-02-13 11:18:16 +0000111 // Keep this after the TraceWriter because TracePackets must not outlive
112 // their originating writer.
113 TraceWriter::TracePacketHandle trace_packet_;
Hector Dearmanc8488032018-03-02 13:12:01 +0000114 // Keep this last.
115 base::WeakPtrFactory<SinkDelegate> weak_factory_;
Lalit Maganti0b65b912018-01-12 18:04:31 +0000116 };
117
Hector Dearmane50e4532018-01-25 14:52:09 +0000118 enum State {
119 kNotStarted = 0,
120 kNotConnected,
121 kConnecting,
122 kConnected,
123 };
124
Hector Dearmanebf07c72018-03-13 10:31:05 +0000125 ProbesProducer(const ProbesProducer&) = delete;
126 ProbesProducer& operator=(const ProbesProducer&) = delete;
127
Hector Dearmane50e4532018-01-25 14:52:09 +0000128 void Connect();
129 void ResetConnectionBackoff();
130 void IncreaseConnectionBackoff();
Anna Zappone27ac99c2018-03-06 14:25:35 +0000131 void AddWatchdogsTimer(DataSourceInstanceID id,
132 const DataSourceConfig& source_config);
Hector Dearmane50e4532018-01-25 14:52:09 +0000133
134 State state_ = kNotStarted;
135 base::TaskRunner* task_runner_;
Lalit Maganti0b65b912018-01-12 18:04:31 +0000136 std::unique_ptr<Service::ProducerEndpoint> endpoint_ = nullptr;
137 std::unique_ptr<FtraceController> ftrace_ = nullptr;
Primiano Tucci2c5cd3b2018-02-06 19:27:02 +0000138 bool ftrace_creation_failed_ = false;
Hector Dearmane50e4532018-01-25 14:52:09 +0000139 uint64_t connection_backoff_ms_ = 0;
140 const char* socket_name_ = nullptr;
Hector Dearmanbcb56fb2018-03-26 13:59:24 +0100141 std::set<DataSourceInstanceID> failed_sources_;
Hector Dearmanebf07c72018-03-13 10:31:05 +0000142 std::map<DataSourceInstanceID, std::unique_ptr<ProcessStatsDataSource>>
143 process_stats_sources_;
Lalit Maganti0b65b912018-01-12 18:04:31 +0000144 std::map<DataSourceInstanceID, std::unique_ptr<SinkDelegate>> delegates_;
Lalit Maganti44ff2a72018-02-27 16:12:24 +0000145 std::map<DataSourceInstanceID, base::Watchdog::Timer> watchdogs_;
Anna Zappone2a6f9042018-03-14 13:26:07 +0000146 std::map<DataSourceInstanceID, std::unique_ptr<InodeFileDataSource>>
Anna Zappone27ac99c2018-03-06 14:25:35 +0000147 file_map_sources_;
Anna Zappone3be7b672018-03-23 17:26:10 +0000148 LRUInodeCache cache_{kLRUInodeCacheSize};
Anna Zappone9ab79602018-03-27 11:57:44 +0100149 std::map<BlockDeviceID, std::unordered_map<Inode, InodeMapValue>>
150 system_inodes_;
Lalit Maganti0b65b912018-01-12 18:04:31 +0000151};
Hector Dearmanebf07c72018-03-13 10:31:05 +0000152
Lalit Maganti0b65b912018-01-12 18:04:31 +0000153} // namespace perfetto
154
Isabelle Taylord404ea12018-02-19 17:28:01 +0000155#endif // SRC_TRACED_PROBES_PROBES_PRODUCER_H_