Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(720)

Side by Side Diff: base/profiler/cpu_profiler_win.cc

Issue 888923004: Temporary commit to evaluate perf impact of prototype CPU profiler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkcr
Patch Set: address comments Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/profiler/cpu_profiler_posix.cc ('k') | chrome/browser/chrome_browser_main.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 namespace {
20
21 const DWORD kSuspendFailed = static_cast<DWORD>(-1);
22
23 int StackTrace64(CONTEXT* context, int max_stack_size,
24 StackTraceEntry* stack_trace,
25 bool* last_frame_is_unknown_function) {
26 #ifdef _WIN64
27 *last_frame_is_unknown_function = false;
28
29 IMAGEHLP_SYMBOL64 sym;
30 sym.SizeOfStruct = sizeof(sym);
31 sym.MaxNameLength = 0;
32
33 for (int i = 0; i < max_stack_size; ++i, ++stack_trace) {
34 // Try to look up unwind metadata for the current function.
35 ULONG64 image_base;
36 PRUNTIME_FUNCTION runtime_function =
37 RtlLookupFunctionEntry(context->Rip, &image_base, nullptr);
38
39 stack_trace->rsp = context->Rsp;
40 stack_trace->rip = context->Rip;
41 stack_trace->module = nullptr;
42
43 if (runtime_function) {
44 KNONVOLATILE_CONTEXT_POINTERS nvcontext = {0};
45 void* handler_data;
46 ULONG64 establisher_frame;
47 RtlVirtualUnwind(0, image_base, context->Rip, runtime_function, context,
48 &handler_data, &establisher_frame, &nvcontext);
49 } else {
50 // If we don't have a RUNTIME_FUNCTION, then we've encountered
51 // a leaf function. Adjust the stack appropriately.
52 context->Rip = *reinterpret_cast<PDWORD64>(context->Rsp);
53 context->Rsp += 8;
54 *last_frame_is_unknown_function = true;
55 }
56
57 if (!context->Rip)
58 return i;
59 }
60 return max_stack_size;
61 #else
62 return 0;
63 #endif
64 }
65
66 // Look up modules from instruction pointers.
67 void FindModulesForIPs(StackTraceEntry* stack_trace, int stack_depth,
68 bool last_frame_is_unknown_function) {
69 const int module_frames = last_frame_is_unknown_function ? stack_depth - 1 :
70 stack_depth;
71 for (int i = 0; i < module_frames; ++i) {
72 HMODULE module = NULL;
73 if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
74 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
75 reinterpret_cast<LPCTSTR>(stack_trace[i].rip),
76 &module)) {
77 stack_trace->module = module;
78 }
79 }
80 }
81
82 int SampleThread(HANDLE thread_handle, int max_stack_size,
83 StackTraceEntry* stack) {
84 if (::SuspendThread(thread_handle) == kSuspendFailed) {
85 LOG(ERROR) << "SuspendThread failed: " << GetLastError();
86 return 0;
87 }
88
89 CONTEXT thread_context = {0};
90 thread_context.ContextFlags = CONTEXT_ALL;
91 if (!::GetThreadContext(thread_handle, &thread_context)) {
92 LOG(ERROR) << "GetThreadContext failed: " << GetLastError();
93 }
94
95 bool last_frame_is_unknown_function = false;
96 int stack_depth = StackTrace64(&thread_context, max_stack_size, stack,
97 &last_frame_is_unknown_function);
98
99 if (::ResumeThread(thread_handle) == kSuspendFailed) {
100 LOG(ERROR) << "ResumeThread failed: " << GetLastError();
101 }
102
103 FindModulesForIPs(stack, stack_depth, last_frame_is_unknown_function);
104
105 return stack_depth;
106 }
107
108 void GetNames(StackTraceEntry* stack_trace, int stack_depth) {
109 HANDLE process = GetCurrentProcess();
110 DWORD64 displacement = 0;
111 DWORD line_displacement = 0;
112 char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(wchar_t)];
113 SYMBOL_INFO* symbol_info = reinterpret_cast<SYMBOL_INFO*>(buffer);
114 symbol_info->SizeOfStruct = sizeof(SYMBOL_INFO);
115 symbol_info->MaxNameLen = MAX_SYM_NAME;
116 std::string file_name;
117
118 IMAGEHLP_LINE64 line;
119 line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
120
121 for (int i = 0; i < stack_depth; ++i, ++stack_trace) {
122 DWORD64 address = stack_trace->rip;
123 if (!SymFromAddr(process, address, &displacement, symbol_info)) {
124 wcscpy_s(reinterpret_cast<wchar_t*>(symbol_info->Name),
125 symbol_info->MaxNameLen, L"failed");
126 }
127 if (!SymGetLineFromAddr64(process, address, &line_displacement, &line)) {
128 line.LineNumber = 0;
129 file_name.clear();
130 } else {
131 file_name.assign(line.FileName);
132 }
133 }
134 }
135
136 }
137
138 CpuProfiler::CpuProfiler()
139 : main_thread_(0),
140 main_thread_stack_depth_(0) {
141 #ifdef _WIN64
142 // Record the main thread's handle.
143 BOOL result = ::DuplicateHandle(::GetCurrentProcess(), ::GetCurrentThread(),
144 ::GetCurrentProcess(), &main_thread_, 0,
145 FALSE, DUPLICATE_SAME_ACCESS);
146 DCHECK(result) << "DuplicateHandle failed";
147
148 // Initialization
149 if (RtlVirtualUnwind == NULL && RtlLookupFunctionEntry == NULL) {
150 const HMODULE nt_dll_handle = ::GetModuleHandle(L"ntdll.dll");
151 reinterpret_cast<void*&>(RtlVirtualUnwind) =
152 ::GetProcAddress(nt_dll_handle, "RtlVirtualUnwind");
153 reinterpret_cast<void*&>(RtlLookupFunctionEntry) =
154 ::GetProcAddress(nt_dll_handle, "RtlLookupFunctionEntry");
155 }
156 #endif
157 }
158
159 CpuProfiler::~CpuProfiler() {
160 #ifdef _WIN64
161 CloseHandle(main_thread_);
162 #endif
163 }
164
165 // static
166 bool CpuProfiler::IsPlatformSupported() {
167 #ifdef _WIN64
168 return true;
169 #else
170 return false;
171 #endif
172 }
173
174 void CpuProfiler::OnTimer() {
175 main_thread_stack_depth_ = SampleThread(
176 main_thread_, arraysize(main_thread_stack_), main_thread_stack_);
177 ProcessStack(main_thread_stack_, main_thread_stack_depth_);
178
179 modules_.clear();
180 main_thread_stack_depth_ = 0;
181 }
182
183 void CpuProfiler::ProcessStack(StackTraceEntry* stack, int stack_depth) {
184 for (int i = 0; i < stack_depth; ++i, ++stack) {
185 HMODULE module = stack->module;
186 if (!module || (modules_.find(module) != modules_.end()))
187 continue;
188
189 wchar_t module_name[MAX_PATH];
190 if (GetModuleFileName(module, module_name, arraysize(module_name))) {
191 modules_.insert(std::pair<HMODULE, base::string16>(module, module_name));
192 }
193 }
194 }
195
196 } // namespace base
OLDNEW
« no previous file with comments | « base/profiler/cpu_profiler_posix.cc ('k') | chrome/browser/chrome_browser_main.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698