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