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

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

Powered by Google App Engine
This is Rietveld 408576698