| 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..1751db6b64b85870ea1e86182a031d000e17ccba
|
| --- /dev/null
|
| +++ b/base/profiler/cpu_profiler_win.cc
|
| @@ -0,0 +1,222 @@
|
| +// 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");
|
| + reinterpret_cast<void*&>(RtlVirtualUnwind) =
|
| + ::GetProcAddress(hNtDll, "RtlVirtualUnwind");
|
| + reinterpret_cast<void*&>(RtlLookupFunctionEntry) =
|
| + ::GetProcAddress(hNtDll, "RtlLookupFunctionEntry");
|
| + }
|
| +#endif
|
| +
|
| + //SymSetOptions(SYMOPT_UNDNAME | SYMOPT_LOAD_LINES | SYMOPT_DEFERRED_LOADS);
|
| + //if (!SymInitialize(GetCurrentProcess(), NULL, TRUE)) {
|
| + // LOG(ERROR) << "SymInitialize failed with error: " << GetLastError();
|
| + //}
|
| +}
|
| +
|
| +CpuProfiler::~CpuProfiler() {}
|
| +
|
| +// static
|
| +bool CpuProfiler::IsPlatformSupported() {
|
| +#ifdef _WIN64
|
| + bool supported = true;
|
| +#else
|
| + bool supported = false;
|
| +#endif
|
| + return supported;
|
| +}
|
| +
|
| +void CpuProfiler::OnTimer() {
|
| + if (!ui_thread_ || !io_thread_) {
|
| + GetThreadIds();
|
| + }
|
| +
|
| + 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) {
|
| + 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);
|
| +
|
| + 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) {
|
| + 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++) {
|
| + // 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,
|
| + reinterpret_cast<LPCTSTR >(context->Rip), &module)) {
|
| + stack_trace->module = module;
|
| + }
|
| +
|
| + KNONVOLATILE_CONTEXT_POINTERS nvcontext;
|
| + RtlZeroMemory(&nvcontext, sizeof(KNONVOLATILE_CONTEXT_POINTERS));
|
| + 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);
|
| + context->Rsp += 8;
|
| + }
|
| +
|
| + if (!context->Rip)
|
| + return i;
|
| + }
|
| + return stack_depth;
|
| +#else
|
| + return 0;
|
| +#endif
|
| +}
|
| +
|
| +void CpuProfiler::GetNames(StackTraceEntry* stack_trace, int stack_depth) {
|
| + 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));
|
| + }
|
| + }
|
| +}
|
| +
|
| +}
|
|
|