Chromium Code Reviews| Index: chrome/common/profiling.cc |
| diff --git a/chrome/common/profiling.cc b/chrome/common/profiling.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4f8ecd70f8796d7f8d63b325deb72d0e8a4672f3 |
| --- /dev/null |
| +++ b/chrome/common/profiling.cc |
| @@ -0,0 +1,110 @@ |
| +// Copyright (c) 2010 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/common/profiling.h" |
| + |
| +#include "base/at_exit.h" |
| +#include "base/command_line.h" |
| +#include "base/debug/profiler.h" |
| +#include "base/message_loop.h" |
| +#include "base/string_util.h" |
| +#include "chrome/common/chrome_switches.h" |
| + |
| +namespace { |
| +std::string GetProfileName() { |
| + const std::string default_profile_name("chrome-profile-{pid}"); |
|
jamesr
2011/02/01 01:21:45
nit: this should be static and some sort of kThing
DaveMoore
2011/02/01 19:21:55
Done.
|
| + static std::string profile_name; |
| + |
| + if (profile_name.empty()) { |
| + const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| + if (command_line.HasSwitch(switches::kProfilingFile)) |
| + profile_name = command_line.GetSwitchValueASCII(switches::kProfilingFile); |
| + else |
| + profile_name = default_profile_name; |
| + std::string process_type = |
| + command_line.GetSwitchValueASCII(switches::kProcessType); |
| + std::string type = process_type.empty() ? |
| + std::string("browser") : std::string(process_type); |
| + ReplaceSubstringsAfterOffset(&profile_name, 0, "{type}", type.c_str()); |
| + } |
| + return profile_name; |
| +} |
| + |
| +void FlushProfilingData(MessageLoop* loop) { |
| + if (Profiling::BeingProfiled()) { |
|
Evan Martin
2011/02/01 02:45:59
Can do an early-exit here to reduce indentation
DaveMoore
2011/02/01 19:21:55
Done.
|
| + base::debug::FlushProfiling(); |
| + const int kProfilingFlushSeconds = 10; |
|
jamesr
2011/02/01 01:21:45
nit: static
Evan Martin
2011/02/01 02:45:59
Just my opinion, but I don't think that's required
DaveMoore
2011/02/01 19:21:55
Done.
|
| + static int flush_seconds = 0; |
| + if (!flush_seconds) { |
|
Evan Martin
2011/02/01 02:45:59
Is it possible for this to race between multiple t
DaveMoore
2011/02/01 19:21:55
This should only be called from the main thread. I
|
| + const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| + std::string profiling_flush = |
| + command_line.GetSwitchValueASCII(switches::kProfilingFlush); |
| + if (!profiling_flush.empty()) { |
| + flush_seconds = atoi(profiling_flush.c_str()); |
| + DCHECK(flush_seconds > 0); |
| + } else { |
| + flush_seconds = kProfilingFlushSeconds; |
| + } |
| + } |
| + loop->PostDelayedTask(FROM_HERE, |
| + NewRunnableFunction(FlushProfilingData, loop), |
| + flush_seconds * 1000); |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| +// static |
| +void Profiling::StartAtStart() { |
| + const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| + std::string process_type = |
| + command_line.GetSwitchValueASCII(switches::kProcessType); |
| + |
| + if (command_line.HasSwitch(switches::kProfilingAtStart)) { |
| + std::string process_type_to_start = |
| + command_line.GetSwitchValueASCII(switches::kProfilingAtStart); |
| + if (process_type == process_type_to_start) |
| + Start(); |
| + } |
| +} |
| + |
| +// static |
| +void Profiling::Start() { |
| + const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| + bool flush = command_line.HasSwitch(switches::kProfilingFlush); |
| + base::debug::StartProfiling(GetProfileName()); |
| + |
| + // Schedule profile data flushing for single process because it doesn't |
| + // get written out correctly on exit. |
| + if (flush && MessageLoop::current()) |
| + FlushProfilingData(MessageLoop::current()); |
| +} |
| + |
| +// static |
| +void Profiling::Stop() { |
| + base::debug::StopProfiling(); |
| +} |
| + |
| +// static |
| +bool Profiling::BeingProfiled() { |
| + return base::debug::BeingProfiled(); |
| +} |
| + |
| +// static |
| +void Profiling::Toggle() { |
| + if (BeingProfiled()) |
| + Stop(); |
| + else |
| + Start(); |
| +} |
| + |
| +// static |
| +void Profiling::MainMessageLoopStarted() { |
| + if (BeingProfiled()) { |
| + const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| + bool flush = command_line.HasSwitch(switches::kProfilingFlush); |
| + if (flush) |
| + FlushProfilingData(MessageLoop::current()); |
| + } |
| +} |