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