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,
11 * software distributed under the License is distributed on an "AS
12 * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13 * express or implied. See the License for the specific language
14 * governing permissions and limitations under the License.
15 */
16 #include "src/traced/probes/probes_producer.h"
17
18 #include <stdio.h>
19 #include <sys/stat.h>
20
21 #include <algorithm>
22 #include <queue>
23 #include <string>
24
25 #include "perfetto/base/logging.h"
26 #include "perfetto/ext/base/utils.h"
27 #include "perfetto/ext/base/watchdog.h"
28 #include "perfetto/ext/base/weak_ptr.h"
29 #include "perfetto/ext/traced/traced.h"
30 #include "perfetto/ext/tracing/core/basic_types.h"
31 #include "perfetto/ext/tracing/core/trace_packet.h"
32 #include "perfetto/ext/tracing/ipc/producer_ipc_client.h"
33 #include "perfetto/tracing/core/data_source_config.h"
34 #include "perfetto/tracing/core/data_source_descriptor.h"
35 #include "perfetto/tracing/core/forward_decls.h"
36 #include "perfetto/tracing/core/trace_config.h"
37 #include "src/android_stats/statsd_logging_helper.h"
38 #include "src/traced/probes/android_game_intervention_list/android_game_intervention_list_data_source.h"
39 #include "src/traced/probes/android_log/android_log_data_source.h"
40 #include "src/traced/probes/android_system_property/android_system_property_data_source.h"
41 #include "src/traced/probes/common/cpu_freq_info.h"
42 #include "src/traced/probes/filesystem/inode_file_data_source.h"
43 #include "src/traced/probes/ftrace/ftrace_data_source.h"
44 #include "src/traced/probes/initial_display_state/initial_display_state_data_source.h"
45 #include "src/traced/probes/metatrace/metatrace_data_source.h"
46 #include "src/traced/probes/packages_list/packages_list_data_source.h"
47 #include "src/traced/probes/power/android_power_data_source.h"
48 #include "src/traced/probes/power/linux_power_sysfs_data_source.h"
49 #include "src/traced/probes/probes_data_source.h"
50 #include "src/traced/probes/ps/process_stats_data_source.h"
51 #include "src/traced/probes/statsd_client/statsd_binder_data_source.h"
52 #include "src/traced/probes/sys_stats/sys_stats_data_source.h"
53 #include "src/traced/probes/system_info/system_info_data_source.h"
54
55 #include "protos/perfetto/config/ftrace/ftrace_config.gen.h"
56 #include "protos/perfetto/trace/filesystem/inode_file_map.pbzero.h"
57 #include "protos/perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h"
58 #include "protos/perfetto/trace/ftrace/ftrace_stats.pbzero.h"
59 #include "protos/perfetto/trace/trace_packet.pbzero.h"
60
61 namespace perfetto {
62 namespace {
63
64 constexpr uint32_t kInitialConnectionBackoffMs = 100;
65 constexpr uint32_t kMaxConnectionBackoffMs = 30 * 1000;
66
67 // Should be larger than FtraceController::kControllerFlushTimeoutMs.
68 constexpr uint32_t kFlushTimeoutMs = 1000;
69
70 constexpr size_t kTracingSharedMemSizeHintBytes = 1024 * 1024;
71 constexpr size_t kTracingSharedMemPageSizeHintBytes = 32 * 1024;
72
73 } // namespace
74
75 // State transition diagram:
76 // +----------------------------+
77 // v +
78 // NotStarted -> NotConnected -> Connecting -> Connected
79 // ^ +
80 // +--------------+
81 //
82
83 ProbesProducer* ProbesProducer::instance_ = nullptr;
84
GetInstance()85 ProbesProducer* ProbesProducer::GetInstance() {
86 return instance_;
87 }
88
ProbesProducer()89 ProbesProducer::ProbesProducer() : weak_factory_(this) {
90 PERFETTO_CHECK(instance_ == nullptr);
91 instance_ = this;
92 }
93
~ProbesProducer()94 ProbesProducer::~ProbesProducer() {
95 instance_ = nullptr;
96 // The ftrace data sources must be deleted before the ftrace controller.
97 data_sources_.clear();
98 ftrace_.reset();
99 }
100
Restart()101 void ProbesProducer::Restart() {
102 // We lost the connection with the tracing service. At this point we need
103 // to reset all the data sources. Trying to handle that manually is going to
104 // be error prone. What we do here is simply destroying the instance and
105 // recreating it again.
106
107 base::TaskRunner* task_runner = task_runner_;
108 const char* socket_name = socket_name_;
109
110 // Invoke destructor and then the constructor again.
111 this->~ProbesProducer();
112 new (this) ProbesProducer();
113
114 ConnectWithRetries(socket_name, task_runner);
115 }
116
117 template <>
118 std::unique_ptr<ProbesDataSource>
CreateDSInstance(TracingSessionID session_id,const DataSourceConfig & config)119 ProbesProducer::CreateDSInstance<FtraceDataSource>(
120 TracingSessionID session_id,
121 const DataSourceConfig& config) {
122 // Don't retry if FtraceController::Create() failed once.
123 // This can legitimately happen on user builds where we cannot access the
124 // debug paths, e.g., because of SELinux rules.
125 if (ftrace_creation_failed_)
126 return nullptr;
127
128 FtraceConfig ftrace_config;
129 ftrace_config.ParseFromString(config.ftrace_config_raw());
130 // Lazily create on the first instance.
131 if (!ftrace_) {
132 ftrace_ = FtraceController::Create(task_runner_, this);
133
134 if (!ftrace_) {
135 PERFETTO_ELOG("Failed to create FtraceController");
136 ftrace_creation_failed_ = true;
137 return nullptr;
138 }
139 }
140
141 PERFETTO_LOG("Ftrace setup (target_buf=%" PRIu32 ")", config.target_buffer());
142 const BufferID buffer_id = static_cast<BufferID>(config.target_buffer());
143 std::unique_ptr<FtraceDataSource> data_source(new FtraceDataSource(
144 ftrace_->GetWeakPtr(), session_id, std::move(ftrace_config),
145 endpoint_->CreateTraceWriter(buffer_id)));
146 if (!ftrace_->AddDataSource(data_source.get())) {
147 PERFETTO_ELOG("Failed to setup ftrace");
148 return nullptr;
149 }
150 return std::unique_ptr<ProbesDataSource>(std::move(data_source));
151 }
152
153 template <>
154 std::unique_ptr<ProbesDataSource>
CreateDSInstance(TracingSessionID session_id,const DataSourceConfig & source_config)155 ProbesProducer::CreateDSInstance<InodeFileDataSource>(
156 TracingSessionID session_id,
157 const DataSourceConfig& source_config) {
158 PERFETTO_LOG("Inode file map setup (target_buf=%" PRIu32 ")",
159 source_config.target_buffer());
160 auto buffer_id = static_cast<BufferID>(source_config.target_buffer());
161 if (system_inodes_.empty())
162 CreateStaticDeviceToInodeMap("/system", &system_inodes_);
163 return std::unique_ptr<InodeFileDataSource>(new InodeFileDataSource(
164 source_config, task_runner_, session_id, &system_inodes_, &cache_,
165 endpoint_->CreateTraceWriter(buffer_id)));
166 }
167
168 template <>
169 std::unique_ptr<ProbesDataSource>
CreateDSInstance(TracingSessionID session_id,const DataSourceConfig & config)170 ProbesProducer::CreateDSInstance<ProcessStatsDataSource>(
171 TracingSessionID session_id,
172 const DataSourceConfig& config) {
173 auto buffer_id = static_cast<BufferID>(config.target_buffer());
174 return std::unique_ptr<ProcessStatsDataSource>(new ProcessStatsDataSource(
175 task_runner_, session_id, endpoint_->CreateTraceWriter(buffer_id), config,
176 std::unique_ptr<CpuFreqInfo>(new CpuFreqInfo())));
177 }
178
179 template <>
180 std::unique_ptr<ProbesDataSource>
CreateDSInstance(TracingSessionID session_id,const DataSourceConfig & config)181 ProbesProducer::CreateDSInstance<StatsdBinderDataSource>(
182 TracingSessionID session_id,
183 const DataSourceConfig& config) {
184 auto buffer_id = static_cast<BufferID>(config.target_buffer());
185 return std::unique_ptr<StatsdBinderDataSource>(new StatsdBinderDataSource(
186 task_runner_, session_id, endpoint_->CreateTraceWriter(buffer_id),
187 config));
188 }
189
190 template <>
191 std::unique_ptr<ProbesDataSource>
CreateDSInstance(TracingSessionID session_id,const DataSourceConfig & config)192 ProbesProducer::CreateDSInstance<AndroidPowerDataSource>(
193 TracingSessionID session_id,
194 const DataSourceConfig& config) {
195 auto buffer_id = static_cast<BufferID>(config.target_buffer());
196 return std::unique_ptr<ProbesDataSource>(
197 new AndroidPowerDataSource(config, task_runner_, session_id,
198 endpoint_->CreateTraceWriter(buffer_id)));
199 }
200
201 template <>
202 std::unique_ptr<ProbesDataSource>
CreateDSInstance(TracingSessionID session_id,const DataSourceConfig & config)203 ProbesProducer::CreateDSInstance<LinuxPowerSysfsDataSource>(
204 TracingSessionID session_id,
205 const DataSourceConfig& config) {
206 auto buffer_id = static_cast<BufferID>(config.target_buffer());
207 return std::unique_ptr<ProbesDataSource>(
208 new LinuxPowerSysfsDataSource(config, task_runner_, session_id,
209 endpoint_->CreateTraceWriter(buffer_id)));
210 }
211
212 template <>
213 std::unique_ptr<ProbesDataSource>
CreateDSInstance(TracingSessionID session_id,const DataSourceConfig & config)214 ProbesProducer::CreateDSInstance<AndroidLogDataSource>(
215 TracingSessionID session_id,
216 const DataSourceConfig& config) {
217 auto buffer_id = static_cast<BufferID>(config.target_buffer());
218 return std::unique_ptr<ProbesDataSource>(
219 new AndroidLogDataSource(config, task_runner_, session_id,
220 endpoint_->CreateTraceWriter(buffer_id)));
221 }
222
223 template <>
224 std::unique_ptr<ProbesDataSource>
CreateDSInstance(TracingSessionID session_id,const DataSourceConfig & config)225 ProbesProducer::CreateDSInstance<PackagesListDataSource>(
226 TracingSessionID session_id,
227 const DataSourceConfig& config) {
228 auto buffer_id = static_cast<BufferID>(config.target_buffer());
229 return std::unique_ptr<ProbesDataSource>(new PackagesListDataSource(
230 config, session_id, endpoint_->CreateTraceWriter(buffer_id)));
231 }
232
233 template <>
234 std::unique_ptr<ProbesDataSource>
CreateDSInstance(TracingSessionID session_id,const DataSourceConfig & config)235 ProbesProducer::CreateDSInstance<AndroidGameInterventionListDataSource>(
236 TracingSessionID session_id,
237 const DataSourceConfig& config) {
238 auto buffer_id = static_cast<BufferID>(config.target_buffer());
239 return std::unique_ptr<ProbesDataSource>(
240 new AndroidGameInterventionListDataSource(
241 config, session_id, endpoint_->CreateTraceWriter(buffer_id)));
242 }
243
244 template <>
245 std::unique_ptr<ProbesDataSource>
CreateDSInstance(TracingSessionID session_id,const DataSourceConfig & config)246 ProbesProducer::CreateDSInstance<SysStatsDataSource>(
247 TracingSessionID session_id,
248 const DataSourceConfig& config) {
249 auto buffer_id = static_cast<BufferID>(config.target_buffer());
250 return std::unique_ptr<SysStatsDataSource>(new SysStatsDataSource(
251 task_runner_, session_id, endpoint_->CreateTraceWriter(buffer_id), config,
252 std::unique_ptr<CpuFreqInfo>(new CpuFreqInfo())));
253 }
254
255 template <>
256 std::unique_ptr<ProbesDataSource>
CreateDSInstance(TracingSessionID session_id,const DataSourceConfig & config)257 ProbesProducer::CreateDSInstance<MetatraceDataSource>(
258 TracingSessionID session_id,
259 const DataSourceConfig& config) {
260 auto buffer_id = static_cast<BufferID>(config.target_buffer());
261 return std::unique_ptr<ProbesDataSource>(new MetatraceDataSource(
262 task_runner_, session_id, endpoint_->CreateTraceWriter(buffer_id)));
263 }
264
265 template <>
266 std::unique_ptr<ProbesDataSource>
CreateDSInstance(TracingSessionID session_id,const DataSourceConfig & config)267 ProbesProducer::CreateDSInstance<SystemInfoDataSource>(
268 TracingSessionID session_id,
269 const DataSourceConfig& config) {
270 auto buffer_id = static_cast<BufferID>(config.target_buffer());
271 return std::unique_ptr<ProbesDataSource>(new SystemInfoDataSource(
272 session_id, endpoint_->CreateTraceWriter(buffer_id),
273 std::unique_ptr<CpuFreqInfo>(new CpuFreqInfo())));
274 }
275
276 template <>
277 std::unique_ptr<ProbesDataSource>
CreateDSInstance(TracingSessionID session_id,const DataSourceConfig & config)278 ProbesProducer::CreateDSInstance<InitialDisplayStateDataSource>(
279 TracingSessionID session_id,
280 const DataSourceConfig& config) {
281 auto buffer_id = static_cast<BufferID>(config.target_buffer());
282 return std::unique_ptr<ProbesDataSource>(new InitialDisplayStateDataSource(
283 task_runner_, config, session_id,
284 endpoint_->CreateTraceWriter(buffer_id)));
285 }
286
287 template <>
288 std::unique_ptr<ProbesDataSource>
CreateDSInstance(TracingSessionID session_id,const DataSourceConfig & config)289 ProbesProducer::CreateDSInstance<AndroidSystemPropertyDataSource>(
290 TracingSessionID session_id,
291 const DataSourceConfig& config) {
292 auto buffer_id = static_cast<BufferID>(config.target_buffer());
293 return std::unique_ptr<ProbesDataSource>(new AndroidSystemPropertyDataSource(
294 task_runner_, config, session_id,
295 endpoint_->CreateTraceWriter(buffer_id)));
296 }
297
298 // Another anonymous namespace. This cannot be moved into the anonymous
299 // namespace on top (it would fail to compile), because the CreateDSInstance
300 // methods need to be fully declared before.
301 namespace {
302
303 using ProbesDataSourceFactoryFunc = std::unique_ptr<ProbesDataSource> (
304 ProbesProducer::*)(TracingSessionID, const DataSourceConfig&);
305
306 struct DataSourceTraits {
307 const ProbesDataSource::Descriptor* descriptor;
308 ProbesDataSourceFactoryFunc factory_func;
309 };
310
311 template <typename T>
Ds()312 constexpr DataSourceTraits Ds() {
313 return DataSourceTraits{&T::descriptor, &ProbesProducer::CreateDSInstance<T>};
314 }
315
316 constexpr const DataSourceTraits kAllDataSources[] = {
317 Ds<AndroidGameInterventionListDataSource>(),
318 Ds<AndroidLogDataSource>(),
319 Ds<AndroidPowerDataSource>(),
320 Ds<AndroidSystemPropertyDataSource>(),
321 Ds<FtraceDataSource>(),
322 Ds<InitialDisplayStateDataSource>(),
323 Ds<InodeFileDataSource>(),
324 Ds<LinuxPowerSysfsDataSource>(),
325 Ds<MetatraceDataSource>(),
326 Ds<PackagesListDataSource>(),
327 Ds<ProcessStatsDataSource>(),
328 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
329 Ds<StatsdBinderDataSource>(),
330 #endif
331 Ds<SysStatsDataSource>(),
332 Ds<SystemInfoDataSource>(),
333 };
334
335 } // namespace
336
OnConnect()337 void ProbesProducer::OnConnect() {
338 PERFETTO_DCHECK(state_ == kConnecting);
339 state_ = kConnected;
340 ResetConnectionBackoff();
341 PERFETTO_LOG("Connected to the service");
342
343 std::array<DataSourceDescriptor, base::ArraySize(kAllDataSources)>
344 proto_descs;
345 // Generate all data source descriptors.
346 for (size_t i = 0; i < proto_descs.size(); i++) {
347 DataSourceDescriptor& proto_desc = proto_descs[i];
348 const ProbesDataSource::Descriptor* desc = kAllDataSources[i].descriptor;
349 for (size_t j = i + 1; j < proto_descs.size(); j++) {
350 if (kAllDataSources[i].descriptor == kAllDataSources[j].descriptor) {
351 PERFETTO_FATAL("Duplicate descriptor name %s",
352 kAllDataSources[i].descriptor->name);
353 }
354 }
355
356 proto_desc.set_name(desc->name);
357 proto_desc.set_will_notify_on_start(true);
358 proto_desc.set_will_notify_on_stop(true);
359 using Flags = ProbesDataSource::Descriptor::Flags;
360 if (desc->flags & Flags::kHandlesIncrementalState)
361 proto_desc.set_handles_incremental_state_clear(true);
362 if (desc->fill_descriptor_func) {
363 desc->fill_descriptor_func(&proto_desc);
364 }
365 }
366
367 // Register all the data sources. Separate from the above loop because, if
368 // generating a data source descriptor takes too long, we don't want to be in
369 // a state where only some data sources are registered.
370 for (const DataSourceDescriptor& proto_desc : proto_descs) {
371 endpoint_->RegisterDataSource(proto_desc);
372 }
373
374 // Used by tracebox to synchronize with traced_probes being registered.
375 if (all_data_sources_registered_cb_) {
376 endpoint_->Sync(all_data_sources_registered_cb_);
377 }
378 }
379
OnDisconnect()380 void ProbesProducer::OnDisconnect() {
381 PERFETTO_DCHECK(state_ == kConnected || state_ == kConnecting);
382 PERFETTO_LOG("Disconnected from tracing service");
383 if (state_ == kConnected)
384 return task_runner_->PostTask([this] { this->Restart(); });
385
386 state_ = kNotConnected;
387 IncreaseConnectionBackoff();
388 task_runner_->PostDelayedTask([this] { this->Connect(); },
389 connection_backoff_ms_);
390 }
391
SetupDataSource(DataSourceInstanceID instance_id,const DataSourceConfig & config)392 void ProbesProducer::SetupDataSource(DataSourceInstanceID instance_id,
393 const DataSourceConfig& config) {
394 PERFETTO_DLOG("SetupDataSource(id=%" PRIu64 ", name=%s)", instance_id,
395 config.name().c_str());
396 PERFETTO_DCHECK(data_sources_.count(instance_id) == 0);
397 TracingSessionID session_id = config.tracing_session_id();
398 PERFETTO_CHECK(session_id > 0);
399
400 std::unique_ptr<ProbesDataSource> data_source;
401
402 for (const DataSourceTraits& rds : kAllDataSources) {
403 if (rds.descriptor->name != config.name()) {
404 continue;
405 }
406 data_source = (this->*(rds.factory_func))(session_id, config);
407 break;
408 }
409
410 if (!data_source) {
411 PERFETTO_ELOG("Failed to create data source '%s'", config.name().c_str());
412 return;
413 }
414
415 session_data_sources_[session_id].emplace(data_source->descriptor,
416 data_source.get());
417 data_sources_[instance_id] = std::move(data_source);
418 }
419
StartDataSource(DataSourceInstanceID instance_id,const DataSourceConfig & config)420 void ProbesProducer::StartDataSource(DataSourceInstanceID instance_id,
421 const DataSourceConfig& config) {
422 PERFETTO_DLOG("StartDataSource(id=%" PRIu64 ", name=%s)", instance_id,
423 config.name().c_str());
424 auto it = data_sources_.find(instance_id);
425 if (it == data_sources_.end()) {
426 // Can happen if SetupDataSource() failed (e.g. ftrace was busy).
427 PERFETTO_ELOG("Data source id=%" PRIu64 " not found", instance_id);
428 return;
429 }
430 ProbesDataSource* data_source = it->second.get();
431 if (data_source->started)
432 return;
433 if (config.trace_duration_ms() != 0) {
434 // We need to ensure this timeout is worse than the worst case
435 // time from us starting to traced managing to disable us.
436 // See b/236814186#comment8 for context
437 // Note: when using prefer_suspend_clock_for_duration the actual duration
438 // might be < timeout measured in in wall time. But this is fine
439 // because the resulting timeout will be conservative (it will be accurate
440 // if the device never suspends, and will be more lax if it does).
441 uint32_t timeout =
442 2 * (kDefaultFlushTimeoutMs + config.trace_duration_ms() +
443 config.stop_timeout_ms());
444 watchdogs_.emplace(
445 instance_id, base::Watchdog::GetInstance()->CreateFatalTimer(
446 timeout, base::WatchdogCrashReason::kTraceDidntStop));
447 }
448 data_source->started = true;
449 data_source->Start();
450 endpoint_->NotifyDataSourceStarted(instance_id);
451 }
452
StopDataSource(DataSourceInstanceID id)453 void ProbesProducer::StopDataSource(DataSourceInstanceID id) {
454 PERFETTO_LOG("Producer stop (id=%" PRIu64 ")", id);
455 auto it = data_sources_.find(id);
456 if (it == data_sources_.end()) {
457 // Can happen if SetupDataSource() failed (e.g. ftrace was busy).
458 PERFETTO_ELOG("Cannot stop data source id=%" PRIu64 ", not found", id);
459 return;
460 }
461 ProbesDataSource* data_source = it->second.get();
462
463 // MetatraceDataSource special case: re-flush to record the final flushes of
464 // other data sources.
465 if (data_source->descriptor == &MetatraceDataSource::descriptor)
466 data_source->Flush(FlushRequestID{0}, [] {});
467
468 TracingSessionID session_id = data_source->tracing_session_id;
469
470 auto session_it = session_data_sources_.find(session_id);
471 if (session_it != session_data_sources_.end()) {
472 auto desc_range = session_it->second.equal_range(data_source->descriptor);
473 for (auto ds_it = desc_range.first; ds_it != desc_range.second; ds_it++) {
474 if (ds_it->second == data_source) {
475 session_it->second.erase(ds_it);
476 if (session_it->second.empty()) {
477 session_data_sources_.erase(session_it);
478 }
479 break;
480 }
481 }
482 }
483 data_sources_.erase(it);
484 watchdogs_.erase(id);
485
486 // We could (and used to) acknowledge the stop before tearing the local state
487 // down, allowing the tracing service and the consumer to carry on quicker.
488 // However in the case of tracebox, the traced_probes subprocess gets killed
489 // as soon as the trace is considered finished (i.e. all data source stops
490 // were acked), and therefore the kill would race against the tracefs
491 // cleanup.
492 endpoint_->NotifyDataSourceStopped(id);
493 }
494
OnTracingSetup()495 void ProbesProducer::OnTracingSetup() {
496 // shared_memory() can be null in test environments when running in-process.
497 if (endpoint_->shared_memory()) {
498 base::Watchdog::GetInstance()->SetMemoryLimit(
499 endpoint_->shared_memory()->size() + base::kWatchdogDefaultMemorySlack,
500 base::kWatchdogDefaultMemoryWindow);
501 }
502 }
503
Flush(FlushRequestID flush_request_id,const DataSourceInstanceID * data_source_ids,size_t num_data_sources)504 void ProbesProducer::Flush(FlushRequestID flush_request_id,
505 const DataSourceInstanceID* data_source_ids,
506 size_t num_data_sources) {
507 PERFETTO_DLOG("ProbesProducer::Flush(%" PRIu64 ") begin", flush_request_id);
508 PERFETTO_DCHECK(flush_request_id);
509 auto log_on_exit = base::OnScopeExit([&] {
510 PERFETTO_DLOG("ProbesProducer::Flush(%" PRIu64 ") end", flush_request_id);
511 });
512
513 // Issue a Flush() to all started data sources.
514 std::vector<std::pair<DataSourceInstanceID, ProbesDataSource*>> ds_to_flush;
515 for (size_t i = 0; i < num_data_sources; i++) {
516 DataSourceInstanceID ds_id = data_source_ids[i];
517 auto it = data_sources_.find(ds_id);
518 if (it == data_sources_.end() || !it->second->started)
519 continue;
520 pending_flushes_.emplace(flush_request_id, ds_id);
521 ds_to_flush.emplace_back(std::make_pair(ds_id, it->second.get()));
522 }
523
524 // If there is nothing to flush, ack immediately.
525 if (ds_to_flush.empty()) {
526 endpoint_->NotifyFlushComplete(flush_request_id);
527 return;
528 }
529
530 // Otherwise post the timeout task and issue all flushes in order.
531 auto weak_this = weak_factory_.GetWeakPtr();
532 task_runner_->PostDelayedTask(
533 [weak_this, flush_request_id] {
534 if (weak_this)
535 weak_this->OnFlushTimeout(flush_request_id);
536 },
537 kFlushTimeoutMs);
538
539 // Issue all the flushes in order. We do this in a separate loop to deal with
540 // the case of data sources invoking the callback synchronously (b/295189870).
541 for (const auto& kv : ds_to_flush) {
542 const DataSourceInstanceID ds_id = kv.first;
543 ProbesDataSource* const data_source = kv.second;
544 auto flush_callback = [weak_this, flush_request_id, ds_id] {
545 if (weak_this)
546 weak_this->OnDataSourceFlushComplete(flush_request_id, ds_id);
547 };
548 PERFETTO_DLOG("Flushing data source %" PRIu64 " %s", ds_id,
549 data_source->descriptor->name);
550 data_source->Flush(flush_request_id, flush_callback);
551 }
552 }
553
OnDataSourceFlushComplete(FlushRequestID flush_request_id,DataSourceInstanceID ds_id)554 void ProbesProducer::OnDataSourceFlushComplete(FlushRequestID flush_request_id,
555 DataSourceInstanceID ds_id) {
556 PERFETTO_DLOG("Flush %" PRIu64 " acked by data source %" PRIu64,
557 flush_request_id, ds_id);
558 auto range = pending_flushes_.equal_range(flush_request_id);
559 for (auto it = range.first; it != range.second; it++) {
560 if (it->second == ds_id) {
561 pending_flushes_.erase(it);
562 break;
563 }
564 }
565
566 if (pending_flushes_.count(flush_request_id))
567 return; // Still waiting for other data sources to ack.
568
569 PERFETTO_DLOG("All data sources acked to flush %" PRIu64, flush_request_id);
570 endpoint_->NotifyFlushComplete(flush_request_id);
571 }
572
OnFlushTimeout(FlushRequestID flush_request_id)573 void ProbesProducer::OnFlushTimeout(FlushRequestID flush_request_id) {
574 if (pending_flushes_.count(flush_request_id) == 0)
575 return; // All acked.
576 PERFETTO_ELOG("Flush(%" PRIu64 ") timed out", flush_request_id);
577 pending_flushes_.erase(flush_request_id);
578 endpoint_->NotifyFlushComplete(flush_request_id);
579 }
580
ClearIncrementalState(const DataSourceInstanceID * data_source_ids,size_t num_data_sources)581 void ProbesProducer::ClearIncrementalState(
582 const DataSourceInstanceID* data_source_ids,
583 size_t num_data_sources) {
584 for (size_t i = 0; i < num_data_sources; i++) {
585 DataSourceInstanceID ds_id = data_source_ids[i];
586 auto it = data_sources_.find(ds_id);
587 if (it == data_sources_.end() || !it->second->started)
588 continue;
589
590 it->second->ClearIncrementalState();
591 }
592 }
593
594 // This function is called by the FtraceController in batches, whenever it has
595 // read one or more pages from one or more cpus and written that into the
596 // userspace tracing buffer. If more than one ftrace data sources are active,
597 // this call typically happens after writing for all session has been handled.
OnFtraceDataWrittenIntoDataSourceBuffers()598 void ProbesProducer::OnFtraceDataWrittenIntoDataSourceBuffers() {
599 for (const auto& tracing_session : session_data_sources_) {
600 // Take the metadata (e.g. new pids) collected from ftrace and pass it to
601 // other interested data sources (e.g. the process scraper to get command
602 // lines on new pids and tgid<>tid mappings). Note: there can be more than
603 // one ftrace data source per session. All of them should be considered
604 // (b/169226092).
605 const std::unordered_multimap<const ProbesDataSource::Descriptor*,
606 ProbesDataSource*>& ds_by_type =
607 tracing_session.second;
608 auto ft_range = ds_by_type.equal_range(&FtraceDataSource::descriptor);
609
610 auto ino_range = ds_by_type.equal_range(&InodeFileDataSource::descriptor);
611 auto ps_range = ds_by_type.equal_range(&ProcessStatsDataSource::descriptor);
612 for (auto ft_it = ft_range.first; ft_it != ft_range.second; ft_it++) {
613 auto* ftrace_ds = static_cast<FtraceDataSource*>(ft_it->second);
614 if (!ftrace_ds->started)
615 continue;
616 auto* metadata = ftrace_ds->mutable_metadata();
617 for (auto ps_it = ps_range.first; ps_it != ps_range.second; ps_it++) {
618 auto* ps_ds = static_cast<ProcessStatsDataSource*>(ps_it->second);
619 if (!ps_ds->started || !ps_ds->on_demand_dumps_enabled())
620 continue;
621 // Ordering the rename pids before the seen pids is important so that
622 // any renamed processes get scraped in the OnPids call.
623 if (!metadata->rename_pids.empty())
624 ps_ds->OnRenamePids(metadata->rename_pids);
625 if (!metadata->pids.empty())
626 ps_ds->OnPids(metadata->pids);
627 if (!metadata->fds.empty())
628 ps_ds->OnFds(metadata->fds);
629 }
630 for (auto in_it = ino_range.first; in_it != ino_range.second; in_it++) {
631 auto* inode_ds = static_cast<InodeFileDataSource*>(in_it->second);
632 if (!inode_ds->started)
633 continue;
634 inode_ds->OnInodes(metadata->inode_and_device);
635 }
636 metadata->Clear();
637 } // for (FtraceDataSource)
638 } // for (tracing_session)
639 }
640
ConnectWithRetries(const char * socket_name,base::TaskRunner * task_runner)641 void ProbesProducer::ConnectWithRetries(const char* socket_name,
642 base::TaskRunner* task_runner) {
643 PERFETTO_DCHECK(state_ == kNotStarted);
644 state_ = kNotConnected;
645
646 ResetConnectionBackoff();
647 socket_name_ = socket_name;
648 task_runner_ = task_runner;
649 Connect();
650 }
651
Connect()652 void ProbesProducer::Connect() {
653 PERFETTO_DCHECK(state_ == kNotConnected);
654 state_ = kConnecting;
655 endpoint_ = ProducerIPCClient::Connect(
656 socket_name_, this, "perfetto.traced_probes", task_runner_,
657 TracingService::ProducerSMBScrapingMode::kDisabled,
658 kTracingSharedMemSizeHintBytes, kTracingSharedMemPageSizeHintBytes);
659 }
660
IncreaseConnectionBackoff()661 void ProbesProducer::IncreaseConnectionBackoff() {
662 connection_backoff_ms_ *= 2;
663 if (connection_backoff_ms_ > kMaxConnectionBackoffMs)
664 connection_backoff_ms_ = kMaxConnectionBackoffMs;
665 }
666
ResetConnectionBackoff()667 void ProbesProducer::ResetConnectionBackoff() {
668 connection_backoff_ms_ = kInitialConnectionBackoffMs;
669 }
670
ActivateTrigger(std::string trigger)671 void ProbesProducer::ActivateTrigger(std::string trigger) {
672 android_stats::MaybeLogTriggerEvent(
673 PerfettoTriggerAtom::kProbesProducerTrigger, trigger);
674
675 task_runner_->PostTask([this, trigger]() {
676 if (!endpoint_) {
677 android_stats::MaybeLogTriggerEvent(
678 PerfettoTriggerAtom::kProbesProducerTriggerFail, trigger);
679 return;
680 }
681 endpoint_->ActivateTriggers({trigger});
682 });
683 }
684
685 } // namespace perfetto
686