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 | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 20 | #include <queue> |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 21 | #include <string> |
| 22 | |
| 23 | #include "perfetto/base/logging.h" |
| 24 | #include "perfetto/traced/traced.h" |
| 25 | #include "perfetto/tracing/core/data_source_config.h" |
| 26 | #include "perfetto/tracing/core/data_source_descriptor.h" |
Hector Dearman | a89cc57 | 2018-02-23 12:02:58 +0000 | [diff] [blame] | 27 | #include "perfetto/tracing/core/ftrace_config.h" |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 28 | #include "perfetto/tracing/core/trace_config.h" |
| 29 | #include "perfetto/tracing/core/trace_packet.h" |
| 30 | |
| 31 | #include "src/process_stats/file_utils.h" |
| 32 | #include "src/process_stats/procfs_utils.h" |
| 33 | |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 34 | #include "perfetto/trace/filesystem/inode_file_map.pbzero.h" |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 35 | #include "perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h" |
| 36 | #include "perfetto/trace/ps/process_tree.pbzero.h" |
| 37 | #include "perfetto/trace/trace_packet.pbzero.h" |
| 38 | |
| 39 | namespace perfetto { |
| 40 | namespace { |
| 41 | |
| 42 | uint64_t kInitialConnectionBackoffMs = 100; |
| 43 | uint64_t kMaxConnectionBackoffMs = 30 * 1000; |
| 44 | const char* kFtraceSourceName = "com.google.perfetto.ftrace"; |
| 45 | const char* kProcessStatsSourceName = "com.google.perfetto.process_stats"; |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 46 | const char* kInodeFileMapSourceName = "com.google.perfetto.inode_file_map"; |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 47 | |
| 48 | } // namespace. |
| 49 | |
| 50 | // State transition diagram: |
| 51 | // +----------------------------+ |
| 52 | // v + |
| 53 | // NotStarted -> NotConnected -> Connecting -> Connected |
| 54 | // ^ + |
| 55 | // +--------------+ |
| 56 | // |
| 57 | |
Hector Dearman | c848803 | 2018-03-02 13:12:01 +0000 | [diff] [blame] | 58 | ProbesProducer::ProbesProducer() {} |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 59 | ProbesProducer::~ProbesProducer() = default; |
| 60 | |
| 61 | void ProbesProducer::OnConnect() { |
| 62 | PERFETTO_DCHECK(state_ == kConnecting); |
| 63 | state_ = kConnected; |
| 64 | ResetConnectionBackoff(); |
| 65 | PERFETTO_LOG("Connected to the service"); |
| 66 | |
| 67 | DataSourceDescriptor ftrace_descriptor; |
| 68 | ftrace_descriptor.set_name(kFtraceSourceName); |
Lalit Maganti | a88807d | 2018-03-05 18:21:38 +0000 | [diff] [blame] | 69 | endpoint_->RegisterDataSource(ftrace_descriptor, [](DataSourceInstanceID) {}); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 70 | |
| 71 | DataSourceDescriptor process_stats_descriptor; |
| 72 | process_stats_descriptor.set_name(kProcessStatsSourceName); |
| 73 | endpoint_->RegisterDataSource(process_stats_descriptor, |
Lalit Maganti | a88807d | 2018-03-05 18:21:38 +0000 | [diff] [blame] | 74 | [](DataSourceInstanceID) {}); |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 75 | |
| 76 | DataSourceDescriptor inode_map_descriptor; |
| 77 | inode_map_descriptor.set_name(kInodeFileMapSourceName); |
| 78 | endpoint_->RegisterDataSource(inode_map_descriptor, |
| 79 | [](DataSourceInstanceID) {}); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void ProbesProducer::OnDisconnect() { |
| 83 | PERFETTO_DCHECK(state_ == kConnected || state_ == kConnecting); |
| 84 | state_ = kNotConnected; |
| 85 | PERFETTO_LOG("Disconnected from tracing service"); |
| 86 | IncreaseConnectionBackoff(); |
| 87 | |
| 88 | // TODO(hjd): Erase all sinks and add e2e test for this. |
| 89 | task_runner_->PostDelayedTask([this] { this->Connect(); }, |
| 90 | connection_backoff_ms_); |
| 91 | } |
| 92 | |
| 93 | void ProbesProducer::CreateDataSourceInstance( |
| 94 | DataSourceInstanceID id, |
| 95 | const DataSourceConfig& source_config) { |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 96 | if (source_config.name() == kFtraceSourceName) { |
| 97 | CreateFtraceDataSourceInstance(id, source_config); |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 98 | } else if (source_config.name() == kInodeFileMapSourceName) { |
| 99 | CreateInodeFileMapDataSourceInstance(id, source_config); |
Hector Dearman | 7745169 | 2018-03-08 16:21:13 +0000 | [diff] [blame^] | 100 | } else if (source_config.name() == kProcessStatsSourceName) { |
| 101 | CreateProcessStatsDataSourceInstance(id, source_config); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 102 | } else { |
| 103 | PERFETTO_ELOG("Data source name: %s not recognised.", |
| 104 | source_config.name().c_str()); |
| 105 | } |
| 106 | } |
| 107 | |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 108 | void ProbesProducer::AddWatchdogsTimer(DataSourceInstanceID id, |
| 109 | const DataSourceConfig& source_config) { |
| 110 | if (source_config.trace_duration_ms() != 0) |
| 111 | watchdogs_.emplace(id, base::Watchdog::GetInstance()->CreateFatalTimer( |
| 112 | 5000 + 2 * source_config.trace_duration_ms())); |
| 113 | } |
| 114 | |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 115 | void ProbesProducer::CreateFtraceDataSourceInstance( |
| 116 | DataSourceInstanceID id, |
| 117 | const DataSourceConfig& source_config) { |
| 118 | // Don't retry if FtraceController::Create() failed once. |
| 119 | // This can legitimately happen on user builds where we cannot access the |
| 120 | // debug paths, e.g., because of SELinux rules. |
| 121 | if (ftrace_creation_failed_) |
| 122 | return; |
| 123 | |
| 124 | // Lazily create on the first instance. |
| 125 | if (!ftrace_) { |
| 126 | ftrace_ = FtraceController::Create(task_runner_); |
| 127 | |
| 128 | if (!ftrace_) { |
| 129 | PERFETTO_ELOG("Failed to create FtraceController"); |
| 130 | ftrace_creation_failed_ = true; |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | ftrace_->DisableAllEvents(); |
| 135 | ftrace_->ClearTrace(); |
| 136 | } |
| 137 | |
| 138 | PERFETTO_LOG("Ftrace start (id=%" PRIu64 ", target_buf=%" PRIu32 ")", id, |
| 139 | source_config.target_buffer()); |
| 140 | |
Hector Dearman | a89cc57 | 2018-02-23 12:02:58 +0000 | [diff] [blame] | 141 | FtraceConfig proto_config = source_config.ftrace_config(); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 142 | |
| 143 | // TODO(hjd): Static cast is bad, target_buffer() should return a BufferID. |
| 144 | auto trace_writer = endpoint_->CreateTraceWriter( |
| 145 | static_cast<BufferID>(source_config.target_buffer())); |
Hector Dearman | c848803 | 2018-03-02 13:12:01 +0000 | [diff] [blame] | 146 | auto delegate = std::unique_ptr<SinkDelegate>( |
| 147 | new SinkDelegate(task_runner_, std::move(trace_writer))); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 148 | auto sink = ftrace_->CreateSink(std::move(proto_config), delegate.get()); |
Hector Dearman | ee3c49d | 2018-02-28 14:10:22 +0000 | [diff] [blame] | 149 | if (!sink) { |
| 150 | PERFETTO_ELOG("Failed to start tracing (maybe someone else is using it?)"); |
| 151 | return; |
| 152 | } |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 153 | delegate->sink(std::move(sink)); |
| 154 | delegates_.emplace(id, std::move(delegate)); |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 155 | AddWatchdogsTimer(id, source_config); |
| 156 | } |
| 157 | |
| 158 | void ProbesProducer::CreateInodeFileMapDataSourceInstance( |
| 159 | DataSourceInstanceID id, |
| 160 | const DataSourceConfig& source_config) { |
| 161 | PERFETTO_LOG("Inode file map start (id=%" PRIu64 ", target_buf=%" PRIu32 ")", |
| 162 | id, source_config.target_buffer()); |
| 163 | auto trace_writer = endpoint_->CreateTraceWriter( |
| 164 | static_cast<BufferID>(source_config.target_buffer())); |
Anna Zappone | 45a282a | 2018-03-07 15:22:25 +0000 | [diff] [blame] | 165 | CreateDeviceToInodeMap("/system/", &system_inodes_); |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 166 | auto file_map_source = std::unique_ptr<InodeFileMapDataSource>( |
Anna Zappone | 45a282a | 2018-03-07 15:22:25 +0000 | [diff] [blame] | 167 | new InodeFileMapDataSource(&system_inodes_, std::move(trace_writer))); |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 168 | file_map_sources_.emplace(id, std::move(file_map_source)); |
| 169 | AddWatchdogsTimer(id, source_config); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void ProbesProducer::CreateProcessStatsDataSourceInstance( |
Hector Dearman | 7745169 | 2018-03-08 16:21:13 +0000 | [diff] [blame^] | 173 | DataSourceInstanceID id, |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 174 | const DataSourceConfig& source_config) { |
Hector Dearman | 7745169 | 2018-03-08 16:21:13 +0000 | [diff] [blame^] | 175 | PERFETTO_DCHECK(process_stats_sources_.count(id) == 0); |
| 176 | process_stats_sources_.insert(id); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 177 | auto trace_writer = endpoint_->CreateTraceWriter( |
| 178 | static_cast<BufferID>(source_config.target_buffer())); |
| 179 | procfs_utils::ProcessMap processes; |
| 180 | auto trace_packet = trace_writer->NewTracePacket(); |
| 181 | protos::pbzero::ProcessTree* process_tree = trace_packet->set_process_tree(); |
| 182 | |
| 183 | file_utils::ForEachPidInProcPath( |
| 184 | "/proc", [&processes, &process_tree](int pid) { |
| 185 | if (!processes.count(pid)) { |
| 186 | if (procfs_utils::ReadTgid(pid) != pid) |
| 187 | return; |
| 188 | processes[pid] = procfs_utils::ReadProcessInfo(pid); |
| 189 | } |
| 190 | ProcessInfo* process = processes[pid].get(); |
| 191 | procfs_utils::ReadProcessThreads(process); |
| 192 | auto* process_writer = process_tree->add_processes(); |
| 193 | process_writer->set_pid(process->pid); |
| 194 | process_writer->set_ppid(process->ppid); |
Isabelle Taylor | e350497 | 2018-02-26 10:27:51 +0000 | [diff] [blame] | 195 | for (const auto& field : process->cmdline) |
| 196 | process_writer->add_cmdline(field.c_str()); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 197 | for (auto& thread : process->threads) { |
| 198 | auto* thread_writer = process_writer->add_threads(); |
| 199 | thread_writer->set_tid(thread.second.tid); |
| 200 | thread_writer->set_name(thread.second.name); |
| 201 | } |
| 202 | }); |
| 203 | trace_packet->Finalize(); |
| 204 | } |
| 205 | |
Anna Zappone | 45a282a | 2018-03-07 15:22:25 +0000 | [diff] [blame] | 206 | // static |
| 207 | void ProbesProducer::CreateDeviceToInodeMap( |
| 208 | const std::string& root_directory, |
| 209 | std::map<uint64_t, InodeMap>* block_device_map) { |
| 210 | // Return immediately if we've already filled in the system map |
| 211 | if (!block_device_map->empty()) |
| 212 | return; |
| 213 | std::queue<std::string> queue; |
| 214 | queue.push(root_directory); |
| 215 | while (!queue.empty()) { |
| 216 | struct dirent* entry; |
| 217 | std::string filepath = queue.front(); |
| 218 | queue.pop(); |
| 219 | DIR* dir = opendir(filepath.c_str()); |
| 220 | filepath += "/"; |
| 221 | if (dir == nullptr) |
| 222 | continue; |
| 223 | while ((entry = readdir(dir)) != nullptr) { |
| 224 | std::string filename = entry->d_name; |
| 225 | if (filename == "." || filename == "..") |
| 226 | continue; |
| 227 | uint64_t inode_number = entry->d_ino; |
| 228 | uint64_t block_device_id = 0; |
| 229 | InodeMap& inode_map = (*block_device_map)[block_device_id]; |
| 230 | // Default |
| 231 | Type type = protos::pbzero::InodeFileMap_Entry_Type_UNKNOWN; |
| 232 | if (entry->d_type == DT_DIR) { |
| 233 | // Continue iterating through files if current entry is a directory |
| 234 | queue.push(filepath + filename); |
| 235 | type = protos::pbzero::InodeFileMap_Entry_Type_DIRECTORY; |
| 236 | } else if (entry->d_type == DT_REG) { |
| 237 | type = protos::pbzero::InodeFileMap_Entry_Type_FILE; |
| 238 | } |
| 239 | inode_map[inode_number].first = type; |
| 240 | inode_map[inode_number].second.emplace(filepath + filename); |
| 241 | } |
| 242 | closedir(dir); |
| 243 | } |
| 244 | } |
| 245 | |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 246 | void ProbesProducer::TearDownDataSourceInstance(DataSourceInstanceID id) { |
| 247 | PERFETTO_LOG("Producer stop (id=%" PRIu64 ")", id); |
Hector Dearman | 7745169 | 2018-03-08 16:21:13 +0000 | [diff] [blame^] | 248 | // |id| could be the id of any of the datasources we handle: |
| 249 | PERFETTO_DCHECK((delegates_.count(id) + process_stats_sources_.count(id) + |
| 250 | file_map_sources_.count(id)) == 1); |
| 251 | delegates_.erase(id); |
| 252 | process_stats_sources_.erase(id); |
| 253 | file_map_sources_.erase(id); |
| 254 | watchdogs_.erase(id); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | void ProbesProducer::ConnectWithRetries(const char* socket_name, |
| 258 | base::TaskRunner* task_runner) { |
| 259 | PERFETTO_DCHECK(state_ == kNotStarted); |
| 260 | state_ = kNotConnected; |
| 261 | |
| 262 | ResetConnectionBackoff(); |
| 263 | socket_name_ = socket_name; |
| 264 | task_runner_ = task_runner; |
| 265 | Connect(); |
| 266 | } |
| 267 | |
| 268 | void ProbesProducer::Connect() { |
| 269 | PERFETTO_DCHECK(state_ == kNotConnected); |
| 270 | state_ = kConnecting; |
| 271 | endpoint_ = ProducerIPCClient::Connect(socket_name_, this, task_runner_); |
| 272 | } |
| 273 | |
| 274 | void ProbesProducer::IncreaseConnectionBackoff() { |
| 275 | connection_backoff_ms_ *= 2; |
| 276 | if (connection_backoff_ms_ > kMaxConnectionBackoffMs) |
| 277 | connection_backoff_ms_ = kMaxConnectionBackoffMs; |
| 278 | } |
| 279 | |
| 280 | void ProbesProducer::ResetConnectionBackoff() { |
| 281 | connection_backoff_ms_ = kInitialConnectionBackoffMs; |
| 282 | } |
| 283 | |
Hector Dearman | c848803 | 2018-03-02 13:12:01 +0000 | [diff] [blame] | 284 | ProbesProducer::SinkDelegate::SinkDelegate(base::TaskRunner* task_runner, |
| 285 | std::unique_ptr<TraceWriter> writer) |
| 286 | : task_runner_(task_runner), |
| 287 | writer_(std::move(writer)), |
| 288 | weak_factory_(this) {} |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 289 | |
| 290 | ProbesProducer::SinkDelegate::~SinkDelegate() = default; |
| 291 | |
| 292 | ProbesProducer::FtraceBundleHandle |
Hector Dearman | c848803 | 2018-03-02 13:12:01 +0000 | [diff] [blame] | 293 | |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 294 | ProbesProducer::SinkDelegate::GetBundleForCpu(size_t) { |
| 295 | trace_packet_ = writer_->NewTracePacket(); |
| 296 | return FtraceBundleHandle(trace_packet_->set_ftrace_events()); |
| 297 | } |
| 298 | |
Hector Dearman | c848803 | 2018-03-02 13:12:01 +0000 | [diff] [blame] | 299 | void ProbesProducer::SinkDelegate::OnBundleComplete( |
| 300 | size_t, |
| 301 | FtraceBundleHandle, |
| 302 | const FtraceMetadata& metadata) { |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 303 | trace_packet_->Finalize(); |
Hector Dearman | c848803 | 2018-03-02 13:12:01 +0000 | [diff] [blame] | 304 | if (!metadata.inodes.empty()) { |
| 305 | auto weak_this = weak_factory_.GetWeakPtr(); |
| 306 | auto inodes = metadata.inodes; |
| 307 | task_runner_->PostTask([weak_this, inodes] { |
| 308 | if (weak_this) |
| 309 | weak_this->OnInodes(inodes); |
| 310 | }); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | void ProbesProducer::SinkDelegate::OnInodes( |
| 315 | const std::vector<uint64_t>& inodes) { |
| 316 | PERFETTO_DLOG("Saw FtraceBundle with %zu inodes.", inodes.size()); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 319 | ProbesProducer::InodeFileMapDataSource::InodeFileMapDataSource( |
Anna Zappone | 45a282a | 2018-03-07 15:22:25 +0000 | [diff] [blame] | 320 | std::map<uint64_t, InodeMap>* file_system_inodes, |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 321 | std::unique_ptr<TraceWriter> writer) |
Anna Zappone | 45a282a | 2018-03-07 15:22:25 +0000 | [diff] [blame] | 322 | : file_system_inodes_(file_system_inodes), writer_(std::move(writer)) {} |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 323 | |
| 324 | ProbesProducer::InodeFileMapDataSource::~InodeFileMapDataSource() = default; |
| 325 | |
| 326 | void ProbesProducer::InodeFileMapDataSource::WriteInodes( |
| 327 | const FtraceMetadata& metadata) { |
| 328 | auto trace_packet = writer_->NewTracePacket(); |
| 329 | auto inode_file_map = trace_packet->set_inode_file_map(); |
Anna Zappone | 45a282a | 2018-03-07 15:22:25 +0000 | [diff] [blame] | 330 | // TODO(azappone): Get block_device_id and mount_points & add to the proto |
| 331 | uint64_t block_device_id = 0; |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 332 | auto inodes = metadata.inodes; |
| 333 | for (const auto& inode : inodes) { |
| 334 | auto* entry = inode_file_map->add_entries(); |
| 335 | entry->set_inode_number(inode); |
Anna Zappone | 45a282a | 2018-03-07 15:22:25 +0000 | [diff] [blame] | 336 | auto block_device_map = file_system_inodes_->find(block_device_id); |
| 337 | if (block_device_map != file_system_inodes_->end()) { |
| 338 | auto inode_map = block_device_map->second.find(inode); |
| 339 | if (inode_map != block_device_map->second.end()) { |
| 340 | entry->set_type(inode_map->second.first); |
| 341 | for (const auto& path : inode_map->second.second) |
| 342 | entry->add_paths(path.c_str()); |
| 343 | } |
| 344 | } |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 345 | } |
| 346 | trace_packet->Finalize(); |
| 347 | } |
| 348 | |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 349 | } // namespace perfetto |