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/ftrace/ftrace_config_muxer.h"
18
19 #include <stdint.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23
24 #include <algorithm>
25 #include <iterator>
26
27 #include "perfetto/ext/base/utils.h"
28 #include "protos/perfetto/trace/ftrace/sched.pbzero.h"
29 #include "src/traced/probes/ftrace/atrace_wrapper.h"
30 #include "src/traced/probes/ftrace/compact_sched.h"
31
32 namespace perfetto {
33 namespace {
34
35 constexpr int kDefaultPerCpuBufferSizeKb = 2 * 1024; // 2mb
36 constexpr int kMaxPerCpuBufferSizeKb = 64 * 1024; // 64mb
37
38 // trace_clocks in preference order.
39 constexpr const char* kClocks[] = {"boot", "global", "local"};
40
AddEventGroup(const ProtoTranslationTable * table,const std::string & group,std::set<GroupAndName> * to)41 void AddEventGroup(const ProtoTranslationTable* table,
42 const std::string& group,
43 std::set<GroupAndName>* to) {
44 const std::vector<const Event*>* events = table->GetEventsByGroup(group);
45 if (!events)
46 return;
47 for (const Event* event : *events)
48 to->insert(GroupAndName(group, event->name));
49 }
50
ReadEventsInGroupFromFs(const FtraceProcfs & ftrace_procfs,const std::string & group)51 std::set<GroupAndName> ReadEventsInGroupFromFs(
52 const FtraceProcfs& ftrace_procfs,
53 const std::string& group) {
54 std::set<std::string> names =
55 ftrace_procfs.GetEventNamesForGroup("events/" + group);
56 std::set<GroupAndName> events;
57 for (const auto& name : names)
58 events.insert(GroupAndName(group, name));
59 return events;
60 }
61
EventToStringGroupAndName(const std::string & event)62 std::pair<std::string, std::string> EventToStringGroupAndName(
63 const std::string& event) {
64 auto slash_pos = event.find("/");
65 if (slash_pos == std::string::npos)
66 return std::make_pair("", event);
67 return std::make_pair(event.substr(0, slash_pos),
68 event.substr(slash_pos + 1));
69 }
70
UnionInPlace(const std::vector<std::string> & unsorted_a,std::vector<std::string> * out)71 void UnionInPlace(const std::vector<std::string>& unsorted_a,
72 std::vector<std::string>* out) {
73 std::vector<std::string> a = unsorted_a;
74 std::sort(a.begin(), a.end());
75 std::sort(out->begin(), out->end());
76 std::vector<std::string> v;
77 std::set_union(a.begin(), a.end(), out->begin(), out->end(),
78 std::back_inserter(v));
79 *out = std::move(v);
80 }
81
IntersectInPlace(const std::vector<std::string> & unsorted_a,std::vector<std::string> * out)82 void IntersectInPlace(const std::vector<std::string>& unsorted_a,
83 std::vector<std::string>* out) {
84 std::vector<std::string> a = unsorted_a;
85 std::sort(a.begin(), a.end());
86 std::sort(out->begin(), out->end());
87 std::vector<std::string> v;
88 std::set_intersection(a.begin(), a.end(), out->begin(), out->end(),
89 std::back_inserter(v));
90 *out = std::move(v);
91 }
92
93 } // namespace
94
GetFtraceEvents(const FtraceConfig & request,const ProtoTranslationTable * table)95 std::set<GroupAndName> FtraceConfigMuxer::GetFtraceEvents(
96 const FtraceConfig& request,
97 const ProtoTranslationTable* table) {
98 std::set<GroupAndName> events;
99 for (const auto& config_value : request.ftrace_events()) {
100 std::string group;
101 std::string name;
102 std::tie(group, name) = EventToStringGroupAndName(config_value);
103 if (name == "*") {
104 for (const auto& event : ReadEventsInGroupFromFs(*ftrace_, group))
105 events.insert(event);
106 } else if (group.empty()) {
107 // If there is no group specified, find an event with that name and
108 // use it's group.
109 const Event* e = table->GetEventByName(name);
110 if (!e) {
111 PERFETTO_DLOG(
112 "Event doesn't exist: %s. Include the group in the config to allow "
113 "the event to be output as a generic event.",
114 name.c_str());
115 continue;
116 }
117 events.insert(GroupAndName(e->group, e->name));
118 } else {
119 events.insert(GroupAndName(group, name));
120 }
121 }
122 if (RequiresAtrace(request)) {
123 events.insert(GroupAndName("ftrace", "print"));
124
125 // Ideally we should keep this code in sync with:
126 // platform/frameworks/native/cmds/atrace/atrace.cpp
127 // It's not a disaster if they go out of sync, we can always add the ftrace
128 // categories manually server side but this is user friendly and reduces the
129 // size of the configs.
130 for (const std::string& category : request.atrace_categories()) {
131 if (category == "gfx") {
132 AddEventGroup(table, "mdss", &events);
133 events.insert(GroupAndName("mdss", "rotator_bw_ao_as_context"));
134 events.insert(GroupAndName("mdss", "mdp_trace_counter"));
135 events.insert(GroupAndName("mdss", "tracing_mark_write"));
136 events.insert(GroupAndName("mdss", "mdp_cmd_wait_pingpong"));
137 events.insert(GroupAndName("mdss", "mdp_cmd_kickoff"));
138 events.insert(GroupAndName("mdss", "mdp_cmd_release_bw"));
139 events.insert(GroupAndName("mdss", "mdp_cmd_readptr_done"));
140 events.insert(GroupAndName("mdss", "mdp_cmd_pingpong_done"));
141 events.insert(GroupAndName("mdss", "mdp_misr_crc"));
142 events.insert(GroupAndName("mdss", "mdp_compare_bw"));
143 events.insert(GroupAndName("mdss", "mdp_perf_update_bus"));
144 events.insert(GroupAndName("mdss", "mdp_video_underrun_done"));
145 events.insert(GroupAndName("mdss", "mdp_commit"));
146 events.insert(GroupAndName("mdss", "mdp_mixer_update"));
147 events.insert(GroupAndName("mdss", "mdp_perf_prefill_calc"));
148 events.insert(GroupAndName("mdss", "mdp_perf_set_ot"));
149 events.insert(GroupAndName("mdss", "mdp_perf_set_wm_levels"));
150 events.insert(GroupAndName("mdss", "mdp_perf_set_panic_luts"));
151 events.insert(GroupAndName("mdss", "mdp_perf_set_qos_luts"));
152 events.insert(GroupAndName("mdss", "mdp_sspp_change"));
153 events.insert(GroupAndName("mdss", "mdp_sspp_set"));
154 AddEventGroup(table, "mali_systrace", &events);
155
156 AddEventGroup(table, "sde", &events);
157 events.insert(GroupAndName("sde", "tracing_mark_write"));
158 events.insert(GroupAndName("sde", "sde_perf_update_bus"));
159 events.insert(GroupAndName("sde", "sde_perf_set_qos_luts"));
160 events.insert(GroupAndName("sde", "sde_perf_set_ot"));
161 events.insert(GroupAndName("sde", "sde_perf_set_danger_luts"));
162 events.insert(GroupAndName("sde", "sde_perf_crtc_update"));
163 events.insert(GroupAndName("sde", "sde_perf_calc_crtc"));
164 events.insert(GroupAndName("sde", "sde_evtlog"));
165 events.insert(GroupAndName("sde", "sde_encoder_underrun"));
166 events.insert(GroupAndName("sde", "sde_cmd_release_bw"));
167 continue;
168 }
169
170 if (category == "ion") {
171 events.insert(GroupAndName("kmem", "ion_alloc_buffer_start"));
172 continue;
173 }
174
175 // Note: sched_wakeup intentionally removed (diverging from atrace), as it
176 // is high-volume, but mostly redundant when sched_waking is also enabled.
177 // The event can still be enabled explicitly when necessary.
178 if (category == "sched") {
179 events.insert(GroupAndName("sched", "sched_switch"));
180 events.insert(GroupAndName("sched", "sched_waking"));
181 events.insert(GroupAndName("sched", "sched_blocked_reason"));
182 events.insert(GroupAndName("sched", "sched_cpu_hotplug"));
183 events.insert(GroupAndName("sched", "sched_pi_setprio"));
184 events.insert(GroupAndName("sched", "sched_process_exit"));
185 AddEventGroup(table, "cgroup", &events);
186 events.insert(GroupAndName("cgroup", "cgroup_transfer_tasks"));
187 events.insert(GroupAndName("cgroup", "cgroup_setup_root"));
188 events.insert(GroupAndName("cgroup", "cgroup_rmdir"));
189 events.insert(GroupAndName("cgroup", "cgroup_rename"));
190 events.insert(GroupAndName("cgroup", "cgroup_remount"));
191 events.insert(GroupAndName("cgroup", "cgroup_release"));
192 events.insert(GroupAndName("cgroup", "cgroup_mkdir"));
193 events.insert(GroupAndName("cgroup", "cgroup_destroy_root"));
194 events.insert(GroupAndName("cgroup", "cgroup_attach_task"));
195 events.insert(GroupAndName("oom", "oom_score_adj_update"));
196 events.insert(GroupAndName("task", "task_rename"));
197 events.insert(GroupAndName("task", "task_newtask"));
198
199 AddEventGroup(table, "systrace", &events);
200 events.insert(GroupAndName("systrace", "0"));
201
202 AddEventGroup(table, "scm", &events);
203 events.insert(GroupAndName("scm", "scm_call_start"));
204 events.insert(GroupAndName("scm", "scm_call_end"));
205 continue;
206 }
207
208 if (category == "irq") {
209 AddEventGroup(table, "irq", &events);
210 events.insert(GroupAndName("irq", "tasklet_hi_exit"));
211 events.insert(GroupAndName("irq", "tasklet_hi_entry"));
212 events.insert(GroupAndName("irq", "tasklet_exit"));
213 events.insert(GroupAndName("irq", "tasklet_entry"));
214 events.insert(GroupAndName("irq", "softirq_raise"));
215 events.insert(GroupAndName("irq", "softirq_exit"));
216 events.insert(GroupAndName("irq", "softirq_entry"));
217 events.insert(GroupAndName("irq", "irq_handler_exit"));
218 events.insert(GroupAndName("irq", "irq_handler_entry"));
219 AddEventGroup(table, "ipi", &events);
220 events.insert(GroupAndName("ipi", "ipi_raise"));
221 events.insert(GroupAndName("ipi", "ipi_exit"));
222 events.insert(GroupAndName("ipi", "ipi_entry"));
223 continue;
224 }
225
226 if (category == "irqoff") {
227 events.insert(GroupAndName("preemptirq", "irq_enable"));
228 events.insert(GroupAndName("preemptirq", "irq_disable"));
229 continue;
230 }
231
232 if (category == "preemptoff") {
233 events.insert(GroupAndName("preemptirq", "preempt_enable"));
234 events.insert(GroupAndName("preemptirq", "preempt_disable"));
235 continue;
236 }
237
238 if (category == "i2c") {
239 AddEventGroup(table, "i2c", &events);
240 events.insert(GroupAndName("i2c", "i2c_read"));
241 events.insert(GroupAndName("i2c", "i2c_write"));
242 events.insert(GroupAndName("i2c", "i2c_result"));
243 events.insert(GroupAndName("i2c", "i2c_reply"));
244 events.insert(GroupAndName("i2c", "smbus_read"));
245 events.insert(GroupAndName("i2c", "smbus_write"));
246 events.insert(GroupAndName("i2c", "smbus_result"));
247 events.insert(GroupAndName("i2c", "smbus_reply"));
248 continue;
249 }
250
251 if (category == "freq") {
252 events.insert(GroupAndName("power", "cpu_frequency"));
253 events.insert(GroupAndName("power", "gpu_frequency"));
254 events.insert(GroupAndName("power", "clock_set_rate"));
255 events.insert(GroupAndName("power", "clock_disable"));
256 events.insert(GroupAndName("power", "clock_enable"));
257 events.insert(GroupAndName("clk", "clk_set_rate"));
258 events.insert(GroupAndName("clk", "clk_disable"));
259 events.insert(GroupAndName("clk", "clk_enable"));
260 events.insert(GroupAndName("power", "cpu_frequency_limits"));
261 events.insert(GroupAndName("power", "suspend_resume"));
262 AddEventGroup(table, "msm_bus", &events);
263 events.insert(GroupAndName("msm_bus", "bus_update_request_end"));
264 events.insert(GroupAndName("msm_bus", "bus_update_request"));
265 events.insert(GroupAndName("msm_bus", "bus_rules_matches"));
266 events.insert(GroupAndName("msm_bus", "bus_max_votes"));
267 events.insert(GroupAndName("msm_bus", "bus_client_status"));
268 events.insert(GroupAndName("msm_bus", "bus_bke_params"));
269 events.insert(GroupAndName("msm_bus", "bus_bimc_config_limiter"));
270 events.insert(GroupAndName("msm_bus", "bus_avail_bw"));
271 events.insert(GroupAndName("msm_bus", "bus_agg_bw"));
272 continue;
273 }
274
275 if (category == "membus") {
276 AddEventGroup(table, "memory_bus", &events);
277 continue;
278 }
279
280 if (category == "idle") {
281 events.insert(GroupAndName("power", "cpu_idle"));
282 continue;
283 }
284
285 if (category == "disk") {
286 events.insert(GroupAndName("f2fs", "f2fs_sync_file_enter"));
287 events.insert(GroupAndName("f2fs", "f2fs_sync_file_exit"));
288 events.insert(GroupAndName("f2fs", "f2fs_write_begin"));
289 events.insert(GroupAndName("f2fs", "f2fs_write_end"));
290 events.insert(GroupAndName("ext4", "ext4_da_write_begin"));
291 events.insert(GroupAndName("ext4", "ext4_da_write_end"));
292 events.insert(GroupAndName("ext4", "ext4_sync_file_enter"));
293 events.insert(GroupAndName("ext4", "ext4_sync_file_exit"));
294 events.insert(GroupAndName("block", "block_rq_issue"));
295 events.insert(GroupAndName("block", "block_rq_complete"));
296 continue;
297 }
298
299 if (category == "mmc") {
300 AddEventGroup(table, "mmc", &events);
301 continue;
302 }
303
304 if (category == "load") {
305 AddEventGroup(table, "cpufreq_interactive", &events);
306 continue;
307 }
308
309 if (category == "sync") {
310 // linux kernel < 4.9
311 AddEventGroup(table, "sync", &events);
312 events.insert(GroupAndName("sync", "sync_pt"));
313 events.insert(GroupAndName("sync", "sync_timeline"));
314 events.insert(GroupAndName("sync", "sync_wait"));
315 // linux kernel == 4.9.x
316 AddEventGroup(table, "fence", &events);
317 events.insert(GroupAndName("fence", "fence_annotate_wait_on"));
318 events.insert(GroupAndName("fence", "fence_destroy"));
319 events.insert(GroupAndName("fence", "fence_emit"));
320 events.insert(GroupAndName("fence", "fence_enable_signal"));
321 events.insert(GroupAndName("fence", "fence_init"));
322 events.insert(GroupAndName("fence", "fence_signaled"));
323 events.insert(GroupAndName("fence", "fence_wait_end"));
324 events.insert(GroupAndName("fence", "fence_wait_start"));
325 // linux kernel > 4.9
326 AddEventGroup(table, "dma_fence", &events);
327 continue;
328 }
329
330 if (category == "workq") {
331 AddEventGroup(table, "workqueue", &events);
332 events.insert(GroupAndName("workqueue", "workqueue_queue_work"));
333 events.insert(GroupAndName("workqueue", "workqueue_execute_start"));
334 events.insert(GroupAndName("workqueue", "workqueue_execute_end"));
335 events.insert(GroupAndName("workqueue", "workqueue_activate_work"));
336 continue;
337 }
338
339 if (category == "memreclaim") {
340 events.insert(GroupAndName("vmscan", "mm_vmscan_direct_reclaim_begin"));
341 events.insert(GroupAndName("vmscan", "mm_vmscan_direct_reclaim_end"));
342 events.insert(GroupAndName("vmscan", "mm_vmscan_kswapd_wake"));
343 events.insert(GroupAndName("vmscan", "mm_vmscan_kswapd_sleep"));
344 AddEventGroup(table, "lowmemorykiller", &events);
345 events.insert(GroupAndName("lowmemorykiller", "lowmemory_kill"));
346 continue;
347 }
348
349 if (category == "regulators") {
350 AddEventGroup(table, "regulator", &events);
351 events.insert(
352 GroupAndName("regulator", "regulator_set_voltage_complete"));
353 events.insert(GroupAndName("regulator", "regulator_set_voltage"));
354 events.insert(GroupAndName("regulator", "regulator_enable_delay"));
355 events.insert(GroupAndName("regulator", "regulator_enable_complete"));
356 events.insert(GroupAndName("regulator", "regulator_enable"));
357 events.insert(GroupAndName("regulator", "regulator_disable_complete"));
358 events.insert(GroupAndName("regulator", "regulator_disable"));
359 continue;
360 }
361
362 if (category == "binder_driver") {
363 events.insert(GroupAndName("binder", "binder_transaction"));
364 events.insert(GroupAndName("binder", "binder_transaction_received"));
365 events.insert(GroupAndName("binder", "binder_transaction_alloc_buf"));
366 events.insert(GroupAndName("binder", "binder_set_priority"));
367 continue;
368 }
369
370 if (category == "binder_lock") {
371 events.insert(GroupAndName("binder", "binder_lock"));
372 events.insert(GroupAndName("binder", "binder_locked"));
373 events.insert(GroupAndName("binder", "binder_unlock"));
374 continue;
375 }
376
377 if (category == "pagecache") {
378 AddEventGroup(table, "filemap", &events);
379 events.insert(
380 GroupAndName("filemap", "mm_filemap_delete_from_page_cache"));
381 events.insert(
382 GroupAndName("filemap", "mm_filemap_delete_from_page_cache"));
383 events.insert(GroupAndName("filemap", "mm_filemap_add_to_page_cache"));
384 events.insert(GroupAndName("filemap", "filemap_set_wb_err"));
385 events.insert(GroupAndName("filemap", "file_check_and_advance_wb_err"));
386 continue;
387 }
388
389 if (category == "memory") {
390 events.insert(GroupAndName("kmem", "rss_stat"));
391 events.insert(GroupAndName("kmem", "ion_heap_grow"));
392 events.insert(GroupAndName("kmem", "ion_heap_shrink"));
393 // ion_stat supersedes ion_heap_grow / shrink for kernel 4.19+
394 events.insert(GroupAndName("ion", "ion_stat"));
395 events.insert(GroupAndName("mm_event", "mm_event_record"));
396 continue;
397 }
398 }
399 }
400 return events;
401 }
402
403 // Post-conditions:
404 // 1. result >= 1 (should have at least one page per CPU)
405 // 2. result * 4 < kMaxTotalBufferSizeKb
406 // 3. If input is 0 output is a good default number.
ComputeCpuBufferSizeInPages(size_t requested_buffer_size_kb)407 size_t ComputeCpuBufferSizeInPages(size_t requested_buffer_size_kb) {
408 if (requested_buffer_size_kb == 0)
409 requested_buffer_size_kb = kDefaultPerCpuBufferSizeKb;
410 if (requested_buffer_size_kb > kMaxPerCpuBufferSizeKb) {
411 PERFETTO_ELOG(
412 "The requested ftrace buf size (%zu KB) is too big, capping to %d KB",
413 requested_buffer_size_kb, kMaxPerCpuBufferSizeKb);
414 requested_buffer_size_kb = kMaxPerCpuBufferSizeKb;
415 }
416
417 size_t pages = requested_buffer_size_kb / (base::kPageSize / 1024);
418 if (pages == 0)
419 return 1;
420
421 return pages;
422 }
423
FtraceConfigMuxer(FtraceProcfs * ftrace,ProtoTranslationTable * table,std::map<std::string,std::vector<GroupAndName>> vendor_events)424 FtraceConfigMuxer::FtraceConfigMuxer(
425 FtraceProcfs* ftrace,
426 ProtoTranslationTable* table,
427 std::map<std::string, std::vector<GroupAndName>> vendor_events)
428 : ftrace_(ftrace),
429 table_(table),
430 current_state_(),
431 ds_configs_(),
432 vendor_events_(vendor_events) {}
433 FtraceConfigMuxer::~FtraceConfigMuxer() = default;
434
SetupConfig(const FtraceConfig & request)435 FtraceConfigId FtraceConfigMuxer::SetupConfig(const FtraceConfig& request) {
436 EventFilter filter;
437 bool is_ftrace_enabled = ftrace_->IsTracingEnabled();
438 if (ds_configs_.empty()) {
439 PERFETTO_DCHECK(active_configs_.empty());
440
441 // If someone outside of perfetto is using ftrace give up now.
442 if (is_ftrace_enabled) {
443 PERFETTO_ELOG("ftrace in use by non-Perfetto.");
444 return 0;
445 }
446
447 // Setup ftrace, without starting it. Setting buffers can be quite slow
448 // (up to hundreds of ms).
449 SetupClock(request);
450 SetupBufferSize(request);
451 } else {
452 // Did someone turn ftrace off behind our back? If so give up.
453 if (!active_configs_.empty() && !is_ftrace_enabled) {
454 PERFETTO_ELOG("ftrace disabled by non-Perfetto.");
455 return 0;
456 }
457 }
458
459 std::set<GroupAndName> events = GetFtraceEvents(request, table_);
460
461 // Vendors can provide a set of extra ftrace categories to be enabled when a
462 // specific atrace category is used (e.g. "gfx" -> ["my_hw/my_custom_event",
463 // "my_hw/my_special_gpu"]). Merge them with the hard coded events for each
464 // categories.
465 for (const std::string& category : request.atrace_categories()) {
466 if (vendor_events_.count(category)) {
467 for (const GroupAndName& event : vendor_events_[category]) {
468 events.insert(event);
469 }
470 }
471 }
472
473 if (RequiresAtrace(request))
474 UpdateAtrace(request);
475
476 for (const auto& group_and_name : events) {
477 const Event* event = table_->GetOrCreateEvent(group_and_name);
478 if (!event) {
479 PERFETTO_DLOG("Can't enable %s, event not known",
480 group_and_name.ToString().c_str());
481 continue;
482 }
483 // Note: ftrace events are always implicitly enabled (and don't have an
484 // "enable" file). So they aren't tracked by the central event filter (but
485 // still need to be added to the per data source event filter to retain the
486 // events during parsing).
487 if (current_state_.ftrace_events.IsEventEnabled(event->ftrace_event_id) ||
488 std::string("ftrace") == event->group) {
489 filter.AddEnabledEvent(event->ftrace_event_id);
490 continue;
491 }
492 if (ftrace_->EnableEvent(event->group, event->name)) {
493 current_state_.ftrace_events.AddEnabledEvent(event->ftrace_event_id);
494 filter.AddEnabledEvent(event->ftrace_event_id);
495 } else {
496 PERFETTO_DPLOG("Failed to enable %s.", group_and_name.ToString().c_str());
497 }
498 }
499
500 auto compact_sched =
501 CreateCompactSchedConfig(request, table_->compact_sched_format());
502
503 std::vector<std::string> apps(request.atrace_apps());
504 std::vector<std::string> categories(request.atrace_categories());
505
506 FtraceConfigId id = ++last_id_;
507 ds_configs_.emplace(
508 std::piecewise_construct, std::forward_as_tuple(id),
509 std::forward_as_tuple(std::move(filter), compact_sched, std::move(apps),
510 std::move(categories)));
511 return id;
512 }
513
ActivateConfig(FtraceConfigId id)514 bool FtraceConfigMuxer::ActivateConfig(FtraceConfigId id) {
515 if (!id || ds_configs_.count(id) == 0) {
516 PERFETTO_DFATAL("Config not found");
517 return false;
518 }
519
520 if (active_configs_.empty()) {
521 if (ftrace_->IsTracingEnabled()) {
522 // If someone outside of perfetto is using ftrace give up now.
523 PERFETTO_ELOG("ftrace in use by non-Perfetto.");
524 return false;
525 }
526 if (!ftrace_->EnableTracing()) {
527 PERFETTO_ELOG("Failed to enable ftrace.");
528 return false;
529 }
530 }
531
532 active_configs_.insert(id);
533 return true;
534 }
535
RemoveConfig(FtraceConfigId config_id)536 bool FtraceConfigMuxer::RemoveConfig(FtraceConfigId config_id) {
537 if (!config_id || !ds_configs_.erase(config_id))
538 return false;
539 EventFilter expected_ftrace_events;
540 std::vector<std::string> expected_apps;
541 std::vector<std::string> expected_categories;
542 for (const auto& id_config : ds_configs_) {
543 const perfetto::FtraceDataSourceConfig& config = id_config.second;
544 expected_ftrace_events.EnableEventsFrom(config.event_filter);
545 UnionInPlace(config.atrace_apps, &expected_apps);
546 UnionInPlace(config.atrace_categories, &expected_categories);
547 }
548 // At this point expected_{apps,categories} contains the union of the
549 // leftover configs (if any) that should be still on. However we did not
550 // necessarily succeed in turning on atrace for each of those configs
551 // previously so we now intersect the {apps,categories} that we *did* manage
552 // to turn on with those we want on to determine the new state we should aim
553 // for:
554 IntersectInPlace(current_state_.atrace_apps, &expected_apps);
555 IntersectInPlace(current_state_.atrace_categories, &expected_categories);
556 // Work out if there is any difference between the current state and the
557 // desired state: It's sufficient to compare sizes here (since we know from
558 // above that expected_{apps,categories} is now a subset of
559 // atrace_{apps,categories}:
560 bool atrace_changed =
561 (current_state_.atrace_apps.size() != expected_apps.size()) ||
562 (current_state_.atrace_categories.size() != expected_categories.size());
563
564 // Disable any events that are currently enabled, but are not in any configs
565 // anymore.
566 std::set<size_t> event_ids = current_state_.ftrace_events.GetEnabledEvents();
567 for (size_t id : event_ids) {
568 if (expected_ftrace_events.IsEventEnabled(id))
569 continue;
570 const Event* event = table_->GetEventById(id);
571 // Any event that was enabled must exist.
572 PERFETTO_DCHECK(event);
573 if (ftrace_->DisableEvent(event->group, event->name))
574 current_state_.ftrace_events.DisableEvent(event->ftrace_event_id);
575 }
576
577 // If there aren't any more active configs, disable ftrace.
578 auto active_it = active_configs_.find(config_id);
579 if (active_it != active_configs_.end()) {
580 active_configs_.erase(active_it);
581 if (active_configs_.empty()) {
582 // This was the last active config, disable ftrace.
583 if (!ftrace_->DisableTracing())
584 PERFETTO_ELOG("Failed to disable ftrace.");
585 }
586 }
587
588 // Even if we don't have any other active configs, we might still have idle
589 // configs around. Tear down the rest of the ftrace config only if all
590 // configs are removed.
591 if (ds_configs_.empty()) {
592 if (ftrace_->SetCpuBufferSizeInPages(1))
593 current_state_.cpu_buffer_size_pages = 1;
594 ftrace_->DisableAllEvents();
595 ftrace_->ClearTrace();
596 }
597
598 if (current_state_.atrace_on) {
599 if (expected_apps.empty() && expected_categories.empty()) {
600 DisableAtrace();
601 } else if (atrace_changed) {
602 // Update atrace to remove the no longer wanted categories/apps. For
603 // some categories this won't disable them (e.g. categories that just
604 // enable ftrace events) for those there is nothing we can do till the
605 // last ftrace config is removed.
606 if (StartAtrace(expected_apps, expected_categories)) {
607 // Update current_state_ to reflect this change.
608 current_state_.atrace_apps = expected_apps;
609 current_state_.atrace_categories = expected_categories;
610 }
611 }
612 }
613
614 return true;
615 }
616
GetDataSourceConfig(FtraceConfigId id)617 const FtraceDataSourceConfig* FtraceConfigMuxer::GetDataSourceConfig(
618 FtraceConfigId id) {
619 if (!ds_configs_.count(id))
620 return nullptr;
621 return &ds_configs_.at(id);
622 }
623
SetupClock(const FtraceConfig &)624 void FtraceConfigMuxer::SetupClock(const FtraceConfig&) {
625 std::string current_clock = ftrace_->GetClock();
626 std::set<std::string> clocks = ftrace_->AvailableClocks();
627
628 for (size_t i = 0; i < base::ArraySize(kClocks); i++) {
629 std::string clock = std::string(kClocks[i]);
630 if (!clocks.count(clock))
631 continue;
632 if (current_clock == clock)
633 break;
634 ftrace_->SetClock(clock);
635 break;
636 }
637 }
638
SetupBufferSize(const FtraceConfig & request)639 void FtraceConfigMuxer::SetupBufferSize(const FtraceConfig& request) {
640 size_t pages = ComputeCpuBufferSizeInPages(request.buffer_size_kb());
641 ftrace_->SetCpuBufferSizeInPages(pages);
642 current_state_.cpu_buffer_size_pages = pages;
643 }
644
GetPerCpuBufferSizePages()645 size_t FtraceConfigMuxer::GetPerCpuBufferSizePages() {
646 return current_state_.cpu_buffer_size_pages;
647 }
648
UpdateAtrace(const FtraceConfig & request)649 void FtraceConfigMuxer::UpdateAtrace(const FtraceConfig& request) {
650 // We want to avoid poisoning current_state_.atrace_{categories, apps}
651 // if for some reason these args make atrace unhappy so we stash the
652 // union into temps and only update current_state_ if we successfully
653 // run atrace.
654
655 std::vector<std::string> combined_categories = request.atrace_categories();
656 UnionInPlace(current_state_.atrace_categories, &combined_categories);
657
658 std::vector<std::string> combined_apps = request.atrace_apps();
659 UnionInPlace(current_state_.atrace_apps, &combined_apps);
660
661 if (current_state_.atrace_on &&
662 combined_apps.size() == current_state_.atrace_apps.size() &&
663 combined_categories.size() == current_state_.atrace_categories.size()) {
664 return;
665 }
666
667 if (StartAtrace(combined_apps, combined_categories)) {
668 current_state_.atrace_categories = combined_categories;
669 current_state_.atrace_apps = combined_apps;
670 current_state_.atrace_on = true;
671 }
672 }
673
674 // static
StartAtrace(const std::vector<std::string> & apps,const std::vector<std::string> & categories)675 bool FtraceConfigMuxer::StartAtrace(
676 const std::vector<std::string>& apps,
677 const std::vector<std::string>& categories) {
678 PERFETTO_DLOG("Update atrace config...");
679
680 std::vector<std::string> args;
681 args.push_back("atrace"); // argv0 for exec()
682 args.push_back("--async_start");
683 args.push_back("--only_userspace");
684
685 for (const auto& category : categories)
686 args.push_back(category);
687
688 if (!apps.empty()) {
689 args.push_back("-a");
690 std::string arg = "";
691 for (const auto& app : apps) {
692 arg += app;
693 arg += ",";
694 }
695 arg.resize(arg.size() - 1);
696 args.push_back(arg);
697 }
698
699 bool result = RunAtrace(args);
700 PERFETTO_DLOG("...done (%s)", result ? "success" : "fail");
701 return result;
702 }
703
DisableAtrace()704 void FtraceConfigMuxer::DisableAtrace() {
705 PERFETTO_DCHECK(current_state_.atrace_on);
706
707 PERFETTO_DLOG("Stop atrace...");
708
709 if (RunAtrace({"atrace", "--async_stop", "--only_userspace"})) {
710 current_state_.atrace_categories.clear();
711 current_state_.atrace_apps.clear();
712 current_state_.atrace_on = false;
713 }
714
715 PERFETTO_DLOG("...done");
716 }
717
718 } // namespace perfetto
719