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