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 #ifndef SRC_TRACED_PROBES_FTRACE_FTRACE_CONFIG_MUXER_H_
18 #define SRC_TRACED_PROBES_FTRACE_FTRACE_CONFIG_MUXER_H_
19 
20 #include <map>
21 #include <set>
22 
23 #include "src/traced/probes/ftrace/compact_sched.h"
24 #include "src/traced/probes/ftrace/ftrace_config_utils.h"
25 #include "src/traced/probes/ftrace/ftrace_controller.h"
26 #include "src/traced/probes/ftrace/ftrace_procfs.h"
27 #include "src/traced/probes/ftrace/proto_translation_table.h"
28 
29 namespace perfetto {
30 
31 // State held by the muxer per data source, used to parse ftrace according to
32 // that data source's config.
33 struct FtraceDataSourceConfig {
FtraceDataSourceConfigFtraceDataSourceConfig34   FtraceDataSourceConfig(EventFilter _event_filter,
35                          CompactSchedConfig _compact_sched,
36                          std::vector<std::string> _atrace_apps,
37                          std::vector<std::string> _atrace_categories,
38                          bool _symbolize_ksyms)
39       : event_filter(std::move(_event_filter)),
40         compact_sched(_compact_sched),
41         atrace_apps(std::move(_atrace_apps)),
42         atrace_categories(std::move(_atrace_categories)),
43         symbolize_ksyms(_symbolize_ksyms) {}
44 
45   // The event filter allows to quickly check if a certain ftrace event with id
46   // x is enabled for this data source.
47   EventFilter event_filter;
48 
49   // Configuration of the optional compact encoding of scheduling events.
50   const CompactSchedConfig compact_sched;
51 
52   // Used only in Android for ATRACE_EVENT/os.Trace() userspace annotations.
53   std::vector<std::string> atrace_apps;
54   std::vector<std::string> atrace_categories;
55 
56   // When enabled will turn on the the kallsyms symbolizer in CpuReader.
57   const bool symbolize_ksyms;
58 };
59 
60 // Ftrace is a bunch of globally modifiable persistent state.
61 // Given a number of FtraceConfig's we need to find the best union of all
62 // the settings to make everyone happy while also watching out for anybody
63 // messing with the ftrace settings at the same time as us.
64 //
65 // Specifically FtraceConfigMuxer takes in a *requested* FtraceConfig
66 // (|SetupConfig|), makes a best effort attempt to modify the ftrace
67 // debugfs files to honor those settings without interrupting other perfetto
68 // traces already in progress or other users of ftrace, then returns an
69 // FtraceConfigId representing that config or zero on failure.
70 //
71 // When you are finished with a config you can signal that with |RemoveConfig|.
72 class FtraceConfigMuxer {
73  public:
74   // The FtraceConfigMuxer and ProtoTranslationTable
75   // should outlive this instance.
76   FtraceConfigMuxer(
77       FtraceProcfs* ftrace,
78       ProtoTranslationTable* table,
79       std::map<std::string, std::vector<GroupAndName>> vendor_events);
80   virtual ~FtraceConfigMuxer();
81 
82   // Ask FtraceConfigMuxer to adjust ftrace procfs settings to
83   // match the requested config. Returns an id to manage this
84   // config or zero on failure.
85   // This is best effort. FtraceConfigMuxer may not be able to adjust the
86   // buffer size right now. Events may be missing or there may be extra events
87   // (if you enable an atrace category we try to give you the matching events).
88   // If someone else is tracing we won't touch atrace (since it resets the
89   // buffer).
90   FtraceConfigId SetupConfig(const FtraceConfig& request);
91 
92   // Activate ftrace for the given config (if not already active).
93   bool ActivateConfig(FtraceConfigId);
94 
95   // Undo changes for the given config. Returns false iff the id is 0
96   // or already removed.
97   bool RemoveConfig(FtraceConfigId);
98 
99   const FtraceDataSourceConfig* GetDataSourceConfig(FtraceConfigId id);
100 
101   // Returns the current per-cpu buffer size, as configured by this muxer
102   // (without consulting debugfs). Constant for a given tracing session.
103   // Note that if there are multiple concurrent tracing sessions, the first
104   // session's buffer size is used for all of them.
105   size_t GetPerCpuBufferSizePages();
106 
107   // public for testing
SetupClockForTesting(const FtraceConfig & request)108   void SetupClockForTesting(const FtraceConfig& request) {
109     SetupClock(request);
110   }
111 
GetFtraceEventsForTesting(const FtraceConfig & request,const ProtoTranslationTable * table)112   std::set<GroupAndName> GetFtraceEventsForTesting(
113       const FtraceConfig& request,
114       const ProtoTranslationTable* table) {
115     return GetFtraceEvents(request, table);
116   }
117 
GetCentralEventFilterForTesting()118   const EventFilter* GetCentralEventFilterForTesting() const {
119     return &current_state_.ftrace_events;
120   }
121 
122  private:
123   static bool StartAtrace(const std::vector<std::string>& apps,
124                           const std::vector<std::string>& categories);
125 
126   struct FtraceState {
127     EventFilter ftrace_events;
128     // Used only in Android for ATRACE_EVENT/os.Trace() userspace
129     std::vector<std::string> atrace_apps;
130     std::vector<std::string> atrace_categories;
131     size_t cpu_buffer_size_pages = 0;
132     bool atrace_on = false;
133   };
134 
135   FtraceConfigMuxer(const FtraceConfigMuxer&) = delete;
136   FtraceConfigMuxer& operator=(const FtraceConfigMuxer&) = delete;
137 
138   void SetupClock(const FtraceConfig& request);
139   void SetupBufferSize(const FtraceConfig& request);
140   void UpdateAtrace(const FtraceConfig& request);
141   void DisableAtrace();
142 
143   // This processes the config to get the exact events.
144   // group/* -> Will read the fs and add all events in group.
145   // event -> Will look up the event to find the group.
146   // atrace category -> Will add events in that category.
147   std::set<GroupAndName> GetFtraceEvents(const FtraceConfig& request,
148                                          const ProtoTranslationTable*);
149 
150   FtraceConfigId GetNextId();
151 
152   FtraceConfigId last_id_ = 1;
153   FtraceProcfs* ftrace_;
154   ProtoTranslationTable* table_;
155 
156   FtraceState current_state_;
157 
158   // Set of all requested tracing configurations, with the associated derived
159   // data used during parsing. Note that not all of these configurations might
160   // be active. When a config is present but not active, we do setup buffer
161   // sizes and events, but don't enable ftrace (i.e. tracing_on).
162   std::map<FtraceConfigId, FtraceDataSourceConfig> ds_configs_;
163 
164   std::map<std::string, std::vector<GroupAndName>> vendor_events_;
165 
166   // Subset of |ds_configs_| that are currently active. At any time ftrace is
167   // enabled iff |active_configs_| is not empty.
168   std::set<FtraceConfigId> active_configs_;
169 };
170 
171 size_t ComputeCpuBufferSizeInPages(size_t requested_buffer_size_kb);
172 
173 }  // namespace perfetto
174 
175 #endif  // SRC_TRACED_PROBES_FTRACE_FTRACE_CONFIG_MUXER_H_
176