| 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/base_switches.h" | 8 #include "base/base_switches.h" |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/debug/profiler.h" | 11 #include "base/debug/profiler.h" |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
| 18 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 19 #include "content/public/common/content_switches.h" | 19 #include "content/public/common/content_switches.h" |
| 20 #include "gin/public/debug.h" | |
| 21 #include "v8/include/v8.h" | |
| 22 | 20 |
| 23 namespace { | 21 namespace { |
| 24 | 22 |
| 25 base::debug::AddDynamicSymbol add_dynamic_symbol_func = NULL; | |
| 26 base::debug::MoveDynamicSymbol move_dynamic_symbol_func = NULL; | |
| 27 | |
| 28 void JitCodeEventHandler(const v8::JitCodeEvent* event) { | |
| 29 DCHECK_NE(static_cast<base::debug::AddDynamicSymbol>(NULL), | |
| 30 add_dynamic_symbol_func); | |
| 31 DCHECK_NE(static_cast<base::debug::MoveDynamicSymbol>(NULL), | |
| 32 move_dynamic_symbol_func); | |
| 33 | |
| 34 switch (event->type) { | |
| 35 case v8::JitCodeEvent::CODE_ADDED: | |
| 36 add_dynamic_symbol_func(event->code_start, event->code_len, | |
| 37 event->name.str, event->name.len); | |
| 38 break; | |
| 39 | |
| 40 case v8::JitCodeEvent::CODE_MOVED: | |
| 41 move_dynamic_symbol_func(event->code_start, event->new_code_start); | |
| 42 break; | |
| 43 | |
| 44 default: | |
| 45 break; | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 std::string GetProfileName() { | 23 std::string GetProfileName() { |
| 50 static const char kDefaultProfileName[] = "chrome-profile-{type}-{pid}"; | 24 static const char kDefaultProfileName[] = "chrome-profile-{type}-{pid}"; |
| 51 CR_DEFINE_STATIC_LOCAL(std::string, profile_name, ()); | 25 CR_DEFINE_STATIC_LOCAL(std::string, profile_name, ()); |
| 52 | 26 |
| 53 if (profile_name.empty()) { | 27 if (profile_name.empty()) { |
| 54 const base::CommandLine& command_line = | 28 const base::CommandLine& command_line = |
| 55 *base::CommandLine::ForCurrentProcess(); | 29 *base::CommandLine::ForCurrentProcess(); |
| 56 if (command_line.HasSwitch(switches::kProfilingFile)) | 30 if (command_line.HasSwitch(switches::kProfilingFile)) |
| 57 profile_name = command_line.GetSwitchValueASCII(switches::kProfilingFile); | 31 profile_name = command_line.GetSwitchValueASCII(switches::kProfilingFile); |
| 58 else | 32 else |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 | 102 |
| 129 } // namespace | 103 } // namespace |
| 130 | 104 |
| 131 // static | 105 // static |
| 132 void Profiling::ProcessStarted() { | 106 void Profiling::ProcessStarted() { |
| 133 const base::CommandLine& command_line = | 107 const base::CommandLine& command_line = |
| 134 *base::CommandLine::ForCurrentProcess(); | 108 *base::CommandLine::ForCurrentProcess(); |
| 135 std::string process_type = | 109 std::string process_type = |
| 136 command_line.GetSwitchValueASCII(switches::kProcessType); | 110 command_line.GetSwitchValueASCII(switches::kProcessType); |
| 137 | 111 |
| 138 // Establish the V8 profiling hooks if we're an instrumented binary. | |
| 139 if (base::debug::IsBinaryInstrumented()) { | |
| 140 base::debug::ReturnAddressLocationResolver resolve_func = | |
| 141 base::debug::GetProfilerReturnAddrResolutionFunc(); | |
| 142 | |
| 143 if (resolve_func != NULL) { | |
| 144 v8::V8::SetReturnAddressLocationResolver(resolve_func); | |
| 145 } | |
| 146 | |
| 147 // Set up the JIT code entry handler and the symbol callbacks if the | |
| 148 // profiler supplies them. | |
| 149 // TODO(siggi): Maybe add a switch or an environment variable to turn off | |
| 150 // V8 profiling? | |
| 151 base::debug::DynamicFunctionEntryHook entry_hook_func = | |
| 152 base::debug::GetProfilerDynamicFunctionEntryHookFunc(); | |
| 153 add_dynamic_symbol_func = base::debug::GetProfilerAddDynamicSymbolFunc(); | |
| 154 move_dynamic_symbol_func = base::debug::GetProfilerMoveDynamicSymbolFunc(); | |
| 155 | |
| 156 if (entry_hook_func != NULL && | |
| 157 add_dynamic_symbol_func != NULL && | |
| 158 move_dynamic_symbol_func != NULL) { | |
| 159 gin::Debug::SetFunctionEntryHook(entry_hook_func); | |
| 160 gin::Debug::SetJitCodeEventHandler(&JitCodeEventHandler); | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 if (command_line.HasSwitch(switches::kProfilingAtStart)) { | 112 if (command_line.HasSwitch(switches::kProfilingAtStart)) { |
| 165 std::string process_type_to_start = | 113 std::string process_type_to_start = |
| 166 command_line.GetSwitchValueASCII(switches::kProfilingAtStart); | 114 command_line.GetSwitchValueASCII(switches::kProfilingAtStart); |
| 167 if (process_type == process_type_to_start) | 115 if (process_type == process_type_to_start) |
| 168 Start(); | 116 Start(); |
| 169 } | 117 } |
| 170 } | 118 } |
| 171 | 119 |
| 172 // static | 120 // static |
| 173 void Profiling::Start() { | 121 void Profiling::Start() { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 193 return base::debug::BeingProfiled(); | 141 return base::debug::BeingProfiled(); |
| 194 } | 142 } |
| 195 | 143 |
| 196 // static | 144 // static |
| 197 void Profiling::Toggle() { | 145 void Profiling::Toggle() { |
| 198 if (BeingProfiled()) | 146 if (BeingProfiled()) |
| 199 Stop(); | 147 Stop(); |
| 200 else | 148 else |
| 201 Start(); | 149 Start(); |
| 202 } | 150 } |
| OLD | NEW |