• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors. All rights reserved.
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 <unordered_set>
14 #include <vector>
15 
16 #include "base/base_export.h"
17 #include "base/gtest_prod_util.h"
18 #include "base/strings/string_piece.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 {
24 namespace trace_event {
25 
26 class ConvertableToTraceFormat;
27 
28 // Options determines how the trace buffer stores data.
29 enum TraceRecordMode {
30   // Record until the trace buffer is full.
31   RECORD_UNTIL_FULL,
32 
33   // Record until the user ends the trace. The trace buffer is a fixed size
34   // and we use it as a ring buffer during recording.
35   RECORD_CONTINUOUSLY,
36 
37   // Record until the trace buffer is full, but with a huge buffer size.
38   RECORD_AS_MUCH_AS_POSSIBLE,
39 
40   // Echo to console. Events are discarded.
41   ECHO_TO_CONSOLE,
42 };
43 
44 class BASE_EXPORT TraceConfig {
45  public:
46   using StringList = std::vector<std::string>;
47 
48   // Specifies the memory dump config for tracing.
49   // Used only when "memory-infra" category is enabled.
50   struct BASE_EXPORT MemoryDumpConfig {
51     MemoryDumpConfig();
52     MemoryDumpConfig(const MemoryDumpConfig& other);
53     ~MemoryDumpConfig();
54 
55     // Specifies the triggers in the memory dump config.
56     struct Trigger {
57       uint32_t min_time_between_dumps_ms;
58       MemoryDumpLevelOfDetail level_of_detail;
59       MemoryDumpType trigger_type;
60     };
61 
62     // Specifies the configuration options for the heap profiler.
63     struct HeapProfiler {
64       // Default value for |breakdown_threshold_bytes|.
65       enum { kDefaultBreakdownThresholdBytes = 1024 };
66 
67       HeapProfiler();
68 
69       // Reset the options to default.
70       void Clear();
71 
72       uint32_t breakdown_threshold_bytes;
73     };
74 
75     // Reset the values in the config.
76     void Clear();
77 
78     void Merge(const MemoryDumpConfig& config);
79 
80     // Set of memory dump modes allowed for the tracing session. The explicitly
81     // triggered dumps will be successful only if the dump mode is allowed in
82     // the config.
83     std::set<MemoryDumpLevelOfDetail> allowed_dump_modes;
84 
85     std::vector<Trigger> triggers;
86     HeapProfiler heap_profiler_options;
87   };
88 
89   class BASE_EXPORT EventFilterConfig {
90    public:
91     EventFilterConfig(const std::string& predicate_name);
92     EventFilterConfig(const EventFilterConfig& tc);
93 
94     ~EventFilterConfig();
95 
96     EventFilterConfig& operator=(const EventFilterConfig& rhs);
97 
98     void InitializeFromConfigDict(const base::DictionaryValue* event_filter);
99 
100     void SetCategoryFilter(const TraceConfigCategoryFilter& category_filter);
101 
102     void ToDict(DictionaryValue* filter_dict) const;
103 
104     bool GetArgAsSet(const char* key, std::unordered_set<std::string>*) const;
105 
106     bool IsCategoryGroupEnabled(const StringPiece& category_group_name) const;
107 
predicate_name()108     const std::string& predicate_name() const { return predicate_name_; }
filter_args()109     base::DictionaryValue* filter_args() const { return args_.get(); }
category_filter()110     const TraceConfigCategoryFilter& category_filter() const {
111       return category_filter_;
112     }
113 
114    private:
115     std::string predicate_name_;
116     TraceConfigCategoryFilter category_filter_;
117     std::unique_ptr<base::DictionaryValue> args_;
118   };
119   typedef std::vector<EventFilterConfig> EventFilters;
120 
121   TraceConfig();
122 
123   // Create TraceConfig object from category filter and trace options strings.
124   //
125   // |category_filter_string| is a comma-delimited list of category wildcards.
126   // A category can have an optional '-' prefix to make it an excluded category.
127   // All the same rules apply above, so for example, having both included and
128   // excluded categories in the same list would not be supported.
129   //
130   // Category filters can also be used to configure synthetic delays.
131   //
132   // |trace_options_string| is a comma-delimited list of trace options.
133   // Possible options are: "record-until-full", "record-continuously",
134   // "record-as-much-as-possible", "trace-to-console", "enable-systrace" and
135   // "enable-argument-filter".
136   // The first 4 options are trace recoding modes and hence
137   // mutually exclusive. If more than one trace recording modes appear in the
138   // options_string, the last one takes precedence. If none of the trace
139   // recording mode is specified, recording mode is RECORD_UNTIL_FULL.
140   //
141   // The trace option will first be reset to the default option
142   // (record_mode set to RECORD_UNTIL_FULL, enable_systrace and
143   // enable_argument_filter set to false) before options parsed from
144   // |trace_options_string| are applied on it. If |trace_options_string| is
145   // invalid, the final state of trace options is undefined.
146   //
147   // Example: TraceConfig("test_MyTest*", "record-until-full");
148   // Example: TraceConfig("test_MyTest*,test_OtherStuff",
149   //                      "record-continuously");
150   // Example: TraceConfig("-excluded_category1,-excluded_category2",
151   //                      "record-until-full, trace-to-console");
152   //          would set ECHO_TO_CONSOLE as the recording mode.
153   // Example: TraceConfig("-*,webkit", "");
154   //          would disable everything but webkit; and use default options.
155   // Example: TraceConfig("-webkit", "");
156   //          would enable everything but webkit; and use default options.
157   // Example: TraceConfig("DELAY(gpu.PresentingFrame;16)", "");
158   //          would make swap buffers always take at least 16 ms; and use
159   //          default options.
160   // Example: TraceConfig("DELAY(gpu.PresentingFrame;16;oneshot)", "");
161   //          would make swap buffers take at least 16 ms the first time it is
162   //          called; and use default options.
163   // Example: TraceConfig("DELAY(gpu.PresentingFrame;16;alternating)", "");
164   //          would make swap buffers take at least 16 ms every other time it
165   //          is called; and use default options.
166   TraceConfig(StringPiece category_filter_string,
167               StringPiece trace_options_string);
168 
169   TraceConfig(StringPiece category_filter_string, TraceRecordMode record_mode);
170 
171   // Create TraceConfig object from the trace config string.
172   //
173   // |config_string| is a dictionary formatted as a JSON string, containing both
174   // category filters and trace options.
175   //
176   // Example:
177   //   {
178   //     "record_mode": "record-continuously",
179   //     "enable_systrace": true,
180   //     "enable_argument_filter": true,
181   //     "included_categories": ["included",
182   //                             "inc_pattern*",
183   //                             "disabled-by-default-memory-infra"],
184   //     "excluded_categories": ["excluded", "exc_pattern*"],
185   //     "synthetic_delays": ["test.Delay1;16", "test.Delay2;32"],
186   //     "memory_dump_config": {
187   //       "triggers": [
188   //         {
189   //           "mode": "detailed",
190   //           "periodic_interval_ms": 2000
191   //         }
192   //       ]
193   //     }
194   //   }
195   //
196   // Note: memory_dump_config can be specified only if
197   // disabled-by-default-memory-infra category is enabled.
198   explicit TraceConfig(StringPiece config_string);
199 
200   // Functionally identical to the above, but takes a parsed dictionary as input
201   // instead of its JSON serialization.
202   explicit TraceConfig(const DictionaryValue& config);
203 
204   TraceConfig(const TraceConfig& tc);
205 
206   ~TraceConfig();
207 
208   TraceConfig& operator=(const TraceConfig& rhs);
209 
210   // Return a list of the synthetic delays specified in this category filter.
211   const StringList& GetSyntheticDelayValues() const;
212 
GetTraceRecordMode()213   TraceRecordMode GetTraceRecordMode() const { return record_mode_; }
IsSystraceEnabled()214   bool IsSystraceEnabled() const { return enable_systrace_; }
IsArgumentFilterEnabled()215   bool IsArgumentFilterEnabled() const { return enable_argument_filter_; }
216 
SetTraceRecordMode(TraceRecordMode mode)217   void SetTraceRecordMode(TraceRecordMode mode) { record_mode_ = mode; }
EnableSystrace()218   void EnableSystrace() { enable_systrace_ = true; }
EnableArgumentFilter()219   void EnableArgumentFilter() { enable_argument_filter_ = true; }
220 
221   // Writes the string representation of the TraceConfig. The string is JSON
222   // formatted.
223   std::string ToString() const;
224 
225   // Returns a copy of the TraceConfig wrapped in a ConvertableToTraceFormat
226   std::unique_ptr<ConvertableToTraceFormat> AsConvertableToTraceFormat() const;
227 
228   // Write the string representation of the CategoryFilter part.
229   std::string ToCategoryFilterString() const;
230 
231   // Returns true if at least one category in the list is enabled by this
232   // trace config. This is used to determine if the category filters are
233   // enabled in the TRACE_* macros.
234   bool IsCategoryGroupEnabled(const StringPiece& category_group_name) const;
235 
236   // Merges config with the current TraceConfig
237   void Merge(const TraceConfig& config);
238 
239   void Clear();
240 
241   // Clears and resets the memory dump config.
242   void ResetMemoryDumpConfig(const MemoryDumpConfig& memory_dump_config);
243 
category_filter()244   const TraceConfigCategoryFilter& category_filter() const {
245     return category_filter_;
246   }
247 
memory_dump_config()248   const MemoryDumpConfig& memory_dump_config() const {
249     return memory_dump_config_;
250   }
251 
event_filters()252   const EventFilters& event_filters() const { return event_filters_; }
SetEventFilters(const EventFilters & filter_configs)253   void SetEventFilters(const EventFilters& filter_configs) {
254     event_filters_ = filter_configs;
255   }
256 
257  private:
258   FRIEND_TEST_ALL_PREFIXES(TraceConfigTest, TraceConfigFromValidLegacyFormat);
259   FRIEND_TEST_ALL_PREFIXES(TraceConfigTest,
260                            TraceConfigFromInvalidLegacyStrings);
261 
262   // The default trace config, used when none is provided.
263   // Allows all non-disabled-by-default categories through, except if they end
264   // in the suffix 'Debug' or 'Test'.
265   void InitializeDefault();
266 
267   // Initialize from a config dictionary.
268   void InitializeFromConfigDict(const DictionaryValue& dict);
269 
270   // Initialize from a config string.
271   void InitializeFromConfigString(StringPiece config_string);
272 
273   // Initialize from category filter and trace options strings
274   void InitializeFromStrings(StringPiece category_filter_string,
275                              StringPiece trace_options_string);
276 
277   void SetMemoryDumpConfigFromConfigDict(
278       const DictionaryValue& memory_dump_config);
279   void SetDefaultMemoryDumpConfig();
280 
281   void SetEventFiltersFromConfigList(const base::ListValue& event_filters);
282   std::unique_ptr<DictionaryValue> ToDict() const;
283 
284   std::string ToTraceOptionsString() const;
285 
286   TraceRecordMode record_mode_;
287   bool enable_systrace_ : 1;
288   bool enable_argument_filter_ : 1;
289 
290   TraceConfigCategoryFilter category_filter_;
291 
292   MemoryDumpConfig memory_dump_config_;
293 
294   EventFilters event_filters_;
295 };
296 
297 }  // namespace trace_event
298 }  // namespace base
299 
300 #endif  // BASE_TRACE_EVENT_TRACE_CONFIG_H_
301