OLD | NEW |
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"; |
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 Loading... |
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::Setup(const base::DictionaryValue* dict) { |
| 84 dict->GetDouble(kConfigRuleTriggerChance, &trigger_chance_); |
| 85 } |
| 86 |
76 namespace { | 87 namespace { |
77 | 88 |
78 class NamedTriggerRule : public BackgroundTracingRule { | 89 class NamedTriggerRule : public BackgroundTracingRule { |
79 private: | 90 private: |
80 NamedTriggerRule(const std::string& named_event) | 91 NamedTriggerRule(const std::string& named_event) |
81 : named_event_(named_event) {} | 92 : named_event_(named_event) {} |
82 | 93 |
83 public: | 94 public: |
84 static scoped_ptr<BackgroundTracingRule> Create( | 95 static scoped_ptr<BackgroundTracingRule> Create( |
85 const base::DictionaryValue* dict) { | 96 const base::DictionaryValue* dict) { |
86 std::string trigger_name; | 97 std::string trigger_name; |
87 if (!dict->GetString(kConfigRuleTriggerNameKey, &trigger_name)) | 98 if (!dict->GetString(kConfigRuleTriggerNameKey, &trigger_name)) |
88 return nullptr; | 99 return nullptr; |
89 | 100 |
90 return scoped_ptr<BackgroundTracingRule>( | 101 return scoped_ptr<BackgroundTracingRule>( |
91 new NamedTriggerRule(trigger_name)); | 102 new NamedTriggerRule(trigger_name)); |
92 } | 103 } |
93 | 104 |
94 void IntoDict(base::DictionaryValue* dict) const override { | 105 void IntoDict(base::DictionaryValue* dict) const override { |
95 DCHECK(dict); | 106 DCHECK(dict); |
| 107 BackgroundTracingRule::IntoDict(dict); |
96 dict->SetString(kConfigRuleKey, kPreemptiveConfigRuleMonitorNamed); | 108 dict->SetString(kConfigRuleKey, kPreemptiveConfigRuleMonitorNamed); |
97 dict->SetString(kConfigRuleTriggerNameKey, named_event_.c_str()); | 109 dict->SetString(kConfigRuleTriggerNameKey, named_event_.c_str()); |
98 } | 110 } |
99 | 111 |
100 bool ShouldTriggerNamedEvent(const std::string& named_event) const override { | 112 bool ShouldTriggerNamedEvent(const std::string& named_event) const override { |
101 return named_event == named_event_; | 113 return named_event == named_event_; |
102 } | 114 } |
103 | 115 |
104 private: | 116 private: |
105 std::string named_event_; | 117 std::string named_event_; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 histogram_name_, | 178 histogram_name_, |
167 base::Bind(&HistogramRule::OnHistogramChangedCallback, | 179 base::Bind(&HistogramRule::OnHistogramChangedCallback, |
168 base::Unretained(this), histogram_name_, | 180 base::Unretained(this), histogram_name_, |
169 histogram_lower_value_, histogram_upper_value_, repeat_)); | 181 histogram_lower_value_, histogram_upper_value_, repeat_)); |
170 | 182 |
171 TracingControllerImpl::GetInstance()->AddTraceMessageFilterObserver(this); | 183 TracingControllerImpl::GetInstance()->AddTraceMessageFilterObserver(this); |
172 } | 184 } |
173 | 185 |
174 void IntoDict(base::DictionaryValue* dict) const override { | 186 void IntoDict(base::DictionaryValue* dict) const override { |
175 DCHECK(dict); | 187 DCHECK(dict); |
| 188 BackgroundTracingRule::IntoDict(dict); |
176 dict->SetString(kConfigRuleKey, kPreemptiveConfigRuleMonitorHistogram); | 189 dict->SetString(kConfigRuleKey, kPreemptiveConfigRuleMonitorHistogram); |
177 dict->SetString(kConfigRuleHistogramNameKey, histogram_name_.c_str()); | 190 dict->SetString(kConfigRuleHistogramNameKey, histogram_name_.c_str()); |
178 dict->SetInteger(kConfigRuleHistogramValue1Key, histogram_lower_value_); | 191 dict->SetInteger(kConfigRuleHistogramValue1Key, histogram_lower_value_); |
179 dict->SetInteger(kConfigRuleHistogramValue2Key, histogram_upper_value_); | 192 dict->SetInteger(kConfigRuleHistogramValue2Key, histogram_upper_value_); |
180 dict->SetBoolean(kConfigRuleHistogramRepeatKey, repeat_); | 193 dict->SetBoolean(kConfigRuleHistogramRepeatKey, repeat_); |
181 if (trigger_delay_ != -1) | 194 if (trigger_delay_ != -1) |
182 dict->SetInteger(kConfigRuleTriggerDelay, trigger_delay_); | 195 dict->SetInteger(kConfigRuleTriggerDelay, trigger_delay_); |
183 } | 196 } |
184 | 197 |
185 void OnHistogramTrigger(const std::string& histogram_name) const override { | 198 void OnHistogramTrigger(const std::string& histogram_name) const override { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 return nullptr; | 271 return nullptr; |
259 | 272 |
260 return scoped_ptr<BackgroundTracingRule>( | 273 return scoped_ptr<BackgroundTracingRule>( |
261 new ReactiveTraceForNSOrTriggerOrFullRule(trigger_name, | 274 new ReactiveTraceForNSOrTriggerOrFullRule(trigger_name, |
262 category_preset)); | 275 category_preset)); |
263 } | 276 } |
264 | 277 |
265 // BackgroundTracingRule implementation | 278 // BackgroundTracingRule implementation |
266 void IntoDict(base::DictionaryValue* dict) const override { | 279 void IntoDict(base::DictionaryValue* dict) const override { |
267 DCHECK(dict); | 280 DCHECK(dict); |
| 281 BackgroundTracingRule::IntoDict(dict); |
268 dict->SetString( | 282 dict->SetString( |
269 kConfigCategoryKey, | 283 kConfigCategoryKey, |
270 BackgroundTracingConfigImpl::CategoryPresetToString(category_preset_)); | 284 BackgroundTracingConfigImpl::CategoryPresetToString(category_preset_)); |
271 dict->SetString(kConfigRuleKey, | 285 dict->SetString(kConfigRuleKey, |
272 kReactiveConfigRuleTraceOnNavigationUntilTriggerOrFull); | 286 kReactiveConfigRuleTraceOnNavigationUntilTriggerOrFull); |
273 dict->SetString(kConfigRuleTriggerNameKey, named_event_.c_str()); | 287 dict->SetString(kConfigRuleTriggerNameKey, named_event_.c_str()); |
274 } | 288 } |
275 | 289 |
276 bool ShouldTriggerNamedEvent(const std::string& named_event) const override { | 290 bool ShouldTriggerNamedEvent(const std::string& named_event) const override { |
277 return named_event == named_event_; | 291 return named_event == named_event_; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 return nullptr; | 333 return nullptr; |
320 | 334 |
321 return scoped_ptr<BackgroundTracingRule>( | 335 return scoped_ptr<BackgroundTracingRule>( |
322 new ReactiveTraceAtRandomIntervalsRule(category_preset, timeout_min, | 336 new ReactiveTraceAtRandomIntervalsRule(category_preset, timeout_min, |
323 timeout_max)); | 337 timeout_max)); |
324 } | 338 } |
325 ~ReactiveTraceAtRandomIntervalsRule() override {} | 339 ~ReactiveTraceAtRandomIntervalsRule() override {} |
326 | 340 |
327 void IntoDict(base::DictionaryValue* dict) const override { | 341 void IntoDict(base::DictionaryValue* dict) const override { |
328 DCHECK(dict); | 342 DCHECK(dict); |
| 343 BackgroundTracingRule::IntoDict(dict); |
329 dict->SetString( | 344 dict->SetString( |
330 kConfigCategoryKey, | 345 kConfigCategoryKey, |
331 BackgroundTracingConfigImpl::CategoryPresetToString(category_preset_)); | 346 BackgroundTracingConfigImpl::CategoryPresetToString(category_preset_)); |
332 dict->SetString(kConfigRuleKey, kReactiveConfigRuleTraceAtRandomIntervals); | 347 dict->SetString(kConfigRuleKey, kReactiveConfigRuleTraceAtRandomIntervals); |
333 dict->SetInteger(kConfigRuleRandomIntervalTimeoutMin, timeout_min_); | 348 dict->SetInteger(kConfigRuleRandomIntervalTimeoutMin, timeout_min_); |
334 dict->SetInteger(kConfigRuleRandomIntervalTimeoutMax, timeout_max_); | 349 dict->SetInteger(kConfigRuleRandomIntervalTimeoutMax, timeout_max_); |
335 } | 350 } |
336 | 351 |
337 void Install() override { | 352 void Install() override { |
338 handle_ = BackgroundTracingManagerImpl::GetInstance()->RegisterTriggerType( | 353 handle_ = BackgroundTracingManagerImpl::GetInstance()->RegisterTriggerType( |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 } // namespace | 412 } // namespace |
398 | 413 |
399 scoped_ptr<BackgroundTracingRule> BackgroundTracingRule::PreemptiveRuleFromDict( | 414 scoped_ptr<BackgroundTracingRule> BackgroundTracingRule::PreemptiveRuleFromDict( |
400 const base::DictionaryValue* dict) { | 415 const base::DictionaryValue* dict) { |
401 DCHECK(dict); | 416 DCHECK(dict); |
402 | 417 |
403 std::string type; | 418 std::string type; |
404 if (!dict->GetString(kConfigRuleKey, &type)) | 419 if (!dict->GetString(kConfigRuleKey, &type)) |
405 return nullptr; | 420 return nullptr; |
406 | 421 |
| 422 scoped_ptr<BackgroundTracingRule> tracing_rule; |
407 if (type == kPreemptiveConfigRuleMonitorNamed) | 423 if (type == kPreemptiveConfigRuleMonitorNamed) |
408 return NamedTriggerRule::Create(dict); | 424 tracing_rule = NamedTriggerRule::Create(dict); |
| 425 else if (type == kPreemptiveConfigRuleMonitorHistogram) |
| 426 tracing_rule = HistogramRule::Create(dict); |
409 | 427 |
410 if (type == kPreemptiveConfigRuleMonitorHistogram) | 428 if (tracing_rule) |
411 return HistogramRule::Create(dict); | 429 tracing_rule->Setup(dict); |
412 | 430 |
413 return nullptr; | 431 return tracing_rule; |
414 } | 432 } |
415 | 433 |
416 scoped_ptr<BackgroundTracingRule> BackgroundTracingRule::ReactiveRuleFromDict( | 434 scoped_ptr<BackgroundTracingRule> BackgroundTracingRule::ReactiveRuleFromDict( |
417 const base::DictionaryValue* dict, | 435 const base::DictionaryValue* dict, |
418 BackgroundTracingConfigImpl::CategoryPreset category_preset) { | 436 BackgroundTracingConfigImpl::CategoryPreset category_preset) { |
419 DCHECK(dict); | 437 DCHECK(dict); |
420 | 438 |
421 std::string type; | 439 std::string type; |
422 if (!dict->GetString(kConfigRuleKey, &type)) | 440 if (!dict->GetString(kConfigRuleKey, &type)) |
423 return nullptr; | 441 return nullptr; |
424 | 442 |
425 if (type == kReactiveConfigRuleTraceOnNavigationUntilTriggerOrFull) | 443 scoped_ptr<BackgroundTracingRule> tracing_rule; |
426 return ReactiveTraceForNSOrTriggerOrFullRule::Create(dict, category_preset); | |
427 | 444 |
428 if (type == kReactiveConfigRuleTraceAtRandomIntervals) | 445 if (type == kReactiveConfigRuleTraceOnNavigationUntilTriggerOrFull) { |
429 return ReactiveTraceAtRandomIntervalsRule::Create(dict, category_preset); | 446 tracing_rule = |
| 447 ReactiveTraceForNSOrTriggerOrFullRule::Create(dict, category_preset); |
| 448 } else if (type == kReactiveConfigRuleTraceAtRandomIntervals) { |
| 449 tracing_rule = |
| 450 ReactiveTraceAtRandomIntervalsRule::Create(dict, category_preset); |
| 451 } |
430 | 452 |
431 return nullptr; | 453 if (tracing_rule) |
| 454 tracing_rule->Setup(dict); |
| 455 |
| 456 return tracing_rule; |
432 } | 457 } |
433 | 458 |
434 } // namespace content | 459 } // namespace content |
OLD | NEW |