xref: /aosp_15_r20/external/cronet/base/trace_event/trace_config.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_TRACE_EVENT_TRACE_CONFIG_H_
6 #define BASE_TRACE_EVENT_TRACE_CONFIG_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <set>
12 #include <string>
13 #include <string_view>
14 #include <unordered_set>
15 #include <vector>
16 
17 #include "base/base_export.h"
18 #include "base/gtest_prod_util.h"
19 #include "base/trace_event/memory_dump_request_args.h"
20 #include "base/trace_event/trace_config_category_filter.h"
21 #include "base/values.h"
22 
23 namespace base::trace_event {
24 
25 class ConvertableToTraceFormat;
26 
27 // Options determines how the trace buffer stores data.
28 // A Java counterpart will be generated for this enum.
29 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.base
30 enum TraceRecordMode {
31   // Record until the trace buffer is full.
32   RECORD_UNTIL_FULL,
33 
34   // Record until the user ends the trace. The trace buffer is a fixed size
35   // and we use it as a ring buffer during recording.
36   RECORD_CONTINUOUSLY,
37 
38   // Record until the trace buffer is full, but with a huge buffer size.
39   RECORD_AS_MUCH_AS_POSSIBLE,
40 
41   // Echo to console. Events are discarded.
42   ECHO_TO_CONSOLE,
43 };
44 
45 class BASE_EXPORT TraceConfig {
46  public:
47   using StringList = std::vector<std::string>;
48 
49   // Specifies the memory dump config for tracing.
50   // Used only when "memory-infra" category is enabled.
51   struct BASE_EXPORT MemoryDumpConfig {
52     MemoryDumpConfig();
53     MemoryDumpConfig(const MemoryDumpConfig& other);
54     ~MemoryDumpConfig();
55 
56     // Specifies the triggers in the memory dump config.
57     struct Trigger {
58       friend bool operator==(const Trigger&, const Trigger&) = default;
59 
60       uint32_t min_time_between_dumps_ms;
61       MemoryDumpLevelOfDetail level_of_detail;
62       MemoryDumpType trigger_type;
63     };
64 
65     // Specifies the configuration options for the heap profiler.
66     struct HeapProfiler {
67       // Default value for |breakdown_threshold_bytes|.
68       enum { kDefaultBreakdownThresholdBytes = 1024 };
69 
70       HeapProfiler();
71 
72       // Reset the options to default.
73       void Clear();
74 
75       friend bool operator==(const HeapProfiler&,
76                              const HeapProfiler&) = default;
77 
78       uint32_t breakdown_threshold_bytes;
79     };
80 
81     friend bool operator==(const MemoryDumpConfig&,
82                            const MemoryDumpConfig&) = default;
83 
84     // Reset the values in the config.
85     void Clear();
86 
87     void Merge(const MemoryDumpConfig& config);
88 
89     // Set of memory dump modes allowed for the tracing session. The explicitly
90     // triggered dumps will be successful only if the dump mode is allowed in
91     // the config.
92     std::set<MemoryDumpLevelOfDetail> allowed_dump_modes;
93 
94     std::vector<Trigger> triggers;
95     HeapProfiler heap_profiler_options;
96   };
97 
98   class BASE_EXPORT ProcessFilterConfig {
99    public:
100     ProcessFilterConfig();
101     explicit ProcessFilterConfig(
102         const std::unordered_set<base::ProcessId>& included_process_ids);
103     ProcessFilterConfig(const ProcessFilterConfig&);
104     ~ProcessFilterConfig();
105 
empty()106     bool empty() const { return included_process_ids_.empty(); }
107 
108     void Clear();
109     void Merge(const ProcessFilterConfig&);
110 
111     void InitializeFromConfigDict(const Value::Dict&);
112     void ToDict(Value::Dict& dict) const;
113 
114     bool IsEnabled(base::ProcessId) const;
included_process_ids()115     const std::unordered_set<base::ProcessId>& included_process_ids() const {
116       return included_process_ids_;
117     }
118 
119     friend bool operator==(const ProcessFilterConfig&,
120                            const ProcessFilterConfig&) = default;
121 
122    private:
123     std::unordered_set<base::ProcessId> included_process_ids_;
124   };
125 
126   class BASE_EXPORT EventFilterConfig {
127    public:
128     explicit EventFilterConfig(const std::string& predicate_name);
129     EventFilterConfig(const EventFilterConfig& tc);
130 
131     ~EventFilterConfig();
132 
133     EventFilterConfig& operator=(const EventFilterConfig& rhs);
134 
135     bool IsEquivalentTo(const EventFilterConfig& other) const;
136 
137     void InitializeFromConfigDict(const Value::Dict& event_filter);
138 
139     void SetCategoryFilter(const TraceConfigCategoryFilter& category_filter);
140 
141     void ToDict(Value::Dict& filter_dict) const;
142 
143     bool GetArgAsSet(const char* key, std::unordered_set<std::string>*) const;
144 
145     bool IsCategoryGroupEnabled(std::string_view category_group_name) const;
146 
predicate_name()147     const std::string& predicate_name() const { return predicate_name_; }
filter_args()148     const Value::Dict& filter_args() const { return args_; }
category_filter()149     const TraceConfigCategoryFilter& category_filter() const {
150       return category_filter_;
151     }
152 
153    private:
154     std::string predicate_name_;
155     TraceConfigCategoryFilter category_filter_;
156     Value::Dict args_;
157   };
158   typedef std::vector<EventFilterConfig> EventFilters;
159 
160   static std::string TraceRecordModeToStr(TraceRecordMode record_mode);
161 
162   TraceConfig();
163 
164   // Create TraceConfig object from category filter and trace options strings.
165   //
166   // |category_filter_string| is a comma-delimited list of category wildcards.
167   // A category can have an optional '-' prefix to make it an excluded category.
168   // All the same rules apply above, so for example, having both included and
169   // excluded categories in the same list would not be supported.
170   //
171   // |trace_options_string| is a comma-delimited list of trace options.
172   // Possible options are: "record-until-full", "record-continuously",
173   // "record-as-much-as-possible", "trace-to-console", "enable-systrace" and
174   // "enable-argument-filter".
175   // The first 4 options are trace recoding modes and hence
176   // mutually exclusive. If more than one trace recording modes appear in the
177   // options_string, the last one takes precedence. If none of the trace
178   // recording mode is specified, recording mode is RECORD_UNTIL_FULL.
179   //
180   // The trace option will first be reset to the default option
181   // (record_mode set to RECORD_UNTIL_FULL, enable_systrace and
182   // enable_argument_filter set to false) before options parsed from
183   // |trace_options_string| are applied on it. If |trace_options_string| is
184   // invalid, the final state of trace options is undefined.
185   //
186   // Example: TraceConfig("test_MyTest*", "record-until-full");
187   // Example: TraceConfig("test_MyTest*,test_OtherStuff",
188   //                      "record-continuously");
189   // Example: TraceConfig("-excluded_category1,-excluded_category2",
190   //                      "record-until-full, trace-to-console");
191   //          would set ECHO_TO_CONSOLE as the recording mode.
192   // Example: TraceConfig("-*,webkit", "");
193   //          would disable everything but webkit; and use default options.
194   // Example: TraceConfig("-webkit", "");
195   //          would enable everything but webkit; and use default options.
196   TraceConfig(std::string_view category_filter_string,
197               std::string_view trace_options_string);
198 
199   TraceConfig(std::string_view category_filter_string,
200               TraceRecordMode record_mode);
201 
202   // Create TraceConfig object from the trace config string.
203   //
204   // |config_string| is a dictionary formatted as a JSON string, containing both
205   // category filters and trace options.
206   //
207   // Example:
208   //   {
209   //     "record_mode": "record-continuously",
210   //     "enable_systrace": true,
211   //     "enable_argument_filter": true,
212   //     "included_categories": ["included",
213   //                             "inc_pattern*",
214   //                             "disabled-by-default-memory-infra"],
215   //     "excluded_categories": ["excluded", "exc_pattern*"],
216   //     "memory_dump_config": {
217   //       "triggers": [
218   //         {
219   //           "mode": "detailed",
220   //           "periodic_interval_ms": 2000
221   //         }
222   //       ]
223   //     }
224   //   }
225   //
226   // Note: memory_dump_config can be specified only if
227   // disabled-by-default-memory-infra category is enabled.
228   explicit TraceConfig(std::string_view config_string);
229 
230   // Functionally identical to the above, but takes a parsed dictionary as input
231   // instead of its JSON serialization.
232   explicit TraceConfig(const Value::Dict& config);
233 
234   TraceConfig(const TraceConfig& tc);
235 
236   ~TraceConfig();
237 
238   TraceConfig& operator=(const TraceConfig& rhs);
239 
240   bool IsEquivalentTo(const TraceConfig& other) const;
241 
GetTraceRecordMode()242   TraceRecordMode GetTraceRecordMode() const { return record_mode_; }
GetTraceBufferSizeInEvents()243   size_t GetTraceBufferSizeInEvents() const {
244     return trace_buffer_size_in_events_;
245   }
GetTraceBufferSizeInKb()246   size_t GetTraceBufferSizeInKb() const { return trace_buffer_size_in_kb_; }
IsSystraceEnabled()247   bool IsSystraceEnabled() const { return enable_systrace_; }
IsArgumentFilterEnabled()248   bool IsArgumentFilterEnabled() const { return enable_argument_filter_; }
249 
SetTraceRecordMode(TraceRecordMode mode)250   void SetTraceRecordMode(TraceRecordMode mode) { record_mode_ = mode; }
SetTraceBufferSizeInEvents(size_t size)251   void SetTraceBufferSizeInEvents(size_t size) {
252     trace_buffer_size_in_events_ = size;
253   }
SetTraceBufferSizeInKb(size_t size)254   void SetTraceBufferSizeInKb(size_t size) { trace_buffer_size_in_kb_ = size; }
EnableSystrace()255   void EnableSystrace() { enable_systrace_ = true; }
256   void EnableSystraceEvent(const std::string& systrace_event);
EnableArgumentFilter()257   void EnableArgumentFilter() { enable_argument_filter_ = true; }
258   void EnableHistogram(const std::string& histogram_name);
259 
260   // Writes the string representation of the TraceConfig. The string is JSON
261   // formatted.
262   std::string ToString() const;
263 
264   // Returns a copy of the TraceConfig wrapped in a ConvertableToTraceFormat
265   std::unique_ptr<ConvertableToTraceFormat> AsConvertableToTraceFormat() const;
266 
267   // Write the string representation of the CategoryFilter part.
268   std::string ToCategoryFilterString() const;
269 
270   // Write the string representation of the trace options part (record mode,
271   // systrace, argument filtering). Does not include category filters, event
272   // filters, or memory dump configs.
273   std::string ToTraceOptionsString() const;
274 
275 #if BUILDFLAG(USE_PERFETTO_CLIENT_LIBRARY)
276   // Write the serialized perfetto::TrackEventConfig corresponding to this
277   // TraceConfig.
278   std::string ToPerfettoTrackEventConfigRaw(
279       bool privacy_filtering_enabled) const;
280 #endif  // BUILDFLAG(USE_PERFETTO_CLIENT_LIBRARY)
281 
282   // Returns true if at least one category in the list is enabled by this
283   // trace config. This is used to determine if the category filters are
284   // enabled in the TRACE_* macros.
285   bool IsCategoryGroupEnabled(std::string_view category_group_name) const;
286 
287   // Merges config with the current TraceConfig
288   void Merge(const TraceConfig& config);
289 
290   void Clear();
291 
292   // Clears and resets the memory dump config.
293   void ResetMemoryDumpConfig(const MemoryDumpConfig& memory_dump_config);
294 
category_filter()295   const TraceConfigCategoryFilter& category_filter() const {
296     return category_filter_;
297   }
298 
memory_dump_config()299   const MemoryDumpConfig& memory_dump_config() const {
300     return memory_dump_config_;
301   }
302 
process_filter_config()303   const ProcessFilterConfig& process_filter_config() const {
304     return process_filter_config_;
305   }
306   void SetProcessFilterConfig(const ProcessFilterConfig&);
307 
event_filters()308   const EventFilters& event_filters() const { return event_filters_; }
SetEventFilters(const EventFilters & filter_configs)309   void SetEventFilters(const EventFilters& filter_configs) {
310     event_filters_ = filter_configs;
311   }
312 
313   // Returns true if event names should not contain package names.
IsEventPackageNameFilterEnabled()314   bool IsEventPackageNameFilterEnabled() const {
315     return enable_event_package_name_filter_;
316   }
317 
318   // If `enabled` is true, event names will not contain package names.
SetEventPackageNameFilterEnabled(bool enabled)319   void SetEventPackageNameFilterEnabled(bool enabled) {
320     enable_event_package_name_filter_ = enabled;
321   }
322 
systrace_events()323   const std::unordered_set<std::string>& systrace_events() const {
324     return systrace_events_;
325   }
326 
histogram_names()327   const std::unordered_set<std::string>& histogram_names() const {
328     return histogram_names_;
329   }
330 
331  private:
332   FRIEND_TEST_ALL_PREFIXES(TraceConfigTest, TraceConfigFromValidLegacyFormat);
333   FRIEND_TEST_ALL_PREFIXES(TraceConfigTest,
334                            TraceConfigFromInvalidLegacyStrings);
335   FRIEND_TEST_ALL_PREFIXES(TraceConfigTest, SystraceEventsSerialization);
336 
337   // The default trace config, used when none is provided.
338   // Allows all non-disabled-by-default categories through, except if they end
339   // in the suffix 'Debug' or 'Test'.
340   void InitializeDefault();
341 
342   // Initialize from a config dictionary.
343   void InitializeFromConfigDict(const Value::Dict& dict);
344 
345   // Initialize from a config string.
346   void InitializeFromConfigString(std::string_view config_string);
347 
348   // Initialize from category filter and trace options strings
349   void InitializeFromStrings(std::string_view category_filter_string,
350                              std::string_view trace_options_string);
351 
352   void SetMemoryDumpConfigFromConfigDict(const Value::Dict& memory_dump_config);
353   void SetDefaultMemoryDumpConfig();
354 
355   void SetHistogramNamesFromConfigList(const Value::List& histogram_names);
356   void SetEventFiltersFromConfigList(const Value::List& event_filters);
357   Value ToValue() const;
358 
359   TraceRecordMode record_mode_;
360   size_t trace_buffer_size_in_events_ = 0;  // 0 specifies default size
361   size_t trace_buffer_size_in_kb_ = 0;      // 0 specifies default size
362   bool enable_systrace_ : 1;
363   bool enable_argument_filter_ : 1;
364 
365   TraceConfigCategoryFilter category_filter_;
366 
367   MemoryDumpConfig memory_dump_config_;
368   ProcessFilterConfig process_filter_config_;
369 
370   EventFilters event_filters_;
371   bool enable_event_package_name_filter_ : 1;
372   std::unordered_set<std::string> histogram_names_;
373   std::unordered_set<std::string> systrace_events_;
374 };
375 
376 }  // namespace base::trace_event
377 
378 #endif  // BASE_TRACE_EVENT_TRACE_CONFIG_H_
379