Chromium Code Reviews| Index: base/profiler/cpu_profiler_win.cc |
| diff --git a/base/profiler/cpu_profiler_win.cc b/base/profiler/cpu_profiler_win.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1ef7aa3964f811b2196c36756b7b7ea4e21ba401 |
| --- /dev/null |
| +++ b/base/profiler/cpu_profiler_win.cc |
| @@ -0,0 +1,221 @@ |
| +// Copyright 2015 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 "base/profiler/cpu_profiler.h" |
| + |
| +#include <Tlhelp32.h> |
| +#include <dbghelp.h> |
| +#include <stddef.h> |
| +#include <windows.h> |
| + |
| +#include "base/debug/stack_trace.h" |
| +#include "base/threading/platform_thread.h" |
| +#include "base/threading/thread_id_name_manager.h" |
| +#include "base/time/time.h" |
| + |
| +namespace base { |
| + |
| +const DWORD kSuspendFailed = static_cast<DWORD>(-1); |
| + |
| +CpuProfiler::CpuProfiler() |
| + : ui_thread_(0), |
| + io_thread_(0), |
| + ui_thread_stack_depth_(0), |
| + io_thread_stack_depth_(0) { |
| +#ifdef _WIN64 |
| + // Initialiazation |
| + if (RtlVirtualUnwind == NULL && RtlLookupFunctionEntry == NULL) { |
| + const HMODULE hNtDll = ::GetModuleHandle(L"ntdll.dll"); |
|
cpu_(ooo_6.6-7.5)
2015/02/03 02:40:53
hNTDLL does not follow the naming conventions
Mike Wittman
2015/02/04 01:37:10
Done.
|
| + reinterpret_cast<void*&>(RtlVirtualUnwind) = |
| + ::GetProcAddress(hNtDll, "RtlVirtualUnwind"); |
| + reinterpret_cast<void*&>(RtlLookupFunctionEntry) = |
| + ::GetProcAddress(hNtDll, "RtlLookupFunctionEntry"); |
| + } |
| +#endif |
| + |
| + //SymSetOptions(SYMOPT_UNDNAME | SYMOPT_LOAD_LINES | SYMOPT_DEFERRED_LOADS); |
|
cpu_(ooo_6.6-7.5)
2015/02/03 02:40:53
remove commented code.
Mike Wittman
2015/02/04 01:37:10
Done.
|
| + //if (!SymInitialize(GetCurrentProcess(), NULL, TRUE)) { |
| + // LOG(ERROR) << "SymInitialize failed with error: " << GetLastError(); |
| + //} |
| +} |
| + |
| +CpuProfiler::~CpuProfiler() {} |
| + |
| +bool CpuProfiler::IsPlatformSupported() { |
| +#ifdef _WIN64 |
| + bool supported = true; |
| +#else |
| + bool supported = false; |
| +#endif |
| + return supported; |
| +} |
| + |
| +void CpuProfiler::OnTimer() { |
| + if (!ui_thread_ || !io_thread_) { |
| + GetThreadIds(); |
| + } |
|
cpu_(ooo_6.6-7.5)
2015/02/03 02:40:53
racy, you should open the thread handles and keep
Mike Wittman
2015/02/04 01:37:10
Done.
|
| + |
| + ui_thread_stack_depth_ = SampleThread(ui_thread_, arraysize(ui_thread_stack_), |
| + ui_thread_stack_); |
| + io_thread_stack_depth_ = SampleThread(io_thread_, arraysize(io_thread_stack_), |
| + io_thread_stack_); |
| + ResolveModules(); |
| + |
| + modules_.clear(); |
| + ui_thread_stack_depth_ = 0; |
| + io_thread_stack_depth_ = 0; |
| +} |
| + |
| +int CpuProfiler::SampleThread(DWORD thread, int max_stack_size, |
| + StackTraceEntry* stack) { |
|
cpu_(ooo_6.6-7.5)
2015/02/03 02:40:53
this method does not need to be a member of the cl
Mike Wittman
2015/02/04 01:37:09
Done.
|
| + HANDLE thread_handle = ::OpenThread(THREAD_GET_CONTEXT | |
| + THREAD_QUERY_INFORMATION | THREAD_SUSPEND_RESUME, FALSE, thread); |
| + if (!thread_handle) |
| + return 0; |
| + |
| + if (::SuspendThread(thread_handle) == kSuspendFailed) { |
| + LOG(ERROR) << "SuspendThread failed: " << GetLastError(); |
| + return 0; |
| + } |
| + |
| + CONTEXT thread_context; |
| + memset(&thread_context, 0, sizeof(thread_context)); |
| + thread_context.ContextFlags = CONTEXT_ALL; |
| + if (!::GetThreadContext(thread_handle, &thread_context)) { |
| + LOG(ERROR) << "GetThreadContext failed: " << GetLastError(); |
| + } |
| + |
| + //debug::StackTrace trace(&thread_context); |
|
cpu_(ooo_6.6-7.5)
2015/02/03 02:40:53
don't keep commented code. It is in or out.
Mike Wittman
2015/02/04 01:37:10
Done.
|
| + |
| + int stack_depth = StackTrace64(&thread_context, max_stack_size, stack); |
| + |
| + //GetNames(stack_trace, stack_depth); |
| + |
| + if (::ResumeThread(thread_handle) == kSuspendFailed) { |
| + LOG(ERROR) << "ResumeThread failed: " << GetLastError(); |
| + } |
| + ::CloseHandle(thread_handle); |
| + return stack_depth; |
| +} |
| + |
| +void CpuProfiler::GetThreadIds() { |
| + HANDLE snapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); |
| + if (INVALID_HANDLE_VALUE == snapshot) { |
| + LOG(ERROR) << "CreateToolhelp32Snapshot failed"; |
| + return; |
| + } |
| + |
| + base::ThreadIdNameManager* manager = base::ThreadIdNameManager::GetInstance(); |
| + DWORD current_process = GetCurrentProcessId(); |
| + THREADENTRY32 thread_entry = {0}; |
| + thread_entry.dwSize = sizeof(THREADENTRY32); |
| + BOOL result = ::Thread32First(snapshot, &thread_entry); |
| + while (result) { |
| + if (thread_entry.th32OwnerProcessID == current_process) { |
| + const char* name = manager->GetName(thread_entry.th32ThreadID); |
| + if (strcmp(name, "CrBrowserMain") == 0) { |
|
cpu_(ooo_6.6-7.5)
2015/02/03 02:40:53
I am afraid that this "CrBrowserMain" knowledge is
Mike Wittman
2015/02/04 01:37:09
Changed this to just collect stacks from the main
|
| + ui_thread_ = thread_entry.th32ThreadID; |
| + } |
| + if (strcmp(name, "Chrome_IOThread") == 0) { |
| + io_thread_ = thread_entry.th32ThreadID; |
| + } |
| + } |
| + result = Thread32Next(snapshot, &thread_entry); |
| + } |
| + CloseHandle(snapshot); |
| +} |
| + |
| +int CpuProfiler::StackTrace64(CONTEXT* context, int stack_depth, |
| + StackTraceEntry* stack_trace) { |
| +#ifdef _WIN64 |
| + IMAGEHLP_SYMBOL64 sym; |
| + sym.SizeOfStruct = sizeof(sym); |
| + sym.MaxNameLength = 0; |
| + |
| + for (int i = 0; i < stack_depth; i++, stack_trace++) { |
|
cpu_(ooo_6.6-7.5)
2015/02/03 02:40:53
for ( ... , ++stack_trace) is the idiomatic use of
Mike Wittman
2015/02/04 01:37:10
Done.
|
| + // Try to look up unwind metadata for the current function. |
| + ULONG64 image_base; |
| + PRUNTIME_FUNCTION runtime_function = |
| + RtlLookupFunctionEntry(context->Rip, &image_base, NULL); |
| + |
| + stack_trace->rsp = context->Rsp; |
| + stack_trace->rip = context->Rip; |
| + stack_trace->module = NULL; |
| + |
| + if (runtime_function) { |
| + HMODULE module = NULL; |
| + if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, |
|
cpu_(ooo_6.6-7.5)
2015/02/03 02:40:53
careful with side effects for example here GET_MOD
Mike Wittman
2015/02/04 01:37:10
Added a call to FreeLibrary, which should be suffi
|
| + reinterpret_cast<LPCTSTR >(context->Rip), &module)) { |
| + stack_trace->module = module; |
| + } |
| + |
| + KNONVOLATILE_CONTEXT_POINTERS nvcontext; |
| + RtlZeroMemory(&nvcontext, sizeof(KNONVOLATILE_CONTEXT_POINTERS)); |
|
cpu_(ooo_6.6-7.5)
2015/02/03 02:40:53
154 not needed, KNONVOLATILE_CONTEXT_POINTERS nvco
Mike Wittman
2015/02/04 01:37:10
Done.
|
| + PVOID handler_data; |
| + ULONG64 establisher_frame; |
| + RtlVirtualUnwind(0, image_base, context->Rip, runtime_function, context, |
| + &handler_data, &establisher_frame, &nvcontext); |
| + } else { |
| + // If we don't have a RUNTIME_FUNCTION, then we've encountered |
| + // a leaf function. Adjust the stack appropriately. |
| + context->Rip = (ULONG64)(*(PULONG64)context->Rsp); |
|
cpu_(ooo_6.6-7.5)
2015/02/03 02:40:53
don't use C style casts. In general this code has
Mike Wittman
2015/02/04 01:37:10
Done.
|
| + context->Rsp += 8; |
| + } |
| + |
| + if (!context->Rip) |
| + return i; |
| + } |
| + return stack_depth; |
| +#else |
| + return 0; |
| +#endif |
| +} |
| + |
| +void CpuProfiler::GetNames(StackTraceEntry* stack_trace, int stack_depth) { |
|
cpu_(ooo_6.6-7.5)
2015/02/03 02:40:53
seems also this one does not need to be part of th
Mike Wittman
2015/02/04 01:37:10
Done.
|
| + HANDLE process = GetCurrentProcess(); |
| + DWORD64 displacement = 0; |
| + DWORD line_displacement = 0; |
| + DWORD64 address = 0; |
| + char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(wchar_t)]; |
| + PSYMBOL_INFO symbol_info = (PSYMBOL_INFO)buffer; |
| + symbol_info->SizeOfStruct = sizeof(SYMBOL_INFO); |
| + symbol_info->MaxNameLen = MAX_SYM_NAME; |
| + std::string file_name; |
| + |
| + IMAGEHLP_LINE64 line; |
| + line.SizeOfStruct = sizeof(IMAGEHLP_LINE64); |
| + |
| + for (int i = 0; i < stack_depth; i++, stack_trace++) { |
| + address = stack_trace->rip; |
| + if (!SymFromAddr(process, address, &displacement, symbol_info)) { |
| + wcscpy(reinterpret_cast<wchar_t*>(symbol_info->Name), L"failed"); |
| + } |
| + if (!SymGetLineFromAddr64(process, address, &line_displacement, &line)) { |
| + line.LineNumber = 0; |
| + file_name.clear(); |
| + } else { |
| + file_name.assign(line.FileName); |
| + } |
| + } |
| +} |
| + |
| +void CpuProfiler::ResolveModules() { |
| + ProcessStack(ui_thread_stack_, ui_thread_stack_depth_); |
| + ProcessStack(io_thread_stack_, io_thread_stack_depth_); |
| +} |
| + |
| +void CpuProfiler::ProcessStack(StackTraceEntry* stack, int stack_depth) { |
| + for (int i = 0; i < stack_depth; i++, stack++) { |
| + HMODULE module = stack->module; |
| + if (!module || (modules_.find(module) != modules_.end())) |
| + continue; |
| + |
| + wchar_t module_name[MAX_PATH]; |
| + if (GetModuleFileName(module, module_name, arraysize(module_name))) { |
| + modules_.insert(std::pair<HMODULE, base::string16>(module, module_name)); |
| + } |
| + } |
| +} |
| + |
| +} |