diff options
author | rlarocque@chromium.org <rlarocque@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-10 20:39:23 +0000 |
---|---|---|
committer | rlarocque@chromium.org <rlarocque@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-10 20:39:23 +0000 |
commit | 61b53f971c0c44895e4eda15205d0d61211a1f91 (patch) | |
tree | d2740af2b6d6ddc9ec09f357d074323c77e33632 | |
parent | fc93e2fead585ef8feb334a2861a9727464ae93d (diff) | |
download | chromium_src-61b53f971c0c44895e4eda15205d0d61211a1f91.zip chromium_src-61b53f971c0c44895e4eda15205d0d61211a1f91.tar.gz chromium_src-61b53f971c0c44895e4eda15205d0d61211a1f91.tar.bz2 |
Implement profiler log writing at shutdown
Add a flag to enable writing profile data during shutdown
(--profiling-output-file=$FILE).
Add TaskProfilerDataSerializer. This class is responsible for
collecting and outputing profiler data. Currently, the collection part
is very simple, since ThreadData::ToValue() is static. It should become
more substantial when we add support for multi-process profiler dumps.
This class has to be located outside of base/ because it needs to access
Chrome-specific functions (ie. for fetching the userAgent).
Move AutoTracking class to the chrome/browser/task_profiler directory.
It needs to access the TaskProfilerDataSerializer, but components in
base/ should not have access to this Chrome-specific class.
BUG=107265, 109459
TEST=
Review URL: http://codereview.chromium.org/9125015
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@121515 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/tracked_objects.h | 24 | ||||
-rw-r--r-- | chrome/browser/chrome_browser_main.cc | 6 | ||||
-rw-r--r-- | chrome/browser/chrome_browser_main.h | 3 | ||||
-rw-r--r-- | chrome/browser/task_profiler/auto_tracking.cc | 21 | ||||
-rw-r--r-- | chrome/browser/task_profiler/auto_tracking.h | 39 | ||||
-rw-r--r-- | chrome/browser/task_profiler/task_profiler_data_serializer.cc | 50 | ||||
-rw-r--r-- | chrome/browser/task_profiler/task_profiler_data_serializer.h | 22 | ||||
-rw-r--r-- | chrome/chrome_browser.gypi | 6 | ||||
-rw-r--r-- | chrome/common/chrome_switches.cc | 4 | ||||
-rw-r--r-- | chrome/common/chrome_switches.h | 1 |
10 files changed, 150 insertions, 26 deletions
diff --git a/base/tracked_objects.h b/base/tracked_objects.h index c0674cc..a108388 100644 --- a/base/tracked_objects.h +++ b/base/tracked_objects.h @@ -705,30 +705,6 @@ class BASE_EXPORT DataCollector { DISALLOW_COPY_AND_ASSIGN(DataCollector); }; -//------------------------------------------------------------------------------ -// Provide simple way to to start global tracking, and to tear down tracking -// when done. The design has evolved to *not* do any teardown (and just leak -// all allocated data structures). As a result, we don't have any code in this -// destructor, and perhaps this whole class should go away. - -class BASE_EXPORT AutoTracking { - public: - AutoTracking() { - ThreadData::Initialize(); - } - - ~AutoTracking() { - // TODO(jar): Consider emitting a CSV dump of the data at this point. This - // should be called after the message loops have all terminated (or at least - // the main message loop is gone), so there is little chance for additional - // tasks to be Run. - } - - private: - - DISALLOW_COPY_AND_ASSIGN(AutoTracking); -}; - } // namespace tracked_objects #endif // BASE_TRACKED_OBJECTS_H_ diff --git a/chrome/browser/chrome_browser_main.cc b/chrome/browser/chrome_browser_main.cc index 1a86986..fd66d4a 100644 --- a/chrome/browser/chrome_browser_main.cc +++ b/chrome/browser/chrome_browser_main.cc @@ -1170,6 +1170,12 @@ int ChromeBrowserMainParts::PreCreateThreadsImpl() { tracked_objects::ThreadData::InitializeAndSetTrackingStatus(status); } + if (parsed_command_line().HasSwitch(switches::kProfilingOutputFile)) { + tracking_objects_.set_output_file_path( + parsed_command_line().GetSwitchValuePath( + switches::kProfilingOutputFile)); + } + // This forces the TabCloseableStateWatcher to be created and, on chromeos, // register for the notifications it needs to track the closeable state of // tabs. diff --git a/chrome/browser/chrome_browser_main.h b/chrome/browser/chrome_browser_main.h index 018be20..321af78 100644 --- a/chrome/browser/chrome_browser_main.h +++ b/chrome/browser/chrome_browser_main.h @@ -14,6 +14,7 @@ #include "base/tracked_objects.h" #include "chrome/browser/first_run/first_run.h" #include "chrome/browser/process_singleton.h" +#include "chrome/browser/task_profiler/auto_tracking.h" #include "chrome/browser/ui/browser_init.h" #include "content/public/browser/browser_main_parts.h" #include "content/public/browser/browser_thread.h" @@ -164,7 +165,7 @@ class ChromeBrowserMainParts : public content::BrowserMainParts { // Creating this object starts tracking the creation and deletion of Task // instance. This MUST be done before main_message_loop, so that it is // destroyed after the main_message_loop. - tracked_objects::AutoTracking tracking_objects_; + task_profiler::AutoTracking tracking_objects_; // Statistical testing infrastructure for the entire browser. NULL until // SetupMetricsAndFieldTrials is called. diff --git a/chrome/browser/task_profiler/auto_tracking.cc b/chrome/browser/task_profiler/auto_tracking.cc new file mode 100644 index 0000000..2473c45 --- /dev/null +++ b/chrome/browser/task_profiler/auto_tracking.cc @@ -0,0 +1,21 @@ +// Copyright (c) 2012 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 "chrome/browser/task_profiler/auto_tracking.h" +#include "chrome/browser/task_profiler/task_profiler_data_serializer.h" + +namespace task_profiler { + +AutoTracking::~AutoTracking() { + if (!output_file_path_.empty()) { + TaskProfilerDataSerializer output; + output.WriteToFile(output_file_path_); + } +} + +void AutoTracking::set_output_file_path(const FilePath &path) { + output_file_path_ = path; +} + +} // namespace task_profiler diff --git a/chrome/browser/task_profiler/auto_tracking.h b/chrome/browser/task_profiler/auto_tracking.h new file mode 100644 index 0000000..e075815 --- /dev/null +++ b/chrome/browser/task_profiler/auto_tracking.h @@ -0,0 +1,39 @@ +// Copyright (c) 2012 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 CHROME_BROWSER_TASK_PROFILER_AUTO_TRACKING_H_ +#define CHROME_BROWSER_TASK_PROFILER_AUTO_TRACKING_H_ +#pragma once + +#include "base/file_path.h" +#include "base/tracked_objects.h" + +//------------------------------------------------------------------------------ +// Provide simple way to to start global tracking, and to tear down tracking +// when done. The design has evolved to *not* do any teardown (and just leak +// all allocated data structures). This class is currently used to ensure +// that the profiler data is output during shutdown, if this feature has been +// requested. + +namespace task_profiler { + +class AutoTracking { + public: + AutoTracking() { + tracked_objects::ThreadData::Initialize(); + } + + ~AutoTracking(); + + void set_output_file_path(const FilePath &path); + + private: + FilePath output_file_path_; + + DISALLOW_COPY_AND_ASSIGN(AutoTracking); +}; + +} // namespace task_profiler + +#endif // CHROME_BROWSER_TASK_PROFILER_AUTO_TRACKING_H_ diff --git a/chrome/browser/task_profiler/task_profiler_data_serializer.cc b/chrome/browser/task_profiler/task_profiler_data_serializer.cc new file mode 100644 index 0000000..d9a467d --- /dev/null +++ b/chrome/browser/task_profiler/task_profiler_data_serializer.cc @@ -0,0 +1,50 @@ +// Copyright (c) 2012 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 "chrome/browser/task_profiler/task_profiler_data_serializer.h" + +#include "base/file_path.h" +#include "base/file_util.h" +#include "base/json/json_value_serializer.h" +#include "base/time.h" +#include "base/tracked_objects.h" +#include "content/public/common/content_client.h" +#include "googleurl/src/gurl.h" + +namespace task_profiler { + +bool TaskProfilerDataSerializer::WriteToFile(const FilePath &path) { + std::string output; + JSONStringValueSerializer serializer(&output); + serializer.set_pretty_print(true); + + scoped_ptr<base::DictionaryValue> root(new DictionaryValue()); + + base::ListValue* snapshot_list = new ListValue(); + base::DictionaryValue* shutdown_snapshot = new DictionaryValue(); + base::ListValue* per_process_data = new ListValue(); + + root->SetInteger("version", 1); + root->SetString("userAgent", content::GetUserAgent(GURL())); + + // TODO(ramant): Collect data from other processes, then add that data to the + // 'per_process_data' array here. + base::DictionaryValue* this_process_data = + tracked_objects::ThreadData::ToValue(false); + per_process_data->Append(this_process_data); + + shutdown_snapshot->SetInteger( + "timestamp", + (base::Time::Now() - base::Time::UnixEpoch()).InSeconds()); + shutdown_snapshot->Set("data", per_process_data); + snapshot_list->Append(shutdown_snapshot); + root->Set("snapshots", snapshot_list); + + serializer.Serialize(*root); + int data_size = static_cast<int>(output.size()); + + return data_size == file_util::WriteFile(path, output.data(), data_size); +} + +} // namespace task_profiler diff --git a/chrome/browser/task_profiler/task_profiler_data_serializer.h b/chrome/browser/task_profiler/task_profiler_data_serializer.h new file mode 100644 index 0000000..8ada670 --- /dev/null +++ b/chrome/browser/task_profiler/task_profiler_data_serializer.h @@ -0,0 +1,22 @@ +// Copyright (c) 2012 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 CHROME_BROWSER_TASK_PROFILER_TASK_PROFILER_DATA_SERIALIZER_H_ +#define CHROME_BROWSER_TASK_PROFILER_TASK_PROFILER_DATA_SERIALIZER_H_ +#pragma once + +class FilePath; + +namespace task_profiler { + +// This class collects task profiler data and serializes it to a file. The file +// format is compatible with the about:profiler UI. +class TaskProfilerDataSerializer { + public: + bool WriteToFile(const FilePath &path); +}; + +} // namespace task_profiler + +#endif // CHROME_BROWSER_TASK_PROFILER_TASK_PROFILER_DATA_SERIALIZER_H_ diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 4818167..bd897a1 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -2477,12 +2477,16 @@ 'browser/task_manager/task_manager_resource_providers.h', 'browser/task_manager/task_manager_worker_resource_provider.cc', 'browser/task_manager/task_manager_worker_resource_provider.h', + 'browser/task_profiler/auto_tracking.cc', + 'browser/task_profiler/auto_tracking.h', + 'browser/task_profiler/task_profiler_data_serializer.cc', + 'browser/task_profiler/task_profiler_data_serializer.h', 'browser/themes/browser_theme_pack.cc', 'browser/themes/browser_theme_pack.h', 'browser/themes/theme_service.cc', - 'browser/themes/theme_service.h', 'browser/themes/theme_service_factory.cc', 'browser/themes/theme_service_factory.h', + 'browser/themes/theme_service.h', 'browser/themes/theme_service_gtk.cc', 'browser/themes/theme_service_mac.mm', 'browser/translate/languages_menu_model.cc', diff --git a/chrome/common/chrome_switches.cc b/chrome/common/chrome_switches.cc index 45713b2..81833b3 100644 --- a/chrome/common/chrome_switches.cc +++ b/chrome/common/chrome_switches.cc @@ -917,6 +917,10 @@ const char kProfilingAtStart[] = "profiling-at-start"; // The default is chrome-profile-{pid}. const char kProfilingFile[] = "profiling-file"; +// Specifies a path for the output of task-level profiling which can be loaded +// and viewed in about:profiler. +const char kProfilingOutputFile[] = "profiling-output-file"; + // Controls whether profile data is periodically flushed to a file. Normally // the data gets written on exit but cases exist where chrome doesn't exit // cleanly (especially when using single-process). A time in seconds can be diff --git a/chrome/common/chrome_switches.h b/chrome/common/chrome_switches.h index b2ff14a..fcbdec5 100644 --- a/chrome/common/chrome_switches.h +++ b/chrome/common/chrome_switches.h @@ -252,6 +252,7 @@ extern const char kProfileDirectory[]; extern const char kProfilingAtStart[]; extern const char kProfilingFile[]; extern const char kProfilingFlush[]; +extern const char kProfilingOutputFile[]; extern const char kPromoServerURL[]; extern const char kProxyAutoDetect[]; extern const char kProxyBypassList[]; |