Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/common/profiling.h" | 5 #include "chrome/common/profiling.h" |
| 6 | 6 |
| 7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/debug/profiler.h" | 10 #include "base/debug/profiler.h" |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
| 15 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
| 16 #include "v8/include/v8.h" | 16 #include "v8/include/v8.h" |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | |
| 20 base::debug::AddDynamicSymbol add_dynamic_symbol_func = NULL; | |
| 21 base::debug::MoveDynamicSymbol move_dynamic_symbol_func = NULL; | |
| 22 | |
| 23 void JitCodeEventHandler(const v8::JitCodeEvent* event) { | |
| 24 DCHECK_NE(static_cast<base::debug::AddDynamicSymbol>(NULL), | |
| 25 add_dynamic_symbol_func); | |
| 26 DCHECK_NE(static_cast<base::debug::MoveDynamicSymbol>(NULL), | |
| 27 move_dynamic_symbol_func); | |
| 28 | |
| 29 switch (event->type) { | |
| 30 case v8::JitCodeEvent::CODE_ADDED: | |
| 31 add_dynamic_symbol_func(event->code_start, event->code_len, | |
| 32 event->name.str, event->name.len); | |
| 33 break; | |
| 34 | |
| 35 case v8::JitCodeEvent::CODE_MOVED: | |
| 36 move_dynamic_symbol_func(event->code_start, event->new_code_start); | |
| 37 break; | |
| 38 } | |
| 39 } | |
| 40 | |
| 19 std::string GetProfileName() { | 41 std::string GetProfileName() { |
| 20 static const char kDefaultProfileName[] = "chrome-profile-{type}-{pid}"; | 42 static const char kDefaultProfileName[] = "chrome-profile-{type}-{pid}"; |
| 21 CR_DEFINE_STATIC_LOCAL(std::string, profile_name, ()); | 43 CR_DEFINE_STATIC_LOCAL(std::string, profile_name, ()); |
| 22 | 44 |
| 23 if (profile_name.empty()) { | 45 if (profile_name.empty()) { |
| 24 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 46 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 25 if (command_line.HasSwitch(switches::kProfilingFile)) | 47 if (command_line.HasSwitch(switches::kProfilingFile)) |
| 26 profile_name = command_line.GetSwitchValueASCII(switches::kProfilingFile); | 48 profile_name = command_line.GetSwitchValueASCII(switches::kProfilingFile); |
| 27 else | 49 else |
| 28 profile_name = std::string(kDefaultProfileName); | 50 profile_name = std::string(kDefaultProfileName); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 g_flush_thread_control = LAZY_INSTANCE_INITIALIZER; | 118 g_flush_thread_control = LAZY_INSTANCE_INITIALIZER; |
| 97 | 119 |
| 98 } // namespace | 120 } // namespace |
| 99 | 121 |
| 100 // static | 122 // static |
| 101 void Profiling::ProcessStarted() { | 123 void Profiling::ProcessStarted() { |
| 102 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 124 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 103 std::string process_type = | 125 std::string process_type = |
| 104 command_line.GetSwitchValueASCII(switches::kProcessType); | 126 command_line.GetSwitchValueASCII(switches::kProcessType); |
| 105 | 127 |
| 106 // Establish the V8 return address resolution hook if we're | 128 // Establish the V8 profiling hooks if we're an instrumented binary. |
| 107 // an instrumented binary. | |
| 108 if (base::debug::IsBinaryInstrumented()) { | 129 if (base::debug::IsBinaryInstrumented()) { |
| 109 base::debug::ReturnAddressLocationResolver resolve_func = | 130 base::debug::ReturnAddressLocationResolver resolve_func = |
| 110 base::debug::GetProfilerReturnAddrResolutionFunc(); | 131 base::debug::GetProfilerReturnAddrResolutionFunc(); |
| 111 | 132 |
| 112 if (resolve_func != NULL) { | 133 if (resolve_func != NULL) { |
| 113 v8::V8::SetReturnAddressLocationResolver(resolve_func); | 134 v8::V8::SetReturnAddressLocationResolver(resolve_func); |
| 114 } | 135 } |
| 136 | |
| 137 // Set up the JIT code entry handler and the symbol callbacks if the | |
| 138 // profiler supplies them. | |
| 139 // TODO(siggi): Maybe add a switch or an environment variable to turn off | |
| 140 // V8 profiling? | |
| 141 base::debug::DynamicFunctionEntryHook entry_hook_func = | |
| 142 base::debug::GetProfilerDynamicFunctionEntryHookFunc(); | |
| 143 add_dynamic_symbol_func = base::debug::GetProfilerAddDynamicSymbolFunc(); | |
| 144 move_dynamic_symbol_func = base::debug::GetProfilerMoveDynamicSymbolFunc(); | |
|
Nico
2013/07/03 19:26:38
nit: can't you just call this in JitCodeEventHandl
Sigurður Ásgeirsson
2013/07/03 19:35:58
Retrieving these functions is relatively expensive
| |
| 145 | |
| 146 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 147 if (isolate != NULL && | |
| 148 entry_hook_func != NULL && | |
| 149 add_dynamic_symbol_func != NULL && | |
| 150 move_dynamic_symbol_func != NULL) { | |
| 151 v8::V8::SetFunctionEntryHook(isolate, entry_hook_func); | |
| 152 v8::V8::SetJitCodeEventHandler(v8::kJitCodeEventDefault, | |
| 153 &JitCodeEventHandler); | |
| 154 } | |
| 115 } | 155 } |
| 116 | 156 |
| 117 if (command_line.HasSwitch(switches::kProfilingAtStart)) { | 157 if (command_line.HasSwitch(switches::kProfilingAtStart)) { |
| 118 std::string process_type_to_start = | 158 std::string process_type_to_start = |
| 119 command_line.GetSwitchValueASCII(switches::kProfilingAtStart); | 159 command_line.GetSwitchValueASCII(switches::kProfilingAtStart); |
| 120 if (process_type == process_type_to_start) | 160 if (process_type == process_type_to_start) |
| 121 Start(); | 161 Start(); |
| 122 } | 162 } |
| 123 } | 163 } |
| 124 | 164 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 145 return base::debug::BeingProfiled(); | 185 return base::debug::BeingProfiled(); |
| 146 } | 186 } |
| 147 | 187 |
| 148 // static | 188 // static |
| 149 void Profiling::Toggle() { | 189 void Profiling::Toggle() { |
| 150 if (BeingProfiled()) | 190 if (BeingProfiled()) |
| 151 Stop(); | 191 Stop(); |
| 152 else | 192 else |
| 153 Start(); | 193 Start(); |
| 154 } | 194 } |
| OLD | NEW |