OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "base/profiler/cpu_profiler.h" |
| 6 |
| 7 #include <Tlhelp32.h> |
| 8 #include <dbghelp.h> |
| 9 #include <stddef.h> |
| 10 #include <windows.h> |
| 11 |
| 12 #include "base/debug/stack_trace.h" |
| 13 #include "base/threading/platform_thread.h" |
| 14 #include "base/threading/thread_id_name_manager.h" |
| 15 #include "base/time/time.h" |
| 16 |
| 17 namespace base { |
| 18 |
| 19 const DWORD kSuspendFailed = static_cast<DWORD>(-1); |
| 20 |
| 21 CpuProfiler::CpuProfiler() |
| 22 : ui_thread_(0), |
| 23 io_thread_(0), |
| 24 ui_thread_stack_depth_(0), |
| 25 io_thread_stack_depth_(0) { |
| 26 #ifdef _WIN64 |
| 27 // Initialiazation |
| 28 if (RtlVirtualUnwind == NULL && RtlLookupFunctionEntry == NULL) { |
| 29 const HMODULE hNtDll = ::GetModuleHandle(L"ntdll.dll"); |
| 30 reinterpret_cast<void*&>(RtlVirtualUnwind) = |
| 31 ::GetProcAddress(hNtDll, "RtlVirtualUnwind"); |
| 32 reinterpret_cast<void*&>(RtlLookupFunctionEntry) = |
| 33 ::GetProcAddress(hNtDll, "RtlLookupFunctionEntry"); |
| 34 } |
| 35 #endif |
| 36 |
| 37 //SymSetOptions(SYMOPT_UNDNAME | SYMOPT_LOAD_LINES | SYMOPT_DEFERRED_LOADS); |
| 38 //if (!SymInitialize(GetCurrentProcess(), NULL, TRUE)) { |
| 39 // LOG(ERROR) << "SymInitialize failed with error: " << GetLastError(); |
| 40 //} |
| 41 } |
| 42 |
| 43 CpuProfiler::~CpuProfiler() {} |
| 44 |
| 45 // static |
| 46 bool CpuProfiler::IsPlatformSupported() { |
| 47 #ifdef _WIN64 |
| 48 bool supported = true; |
| 49 #else |
| 50 bool supported = false; |
| 51 #endif |
| 52 return supported; |
| 53 } |
| 54 |
| 55 void CpuProfiler::OnTimer() { |
| 56 if (!ui_thread_ || !io_thread_) { |
| 57 GetThreadIds(); |
| 58 } |
| 59 |
| 60 ui_thread_stack_depth_ = SampleThread(ui_thread_, arraysize(ui_thread_stack_), |
| 61 ui_thread_stack_); |
| 62 io_thread_stack_depth_ = SampleThread(io_thread_, arraysize(io_thread_stack_), |
| 63 io_thread_stack_); |
| 64 ResolveModules(); |
| 65 |
| 66 modules_.clear(); |
| 67 ui_thread_stack_depth_ = 0; |
| 68 io_thread_stack_depth_ = 0; |
| 69 } |
| 70 |
| 71 int CpuProfiler::SampleThread(DWORD thread, int max_stack_size, |
| 72 StackTraceEntry* stack) { |
| 73 HANDLE thread_handle = ::OpenThread(THREAD_GET_CONTEXT | |
| 74 THREAD_QUERY_INFORMATION | THREAD_SUSPEND_RESUME, FALSE, thread); |
| 75 if (!thread_handle) |
| 76 return 0; |
| 77 |
| 78 if (::SuspendThread(thread_handle) == kSuspendFailed) { |
| 79 LOG(ERROR) << "SuspendThread failed: " << GetLastError(); |
| 80 return 0; |
| 81 } |
| 82 |
| 83 CONTEXT thread_context; |
| 84 memset(&thread_context, 0, sizeof(thread_context)); |
| 85 thread_context.ContextFlags = CONTEXT_ALL; |
| 86 if (!::GetThreadContext(thread_handle, &thread_context)) { |
| 87 LOG(ERROR) << "GetThreadContext failed: " << GetLastError(); |
| 88 } |
| 89 |
| 90 //debug::StackTrace trace(&thread_context); |
| 91 |
| 92 int stack_depth = StackTrace64(&thread_context, max_stack_size, stack); |
| 93 |
| 94 //GetNames(stack_trace, stack_depth); |
| 95 |
| 96 if (::ResumeThread(thread_handle) == kSuspendFailed) { |
| 97 LOG(ERROR) << "ResumeThread failed: " << GetLastError(); |
| 98 } |
| 99 ::CloseHandle(thread_handle); |
| 100 return stack_depth; |
| 101 } |
| 102 |
| 103 void CpuProfiler::GetThreadIds() { |
| 104 HANDLE snapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); |
| 105 if (INVALID_HANDLE_VALUE == snapshot) { |
| 106 LOG(ERROR) << "CreateToolhelp32Snapshot failed"; |
| 107 return; |
| 108 } |
| 109 |
| 110 base::ThreadIdNameManager* manager = base::ThreadIdNameManager::GetInstance(); |
| 111 DWORD current_process = GetCurrentProcessId(); |
| 112 THREADENTRY32 thread_entry = {0}; |
| 113 thread_entry.dwSize = sizeof(THREADENTRY32); |
| 114 BOOL result = ::Thread32First(snapshot, &thread_entry); |
| 115 while (result) { |
| 116 if (thread_entry.th32OwnerProcessID == current_process) { |
| 117 const char* name = manager->GetName(thread_entry.th32ThreadID); |
| 118 if (strcmp(name, "CrBrowserMain") == 0) { |
| 119 ui_thread_ = thread_entry.th32ThreadID; |
| 120 } |
| 121 if (strcmp(name, "Chrome_IOThread") == 0) { |
| 122 io_thread_ = thread_entry.th32ThreadID; |
| 123 } |
| 124 } |
| 125 result = Thread32Next(snapshot, &thread_entry); |
| 126 } |
| 127 CloseHandle(snapshot); |
| 128 } |
| 129 |
| 130 int CpuProfiler::StackTrace64(CONTEXT* context, int stack_depth, |
| 131 StackTraceEntry* stack_trace) { |
| 132 #ifdef _WIN64 |
| 133 IMAGEHLP_SYMBOL64 sym; |
| 134 sym.SizeOfStruct = sizeof(sym); |
| 135 sym.MaxNameLength = 0; |
| 136 |
| 137 for (int i = 0; i < stack_depth; i++, stack_trace++) { |
| 138 // Try to look up unwind metadata for the current function. |
| 139 ULONG64 image_base; |
| 140 PRUNTIME_FUNCTION runtime_function = |
| 141 RtlLookupFunctionEntry(context->Rip, &image_base, NULL); |
| 142 |
| 143 stack_trace->rsp = context->Rsp; |
| 144 stack_trace->rip = context->Rip; |
| 145 stack_trace->module = NULL; |
| 146 |
| 147 if (runtime_function) { |
| 148 HMODULE module = NULL; |
| 149 if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, |
| 150 reinterpret_cast<LPCTSTR >(context->Rip), &module)) { |
| 151 stack_trace->module = module; |
| 152 } |
| 153 |
| 154 KNONVOLATILE_CONTEXT_POINTERS nvcontext; |
| 155 RtlZeroMemory(&nvcontext, sizeof(KNONVOLATILE_CONTEXT_POINTERS)); |
| 156 PVOID handler_data; |
| 157 ULONG64 establisher_frame; |
| 158 RtlVirtualUnwind(0, image_base, context->Rip, runtime_function, context, |
| 159 &handler_data, &establisher_frame, &nvcontext); |
| 160 } else { |
| 161 // If we don't have a RUNTIME_FUNCTION, then we've encountered |
| 162 // a leaf function. Adjust the stack appropriately. |
| 163 context->Rip = (ULONG64)(*(PULONG64)context->Rsp); |
| 164 context->Rsp += 8; |
| 165 } |
| 166 |
| 167 if (!context->Rip) |
| 168 return i; |
| 169 } |
| 170 return stack_depth; |
| 171 #else |
| 172 return 0; |
| 173 #endif |
| 174 } |
| 175 |
| 176 void CpuProfiler::GetNames(StackTraceEntry* stack_trace, int stack_depth) { |
| 177 HANDLE process = GetCurrentProcess(); |
| 178 DWORD64 displacement = 0; |
| 179 DWORD line_displacement = 0; |
| 180 DWORD64 address = 0; |
| 181 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(wchar_t)]; |
| 182 PSYMBOL_INFO symbol_info = (PSYMBOL_INFO)buffer; |
| 183 symbol_info->SizeOfStruct = sizeof(SYMBOL_INFO); |
| 184 symbol_info->MaxNameLen = MAX_SYM_NAME; |
| 185 std::string file_name; |
| 186 |
| 187 IMAGEHLP_LINE64 line; |
| 188 line.SizeOfStruct = sizeof(IMAGEHLP_LINE64); |
| 189 |
| 190 for (int i = 0; i < stack_depth; i++, stack_trace++) { |
| 191 address = stack_trace->rip; |
| 192 if (!SymFromAddr(process, address, &displacement, symbol_info)) { |
| 193 wcscpy(reinterpret_cast<wchar_t*>(symbol_info->Name), L"failed"); |
| 194 } |
| 195 if (!SymGetLineFromAddr64(process, address, &line_displacement, &line)) { |
| 196 line.LineNumber = 0; |
| 197 file_name.clear(); |
| 198 } else { |
| 199 file_name.assign(line.FileName); |
| 200 } |
| 201 } |
| 202 } |
| 203 |
| 204 void CpuProfiler::ResolveModules() { |
| 205 ProcessStack(ui_thread_stack_, ui_thread_stack_depth_); |
| 206 ProcessStack(io_thread_stack_, io_thread_stack_depth_); |
| 207 } |
| 208 |
| 209 void CpuProfiler::ProcessStack(StackTraceEntry* stack, int stack_depth) { |
| 210 for (int i = 0; i < stack_depth; i++, stack++) { |
| 211 HMODULE module = stack->module; |
| 212 if (!module || (modules_.find(module) != modules_.end())) |
| 213 continue; |
| 214 |
| 215 wchar_t module_name[MAX_PATH]; |
| 216 if (GetModuleFileName(module, module_name, arraysize(module_name))) { |
| 217 modules_.insert(std::pair<HMODULE, base::string16>(module, module_name)); |
| 218 } |
| 219 } |
| 220 } |
| 221 |
| 222 } |
OLD | NEW |