OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2010 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. | |
4 | |
5 #include "chrome/common/profiling.h" | |
6 | |
7 #include "base/at_exit.h" | |
8 #include "base/command_line.h" | |
9 #include "base/debug/profiler.h" | |
10 #include "base/message_loop.h" | |
11 #include "base/string_util.h" | |
12 #include "chrome/common/chrome_switches.h" | |
13 | |
14 namespace { | |
15 std::string GetProfileName() { | |
16 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.
| |
17 static std::string profile_name; | |
18 | |
19 if (profile_name.empty()) { | |
20 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
21 if (command_line.HasSwitch(switches::kProfilingFile)) | |
22 profile_name = command_line.GetSwitchValueASCII(switches::kProfilingFile); | |
23 else | |
24 profile_name = default_profile_name; | |
25 std::string process_type = | |
26 command_line.GetSwitchValueASCII(switches::kProcessType); | |
27 std::string type = process_type.empty() ? | |
28 std::string("browser") : std::string(process_type); | |
29 ReplaceSubstringsAfterOffset(&profile_name, 0, "{type}", type.c_str()); | |
30 } | |
31 return profile_name; | |
32 } | |
33 | |
34 void FlushProfilingData(MessageLoop* loop) { | |
35 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.
| |
36 base::debug::FlushProfiling(); | |
37 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.
| |
38 static int flush_seconds = 0; | |
39 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
| |
40 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
41 std::string profiling_flush = | |
42 command_line.GetSwitchValueASCII(switches::kProfilingFlush); | |
43 if (!profiling_flush.empty()) { | |
44 flush_seconds = atoi(profiling_flush.c_str()); | |
45 DCHECK(flush_seconds > 0); | |
46 } else { | |
47 flush_seconds = kProfilingFlushSeconds; | |
48 } | |
49 } | |
50 loop->PostDelayedTask(FROM_HERE, | |
51 NewRunnableFunction(FlushProfilingData, loop), | |
52 flush_seconds * 1000); | |
53 } | |
54 } | |
55 | |
56 } // namespace | |
57 | |
58 // static | |
59 void Profiling::StartAtStart() { | |
60 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
61 std::string process_type = | |
62 command_line.GetSwitchValueASCII(switches::kProcessType); | |
63 | |
64 if (command_line.HasSwitch(switches::kProfilingAtStart)) { | |
65 std::string process_type_to_start = | |
66 command_line.GetSwitchValueASCII(switches::kProfilingAtStart); | |
67 if (process_type == process_type_to_start) | |
68 Start(); | |
69 } | |
70 } | |
71 | |
72 // static | |
73 void Profiling::Start() { | |
74 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
75 bool flush = command_line.HasSwitch(switches::kProfilingFlush); | |
76 base::debug::StartProfiling(GetProfileName()); | |
77 | |
78 // Schedule profile data flushing for single process because it doesn't | |
79 // get written out correctly on exit. | |
80 if (flush && MessageLoop::current()) | |
81 FlushProfilingData(MessageLoop::current()); | |
82 } | |
83 | |
84 // static | |
85 void Profiling::Stop() { | |
86 base::debug::StopProfiling(); | |
87 } | |
88 | |
89 // static | |
90 bool Profiling::BeingProfiled() { | |
91 return base::debug::BeingProfiled(); | |
92 } | |
93 | |
94 // static | |
95 void Profiling::Toggle() { | |
96 if (BeingProfiled()) | |
97 Stop(); | |
98 else | |
99 Start(); | |
100 } | |
101 | |
102 // static | |
103 void Profiling::MainMessageLoopStarted() { | |
104 if (BeingProfiled()) { | |
105 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
106 bool flush = command_line.HasSwitch(switches::kProfilingFlush); | |
107 if (flush) | |
108 FlushProfilingData(MessageLoop::current()); | |
109 } | |
110 } | |
OLD | NEW |