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