Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +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 | |
| 17 | #include "src/traced/probes/probes_producer.h" |
| 18 | |
| 19 | #include <stdio.h> |
Anna Zappone | 4ea73c0 | 2018-03-09 16:01:21 +0000 | [diff] [blame] | 20 | #include <sys/stat.h> |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 21 | #include <queue> |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 22 | #include <string> |
| 23 | |
| 24 | #include "perfetto/base/logging.h" |
| 25 | #include "perfetto/traced/traced.h" |
| 26 | #include "perfetto/tracing/core/data_source_config.h" |
| 27 | #include "perfetto/tracing/core/data_source_descriptor.h" |
Hector Dearman | a89cc57 | 2018-02-23 12:02:58 +0000 | [diff] [blame] | 28 | #include "perfetto/tracing/core/ftrace_config.h" |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 29 | #include "perfetto/tracing/core/trace_config.h" |
| 30 | #include "perfetto/tracing/core/trace_packet.h" |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame^] | 31 | #include "src/traced/probes/filesystem/inode_file_data_source.h" |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 32 | |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 33 | #include "perfetto/trace/filesystem/inode_file_map.pbzero.h" |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 34 | #include "perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h" |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 35 | #include "perfetto/trace/trace_packet.pbzero.h" |
| 36 | |
| 37 | namespace perfetto { |
| 38 | namespace { |
| 39 | |
| 40 | uint64_t kInitialConnectionBackoffMs = 100; |
| 41 | uint64_t kMaxConnectionBackoffMs = 30 * 1000; |
| 42 | const char* kFtraceSourceName = "com.google.perfetto.ftrace"; |
| 43 | const char* kProcessStatsSourceName = "com.google.perfetto.process_stats"; |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 44 | const char* kInodeFileMapSourceName = "com.google.perfetto.inode_file_map"; |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 45 | |
| 46 | } // namespace. |
| 47 | |
| 48 | // State transition diagram: |
| 49 | // +----------------------------+ |
| 50 | // v + |
| 51 | // NotStarted -> NotConnected -> Connecting -> Connected |
| 52 | // ^ + |
| 53 | // +--------------+ |
| 54 | // |
| 55 | |
Hector Dearman | c848803 | 2018-03-02 13:12:01 +0000 | [diff] [blame] | 56 | ProbesProducer::ProbesProducer() {} |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 57 | ProbesProducer::~ProbesProducer() = default; |
| 58 | |
| 59 | void ProbesProducer::OnConnect() { |
| 60 | PERFETTO_DCHECK(state_ == kConnecting); |
| 61 | state_ = kConnected; |
| 62 | ResetConnectionBackoff(); |
| 63 | PERFETTO_LOG("Connected to the service"); |
| 64 | |
| 65 | DataSourceDescriptor ftrace_descriptor; |
| 66 | ftrace_descriptor.set_name(kFtraceSourceName); |
Lalit Maganti | a88807d | 2018-03-05 18:21:38 +0000 | [diff] [blame] | 67 | endpoint_->RegisterDataSource(ftrace_descriptor, [](DataSourceInstanceID) {}); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 68 | |
| 69 | DataSourceDescriptor process_stats_descriptor; |
| 70 | process_stats_descriptor.set_name(kProcessStatsSourceName); |
| 71 | endpoint_->RegisterDataSource(process_stats_descriptor, |
Lalit Maganti | a88807d | 2018-03-05 18:21:38 +0000 | [diff] [blame] | 72 | [](DataSourceInstanceID) {}); |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 73 | |
| 74 | DataSourceDescriptor inode_map_descriptor; |
| 75 | inode_map_descriptor.set_name(kInodeFileMapSourceName); |
| 76 | endpoint_->RegisterDataSource(inode_map_descriptor, |
| 77 | [](DataSourceInstanceID) {}); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void ProbesProducer::OnDisconnect() { |
| 81 | PERFETTO_DCHECK(state_ == kConnected || state_ == kConnecting); |
| 82 | state_ = kNotConnected; |
| 83 | PERFETTO_LOG("Disconnected from tracing service"); |
| 84 | IncreaseConnectionBackoff(); |
| 85 | |
| 86 | // TODO(hjd): Erase all sinks and add e2e test for this. |
| 87 | task_runner_->PostDelayedTask([this] { this->Connect(); }, |
| 88 | connection_backoff_ms_); |
| 89 | } |
| 90 | |
| 91 | void ProbesProducer::CreateDataSourceInstance( |
| 92 | DataSourceInstanceID id, |
| 93 | const DataSourceConfig& source_config) { |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 94 | if (source_config.name() == kFtraceSourceName) { |
| 95 | CreateFtraceDataSourceInstance(id, source_config); |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 96 | } else if (source_config.name() == kInodeFileMapSourceName) { |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame^] | 97 | CreateInodeFileDataSourceInstance(id, source_config); |
Hector Dearman | 7745169 | 2018-03-08 16:21:13 +0000 | [diff] [blame] | 98 | } else if (source_config.name() == kProcessStatsSourceName) { |
| 99 | CreateProcessStatsDataSourceInstance(id, source_config); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 100 | } else { |
| 101 | PERFETTO_ELOG("Data source name: %s not recognised.", |
| 102 | source_config.name().c_str()); |
| 103 | } |
| 104 | } |
| 105 | |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 106 | void ProbesProducer::AddWatchdogsTimer(DataSourceInstanceID id, |
| 107 | const DataSourceConfig& source_config) { |
| 108 | if (source_config.trace_duration_ms() != 0) |
| 109 | watchdogs_.emplace(id, base::Watchdog::GetInstance()->CreateFatalTimer( |
| 110 | 5000 + 2 * source_config.trace_duration_ms())); |
| 111 | } |
| 112 | |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 113 | void ProbesProducer::CreateFtraceDataSourceInstance( |
| 114 | DataSourceInstanceID id, |
| 115 | const DataSourceConfig& source_config) { |
| 116 | // Don't retry if FtraceController::Create() failed once. |
| 117 | // This can legitimately happen on user builds where we cannot access the |
| 118 | // debug paths, e.g., because of SELinux rules. |
| 119 | if (ftrace_creation_failed_) |
| 120 | return; |
| 121 | |
| 122 | // Lazily create on the first instance. |
| 123 | if (!ftrace_) { |
| 124 | ftrace_ = FtraceController::Create(task_runner_); |
| 125 | |
| 126 | if (!ftrace_) { |
| 127 | PERFETTO_ELOG("Failed to create FtraceController"); |
| 128 | ftrace_creation_failed_ = true; |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | ftrace_->DisableAllEvents(); |
| 133 | ftrace_->ClearTrace(); |
| 134 | } |
| 135 | |
| 136 | PERFETTO_LOG("Ftrace start (id=%" PRIu64 ", target_buf=%" PRIu32 ")", id, |
| 137 | source_config.target_buffer()); |
| 138 | |
Hector Dearman | a89cc57 | 2018-02-23 12:02:58 +0000 | [diff] [blame] | 139 | FtraceConfig proto_config = source_config.ftrace_config(); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 140 | |
| 141 | // TODO(hjd): Static cast is bad, target_buffer() should return a BufferID. |
| 142 | auto trace_writer = endpoint_->CreateTraceWriter( |
| 143 | static_cast<BufferID>(source_config.target_buffer())); |
Hector Dearman | c848803 | 2018-03-02 13:12:01 +0000 | [diff] [blame] | 144 | auto delegate = std::unique_ptr<SinkDelegate>( |
| 145 | new SinkDelegate(task_runner_, std::move(trace_writer))); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 146 | auto sink = ftrace_->CreateSink(std::move(proto_config), delegate.get()); |
Hector Dearman | ee3c49d | 2018-02-28 14:10:22 +0000 | [diff] [blame] | 147 | if (!sink) { |
| 148 | PERFETTO_ELOG("Failed to start tracing (maybe someone else is using it?)"); |
| 149 | return; |
| 150 | } |
Hector Dearman | 210dc6f | 2018-03-12 10:51:07 +0000 | [diff] [blame] | 151 | delegate->set_sink(std::move(sink)); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 152 | delegates_.emplace(id, std::move(delegate)); |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 153 | AddWatchdogsTimer(id, source_config); |
| 154 | } |
| 155 | |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame^] | 156 | void ProbesProducer::CreateInodeFileDataSourceInstance( |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 157 | DataSourceInstanceID id, |
| 158 | const DataSourceConfig& source_config) { |
| 159 | PERFETTO_LOG("Inode file map start (id=%" PRIu64 ", target_buf=%" PRIu32 ")", |
| 160 | id, source_config.target_buffer()); |
| 161 | auto trace_writer = endpoint_->CreateTraceWriter( |
| 162 | static_cast<BufferID>(source_config.target_buffer())); |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame^] | 163 | if (system_inodes_.empty()) |
| 164 | CreateDeviceToInodeMap("/system/", &system_inodes_); |
| 165 | auto file_map_source = std::unique_ptr<InodeFileDataSource>( |
| 166 | new InodeFileDataSource(&system_inodes_, std::move(trace_writer))); |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 167 | file_map_sources_.emplace(id, std::move(file_map_source)); |
| 168 | AddWatchdogsTimer(id, source_config); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void ProbesProducer::CreateProcessStatsDataSourceInstance( |
Hector Dearman | 7745169 | 2018-03-08 16:21:13 +0000 | [diff] [blame] | 172 | DataSourceInstanceID id, |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 173 | const DataSourceConfig& source_config) { |
Hector Dearman | 7745169 | 2018-03-08 16:21:13 +0000 | [diff] [blame] | 174 | PERFETTO_DCHECK(process_stats_sources_.count(id) == 0); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 175 | auto trace_writer = endpoint_->CreateTraceWriter( |
| 176 | static_cast<BufferID>(source_config.target_buffer())); |
Hector Dearman | ebf07c7 | 2018-03-13 10:31:05 +0000 | [diff] [blame] | 177 | auto source = std::unique_ptr<ProcessStatsDataSource>( |
| 178 | new ProcessStatsDataSource(std::move(trace_writer))); |
| 179 | auto it_and_inserted = process_stats_sources_.emplace(id, std::move(source)); |
| 180 | PERFETTO_DCHECK(it_and_inserted.second); |
| 181 | it_and_inserted.first->second->WriteAllProcesses(); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void ProbesProducer::TearDownDataSourceInstance(DataSourceInstanceID id) { |
| 185 | PERFETTO_LOG("Producer stop (id=%" PRIu64 ")", id); |
Hector Dearman | 7745169 | 2018-03-08 16:21:13 +0000 | [diff] [blame] | 186 | // |id| could be the id of any of the datasources we handle: |
| 187 | PERFETTO_DCHECK((delegates_.count(id) + process_stats_sources_.count(id) + |
| 188 | file_map_sources_.count(id)) == 1); |
| 189 | delegates_.erase(id); |
| 190 | process_stats_sources_.erase(id); |
| 191 | file_map_sources_.erase(id); |
| 192 | watchdogs_.erase(id); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void ProbesProducer::ConnectWithRetries(const char* socket_name, |
| 196 | base::TaskRunner* task_runner) { |
| 197 | PERFETTO_DCHECK(state_ == kNotStarted); |
| 198 | state_ = kNotConnected; |
| 199 | |
| 200 | ResetConnectionBackoff(); |
| 201 | socket_name_ = socket_name; |
| 202 | task_runner_ = task_runner; |
| 203 | Connect(); |
| 204 | } |
| 205 | |
| 206 | void ProbesProducer::Connect() { |
| 207 | PERFETTO_DCHECK(state_ == kNotConnected); |
| 208 | state_ = kConnecting; |
| 209 | endpoint_ = ProducerIPCClient::Connect(socket_name_, this, task_runner_); |
| 210 | } |
| 211 | |
| 212 | void ProbesProducer::IncreaseConnectionBackoff() { |
| 213 | connection_backoff_ms_ *= 2; |
| 214 | if (connection_backoff_ms_ > kMaxConnectionBackoffMs) |
| 215 | connection_backoff_ms_ = kMaxConnectionBackoffMs; |
| 216 | } |
| 217 | |
| 218 | void ProbesProducer::ResetConnectionBackoff() { |
| 219 | connection_backoff_ms_ = kInitialConnectionBackoffMs; |
| 220 | } |
| 221 | |
Hector Dearman | c848803 | 2018-03-02 13:12:01 +0000 | [diff] [blame] | 222 | ProbesProducer::SinkDelegate::SinkDelegate(base::TaskRunner* task_runner, |
| 223 | std::unique_ptr<TraceWriter> writer) |
| 224 | : task_runner_(task_runner), |
| 225 | writer_(std::move(writer)), |
| 226 | weak_factory_(this) {} |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 227 | |
| 228 | ProbesProducer::SinkDelegate::~SinkDelegate() = default; |
| 229 | |
| 230 | ProbesProducer::FtraceBundleHandle |
Hector Dearman | c848803 | 2018-03-02 13:12:01 +0000 | [diff] [blame] | 231 | |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 232 | ProbesProducer::SinkDelegate::GetBundleForCpu(size_t) { |
| 233 | trace_packet_ = writer_->NewTracePacket(); |
| 234 | return FtraceBundleHandle(trace_packet_->set_ftrace_events()); |
| 235 | } |
| 236 | |
Hector Dearman | c848803 | 2018-03-02 13:12:01 +0000 | [diff] [blame] | 237 | void ProbesProducer::SinkDelegate::OnBundleComplete( |
| 238 | size_t, |
| 239 | FtraceBundleHandle, |
| 240 | const FtraceMetadata& metadata) { |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 241 | trace_packet_->Finalize(); |
Hector Dearman | c848803 | 2018-03-02 13:12:01 +0000 | [diff] [blame] | 242 | if (!metadata.inodes.empty()) { |
| 243 | auto weak_this = weak_factory_.GetWeakPtr(); |
| 244 | auto inodes = metadata.inodes; |
| 245 | task_runner_->PostTask([weak_this, inodes] { |
| 246 | if (weak_this) |
| 247 | weak_this->OnInodes(inodes); |
| 248 | }); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | void ProbesProducer::SinkDelegate::OnInodes( |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame^] | 253 | const std::vector<std::pair<Inode, uint32_t>>& inodes) { |
| 254 | PERFETTO_LOG("Saw FtraceBundle with %zu inodes.", inodes.size()); |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 257 | } // namespace perfetto |