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