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> |
Primiano Tucci | 5252660 | 2018-03-29 22:53:10 +0100 | [diff] [blame] | 21 | |
| 22 | #include <algorithm> |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 23 | #include <queue> |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 24 | #include <string> |
| 25 | |
| 26 | #include "perfetto/base/logging.h" |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 27 | #include "perfetto/base/utils.h" |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 28 | #include "perfetto/base/weak_ptr.h" |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 29 | #include "perfetto/traced/traced.h" |
| 30 | #include "perfetto/tracing/core/data_source_config.h" |
| 31 | #include "perfetto/tracing/core/data_source_descriptor.h" |
Hector Dearman | a89cc57 | 2018-02-23 12:02:58 +0000 | [diff] [blame] | 32 | #include "perfetto/tracing/core/ftrace_config.h" |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 33 | #include "perfetto/tracing/core/trace_config.h" |
| 34 | #include "perfetto/tracing/core/trace_packet.h" |
Primiano Tucci | c2eb510 | 2018-05-15 10:40:01 +0100 | [diff] [blame] | 35 | #include "perfetto/tracing/ipc/producer_ipc_client.h" |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 36 | #include "src/traced/probes/filesystem/inode_file_data_source.h" |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 37 | #include "src/traced/probes/ftrace/ftrace_data_source.h" |
| 38 | #include "src/traced/probes/probes_data_source.h" |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 39 | |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 40 | #include "perfetto/trace/filesystem/inode_file_map.pbzero.h" |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 41 | #include "perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h" |
Hector Dearman | 3afb1e0 | 2018-04-27 16:46:52 +0100 | [diff] [blame] | 42 | #include "perfetto/trace/ftrace/ftrace_stats.pbzero.h" |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 43 | #include "perfetto/trace/trace_packet.pbzero.h" |
| 44 | |
| 45 | namespace perfetto { |
| 46 | namespace { |
| 47 | |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 48 | constexpr uint32_t kInitialConnectionBackoffMs = 100; |
| 49 | constexpr uint32_t kMaxConnectionBackoffMs = 30 * 1000; |
Primiano Tucci | 50e9eda | 2018-11-20 10:52:35 +0100 | [diff] [blame^] | 50 | |
| 51 | // Should be larger than FtraceController::kFlushTimeoutMs. |
| 52 | constexpr uint32_t kFlushTimeoutMs = 1000; |
| 53 | |
Primiano Tucci | 578d784 | 2018-03-29 15:27:05 +0100 | [diff] [blame] | 54 | constexpr char kFtraceSourceName[] = "linux.ftrace"; |
| 55 | constexpr char kProcessStatsSourceName[] = "linux.process_stats"; |
| 56 | constexpr char kInodeMapSourceName[] = "linux.inode_file_map"; |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 57 | constexpr char kSysStatsSourceName[] = "linux.sys_stats"; |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 58 | |
| 59 | } // namespace. |
| 60 | |
| 61 | // State transition diagram: |
| 62 | // +----------------------------+ |
| 63 | // v + |
| 64 | // NotStarted -> NotConnected -> Connecting -> Connected |
| 65 | // ^ + |
| 66 | // +--------------+ |
| 67 | // |
| 68 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 69 | ProbesProducer::ProbesProducer() : weak_factory_(this) {} |
| 70 | ProbesProducer::~ProbesProducer() { |
| 71 | // The ftrace data sources must be deleted before the ftrace controller. |
| 72 | data_sources_.clear(); |
| 73 | ftrace_.reset(); |
| 74 | } |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 75 | |
| 76 | void ProbesProducer::OnConnect() { |
| 77 | PERFETTO_DCHECK(state_ == kConnecting); |
| 78 | state_ = kConnected; |
| 79 | ResetConnectionBackoff(); |
| 80 | PERFETTO_LOG("Connected to the service"); |
| 81 | |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 82 | { |
| 83 | DataSourceDescriptor desc; |
| 84 | desc.set_name(kFtraceSourceName); |
| 85 | endpoint_->RegisterDataSource(desc); |
| 86 | } |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 87 | |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 88 | { |
| 89 | DataSourceDescriptor desc; |
| 90 | desc.set_name(kProcessStatsSourceName); |
| 91 | endpoint_->RegisterDataSource(desc); |
| 92 | } |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 93 | |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 94 | { |
| 95 | DataSourceDescriptor desc; |
| 96 | desc.set_name(kInodeMapSourceName); |
| 97 | endpoint_->RegisterDataSource(desc); |
| 98 | } |
| 99 | |
| 100 | { |
| 101 | DataSourceDescriptor desc; |
| 102 | desc.set_name(kSysStatsSourceName); |
| 103 | endpoint_->RegisterDataSource(desc); |
| 104 | } |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void ProbesProducer::OnDisconnect() { |
| 108 | PERFETTO_DCHECK(state_ == kConnected || state_ == kConnecting); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 109 | PERFETTO_LOG("Disconnected from tracing service"); |
Primiano Tucci | e56411c | 2018-03-30 01:15:02 +0100 | [diff] [blame] | 110 | if (state_ == kConnected) |
| 111 | return task_runner_->PostTask([this] { this->Restart(); }); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 112 | |
Primiano Tucci | e56411c | 2018-03-30 01:15:02 +0100 | [diff] [blame] | 113 | state_ = kNotConnected; |
| 114 | IncreaseConnectionBackoff(); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 115 | task_runner_->PostDelayedTask([this] { this->Connect(); }, |
| 116 | connection_backoff_ms_); |
| 117 | } |
| 118 | |
Primiano Tucci | e56411c | 2018-03-30 01:15:02 +0100 | [diff] [blame] | 119 | void ProbesProducer::Restart() { |
| 120 | // We lost the connection with the tracing service. At this point we need |
| 121 | // to reset all the data sources. Trying to handle that manually is going to |
| 122 | // be error prone. What we do here is simply desroying the instance and |
| 123 | // recreating it again. |
| 124 | // TODO(hjd): Add e2e test for this. |
| 125 | |
| 126 | base::TaskRunner* task_runner = task_runner_; |
| 127 | const char* socket_name = socket_name_; |
| 128 | |
| 129 | // Invoke destructor and then the constructor again. |
| 130 | this->~ProbesProducer(); |
| 131 | new (this) ProbesProducer(); |
| 132 | |
| 133 | ConnectWithRetries(socket_name, task_runner); |
| 134 | } |
| 135 | |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 136 | void ProbesProducer::SetupDataSource(DataSourceInstanceID instance_id, |
Primiano Tucci | afb72b5 | 2018-09-25 09:37:24 +0100 | [diff] [blame] | 137 | const DataSourceConfig& config) { |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 138 | PERFETTO_DLOG("SetupDataSource(id=%" PRIu64 ", name=%s)", instance_id, |
| 139 | config.name().c_str()); |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 140 | PERFETTO_DCHECK(data_sources_.count(instance_id) == 0); |
Primiano Tucci | 03de28f | 2018-08-01 11:29:46 +0100 | [diff] [blame] | 141 | TracingSessionID session_id = config.tracing_session_id(); |
| 142 | PERFETTO_CHECK(session_id > 0); |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 143 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 144 | std::unique_ptr<ProbesDataSource> data_source; |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 145 | if (config.name() == kFtraceSourceName) { |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 146 | data_source = CreateFtraceDataSource(session_id, instance_id, config); |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 147 | } else if (config.name() == kInodeMapSourceName) { |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 148 | data_source = CreateInodeFileDataSource(session_id, instance_id, config); |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 149 | } else if (config.name() == kProcessStatsSourceName) { |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 150 | data_source = CreateProcessStatsDataSource(session_id, instance_id, config); |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 151 | } else if (config.name() == kSysStatsSourceName) { |
| 152 | data_source = CreateSysStatsDataSource(session_id, instance_id, config); |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | if (!data_source) { |
| 156 | PERFETTO_ELOG("Failed to create data source '%s'", config.name().c_str()); |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 157 | return; |
| 158 | } |
| 159 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 160 | session_data_sources_.emplace(session_id, data_source.get()); |
| 161 | data_sources_[instance_id] = std::move(data_source); |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 162 | } |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 163 | |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 164 | void ProbesProducer::StartDataSource(DataSourceInstanceID instance_id, |
| 165 | const DataSourceConfig& config) { |
| 166 | PERFETTO_DLOG("StartDataSource(id=%" PRIu64 ", name=%s)", instance_id, |
| 167 | config.name().c_str()); |
| 168 | auto it = data_sources_.find(instance_id); |
| 169 | if (it == data_sources_.end()) { |
Primiano Tucci | 8a0dc5d | 2018-10-02 12:51:11 +0100 | [diff] [blame] | 170 | // Can happen if SetupDataSource() failed (e.g. ftrace was busy). |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 171 | PERFETTO_ELOG("Data source id=%" PRIu64 " not found", instance_id); |
| 172 | return; |
| 173 | } |
| 174 | ProbesDataSource* data_source = it->second.get(); |
| 175 | if (data_source->started) |
| 176 | return; |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 177 | if (config.trace_duration_ms() != 0) { |
| 178 | uint32_t timeout = 5000 + 2 * config.trace_duration_ms(); |
| 179 | watchdogs_.emplace( |
| 180 | instance_id, base::Watchdog::GetInstance()->CreateFatalTimer(timeout)); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 181 | } |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 182 | data_source->started = true; |
| 183 | data_source->Start(); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 186 | std::unique_ptr<ProbesDataSource> ProbesProducer::CreateFtraceDataSource( |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 187 | TracingSessionID session_id, |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 188 | DataSourceInstanceID id, |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 189 | const DataSourceConfig& config) { |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 190 | // Don't retry if FtraceController::Create() failed once. |
| 191 | // This can legitimately happen on user builds where we cannot access the |
| 192 | // debug paths, e.g., because of SELinux rules. |
| 193 | if (ftrace_creation_failed_) |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 194 | return nullptr; |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 195 | |
| 196 | // Lazily create on the first instance. |
| 197 | if (!ftrace_) { |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 198 | ftrace_ = FtraceController::Create(task_runner_, this); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 199 | |
| 200 | if (!ftrace_) { |
| 201 | PERFETTO_ELOG("Failed to create FtraceController"); |
| 202 | ftrace_creation_failed_ = true; |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 203 | return nullptr; |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | ftrace_->DisableAllEvents(); |
| 207 | ftrace_->ClearTrace(); |
| 208 | } |
| 209 | |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 210 | PERFETTO_LOG("Ftrace setup (id=%" PRIu64 ", target_buf=%" PRIu32 ")", id, |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 211 | config.target_buffer()); |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 212 | const BufferID buffer_id = static_cast<BufferID>(config.target_buffer()); |
| 213 | std::unique_ptr<FtraceDataSource> data_source(new FtraceDataSource( |
| 214 | ftrace_->GetWeakPtr(), session_id, config.ftrace_config(), |
| 215 | endpoint_->CreateTraceWriter(buffer_id))); |
| 216 | if (!ftrace_->AddDataSource(data_source.get())) { |
| 217 | PERFETTO_ELOG( |
Primiano Tucci | 8a0dc5d | 2018-10-02 12:51:11 +0100 | [diff] [blame] | 218 | "Failed to setup tracing (too many concurrent sessions or ftrace is " |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 219 | "already in use)"); |
| 220 | return nullptr; |
Hector Dearman | ee3c49d | 2018-02-28 14:10:22 +0000 | [diff] [blame] | 221 | } |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 222 | return std::move(data_source); |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 225 | std::unique_ptr<ProbesDataSource> ProbesProducer::CreateInodeFileDataSource( |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 226 | TracingSessionID session_id, |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 227 | DataSourceInstanceID id, |
Primiano Tucci | dae3565 | 2018-03-29 18:32:02 +0100 | [diff] [blame] | 228 | DataSourceConfig source_config) { |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 229 | PERFETTO_LOG("Inode file map setup (id=%" PRIu64 ", target_buf=%" PRIu32 ")", |
Anna Zappone | 27ac99c | 2018-03-06 14:25:35 +0000 | [diff] [blame] | 230 | id, source_config.target_buffer()); |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 231 | auto buffer_id = static_cast<BufferID>(source_config.target_buffer()); |
Anna Zappone | 2a6f904 | 2018-03-14 13:26:07 +0000 | [diff] [blame] | 232 | if (system_inodes_.empty()) |
Hector Dearman | 7fabd70 | 2018-03-28 12:37:15 +0100 | [diff] [blame] | 233 | CreateStaticDeviceToInodeMap("/system", &system_inodes_); |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 234 | return std::unique_ptr<InodeFileDataSource>(new InodeFileDataSource( |
| 235 | std::move(source_config), task_runner_, session_id, &system_inodes_, |
| 236 | &cache_, endpoint_->CreateTraceWriter(buffer_id))); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 239 | std::unique_ptr<ProbesDataSource> ProbesProducer::CreateProcessStatsDataSource( |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 240 | TracingSessionID session_id, |
Hector Dearman | 7745169 | 2018-03-08 16:21:13 +0000 | [diff] [blame] | 241 | DataSourceInstanceID id, |
Hector Dearman | 0ff07c7 | 2018-03-15 09:54:46 +0000 | [diff] [blame] | 242 | const DataSourceConfig& config) { |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 243 | base::ignore_result(id); |
| 244 | auto buffer_id = static_cast<BufferID>(config.target_buffer()); |
Primiano Tucci | 8a0dc5d | 2018-10-02 12:51:11 +0100 | [diff] [blame] | 245 | return std::unique_ptr<ProcessStatsDataSource>(new ProcessStatsDataSource( |
Primiano Tucci | c9d4a8b | 2018-10-30 20:19:01 -0700 | [diff] [blame] | 246 | task_runner_, session_id, endpoint_->CreateTraceWriter(buffer_id), |
| 247 | config)); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 250 | std::unique_ptr<SysStatsDataSource> ProbesProducer::CreateSysStatsDataSource( |
| 251 | TracingSessionID session_id, |
| 252 | DataSourceInstanceID id, |
| 253 | const DataSourceConfig& config) { |
| 254 | base::ignore_result(id); |
| 255 | auto buffer_id = static_cast<BufferID>(config.target_buffer()); |
Primiano Tucci | 8a0dc5d | 2018-10-02 12:51:11 +0100 | [diff] [blame] | 256 | return std::unique_ptr<SysStatsDataSource>( |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 257 | new SysStatsDataSource(task_runner_, session_id, |
| 258 | endpoint_->CreateTraceWriter(buffer_id), config)); |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 259 | } |
| 260 | |
Primiano Tucci | afb72b5 | 2018-09-25 09:37:24 +0100 | [diff] [blame] | 261 | void ProbesProducer::StopDataSource(DataSourceInstanceID id) { |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 262 | PERFETTO_LOG("Producer stop (id=%" PRIu64 ")", id); |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 263 | auto it = data_sources_.find(id); |
| 264 | if (it == data_sources_.end()) { |
Primiano Tucci | 8a0dc5d | 2018-10-02 12:51:11 +0100 | [diff] [blame] | 265 | // Can happen if SetupDataSource() failed (e.g. ftrace was busy). |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 266 | PERFETTO_ELOG("Cannot stop data source id=%" PRIu64 ", not found", id); |
| 267 | return; |
| 268 | } |
| 269 | ProbesDataSource* data_source = it->second.get(); |
| 270 | TracingSessionID session_id = data_source->tracing_session_id; |
| 271 | auto range = session_data_sources_.equal_range(session_id); |
| 272 | for (auto kv = range.first; kv != range.second; kv++) { |
| 273 | if (kv->second != data_source) |
| 274 | continue; |
| 275 | session_data_sources_.erase(kv); |
| 276 | break; |
| 277 | } |
| 278 | data_sources_.erase(it); |
Hector Dearman | 7745169 | 2018-03-08 16:21:13 +0000 | [diff] [blame] | 279 | watchdogs_.erase(id); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Primiano Tucci | dca727d | 2018-04-04 11:31:55 +0200 | [diff] [blame] | 282 | void ProbesProducer::OnTracingSetup() {} |
Isabelle Taylor | 69faa90 | 2018-03-21 15:42:03 +0000 | [diff] [blame] | 283 | |
Primiano Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 284 | void ProbesProducer::Flush(FlushRequestID flush_request_id, |
| 285 | const DataSourceInstanceID* data_source_ids, |
| 286 | size_t num_data_sources) { |
Primiano Tucci | 50e9eda | 2018-11-20 10:52:35 +0100 | [diff] [blame^] | 287 | PERFETTO_DCHECK(flush_request_id); |
| 288 | auto weak_this = weak_factory_.GetWeakPtr(); |
| 289 | |
| 290 | // Issue a Flush() to all started data sources. |
| 291 | bool flush_queued = false; |
Primiano Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 292 | for (size_t i = 0; i < num_data_sources; i++) { |
Primiano Tucci | 50e9eda | 2018-11-20 10:52:35 +0100 | [diff] [blame^] | 293 | DataSourceInstanceID ds_id = data_source_ids[i]; |
| 294 | auto it = data_sources_.find(ds_id); |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 295 | if (it == data_sources_.end() || !it->second->started) |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 296 | continue; |
Primiano Tucci | 50e9eda | 2018-11-20 10:52:35 +0100 | [diff] [blame^] | 297 | pending_flushes_.emplace(flush_request_id, ds_id); |
| 298 | flush_queued = true; |
| 299 | auto flush_callback = [weak_this, flush_request_id, ds_id] { |
| 300 | if (weak_this) |
| 301 | weak_this->OnDataSourceFlushComplete(flush_request_id, ds_id); |
| 302 | }; |
| 303 | it->second->Flush(flush_request_id, flush_callback); |
Primiano Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 304 | } |
Primiano Tucci | 50e9eda | 2018-11-20 10:52:35 +0100 | [diff] [blame^] | 305 | |
| 306 | // If there is nothing to flush, ack immediately. |
| 307 | if (!flush_queued) { |
| 308 | endpoint_->NotifyFlushComplete(flush_request_id); |
| 309 | return; |
| 310 | } |
| 311 | |
| 312 | // Otherwise, post the timeout task. |
| 313 | task_runner_->PostDelayedTask( |
| 314 | [weak_this, flush_request_id] { |
| 315 | if (weak_this) |
| 316 | weak_this->OnFlushTimeout(flush_request_id); |
| 317 | }, |
| 318 | kFlushTimeoutMs); |
| 319 | } |
| 320 | |
| 321 | void ProbesProducer::OnDataSourceFlushComplete(FlushRequestID flush_request_id, |
| 322 | DataSourceInstanceID ds_id) { |
| 323 | PERFETTO_DLOG("Flush %" PRIu64 " acked by data source %" PRIu64, |
| 324 | flush_request_id, ds_id); |
| 325 | auto range = pending_flushes_.equal_range(flush_request_id); |
| 326 | for (auto it = range.first; it != range.second; it++) { |
| 327 | if (it->second == ds_id) { |
| 328 | pending_flushes_.erase(it); |
| 329 | break; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | if (pending_flushes_.count(flush_request_id)) |
| 334 | return; // Still waiting for other data sources to ack. |
| 335 | |
| 336 | PERFETTO_DLOG("All data sources acked to flush %" PRIu64, flush_request_id); |
| 337 | endpoint_->NotifyFlushComplete(flush_request_id); |
| 338 | } |
| 339 | |
| 340 | void ProbesProducer::OnFlushTimeout(FlushRequestID flush_request_id) { |
| 341 | if (pending_flushes_.count(flush_request_id) == 0) |
| 342 | return; // All acked. |
| 343 | PERFETTO_ELOG("Flush(%" PRIu64 ") timed out", flush_request_id); |
| 344 | pending_flushes_.erase(flush_request_id); |
Primiano Tucci | d52e627 | 2018-04-06 19:06:53 +0200 | [diff] [blame] | 345 | endpoint_->NotifyFlushComplete(flush_request_id); |
| 346 | } |
| 347 | |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 348 | // This function is called by the FtraceController in batches, whenever it has |
| 349 | // read one or more pages from one or more cpus and written that into the |
| 350 | // userspace tracing buffer. If more than one ftrace data sources are active, |
| 351 | // this call typically happens after writing for all session has been handled. |
| 352 | void ProbesProducer::OnFtraceDataWrittenIntoDataSourceBuffers() { |
| 353 | TracingSessionID last_session_id = 0; |
| 354 | FtraceMetadata* metadata = nullptr; |
| 355 | InodeFileDataSource* inode_data_source = nullptr; |
| 356 | ProcessStatsDataSource* ps_data_source = nullptr; |
| 357 | |
| 358 | // unordered_multimap guarantees that entries with the same key are contiguous |
| 359 | // in the iteration. |
| 360 | for (auto it = session_data_sources_.begin(); /* check below*/; it++) { |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 361 | // If this is the last iteration or the session id has changed, |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 362 | // dispatch the metadata update to the linked data sources, if any. |
| 363 | if (it == session_data_sources_.end() || it->first != last_session_id) { |
| 364 | bool has_inodes = metadata && !metadata->inode_and_device.empty(); |
| 365 | bool has_pids = metadata && !metadata->pids.empty(); |
| 366 | if (has_inodes && inode_data_source) |
| 367 | inode_data_source->OnInodes(metadata->inode_and_device); |
| 368 | if (has_pids && ps_data_source) |
| 369 | ps_data_source->OnPids(metadata->pids); |
| 370 | if (metadata) |
| 371 | metadata->Clear(); |
| 372 | metadata = nullptr; |
| 373 | inode_data_source = nullptr; |
| 374 | ps_data_source = nullptr; |
| 375 | if (it == session_data_sources_.end()) |
| 376 | break; |
| 377 | last_session_id = it->first; |
| 378 | } |
| 379 | ProbesDataSource* ds = it->second; |
Primiano Tucci | 674076d | 2018-10-01 10:41:09 +0100 | [diff] [blame] | 380 | if (!ds->started) |
| 381 | continue; |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 382 | switch (ds->type_id) { |
| 383 | case FtraceDataSource::kTypeId: |
| 384 | metadata = static_cast<FtraceDataSource*>(ds)->mutable_metadata(); |
| 385 | break; |
| 386 | case InodeFileDataSource::kTypeId: |
| 387 | inode_data_source = static_cast<InodeFileDataSource*>(ds); |
| 388 | break; |
Primiano Tucci | c9d4a8b | 2018-10-30 20:19:01 -0700 | [diff] [blame] | 389 | case ProcessStatsDataSource::kTypeId: { |
| 390 | // A trace session might have declared more than one ps data source. |
| 391 | // In those cases we often use one for a full dump on startup ( |
| 392 | // targeting a dedicated buffer) and another one for on-demand dumps |
| 393 | // targeting the main buffer. |
| 394 | // Only use the one that has on-demand dumps enabled, if any. |
| 395 | auto ps = static_cast<ProcessStatsDataSource*>(ds); |
| 396 | if (ps->on_demand_dumps_enabled()) |
| 397 | ps_data_source = ps; |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 398 | break; |
Primiano Tucci | c9d4a8b | 2018-10-30 20:19:01 -0700 | [diff] [blame] | 399 | } |
Primiano Tucci | 82a8bfd | 2018-09-19 11:33:04 +0100 | [diff] [blame] | 400 | case SysStatsDataSource::kTypeId: |
| 401 | break; |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 402 | default: |
Florian Mayer | 6db99a9 | 2018-10-18 11:34:55 +0100 | [diff] [blame] | 403 | PERFETTO_DFATAL("Invalid data source."); |
Primiano Tucci | fd8240d | 2018-08-01 09:34:54 +0100 | [diff] [blame] | 404 | } // switch (type_id) |
| 405 | } // for (session_data_sources_) |
| 406 | } |
| 407 | |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 408 | void ProbesProducer::ConnectWithRetries(const char* socket_name, |
| 409 | base::TaskRunner* task_runner) { |
| 410 | PERFETTO_DCHECK(state_ == kNotStarted); |
| 411 | state_ = kNotConnected; |
| 412 | |
| 413 | ResetConnectionBackoff(); |
| 414 | socket_name_ = socket_name; |
| 415 | task_runner_ = task_runner; |
| 416 | Connect(); |
| 417 | } |
| 418 | |
| 419 | void ProbesProducer::Connect() { |
| 420 | PERFETTO_DCHECK(state_ == kNotConnected); |
| 421 | state_ = kConnecting; |
Isabelle Taylor | 86262cb | 2018-03-27 16:00:54 +0100 | [diff] [blame] | 422 | endpoint_ = ProducerIPCClient::Connect( |
Primiano Tucci | 578d784 | 2018-03-29 15:27:05 +0100 | [diff] [blame] | 423 | socket_name_, this, "perfetto.traced_probes", task_runner_); |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | void ProbesProducer::IncreaseConnectionBackoff() { |
| 427 | connection_backoff_ms_ *= 2; |
| 428 | if (connection_backoff_ms_ > kMaxConnectionBackoffMs) |
| 429 | connection_backoff_ms_ = kMaxConnectionBackoffMs; |
| 430 | } |
| 431 | |
| 432 | void ProbesProducer::ResetConnectionBackoff() { |
| 433 | connection_backoff_ms_ = kInitialConnectionBackoffMs; |
| 434 | } |
| 435 | |
Isabelle Taylor | d404ea1 | 2018-02-19 17:28:01 +0000 | [diff] [blame] | 436 | } // namespace perfetto |