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