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