Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(247)

Side by Side Diff: base/trace_event/trace_config.cc

Issue 1115023003: [Startup Tracing] The TraceConfig class [STALE] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 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 #include "base/trace_event/trace_config.h"
6
7 #include "base/json/json_reader.h"
8 #include "base/json/json_writer.h"
9 #include "base/strings/string_split.h"
10 #include "base/strings/string_tokenizer.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/trace_event/trace_event.h"
13
14 namespace base {
15 namespace trace_event {
16
17 namespace {
18
19 // String options that can be used to initialize TraceOptions.
20 const char kRecordUntilFull[] = "record-until-full";
21 const char kRecordContinuously[] = "record-continuously";
22 const char kRecordAsMuchAsPossible[] = "record-as-much-as-possible";
23 const char kTraceToConsole[] = "trace-to-console";
24 const char kEnableSampling[] = "enable-sampling";
25 const char kEnableSystrace[] = "enable-systrace";
26 const char kEnableArgumentFilter[] = "enable-argument-filter";
27
28 // String parameters that can be used to parse the trace config string.
29 const char kRecordModeParam[] = "record_mode";
30 const char kEnableSamplingParam[] = "enable_sampling";
31 const char kEnableSystraceParam[] = "enable_systrace";
32 const char kIncludedCategoriesParam[] = "included_categories";
33 const char kDisabledCategoriesParam[] = "disabled_categories";
34 const char kExcludedCategoriesParam[] = "excluded_categories";
35 const char kSyntheticDelaysParam[] = "synthetic_delays";
36
37 const char kSyntheticDelayCategoryFilterPrefix[] = "DELAY(";
38
39 } // namespace
40
41 bool TraceOptions::SetFromString(const std::string& options_string) {
42 record_mode = RECORD_UNTIL_FULL;
43 enable_sampling = false;
44 enable_systrace = false;
45
46 std::vector<std::string> split;
47 std::vector<std::string>::iterator iter;
48 base::SplitString(options_string, ',', &split);
49 for (iter = split.begin(); iter != split.end(); ++iter) {
50 if (*iter == kRecordUntilFull) {
51 record_mode = RECORD_UNTIL_FULL;
52 } else if (*iter == kRecordContinuously) {
53 record_mode = RECORD_CONTINUOUSLY;
54 } else if (*iter == kTraceToConsole) {
55 record_mode = ECHO_TO_CONSOLE;
56 } else if (*iter == kRecordAsMuchAsPossible) {
57 record_mode = RECORD_AS_MUCH_AS_POSSIBLE;
58 } else if (*iter == kEnableSampling) {
59 enable_sampling = true;
60 } else if (*iter == kEnableSystrace) {
61 enable_systrace = true;
62 } else if (*iter == kEnableArgumentFilter) {
63 enable_argument_filter = true;
64 } else {
65 return false;
66 }
67 }
68 return true;
69 }
70
71 std::string TraceOptions::ToString() const {
72 std::string ret;
73 switch (record_mode) {
74 case RECORD_UNTIL_FULL:
75 ret = kRecordUntilFull;
76 break;
77 case RECORD_CONTINUOUSLY:
78 ret = kRecordContinuously;
79 break;
80 case ECHO_TO_CONSOLE:
81 ret = kTraceToConsole;
82 break;
83 case RECORD_AS_MUCH_AS_POSSIBLE:
84 ret = kRecordAsMuchAsPossible;
85 break;
86 default:
87 NOTREACHED();
88 }
89 if (enable_sampling)
90 ret = ret + "," + kEnableSampling;
91 if (enable_systrace)
92 ret = ret + "," + kEnableSystrace;
93 if (enable_argument_filter)
94 ret = ret + "," + kEnableArgumentFilter;
95 return ret;
96 }
97
98 TraceConfig::TraceConfig() {
99 InitializeDefault();
100 }
101
102 TraceConfig::TraceConfig(const CategoryFilter& cf,
103 const TraceOptions& options) {
104 record_mode_ = options.record_mode;
105 enable_sampling_ = options.enable_sampling;
106 enable_systrace_ = options.enable_systrace;
107
108 included_categories_ = cf.config_.included_categories_;
109 disabled_categories_ = cf.config_.disabled_categories_;
110 excluded_categories_ = cf.config_.excluded_categories_;
111 synthetic_delays_ = cf.config_.synthetic_delays_;
112 }
113
114 TraceConfig::TraceConfig(const std::string& category_filter_string,
115 const std::string& trace_options_string) {
116 if (!category_filter_string.empty()) {
117 std::vector<std::string> split;
118 std::vector<std::string>::iterator iter;
119 base::SplitString(category_filter_string, ',', &split);
120 for (iter = split.begin(); iter != split.end(); ++iter) {
121 std::string category = *iter;
122 // Ignore empty categories.
123 if (category.empty())
124 continue;
125 // Synthetic delays are of the form 'DELAY(delay;option;option;...)'.
126 if (category.find(kSyntheticDelayCategoryFilterPrefix) == 0 &&
127 category.at(category.size() - 1) == ')') {
128 category = category.substr(
129 strlen(kSyntheticDelayCategoryFilterPrefix),
130 category.size() - strlen(kSyntheticDelayCategoryFilterPrefix) - 1);
131 size_t name_length = category.find(';');
132 if (name_length != std::string::npos && name_length > 0 &&
133 name_length != category.size() - 1) {
134 synthetic_delays_.push_back(category);
135 }
136 } else if (category.at(0) == '-') {
137 // Excluded categories start with '-'.
138 // Remove '-' from category string.
139 category = category.substr(1);
140 excluded_categories_.push_back(category);
141 } else if (category.compare(0, strlen(TRACE_DISABLED_BY_DEFAULT("")),
142 TRACE_DISABLED_BY_DEFAULT("")) == 0) {
143 disabled_categories_.push_back(category);
144 } else {
145 included_categories_.push_back(category);
146 }
147 }
148 }
149
150 record_mode_ = RECORD_UNTIL_FULL;
151 enable_sampling_ = false;
152 enable_systrace_ = false;
153 if(!trace_options_string.empty()) {
154 std::vector<std::string> split;
155 std::vector<std::string>::iterator iter;
156 base::SplitString(trace_options_string, ',', &split);
157 for (iter = split.begin(); iter != split.end(); ++iter) {
158 if (*iter == kRecordUntilFull) {
159 record_mode_ = RECORD_UNTIL_FULL;
160 } else if (*iter == kRecordContinuously) {
161 record_mode_ = RECORD_CONTINUOUSLY;
162 } else if (*iter == kTraceToConsole) {
163 record_mode_ = ECHO_TO_CONSOLE;
164 } else if (*iter == kRecordAsMuchAsPossible) {
165 record_mode_ = RECORD_AS_MUCH_AS_POSSIBLE;
166 } else if (*iter == kEnableSampling) {
167 enable_sampling_ = true;
168 } else if (*iter == kEnableSystrace) {
169 enable_systrace_ = true;
170 }
171 }
172 }
173 }
174
175 TraceConfig::TraceConfig(const std::string& config_string) {
176 if (!config_string.empty())
177 Initialize(config_string);
178 else
179 InitializeDefault();
180 }
181
182 TraceConfig::TraceConfig(const TraceConfig& tc)
183 : record_mode_(tc.record_mode_),
184 enable_sampling_(tc.enable_sampling_),
185 enable_systrace_(tc.enable_systrace_),
186 included_categories_(tc.included_categories_),
187 disabled_categories_(tc.disabled_categories_),
188 excluded_categories_(tc.excluded_categories_),
189 synthetic_delays_(tc.synthetic_delays_) {
190 }
191
192 TraceConfig::~TraceConfig() {
193 }
194
195 TraceConfig& TraceConfig::operator=(const TraceConfig& rhs) {
196 if (this == &rhs)
197 return *this;
198
199 record_mode_ = rhs.record_mode_;
200 enable_sampling_ = rhs.enable_sampling_;
201 enable_systrace_ = rhs.enable_systrace_;
202 included_categories_ = rhs.included_categories_;
203 disabled_categories_ = rhs.disabled_categories_;
204 excluded_categories_ = rhs.excluded_categories_;
205 synthetic_delays_ = rhs.synthetic_delays_;
206 return *this;
207 }
208
209 void TraceConfig::Initialize(const std::string& config_string) {
210 scoped_ptr<base::Value> value(base::JSONReader::Read(config_string));
211 if (!value || !value->IsType(base::Value::TYPE_DICTIONARY)) {
212 InitializeDefault();
213 return;
214 }
215 scoped_ptr<base::DictionaryValue> dict(
216 static_cast<base::DictionaryValue*>(value.release()));
217
218 record_mode_ = RECORD_UNTIL_FULL;
219 std::string record_mode;
220 if (dict->GetString(kRecordModeParam, &record_mode)) {
221 if (record_mode == kRecordUntilFull) {
222 record_mode_ = RECORD_UNTIL_FULL;
223 } else if (record_mode == kRecordContinuously) {
224 record_mode_ = RECORD_CONTINUOUSLY;
225 } else if (record_mode == kTraceToConsole) {
226 record_mode_ = ECHO_TO_CONSOLE;
227 } else if (record_mode == kRecordAsMuchAsPossible) {
228 record_mode_ = RECORD_AS_MUCH_AS_POSSIBLE;
229 }
230 }
231
232 bool enable_sampling;
233 if (!dict->GetBoolean(kEnableSamplingParam, &enable_sampling))
234 enable_sampling_ = false;
235 else
236 enable_sampling_ = enable_sampling;
237
238 bool enable_systrace;
239 if (!dict->GetBoolean(kEnableSystraceParam, &enable_systrace))
240 enable_systrace_ = false;
241 else
242 enable_systrace_ = enable_systrace;
243
244 base::ListValue* category_list = NULL;
245 if (dict->GetList(kIncludedCategoriesParam, &category_list))
246 SetCategoriesFromList(included_categories_, *category_list);
247 if (dict->GetList(kDisabledCategoriesParam, &category_list))
248 SetCategoriesFromList(disabled_categories_, *category_list);
249 if (dict->GetList(kExcludedCategoriesParam, &category_list))
250 SetCategoriesFromList(excluded_categories_, *category_list);
251 if (dict->GetList(kSyntheticDelaysParam, &category_list))
252 SetSyntheticDelaysFromList(synthetic_delays_, *category_list);
253 }
254
255 void TraceConfig::InitializeDefault() {
256 record_mode_ = RECORD_UNTIL_FULL;
257 enable_sampling_ = false;
258 enable_systrace_ = false;
259 excluded_categories_.push_back("*Debug");
260 excluded_categories_.push_back("*Test");
261 }
262
263 const TraceConfig::StringList& TraceConfig::GetSyntheticDelayValues() const {
264 return synthetic_delays_;
265 }
266
267 void TraceConfig::ToDict(base::DictionaryValue& dict) const {
268 switch (record_mode_) {
269 case RECORD_UNTIL_FULL:
270 dict.SetString(kRecordModeParam, kRecordUntilFull);
271 break;
272 case RECORD_CONTINUOUSLY:
273 dict.SetString(kRecordModeParam, kRecordContinuously);
274 break;
275 case ECHO_TO_CONSOLE:
276 dict.SetString(kRecordModeParam, kTraceToConsole);
277 break;
278 case RECORD_AS_MUCH_AS_POSSIBLE:
279 dict.SetString(kRecordModeParam, kRecordAsMuchAsPossible);
280 break;
281 default:
282 NOTREACHED();
283 }
284
285 if (enable_sampling_)
286 dict.SetBoolean(kEnableSamplingParam, true);
287 else
288 dict.SetBoolean(kEnableSamplingParam, false);
289
290 if (enable_systrace_)
291 dict.SetBoolean(kEnableSystraceParam, true);
292 else
293 dict.SetBoolean(kEnableSystraceParam, false);
294
295 AddCategoryToDict(dict, kIncludedCategoriesParam, included_categories_);
296 AddCategoryToDict(dict, kDisabledCategoriesParam, disabled_categories_);
297 AddCategoryToDict(dict, kExcludedCategoriesParam, excluded_categories_);
298 AddCategoryToDict(dict, kSyntheticDelaysParam, synthetic_delays_);
299 }
300
301 std::string TraceConfig::ToString() const {
302 base::DictionaryValue dict;
303 ToDict(dict);
304
305 std::string json;
306 base::JSONWriter::Write(dict, &json);
307
308 return json;
309 }
310
311 std::string TraceConfig::ToCategoryFilterString() const {
312 CategoryFilter cf;
313 cf.config_ = *this;
314 return cf.ToString();
315 }
316
317 std::string TraceConfig::ToTraceOptionsString() const {
318 TraceOptions to;
319 to.record_mode = record_mode_;
320 to.enable_sampling = enable_sampling_;
321 to.enable_systrace = enable_systrace_;
322 return to.ToString();
323 }
324
325 void TraceConfig::AddCategoryToDict(base::DictionaryValue& dict,
326 const char* param,
327 const StringList& categories) const {
328 if (categories.empty())
329 return;
330
331 scoped_ptr<base::ListValue> list(new base::ListValue());
332 for (StringList::const_iterator ci = categories.begin();
333 ci != categories.end();
334 ++ci) {
335 list->AppendString(*ci);
336 }
337
338 dict.Set(param, list.Pass());
339 }
340
341 void TraceConfig::Merge(const TraceConfig& config) {
342 if (record_mode_ != config.record_mode_
343 || enable_sampling_ != config.enable_sampling_
344 || enable_systrace_ != config.enable_systrace_) {
345 DLOG(ERROR) << "Attempting to merge trace config with a different "
346 << "set of options.";
347 }
348
349 // Keep included patterns only if both filters have an included entry.
350 // Otherwise, one of the filter was specifying "*" and we want to honor the
351 // broadest filter.
352 if (HasIncludedPatterns() && config.HasIncludedPatterns()) {
353 included_categories_.insert(included_categories_.end(),
354 config.included_categories_.begin(),
355 config.included_categories_.end());
356 } else {
357 included_categories_.clear();
358 }
359
360 disabled_categories_.insert(disabled_categories_.end(),
361 config.disabled_categories_.begin(),
362 config.disabled_categories_.end());
363 excluded_categories_.insert(excluded_categories_.end(),
364 config.excluded_categories_.begin(),
365 config.excluded_categories_.end());
366 synthetic_delays_.insert(synthetic_delays_.end(),
367 config.synthetic_delays_.begin(),
368 config.synthetic_delays_.end());
369 }
370
371 void TraceConfig::Clear() {
372 record_mode_ = RECORD_UNTIL_FULL;
373 enable_sampling_ = false;
374 enable_systrace_ = false;
375 included_categories_.clear();
376 disabled_categories_.clear();
377 excluded_categories_.clear();
378 synthetic_delays_.clear();
379 }
380
381 void TraceConfig::SetCategoriesFromList(StringList& categories,
382 const base::ListValue& list) {
383 categories.clear();
384 for (size_t i = 0; i < list.GetSize(); ++i) {
385 std::string category;
386 if (list.GetString(i, &category))
387 categories.push_back(category);
388 }
389 }
390
391 void TraceConfig::SetSyntheticDelaysFromList(StringList& delays,
392 const base::ListValue& list) {
393 delays.clear();
394 for (size_t i = 0; i < list.GetSize(); ++i) {
395 std::string delay;
396 if (!list.GetString(i, &delay))
397 continue;
398 // Synthetic delays are of the form "delay;option;option;...".
399 size_t name_length = delay.find(';');
400 if (name_length != std::string::npos && name_length > 0 &&
401 name_length != delay.size() - 1) {
402 delays.push_back(delay);
403 }
404 }
405 }
406
407 bool TraceConfig::IsCategoryGroupEnabled(
408 const char* category_group_name) const {
409 // TraceLog should call this method only as part of enabling/disabling
410 // categories.
411
412 bool had_enabled_by_default = false;
413 DCHECK(category_group_name);
414 CStringTokenizer category_group_tokens(
415 category_group_name, category_group_name + strlen(category_group_name),
416 ",");
417 while (category_group_tokens.GetNext()) {
418 std::string category_group_token = category_group_tokens.token();
419 // Don't allow empty tokens, nor tokens with leading or trailing space.
420 DCHECK(!TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
421 category_group_token))
422 << "Disallowed category string";
423 if (IsCategoryEnabled(category_group_token.c_str())) {
424 return true;
425 }
426 if (!MatchPattern(category_group_token.c_str(),
427 TRACE_DISABLED_BY_DEFAULT("*")))
428 had_enabled_by_default = true;
429 }
430 // Do a second pass to check for explicitly disabled categories
431 // (those explicitly enabled have priority due to first pass).
432 category_group_tokens.Reset();
433 bool category_group_disabled = false;
434 while (category_group_tokens.GetNext()) {
435 std::string category_group_token = category_group_tokens.token();
436 for (StringList::const_iterator ci = excluded_categories_.begin();
437 ci != excluded_categories_.end();
438 ++ci) {
439 if (MatchPattern(category_group_token.c_str(), ci->c_str())) {
440 // Current token of category_group_name is present in excluded_list.
441 // Flag the exclusion and proceed further to check if any of the
442 // remaining categories of category_group_name is not present in the
443 // excluded_ list.
444 category_group_disabled = true;
445 break;
446 }
447 // One of the category of category_group_name is not present in
448 // excluded_ list. So, it has to be included_ list. Enable the
449 // category_group_name for recording.
450 category_group_disabled = false;
451 }
452 // One of the categories present in category_group_name is not present in
453 // excluded_ list. Implies this category_group_name group can be enabled
454 // for recording, since one of its groups is enabled for recording.
455 if (!category_group_disabled)
456 break;
457 }
458 // If the category group is not excluded, and there are no included patterns
459 // we consider this category group enabled, as long as it had categories
460 // other than disabled-by-default.
461 return !category_group_disabled &&
462 included_categories_.empty() && had_enabled_by_default;
463 }
464
465 bool TraceConfig::IsCategoryEnabled(const char* category_name) const {
466 StringList::const_iterator ci;
467
468 // Check the disabled- filters and the disabled-* wildcard first so that a
469 // "*" filter does not include the disabled.
470 for (ci = disabled_categories_.begin();
471 ci != disabled_categories_.end();
472 ++ci) {
473 if (MatchPattern(category_name, ci->c_str()))
474 return true;
475 }
476
477 if (MatchPattern(category_name, TRACE_DISABLED_BY_DEFAULT("*")))
478 return false;
479
480 for (ci = included_categories_.begin();
481 ci != included_categories_.end();
482 ++ci) {
483 if (MatchPattern(category_name, ci->c_str()))
484 return true;
485 }
486
487 return false;
488 }
489
490 bool TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(
491 const std::string& str) {
492 return str.empty() ||
493 str.at(0) == ' ' ||
494 str.at(str.length() - 1) == ' ';
495 }
496
497 bool TraceConfig::HasIncludedPatterns() const {
498 return !included_categories_.empty();
499 }
500
501 bool CategoryFilter::IsEmptyOrContainsLeadingOrTrailingWhitespace(
502 const std::string& str) {
503 return TraceConfig::IsEmptyOrContainsLeadingOrTrailingWhitespace(str);
504 }
505
506 CategoryFilter::CategoryFilter(const std::string& filter_string)
507 : config_(TraceConfig(filter_string, "")) {
508 }
509
510 CategoryFilter::CategoryFilter()
511 : config_(TraceConfig()) {
512 }
513
514 CategoryFilter::CategoryFilter(const CategoryFilter& cf)
515 : config_(cf.config_) {
516 }
517
518 CategoryFilter::~CategoryFilter() {
519 }
520
521 CategoryFilter& CategoryFilter::operator=(const CategoryFilter& rhs) {
522 if (this == &rhs)
523 return *this;
524
525 config_ = rhs.config_;
526 return *this;
527 }
528
529 void CategoryFilter::WriteString(const StringList& values,
530 std::string* out,
531 bool included) const {
532 bool prepend_comma = !out->empty();
533 int token_cnt = 0;
534 for (StringList::const_iterator ci = values.begin();
535 ci != values.end(); ++ci) {
536 if (token_cnt > 0 || prepend_comma)
537 StringAppendF(out, ",");
538 StringAppendF(out, "%s%s", (included ? "" : "-"), ci->c_str());
539 ++token_cnt;
540 }
541 }
542
543 void CategoryFilter::WriteString(const StringList& delays,
544 std::string* out) const {
545 bool prepend_comma = !out->empty();
546 int token_cnt = 0;
547 for (StringList::const_iterator ci = delays.begin();
548 ci != delays.end(); ++ci) {
549 if (token_cnt > 0 || prepend_comma)
550 StringAppendF(out, ",");
551 StringAppendF(out, "%s%s)", kSyntheticDelayCategoryFilterPrefix,
552 ci->c_str());
553 ++token_cnt;
554 }
555 }
556
557 std::string CategoryFilter::ToString() const {
558 std::string filter_string;
559 WriteString(config_.included_categories_, &filter_string, true);
560 WriteString(config_.disabled_categories_, &filter_string, true);
561 WriteString(config_.excluded_categories_, &filter_string, false);
562 WriteString(config_.synthetic_delays_, &filter_string);
563 return filter_string;
564 }
565
566 bool CategoryFilter::IsCategoryGroupEnabled(
567 const char* category_group_name) const {
568 return config_.IsCategoryGroupEnabled(category_group_name);
569 }
570
571 bool CategoryFilter::IsCategoryEnabled(const char* category_name) const {
572 return config_.IsCategoryEnabled(category_name);
573 }
574
575 bool CategoryFilter::HasIncludedPatterns() const {
576 return config_.HasIncludedPatterns();
577 }
578
579 void CategoryFilter::Merge(const CategoryFilter& nested_filter) {
580 config_.Merge(nested_filter.config_);
581 }
582
583 void CategoryFilter::Clear() {
584 config_.included_categories_.clear();
585 config_.disabled_categories_.clear();
586 config_.excluded_categories_.clear();
587 }
588
589 const CategoryFilter::StringList&
590 CategoryFilter::GetSyntheticDelayValues() const {
591 return config_.GetSyntheticDelayValues();
592 }
593
594 } // namespace trace_event
595 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698