Skip to content

Commit

Permalink
Reland [Startup Tracing] Add --trace-config-file flag
Browse files Browse the repository at this point in the history
Original CL: https://codereview.chromium.org/1315463002/

This CL adds --trace-config-file flag. Please see trace_config_file.h for details.

Design doc:
https://docs.google.com/document/d/1PgdXUOJF3WtEmYWUyGRbC2Fz2ICCZKO9jPvpLPRSHH8/edit?usp=sharing

BUG=317481, 482098
TBR=dsinclair@chromium.org,blundell@chromium.org,sievers@chromium.org,sky@chromium.org,msw@chromium.org

Review URL: https://codereview.chromium.org/1317333002

Cr-Commit-Position: refs/heads/master@{#346189}
  • Loading branch information
wangzhen127 authored and Commit bot committed Aug 28, 2015
1 parent bf26ad0 commit a556ab6
Show file tree
Hide file tree
Showing 23 changed files with 583 additions and 143 deletions.
1 change: 1 addition & 0 deletions components/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ test("components_unittests") {
"//components/proximity_auth:unit_tests",
"//components/sessions:unit_tests",
"//components/storage_monitor:unit_tests",
"//components/tracing:unit_tests",
]
}

Expand Down
5 changes: 5 additions & 0 deletions components/components_tests.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,9 @@
'sync_driver/tab_node_pool_unittest.cc',
'sync_driver/ui_data_type_controller_unittest.cc',
],
'tracing_unittest_sources': [
'tracing/trace_config_file_unittest.cc',
],
'translate_unittest_sources': [
'translate/core/browser/language_state_unittest.cc',
'translate/core/browser/translate_browser_metrics_unittest.cc',
Expand Down Expand Up @@ -1175,6 +1178,7 @@
'<@(copresence_unittest_sources)',
'<@(feedback_unittest_sources)',
'<@(proximity_auth_unittest_sources)',
'<@(tracing_unittest_sources)',
],
'sources!': [
'variations/variations_request_scheduler_mobile_unittest.cc',
Expand All @@ -1194,6 +1198,7 @@
'components.gyp:pref_registry_test_support',
'components.gyp:proximity_auth',
'components.gyp:proximity_auth_test_support',
'tracing.gyp:tracing',
],
}],
['chromeos==1', {
Expand Down
4 changes: 2 additions & 2 deletions components/tracing.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
'tracing/child_memory_dump_manager_delegate_impl.h',
'tracing/child_trace_message_filter.cc',
'tracing/child_trace_message_filter.h',
'tracing/startup_tracing.cc',
'tracing/startup_tracing.h',
'tracing/trace_config_file.cc',
'tracing/trace_config_file.h',
'tracing/tracing_export.h',
'tracing/tracing_messages.cc',
'tracing/tracing_messages.h',
Expand Down
18 changes: 16 additions & 2 deletions components/tracing/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ component("tracing") {

component("startup_tracing") {
sources = [
"startup_tracing.cc",
"startup_tracing.h",
"trace_config_file.cc",
"trace_config_file.h",
"tracing_export.h",
"tracing_switches.cc",
"tracing_switches.h",
Expand All @@ -36,3 +36,17 @@ component("startup_tracing") {
"//base",
]
}

source_set("unit_tests") {
testonly = true

sources = [
"trace_config_file_unittest.cc",
]

deps = [
":startup_tracing",
"//base/test:test_support",
"//testing/gtest",
]
}
66 changes: 0 additions & 66 deletions components/tracing/startup_tracing.cc

This file was deleted.

20 changes: 0 additions & 20 deletions components/tracing/startup_tracing.h

This file was deleted.

140 changes: 140 additions & 0 deletions components/tracing/trace_config_file.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Copyright (c) 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/tracing/trace_config_file.h"

#include <string>

#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/memory/singleton.h"
#include "base/values.h"
#include "components/tracing/tracing_switches.h"

namespace tracing {

namespace {

// Maximum trace config file size that will be loaded, in bytes.
const size_t kTraceConfigFileSizeLimit = 64 * 1024;

// Trace config file path:
// - Android: /data/local/chrome-trace-config.json
// - Others: specified by --trace-config-file flag.
#if defined(OS_ANDROID)
const base::FilePath::CharType kAndroidTraceConfigFile[] =
FILE_PATH_LITERAL("/data/local/chrome-trace-config.json");
#endif

const base::FilePath::CharType kDefaultResultFile[] =
FILE_PATH_LITERAL("chrometrace.log");

// String parameters that can be used to parse the trace config file content.
const char kTraceConfigParam[] = "trace_config";
const char kStartupDurationParam[] = "startup_duration";
const char kResultFileParam[] = "result_file";

} // namespace

TraceConfigFile* TraceConfigFile::GetInstance() {
return Singleton<TraceConfigFile,
DefaultSingletonTraits<TraceConfigFile>>::get();
}

TraceConfigFile::TraceConfigFile()
: is_enabled_(false),
trace_config_(base::trace_event::TraceConfig()),
startup_duration_(0),
result_file_(kDefaultResultFile) {
#if defined(OS_ANDROID)
base::FilePath trace_config_file(kAndroidTraceConfigFile);
#else
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
if (!command_line.HasSwitch(switches::kTraceConfigFile) ||
command_line.HasSwitch(switches::kTraceStartup) ||
command_line.HasSwitch(switches::kTraceShutdown)) {
return;
}
base::FilePath trace_config_file =
command_line.GetSwitchValuePath(switches::kTraceConfigFile);
#endif

if (trace_config_file.empty()) {
// If the trace config file path is not specified, trace Chrome with the
// default configuration for 5 sec.
startup_duration_ = 5;
is_enabled_ = true;
return;
}

if (!base::PathExists(trace_config_file))
return;

std::string trace_config_file_content;
if (!base::ReadFileToString(trace_config_file,
&trace_config_file_content,
kTraceConfigFileSizeLimit)) {
return;
}
is_enabled_ = ParseTraceConfigFileContent(trace_config_file_content);
}

TraceConfigFile::~TraceConfigFile() {
}

bool TraceConfigFile::ParseTraceConfigFileContent(std::string content) {
scoped_ptr<base::Value> value(base::JSONReader::Read(content));
if (!value || !value->IsType(base::Value::TYPE_DICTIONARY))
return false;

scoped_ptr<base::DictionaryValue> dict(
static_cast<base::DictionaryValue*>(value.release()));

base::DictionaryValue* trace_config_dict = NULL;
if (!dict->GetDictionary(kTraceConfigParam, &trace_config_dict))
return false;

std::string trace_config_str;
base::JSONWriter::Write(*trace_config_dict, &trace_config_str);
trace_config_ = base::trace_event::TraceConfig(trace_config_str);

if (!dict->GetInteger(kStartupDurationParam, &startup_duration_))
startup_duration_ = 0;

if (startup_duration_ < 0)
startup_duration_ = 0;

std::string result_file_str;
if (dict->GetString(kResultFileParam, &result_file_str))
result_file_ = base::FilePath().AppendASCII(result_file_str);

return true;
}

bool TraceConfigFile::IsEnabled() const {
return is_enabled_;
}

base::trace_event::TraceConfig TraceConfigFile::GetTraceConfig() const {
DCHECK(IsEnabled());
return trace_config_;
}

int TraceConfigFile::GetStartupDuration() const {
DCHECK(IsEnabled());
return startup_duration_;
}

#if !defined(OS_ANDROID)
base::FilePath TraceConfigFile::GetResultFile() const {
DCHECK(IsEnabled());
return result_file_;
}
#endif

} // namespace tracing
94 changes: 94 additions & 0 deletions components/tracing/trace_config_file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (c) 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_TRACING_TRACE_CONFIG_FILE_H_
#define COMPONENTS_TRACING_TRACE_CONFIG_FILE_H_

#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/trace_event/trace_config.h"
#include "components/tracing/tracing_export.h"

template <typename Type> struct DefaultSingletonTraits;

namespace tracing {

// TraceConfigFile is a singleton that contains the configurations of tracing.
// One can create a trace config file and use it to configure startup and/or
// shutdown tracing.
//
// The trace config file should be JSON formated. One example is:
// {
// "trace_config": {
// "record_mode": "record-until-full",
// "included_categories": ["cc", "skia"]
// },
// "startup_duration": 5,
// "result_file": "chrometrace.log"
// }
//
// trace_config: The configuration of tracing. Please see the details in
// base/trace_event/trace_config.h.
//
// startup_duration: The duration for startup tracing in terms of seconds.
// Tracing will stop automatically after the duration. If this
// value is not specified, the duration is 0 and one needs
// to stop tracing by other ways, e.g., by DevTools, or get
// the result file after shutting the browser down.
//
// result_file: The file that contains the trace log. The default result
// file path is chrometrace.log. Chrome will dump the trace
// log to this file
// 1) after startup_duration if it is specified;
// 2) or after browser shutdown if startup duration is 0.
// One can also stop tracing and get the result by other ways,
// e.g., by DevTools. In that case, the trace log will not be
// saved to this file.
// Notice: This is not supported on Android. The result file
// path will be generated by tracing controller.
//
// The trace config file can be specified by the --trace-config-file flag on
// most platforms except on Android, e.g., --trace-config-file=path/to/file/.
// This flag should not be used with --trace-startup or --trace-shutdown. If
// those two flags are used, --trace-config-file flag will be ignored. If the
// --trace-config-file flag is used without the file path, Chrome will do
// startup tracing with 5 seconds' startup duration.
//
// On Android, Chrome does not read the --trace-config-file flag, because not
// all Chrome based browsers read customized flag, e.g., Android WebView. Chrome
// on Android reads from a fixed file location:
// /data/local/chrome-trace-config.json
// If this file exists, Chrome will start tracing according to the configuration
// specified in the file, otherwise, Chrome will not start tracing.
class TRACING_EXPORT TraceConfigFile {
public:
static TraceConfigFile* GetInstance();

bool IsEnabled() const;
base::trace_event::TraceConfig GetTraceConfig() const;
int GetStartupDuration() const;
#if !defined(OS_ANDROID)
base::FilePath GetResultFile() const;
#endif

private:
// This allows constructor and destructor to be private and usable only
// by the Singleton class.
friend struct DefaultSingletonTraits<TraceConfigFile>;
TraceConfigFile();
~TraceConfigFile();

bool ParseTraceConfigFileContent(std::string content);

bool is_enabled_;
base::trace_event::TraceConfig trace_config_;
int startup_duration_;
base::FilePath result_file_;

DISALLOW_COPY_AND_ASSIGN(TraceConfigFile);
};

} // namespace tracing

#endif // COMPONENTS_TRACING_TRACE_CONFIG_FILE_H_
Loading

0 comments on commit a556ab6

Please sign in to comment.