Commit f6e5c9df authored by Roman Kuksin's avatar Roman Kuksin Committed by Commit Bot

Add "trace_config" field to BackgroundTracing config

Change-Id: Ief54847dc4d06fe1620d9dafb1df29e3104f000d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1735458Reviewed-by: default avataroysteine <oysteine@chromium.org>
Commit-Queue: oysteine <oysteine@chromium.org>
Cr-Commit-Position: refs/heads/master@{#685273}
parent 61244891
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <set> #include <set>
#include <utility> #include <utility>
#include "base/json/json_reader.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/process/process_handle.h" #include "base/process/process_handle.h"
...@@ -34,6 +35,7 @@ const char kConfigTraceBrowserProcessOnly[] = "trace_browser_process_only"; ...@@ -34,6 +35,7 @@ const char kConfigTraceBrowserProcessOnly[] = "trace_browser_process_only";
const char kConfigCategoryKey[] = "category"; const char kConfigCategoryKey[] = "category";
const char kConfigCustomCategoriesKey[] = "custom_categories"; const char kConfigCustomCategoriesKey[] = "custom_categories";
const char kConfigTraceConfigKey[] = "trace_config";
const char kConfigCategoryBenchmark[] = "BENCHMARK"; const char kConfigCategoryBenchmark[] = "BENCHMARK";
const char kConfigCategoryBenchmarkDeep[] = "BENCHMARK_DEEP"; const char kConfigCategoryBenchmarkDeep[] = "BENCHMARK_DEEP";
const char kConfigCategoryBenchmarkGPU[] = "BENCHMARK_GPU"; const char kConfigCategoryBenchmarkGPU[] = "BENCHMARK_GPU";
...@@ -50,6 +52,7 @@ const char kConfigCategoryBenchmarkServiceworker[] = "BENCHMARK_SERVICEWORKER"; ...@@ -50,6 +52,7 @@ const char kConfigCategoryBenchmarkServiceworker[] = "BENCHMARK_SERVICEWORKER";
const char kConfigCategoryBenchmarkPower[] = "BENCHMARK_POWER"; const char kConfigCategoryBenchmarkPower[] = "BENCHMARK_POWER";
const char kConfigCategoryBlinkStyle[] = "BLINK_STYLE"; const char kConfigCategoryBlinkStyle[] = "BLINK_STYLE";
const char kConfigCategoryCustom[] = "CUSTOM"; const char kConfigCategoryCustom[] = "CUSTOM";
const char kConfigCustomConfig[] = "CUSTOM_CONFIG";
const char kConfigLowRamBufferSizeKb[] = "low_ram_buffer_size_kb"; const char kConfigLowRamBufferSizeKb[] = "low_ram_buffer_size_kb";
const char kConfigMediumRamBufferSizeKb[] = "medium_ram_buffer_size_kb"; const char kConfigMediumRamBufferSizeKb[] = "medium_ram_buffer_size_kb";
...@@ -102,6 +105,8 @@ std::string BackgroundTracingConfigImpl::CategoryPresetToString( ...@@ -102,6 +105,8 @@ std::string BackgroundTracingConfigImpl::CategoryPresetToString(
return kConfigCategoryBlinkStyle; return kConfigCategoryBlinkStyle;
case BackgroundTracingConfigImpl::CUSTOM_CATEGORY_PRESET: case BackgroundTracingConfigImpl::CUSTOM_CATEGORY_PRESET:
return kConfigCategoryCustom; return kConfigCategoryCustom;
case BackgroundTracingConfigImpl::CUSTOM_TRACE_CONFIG:
return kConfigCustomConfig;
case BackgroundTracingConfigImpl::CATEGORY_PRESET_UNSET: case BackgroundTracingConfigImpl::CATEGORY_PRESET_UNSET:
NOTREACHED(); NOTREACHED();
} }
...@@ -189,6 +194,12 @@ bool BackgroundTracingConfigImpl::StringToCategoryPreset( ...@@ -189,6 +194,12 @@ bool BackgroundTracingConfigImpl::StringToCategoryPreset(
void BackgroundTracingConfigImpl::IntoDict(base::DictionaryValue* dict) { void BackgroundTracingConfigImpl::IntoDict(base::DictionaryValue* dict) {
if (category_preset_ == CUSTOM_CATEGORY_PRESET) { if (category_preset_ == CUSTOM_CATEGORY_PRESET) {
dict->SetString(kConfigCustomCategoriesKey, custom_categories_); dict->SetString(kConfigCustomCategoriesKey, custom_categories_);
} else if (category_preset_ == CUSTOM_TRACE_CONFIG) {
base::Optional<base::Value> trace_config =
base::JSONReader::Read(trace_config_.ToString());
if (trace_config) {
dict->SetKey(kConfigTraceConfigKey, std::move(*trace_config));
}
} }
switch (tracing_mode()) { switch (tracing_mode()) {
...@@ -242,10 +253,19 @@ TraceConfig BackgroundTracingConfigImpl::GetTraceConfig() const { ...@@ -242,10 +253,19 @@ TraceConfig BackgroundTracingConfigImpl::GetTraceConfig() const {
? base::trace_event::RECORD_UNTIL_FULL ? base::trace_event::RECORD_UNTIL_FULL
: base::trace_event::RECORD_CONTINUOUSLY; : base::trace_event::RECORD_CONTINUOUSLY;
TraceConfig chrome_config = TraceConfig chrome_config;
(category_preset() == CUSTOM_CATEGORY_PRESET if (category_preset() == CUSTOM_TRACE_CONFIG) {
? TraceConfig(custom_categories_, record_mode) chrome_config = trace_config_;
: GetConfigForCategoryPreset(category_preset(), record_mode)); if (!chrome_config.process_filter_config().included_process_ids().empty()) {
// |included_process_ids| are not allowed in BackgroundTracing because
// PIDs can't be known ahead of time.
chrome_config.SetProcessFilterConfig(TraceConfig::ProcessFilterConfig());
}
} else if (category_preset() == CUSTOM_CATEGORY_PRESET) {
chrome_config = TraceConfig(custom_categories_, record_mode);
} else {
chrome_config = GetConfigForCategoryPreset(category_preset(), record_mode);
}
if (trace_browser_process_only_) { if (trace_browser_process_only_) {
TraceConfig::ProcessFilterConfig process_config({base::GetCurrentProcId()}); TraceConfig::ProcessFilterConfig process_config({base::GetCurrentProcId()});
...@@ -322,7 +342,11 @@ BackgroundTracingConfigImpl::PreemptiveFromDict( ...@@ -322,7 +342,11 @@ BackgroundTracingConfigImpl::PreemptiveFromDict(
std::unique_ptr<BackgroundTracingConfigImpl> config( std::unique_ptr<BackgroundTracingConfigImpl> config(
new BackgroundTracingConfigImpl(BackgroundTracingConfigImpl::PREEMPTIVE)); new BackgroundTracingConfigImpl(BackgroundTracingConfigImpl::PREEMPTIVE));
if (dict->GetString(kConfigCustomCategoriesKey, const base::DictionaryValue* trace_config = nullptr;
if (dict->GetDictionary(kConfigTraceConfigKey, &trace_config)) {
config->trace_config_ = TraceConfig(*trace_config);
config->category_preset_ = CUSTOM_TRACE_CONFIG;
} else if (dict->GetString(kConfigCustomCategoriesKey,
&config->custom_categories_)) { &config->custom_categories_)) {
config->category_preset_ = CUSTOM_CATEGORY_PRESET; config->category_preset_ = CUSTOM_CATEGORY_PRESET;
} else { } else {
...@@ -365,7 +389,12 @@ BackgroundTracingConfigImpl::ReactiveFromDict( ...@@ -365,7 +389,12 @@ BackgroundTracingConfigImpl::ReactiveFromDict(
std::string category_preset_string; std::string category_preset_string;
bool has_global_categories = false; bool has_global_categories = false;
if (dict->GetString(kConfigCustomCategoriesKey, const base::DictionaryValue* trace_config = nullptr;
if (dict->GetDictionary(kConfigTraceConfigKey, &trace_config)) {
config->trace_config_ = TraceConfig(*trace_config);
config->category_preset_ = CUSTOM_TRACE_CONFIG;
has_global_categories = true;
} else if (dict->GetString(kConfigCustomCategoriesKey,
&config->custom_categories_)) { &config->custom_categories_)) {
config->category_preset_ = CUSTOM_CATEGORY_PRESET; config->category_preset_ = CUSTOM_CATEGORY_PRESET;
has_global_categories = true; has_global_categories = true;
...@@ -487,6 +516,7 @@ TraceConfig BackgroundTracingConfigImpl::GetConfigForCategoryPreset( ...@@ -487,6 +516,7 @@ TraceConfig BackgroundTracingConfigImpl::GetConfigForCategoryPreset(
} }
case BackgroundTracingConfigImpl::CategoryPreset::CATEGORY_PRESET_UNSET: case BackgroundTracingConfigImpl::CategoryPreset::CATEGORY_PRESET_UNSET:
case BackgroundTracingConfigImpl::CategoryPreset::CUSTOM_CATEGORY_PRESET: case BackgroundTracingConfigImpl::CategoryPreset::CUSTOM_CATEGORY_PRESET:
case BackgroundTracingConfigImpl::CategoryPreset::CUSTOM_TRACE_CONFIG:
NOTREACHED(); NOTREACHED();
} }
NOTREACHED(); NOTREACHED();
......
...@@ -32,6 +32,7 @@ class CONTENT_EXPORT BackgroundTracingConfigImpl ...@@ -32,6 +32,7 @@ class CONTENT_EXPORT BackgroundTracingConfigImpl
enum CategoryPreset { enum CategoryPreset {
CATEGORY_PRESET_UNSET, CATEGORY_PRESET_UNSET,
CUSTOM_CATEGORY_PRESET, CUSTOM_CATEGORY_PRESET,
CUSTOM_TRACE_CONFIG,
BENCHMARK, BENCHMARK,
BENCHMARK_DEEP, BENCHMARK_DEEP,
BENCHMARK_GPU, BENCHMARK_GPU,
...@@ -110,6 +111,7 @@ class CONTENT_EXPORT BackgroundTracingConfigImpl ...@@ -110,6 +111,7 @@ class CONTENT_EXPORT BackgroundTracingConfigImpl
void SetBufferSizeLimits(const base::DictionaryValue* dict); void SetBufferSizeLimits(const base::DictionaryValue* dict);
int GetMaximumTraceBufferSizeKb() const; int GetMaximumTraceBufferSizeKb() const;
base::trace_event::TraceConfig trace_config_;
CategoryPreset category_preset_; CategoryPreset category_preset_;
std::vector<std::unique_ptr<BackgroundTracingRule>> rules_; std::vector<std::unique_ptr<BackgroundTracingRule>> rules_;
std::string scenario_name_; std::string scenario_name_;
......
...@@ -10,11 +10,14 @@ ...@@ -10,11 +10,14 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/command_line.h" #include "base/command_line.h"
#include "base/json/json_reader.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ref_counted_memory.h" #include "base/memory/ref_counted_memory.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/process/process_handle.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/strings/pattern.h" #include "base/strings/pattern.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/post_task.h" #include "base/task/post_task.h"
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "base/test/test_timeouts.h" #include "base/test/test_timeouts.h"
...@@ -184,6 +187,8 @@ class TestTraceReceiverHelper { ...@@ -184,6 +187,8 @@ class TestTraceReceiverHelper {
void WaitForTraceReceived() { wait_for_trace_received_.Run(); } void WaitForTraceReceived() { wait_for_trace_received_.Run(); }
bool trace_received() const { return trace_received_; } bool trace_received() const { return trace_received_; }
const base::Value& get_metadata() const { return *metadata_; }
bool TraceHasMatchingString(const char* text) const { bool TraceHasMatchingString(const char* text) const {
return file_contents_.find(text) != std::string::npos; return file_contents_.find(text) != std::string::npos;
} }
...@@ -192,7 +197,8 @@ class TestTraceReceiverHelper { ...@@ -192,7 +197,8 @@ class TestTraceReceiverHelper {
const scoped_refptr<base::RefCountedString>& file_contents, const scoped_refptr<base::RefCountedString>& file_contents,
std::unique_ptr<const base::DictionaryValue> metadata, std::unique_ptr<const base::DictionaryValue> metadata,
BackgroundTracingManager::FinishedProcessingCallback done_callback) { BackgroundTracingManager::FinishedProcessingCallback done_callback) {
// Receive the trace. metadata_ = std::move(metadata);
EXPECT_TRUE(file_contents); EXPECT_TRUE(file_contents);
EXPECT_FALSE(trace_received_); EXPECT_FALSE(trace_received_);
trace_received_ = true; trace_received_ = true;
...@@ -229,6 +235,7 @@ class TestTraceReceiverHelper { ...@@ -229,6 +235,7 @@ class TestTraceReceiverHelper {
} }
private: private:
std::unique_ptr<const base::DictionaryValue> metadata_;
base::RunLoop wait_for_trace_received_; base::RunLoop wait_for_trace_received_;
bool trace_received_ = false; bool trace_received_ = false;
std::string file_contents_; std::string file_contents_;
...@@ -891,11 +898,72 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, ...@@ -891,11 +898,72 @@ IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest,
// Our reference value is "1", so a value of "2" should trigger a trace. // Our reference value is "1", so a value of "2" should trigger a trace.
LOCAL_HISTOGRAM_COUNTS("fake", 2); LOCAL_HISTOGRAM_COUNTS("fake", 2);
trace_receiver_helper.WaitForTraceReceived();
EXPECT_TRUE(trace_receiver_helper.trace_received());
const std::string* trace_config =
trace_receiver_helper.get_metadata().FindStringKey("trace-config");
ASSERT_TRUE(trace_config);
EXPECT_NE(trace_config->find("record-continuously"), trace_config->npos)
<< *trace_config;
EXPECT_TRUE(BackgroundTracingManager::GetInstance()->HasActiveScenario());
BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted();
}
IN_PROC_BROWSER_TEST_F(BackgroundTracingManagerBrowserTest, CustomConfig) {
TestBackgroundTracingHelper background_tracing_helper;
TestTraceReceiverHelper trace_receiver_helper;
base::DictionaryValue dict;
dict.SetString("mode", "PREEMPTIVE_TRACING_MODE");
dict.SetString("category", "BENCHMARK");
dict.SetKey("trace_config", std::move(*base::JSONReader::Read(R"(
{
"included_categories": ["*"],
"record_mode": "record-until-full"
})")));
std::unique_ptr<base::ListValue> rules_list(new base::ListValue());
{
std::unique_ptr<base::DictionaryValue> rules_dict(
new base::DictionaryValue());
rules_dict->SetString("rule",
"MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE");
rules_dict->SetString("histogram_name", "fake");
rules_dict->SetInteger("histogram_value", 1);
rules_list->Append(std::move(rules_dict));
}
dict.Set("configs", std::move(rules_list));
std::unique_ptr<BackgroundTracingConfig> config(
BackgroundTracingConfigImpl::FromDict(&dict));
EXPECT_TRUE(config);
EXPECT_TRUE(BackgroundTracingManager::GetInstance()->SetActiveScenario(
std::move(config), trace_receiver_helper.get_receive_callback(),
BackgroundTracingManager::NO_DATA_FILTERING));
background_tracing_helper.WaitForTracingEnabled();
// Our reference value is "1", so a value of "2" should trigger a trace.
LOCAL_HISTOGRAM_COUNTS("fake", 2);
trace_receiver_helper.WaitForTraceReceived(); trace_receiver_helper.WaitForTraceReceived();
BackgroundTracingManager::GetInstance()->AbortScenarioForTesting(); BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
background_tracing_helper.WaitForScenarioAborted(); background_tracing_helper.WaitForScenarioAborted();
EXPECT_TRUE(trace_receiver_helper.trace_received()); EXPECT_TRUE(trace_receiver_helper.trace_received());
const std::string* trace_config =
trace_receiver_helper.get_metadata().FindStringKey("trace-config");
ASSERT_TRUE(trace_config);
EXPECT_NE(trace_config->find("record-until-full"), trace_config->npos)
<< *trace_config;
} }
// This tests that histogram triggers for reactive mode configs. // This tests that histogram triggers for reactive mode configs.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment