Add long running gmail memory benchmark for background tab.
[chromium-blink-merge.git] / content / browser / tracing / background_tracing_config_dict.cc
blob288d9499253697ea6aadb2aeef397f1d0bcff55a
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.
5 #include "base/macros.h"
6 #include "base/values.h"
7 #include "content/public/browser/background_tracing_preemptive_config.h"
8 #include "content/public/browser/background_tracing_reactive_config.h"
10 namespace content {
12 namespace {
14 const char kConfigsKey[] = "configs";
16 const char kConfigModeKey[] = "mode";
17 const char kConfigModePreemptive[] = "PREEMPTIVE_TRACING_MODE";
18 const char kConfigModeReactive[] = "REACTIVE_TRACING_MODE";
20 const char kConfigCategoryKey[] = "category";
21 const char kConfigCategoryBenchmark[] = "BENCHMARK";
22 const char kConfigCategoryBenchmarkDeep[] = "BENCHMARK_DEEP";
24 const char kConfigRuleKey[] = "rule";
25 const char kConfigRuleTriggerNameKey[] = "trigger_name";
26 const char kConfigRuleHistogramNameKey[] = "histogram_name";
27 const char kConfigRuleHistogramValueKey[] = "histogram_value";
29 const char kPreemptiveConfigRuleMonitorNamed[] =
30 "MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED";
32 const char kPreemptiveConfigRuleMonitorHistogram[] =
33 "MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE";
35 const char kReactiveConfigRuleTraceFor10sOrTriggerOrFull[] =
36 "TRACE_FOR_10S_OR_TRIGGER_OR_FULL";
38 std::string CategoryPresetToString(
39 BackgroundTracingConfig::CategoryPreset category_preset) {
40 switch (category_preset) {
41 case BackgroundTracingConfig::BENCHMARK:
42 return kConfigCategoryBenchmark;
43 case BackgroundTracingConfig::BENCHMARK_DEEP:
44 return kConfigCategoryBenchmarkDeep;
45 break;
47 NOTREACHED();
48 return "";
51 bool StringToCategoryPreset(
52 const std::string& category_preset_string,
53 BackgroundTracingConfig::CategoryPreset* category_preset) {
54 if (category_preset_string == kConfigCategoryBenchmark) {
55 *category_preset = BackgroundTracingConfig::BENCHMARK;
56 return true;
57 } else if (category_preset_string == kConfigCategoryBenchmarkDeep) {
58 *category_preset = BackgroundTracingConfig::BENCHMARK_DEEP;
59 return true;
62 return false;
65 scoped_ptr<BackgroundTracingPreemptiveConfig>
66 BackgroundTracingPreemptiveConfig_FromDict(const base::DictionaryValue* dict) {
67 DCHECK(dict);
69 scoped_ptr<BackgroundTracingPreemptiveConfig> config(
70 new BackgroundTracingPreemptiveConfig());
72 std::string category_preset_string;
73 if (!dict->GetString(kConfigCategoryKey, &category_preset_string))
74 return nullptr;
76 if (!StringToCategoryPreset(category_preset_string, &config->category_preset))
77 return nullptr;
79 const base::ListValue* configs_list = nullptr;
80 if (!dict->GetList(kConfigsKey, &configs_list))
81 return nullptr;
83 for (auto it = configs_list->begin(); it != configs_list->end(); ++it) {
84 const base::DictionaryValue* config_dict = nullptr;
85 if (!(*it)->GetAsDictionary(&config_dict))
86 return nullptr;
88 std::string type;
89 if (!config_dict->GetString(kConfigRuleKey, &type))
90 return nullptr;
92 if (type == kPreemptiveConfigRuleMonitorNamed) {
93 std::string trigger_name;
94 if (!config_dict->GetString(kConfigRuleTriggerNameKey, &trigger_name))
95 return nullptr;
97 BackgroundTracingPreemptiveConfig::MonitoringRule rule;
98 rule.type = BackgroundTracingPreemptiveConfig::
99 MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED;
100 rule.named_trigger_info.trigger_name = trigger_name;
102 config->configs.push_back(rule);
103 } else if (type == kPreemptiveConfigRuleMonitorHistogram) {
104 std::string histogram_name;
105 if (!config_dict->GetString(kConfigRuleHistogramNameKey, &histogram_name))
106 return nullptr;
108 int histogram_value;
109 if (!config_dict->GetInteger(kConfigRuleHistogramValueKey,
110 &histogram_value))
111 return nullptr;
113 BackgroundTracingPreemptiveConfig::MonitoringRule rule;
114 rule.type = BackgroundTracingPreemptiveConfig::
115 MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE;
116 rule.histogram_trigger_info.histogram_name = histogram_name;
117 rule.histogram_trigger_info.histogram_value = histogram_value;
119 config->configs.push_back(rule);
120 } else {
121 continue;
125 return config.Pass();
128 scoped_ptr<BackgroundTracingReactiveConfig>
129 BackgroundTracingReactiveConfig_FromDict(const base::DictionaryValue* dict) {
130 DCHECK(dict);
132 scoped_ptr<BackgroundTracingReactiveConfig> config(
133 new BackgroundTracingReactiveConfig());
135 const base::ListValue* configs_list = nullptr;
136 if (!dict->GetList(kConfigsKey, &configs_list))
137 return nullptr;
139 for (auto it = configs_list->begin(); it != configs_list->end(); ++it) {
140 const base::DictionaryValue* config_dict = nullptr;
141 if (!(*it)->GetAsDictionary(&config_dict))
142 return nullptr;
144 BackgroundTracingReactiveConfig::TracingRule rule;
146 std::string category_preset_string;
147 if (!config_dict->GetString(kConfigCategoryKey, &category_preset_string))
148 return nullptr;
150 if (!StringToCategoryPreset(category_preset_string, &rule.category_preset))
151 return nullptr;
153 std::string type;
154 if (!config_dict->GetString(kConfigRuleKey, &type))
155 return nullptr;
157 if (type != kReactiveConfigRuleTraceFor10sOrTriggerOrFull)
158 return nullptr;
160 rule.type = BackgroundTracingReactiveConfig::
161 TRACE_FOR_10S_OR_TRIGGER_OR_FULL;
163 std::string trigger_name;
164 if (!config_dict->GetString(kConfigRuleTriggerNameKey, &trigger_name))
165 return nullptr;
167 rule.trigger_name = trigger_name;
169 config->configs.push_back(rule);
172 return config.Pass();
175 bool BackgroundTracingPreemptiveConfig_IntoDict(
176 const BackgroundTracingPreemptiveConfig* config,
177 base::DictionaryValue* dict) {
178 dict->SetString(kConfigCategoryKey,
179 CategoryPresetToString(config->category_preset));
181 scoped_ptr<base::ListValue> configs_list(new base::ListValue());
183 for (size_t i = 0; i < config->configs.size(); ++i) {
184 scoped_ptr<base::DictionaryValue> config_dict(new base::DictionaryValue());
186 if (config->configs[i].type == BackgroundTracingPreemptiveConfig::
187 MONITOR_AND_DUMP_WHEN_TRIGGER_NAMED) {
188 config_dict->SetString(kConfigRuleKey, kPreemptiveConfigRuleMonitorNamed);
189 config_dict->SetString(
190 kConfigRuleTriggerNameKey,
191 config->configs[i].named_trigger_info.trigger_name.c_str());
192 } else if (config->configs[i].type ==
193 BackgroundTracingPreemptiveConfig::
194 MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE) {
195 config_dict->SetString(kConfigRuleKey,
196 kPreemptiveConfigRuleMonitorHistogram);
197 config_dict->SetString(
198 kConfigRuleHistogramNameKey,
199 config->configs[i].histogram_trigger_info.histogram_name.c_str());
200 config_dict->SetInteger(
201 kConfigRuleHistogramValueKey,
202 config->configs[i].histogram_trigger_info.histogram_value);
203 } else {
204 continue;
207 configs_list->Append(config_dict.Pass());
210 dict->Set(kConfigsKey, configs_list.Pass());
212 return true;
215 bool BackgroundTracingReactiveConfig_IntoDict(
216 const BackgroundTracingReactiveConfig* config,
217 base::DictionaryValue* dict) {
218 scoped_ptr<base::ListValue> configs_list(new base::ListValue());
220 for (size_t i = 0; i < config->configs.size(); ++i) {
221 scoped_ptr<base::DictionaryValue> config_dict(new base::DictionaryValue());
223 config_dict->SetString(
224 kConfigCategoryKey,
225 CategoryPresetToString(config->configs[i].category_preset));
227 switch (config->configs[i].type) {
228 case BackgroundTracingReactiveConfig::TRACE_FOR_10S_OR_TRIGGER_OR_FULL:
229 config_dict->SetString(
230 kConfigRuleKey,
231 kReactiveConfigRuleTraceFor10sOrTriggerOrFull);
232 break;
233 default:
234 NOTREACHED();
235 continue;
238 config_dict->SetString(kConfigRuleTriggerNameKey,
239 config->configs[i].trigger_name.c_str());
241 configs_list->Append(config_dict.Pass());
244 dict->Set(kConfigsKey, configs_list.Pass());
246 return true;
249 } // namespace
251 scoped_ptr<BackgroundTracingConfig> BackgroundTracingConfig::FromDict(
252 const base::DictionaryValue* dict) {
253 DCHECK(dict);
255 std::string mode;
256 if (!dict->GetString(kConfigModeKey, &mode))
257 return nullptr;
259 scoped_ptr<BackgroundTracingConfig> config;
261 if (mode == kConfigModePreemptive) {
262 config = BackgroundTracingPreemptiveConfig_FromDict(dict);
263 } else if (mode == kConfigModeReactive) {
264 config = BackgroundTracingReactiveConfig_FromDict(dict);
265 } else {
266 return nullptr;
269 return config.Pass();
272 void BackgroundTracingConfig::IntoDict(const BackgroundTracingConfig* config,
273 base::DictionaryValue* dict) {
274 switch (config->mode) {
275 case PREEMPTIVE_TRACING_MODE:
276 dict->SetString(kConfigModeKey, kConfigModePreemptive);
277 if (!BackgroundTracingPreemptiveConfig_IntoDict(
278 static_cast<const BackgroundTracingPreemptiveConfig*>(config),
279 dict))
280 dict->Clear();
281 break;
282 case REACTIVE_TRACING_MODE:
283 dict->SetString(kConfigModeKey, kConfigModeReactive);
284 if (!BackgroundTracingReactiveConfig_IntoDict(
285 static_cast<const BackgroundTracingReactiveConfig*>(config),
286 dict))
287 dict->Clear();
288 break;
292 } // namspace content