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

Side by Side Diff: content/browser/tracing/background_tracing_rule.cc

Issue 1427873003: Background tracing: Added trace_chance (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 #include "content/browser/tracing/background_tracing_rule.h" 4 #include "content/browser/tracing/background_tracing_rule.h"
5 5
6 #include <string> 6 #include <string>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/metrics/histogram_macros.h" 9 #include "base/metrics/histogram_macros.h"
10 #include "base/metrics/statistics_recorder.h" 10 #include "base/metrics/statistics_recorder.h"
11 #include "base/rand_util.h" 11 #include "base/rand_util.h"
12 #include "base/strings/safe_sprintf.h" 12 #include "base/strings/safe_sprintf.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "components/tracing/tracing_messages.h" 14 #include "components/tracing/tracing_messages.h"
15 #include "content/browser/tracing/background_tracing_manager_impl.h" 15 #include "content/browser/tracing/background_tracing_manager_impl.h"
16 #include "content/browser/tracing/trace_message_filter.h" 16 #include "content/browser/tracing/trace_message_filter.h"
17 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
18 18
19 namespace { 19 namespace {
20 20
21 const char kConfigRuleKey[] = "rule"; 21 const char kConfigRuleKey[] = "rule";
22 const char kConfigCategoryKey[] = "category"; 22 const char kConfigCategoryKey[] = "category";
23 const char kConfigRuleTriggerNameKey[] = "trigger_name"; 23 const char kConfigRuleTriggerNameKey[] = "trigger_name";
24 const char kConfigRuleTriggerDelay[] = "trigger_delay"; 24 const char kConfigRuleTriggerDelay[] = "trigger_delay";
25 const char kConfigRuleTriggerChance[] = "trigger_chance";
shatch 2015/10/30 15:50:28 Add unit tests.
oystein (OOO til 10th of July) 2015/10/30 19:11:43 Done.
25 26
26 const char kConfigRuleHistogramNameKey[] = "histogram_name"; 27 const char kConfigRuleHistogramNameKey[] = "histogram_name";
27 const char kConfigRuleHistogramValueOldKey[] = "histogram_value"; 28 const char kConfigRuleHistogramValueOldKey[] = "histogram_value";
28 const char kConfigRuleHistogramValue1Key[] = "histogram_lower_value"; 29 const char kConfigRuleHistogramValue1Key[] = "histogram_lower_value";
29 const char kConfigRuleHistogramValue2Key[] = "histogram_upper_value"; 30 const char kConfigRuleHistogramValue2Key[] = "histogram_upper_value";
30 const char kConfigRuleHistogramRepeatKey[] = "histogram_repeat"; 31 const char kConfigRuleHistogramRepeatKey[] = "histogram_repeat";
31 32
32 const char kConfigRuleRandomIntervalTimeoutMin[] = "timeout_min"; 33 const char kConfigRuleRandomIntervalTimeoutMin[] = "timeout_min";
33 const char kConfigRuleRandomIntervalTimeoutMax[] = "timeout_max"; 34 const char kConfigRuleRandomIntervalTimeoutMax[] = "timeout_max";
34 35
(...skipping 13 matching lines...) Expand all
48 "ReactiveTraceAtRandomIntervals"; 49 "ReactiveTraceAtRandomIntervals";
49 50
50 const int kReactiveConfigNavigationTimeout = 30; 51 const int kReactiveConfigNavigationTimeout = 30;
51 const int kReactiveTraceRandomStartTimeMin = 60; 52 const int kReactiveTraceRandomStartTimeMin = 60;
52 const int kReactiveTraceRandomStartTimeMax = 120; 53 const int kReactiveTraceRandomStartTimeMax = 120;
53 54
54 } // namespace 55 } // namespace
55 56
56 namespace content { 57 namespace content {
57 58
58 BackgroundTracingRule::BackgroundTracingRule() {} 59 BackgroundTracingRule::BackgroundTracingRule() : trigger_chance_(1.0) {}
59 60
60 BackgroundTracingRule::~BackgroundTracingRule() {} 61 BackgroundTracingRule::~BackgroundTracingRule() {}
61 62
62 bool BackgroundTracingRule::ShouldTriggerNamedEvent( 63 bool BackgroundTracingRule::ShouldTriggerNamedEvent(
63 const std::string& named_event) const { 64 const std::string& named_event) const {
64 return false; 65 return false;
65 } 66 }
66 67
67 BackgroundTracingConfigImpl::CategoryPreset 68 BackgroundTracingConfigImpl::CategoryPreset
68 BackgroundTracingRule::GetCategoryPreset() const { 69 BackgroundTracingRule::GetCategoryPreset() const {
69 return BackgroundTracingConfigImpl::BENCHMARK; 70 return BackgroundTracingConfigImpl::BENCHMARK;
70 } 71 }
71 72
72 int BackgroundTracingRule::GetTraceTimeout() const { 73 int BackgroundTracingRule::GetTraceTimeout() const {
73 return -1; 74 return -1;
74 } 75 }
75 76
77 void BackgroundTracingRule::IntoDict(base::DictionaryValue* dict) const {
78 DCHECK(dict);
79 if (trigger_chance_ < 1.0)
80 dict->SetDouble(kConfigRuleTriggerChance, trigger_chance_);
81 }
82
83 void BackgroundTracingRule::FromDict(const base::DictionaryValue* dict) {
84 double trigger_chance;
shatch 2015/10/30 15:50:28 nit: Can't you just pass trigger_chance_ in direct
oystein (OOO til 10th of July) 2015/10/30 19:11:43 Done.
85 if (dict->GetDouble(kConfigRuleTriggerChance, &trigger_chance))
86 trigger_chance_ = trigger_chance;
87 }
88
76 namespace { 89 namespace {
77 90
78 class NamedTriggerRule : public BackgroundTracingRule { 91 class NamedTriggerRule : public BackgroundTracingRule {
79 private: 92 private:
80 NamedTriggerRule(const std::string& named_event) 93 NamedTriggerRule(const std::string& named_event)
81 : named_event_(named_event) {} 94 : named_event_(named_event) {}
82 95
83 public: 96 public:
84 static scoped_ptr<BackgroundTracingRule> Create( 97 static scoped_ptr<BackgroundTracingRule> Create(
85 const base::DictionaryValue* dict) { 98 const base::DictionaryValue* dict) {
86 std::string trigger_name; 99 std::string trigger_name;
87 if (!dict->GetString(kConfigRuleTriggerNameKey, &trigger_name)) 100 if (!dict->GetString(kConfigRuleTriggerNameKey, &trigger_name))
88 return nullptr; 101 return nullptr;
89 102
90 return scoped_ptr<BackgroundTracingRule>( 103 return scoped_ptr<BackgroundTracingRule>(
91 new NamedTriggerRule(trigger_name)); 104 new NamedTriggerRule(trigger_name));
92 } 105 }
93 106
94 void IntoDict(base::DictionaryValue* dict) const override { 107 void IntoDict(base::DictionaryValue* dict) const override {
95 DCHECK(dict); 108 DCHECK(dict);
109 BackgroundTracingRule::IntoDict(dict);
96 dict->SetString(kConfigRuleKey, kPreemptiveConfigRuleMonitorNamed); 110 dict->SetString(kConfigRuleKey, kPreemptiveConfigRuleMonitorNamed);
97 dict->SetString(kConfigRuleTriggerNameKey, named_event_.c_str()); 111 dict->SetString(kConfigRuleTriggerNameKey, named_event_.c_str());
98 } 112 }
99 113
100 bool ShouldTriggerNamedEvent(const std::string& named_event) const override { 114 bool ShouldTriggerNamedEvent(const std::string& named_event) const override {
101 return named_event == named_event_; 115 return named_event == named_event_;
102 } 116 }
103 117
104 private: 118 private:
105 std::string named_event_; 119 std::string named_event_;
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 histogram_name_, 180 histogram_name_,
167 base::Bind(&HistogramRule::OnHistogramChangedCallback, 181 base::Bind(&HistogramRule::OnHistogramChangedCallback,
168 base::Unretained(this), histogram_name_, 182 base::Unretained(this), histogram_name_,
169 histogram_lower_value_, histogram_upper_value_, repeat_)); 183 histogram_lower_value_, histogram_upper_value_, repeat_));
170 184
171 TracingControllerImpl::GetInstance()->AddTraceMessageFilterObserver(this); 185 TracingControllerImpl::GetInstance()->AddTraceMessageFilterObserver(this);
172 } 186 }
173 187
174 void IntoDict(base::DictionaryValue* dict) const override { 188 void IntoDict(base::DictionaryValue* dict) const override {
175 DCHECK(dict); 189 DCHECK(dict);
190 BackgroundTracingRule::IntoDict(dict);
176 dict->SetString(kConfigRuleKey, kPreemptiveConfigRuleMonitorHistogram); 191 dict->SetString(kConfigRuleKey, kPreemptiveConfigRuleMonitorHistogram);
177 dict->SetString(kConfigRuleHistogramNameKey, histogram_name_.c_str()); 192 dict->SetString(kConfigRuleHistogramNameKey, histogram_name_.c_str());
178 dict->SetInteger(kConfigRuleHistogramValue1Key, histogram_lower_value_); 193 dict->SetInteger(kConfigRuleHistogramValue1Key, histogram_lower_value_);
179 dict->SetInteger(kConfigRuleHistogramValue2Key, histogram_upper_value_); 194 dict->SetInteger(kConfigRuleHistogramValue2Key, histogram_upper_value_);
180 dict->SetBoolean(kConfigRuleHistogramRepeatKey, repeat_); 195 dict->SetBoolean(kConfigRuleHistogramRepeatKey, repeat_);
181 if (trigger_delay_ != -1) 196 if (trigger_delay_ != -1)
182 dict->SetInteger(kConfigRuleTriggerDelay, trigger_delay_); 197 dict->SetInteger(kConfigRuleTriggerDelay, trigger_delay_);
183 } 198 }
184 199
185 void OnHistogramTrigger(const std::string& histogram_name) const override { 200 void OnHistogramTrigger(const std::string& histogram_name) const override {
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 return nullptr; 273 return nullptr;
259 274
260 return scoped_ptr<BackgroundTracingRule>( 275 return scoped_ptr<BackgroundTracingRule>(
261 new ReactiveTraceForNSOrTriggerOrFullRule(trigger_name, 276 new ReactiveTraceForNSOrTriggerOrFullRule(trigger_name,
262 category_preset)); 277 category_preset));
263 } 278 }
264 279
265 // BackgroundTracingRule implementation 280 // BackgroundTracingRule implementation
266 void IntoDict(base::DictionaryValue* dict) const override { 281 void IntoDict(base::DictionaryValue* dict) const override {
267 DCHECK(dict); 282 DCHECK(dict);
283 BackgroundTracingRule::IntoDict(dict);
268 dict->SetString( 284 dict->SetString(
269 kConfigCategoryKey, 285 kConfigCategoryKey,
270 BackgroundTracingConfigImpl::CategoryPresetToString(category_preset_)); 286 BackgroundTracingConfigImpl::CategoryPresetToString(category_preset_));
271 dict->SetString(kConfigRuleKey, 287 dict->SetString(kConfigRuleKey,
272 kReactiveConfigRuleTraceOnNavigationUntilTriggerOrFull); 288 kReactiveConfigRuleTraceOnNavigationUntilTriggerOrFull);
273 dict->SetString(kConfigRuleTriggerNameKey, named_event_.c_str()); 289 dict->SetString(kConfigRuleTriggerNameKey, named_event_.c_str());
274 } 290 }
275 291
276 bool ShouldTriggerNamedEvent(const std::string& named_event) const override { 292 bool ShouldTriggerNamedEvent(const std::string& named_event) const override {
277 return named_event == named_event_; 293 return named_event == named_event_;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 return nullptr; 335 return nullptr;
320 336
321 return scoped_ptr<BackgroundTracingRule>( 337 return scoped_ptr<BackgroundTracingRule>(
322 new ReactiveTraceAtRandomIntervalsRule(category_preset, timeout_min, 338 new ReactiveTraceAtRandomIntervalsRule(category_preset, timeout_min,
323 timeout_max)); 339 timeout_max));
324 } 340 }
325 ~ReactiveTraceAtRandomIntervalsRule() override {} 341 ~ReactiveTraceAtRandomIntervalsRule() override {}
326 342
327 void IntoDict(base::DictionaryValue* dict) const override { 343 void IntoDict(base::DictionaryValue* dict) const override {
328 DCHECK(dict); 344 DCHECK(dict);
345 BackgroundTracingRule::IntoDict(dict);
329 dict->SetString( 346 dict->SetString(
330 kConfigCategoryKey, 347 kConfigCategoryKey,
331 BackgroundTracingConfigImpl::CategoryPresetToString(category_preset_)); 348 BackgroundTracingConfigImpl::CategoryPresetToString(category_preset_));
332 dict->SetString(kConfigRuleKey, kReactiveConfigRuleTraceAtRandomIntervals); 349 dict->SetString(kConfigRuleKey, kReactiveConfigRuleTraceAtRandomIntervals);
333 dict->SetInteger(kConfigRuleRandomIntervalTimeoutMin, timeout_min_); 350 dict->SetInteger(kConfigRuleRandomIntervalTimeoutMin, timeout_min_);
334 dict->SetInteger(kConfigRuleRandomIntervalTimeoutMax, timeout_max_); 351 dict->SetInteger(kConfigRuleRandomIntervalTimeoutMax, timeout_max_);
335 } 352 }
336 353
337 void Install() override { 354 void Install() override {
338 handle_ = BackgroundTracingManagerImpl::GetInstance()->RegisterTriggerType( 355 handle_ = BackgroundTracingManagerImpl::GetInstance()->RegisterTriggerType(
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 } // namespace 414 } // namespace
398 415
399 scoped_ptr<BackgroundTracingRule> BackgroundTracingRule::PreemptiveRuleFromDict( 416 scoped_ptr<BackgroundTracingRule> BackgroundTracingRule::PreemptiveRuleFromDict(
400 const base::DictionaryValue* dict) { 417 const base::DictionaryValue* dict) {
401 DCHECK(dict); 418 DCHECK(dict);
402 419
403 std::string type; 420 std::string type;
404 if (!dict->GetString(kConfigRuleKey, &type)) 421 if (!dict->GetString(kConfigRuleKey, &type))
405 return nullptr; 422 return nullptr;
406 423
424 scoped_ptr<BackgroundTracingRule> tracing_rule;
407 if (type == kPreemptiveConfigRuleMonitorNamed) 425 if (type == kPreemptiveConfigRuleMonitorNamed)
408 return NamedTriggerRule::Create(dict); 426 tracing_rule = NamedTriggerRule::Create(dict);
427 else if (type == kPreemptiveConfigRuleMonitorHistogram)
428 tracing_rule = HistogramRule::Create(dict);
409 429
410 if (type == kPreemptiveConfigRuleMonitorHistogram) 430 if (tracing_rule)
411 return HistogramRule::Create(dict); 431 tracing_rule->FromDict(dict);
shatch 2015/10/30 15:50:28 nit: Maybe rename this? "FromDict" kinda implies y
oystein (OOO til 10th of July) 2015/10/30 19:11:43 Renamed to "Setup".
412 432
413 return nullptr; 433 return tracing_rule;
414 } 434 }
415 435
416 scoped_ptr<BackgroundTracingRule> BackgroundTracingRule::ReactiveRuleFromDict( 436 scoped_ptr<BackgroundTracingRule> BackgroundTracingRule::ReactiveRuleFromDict(
417 const base::DictionaryValue* dict, 437 const base::DictionaryValue* dict,
418 BackgroundTracingConfigImpl::CategoryPreset category_preset) { 438 BackgroundTracingConfigImpl::CategoryPreset category_preset) {
419 DCHECK(dict); 439 DCHECK(dict);
420 440
421 std::string type; 441 std::string type;
422 if (!dict->GetString(kConfigRuleKey, &type)) 442 if (!dict->GetString(kConfigRuleKey, &type))
423 return nullptr; 443 return nullptr;
424 444
425 if (type == kReactiveConfigRuleTraceOnNavigationUntilTriggerOrFull) 445 scoped_ptr<BackgroundTracingRule> tracing_rule;
426 return ReactiveTraceForNSOrTriggerOrFullRule::Create(dict, category_preset);
427 446
428 if (type == kReactiveConfigRuleTraceAtRandomIntervals) 447 if (type == kReactiveConfigRuleTraceOnNavigationUntilTriggerOrFull) {
429 return ReactiveTraceAtRandomIntervalsRule::Create(dict, category_preset); 448 tracing_rule =
449 ReactiveTraceForNSOrTriggerOrFullRule::Create(dict, category_preset);
450 } else if (type == kReactiveConfigRuleTraceAtRandomIntervals) {
451 tracing_rule =
452 ReactiveTraceAtRandomIntervalsRule::Create(dict, category_preset);
453 }
430 454
431 return nullptr; 455 if (tracing_rule)
456 tracing_rule->FromDict(dict);
457
458 return tracing_rule;
432 } 459 }
433 460
434 } // namespace content 461 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698