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