OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 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 "chrome/test/perf/mem_usage.h" | |
6 | |
7 #include <windows.h> | |
8 #include <psapi.h> | |
9 | |
10 #include "base/file_path.h" | |
11 #include "base/path_service.h" | |
12 #include "base/process_util.h" | |
13 #include "base/scoped_ptr.h" | |
14 #include "chrome/common/chrome_constants.h" | |
15 #include "chrome/common/chrome_paths.h" | |
16 #include "chrome/test/chrome_process_util.h" | |
17 | |
18 // GetPerformanceInfo is not available on WIN2K. So we'll | |
19 // load it on-the-fly. | |
20 const wchar_t kPsapiDllName[] = L"psapi.dll"; | |
21 typedef BOOL (WINAPI *GetPerformanceInfoFunction) ( | |
22 PPERFORMANCE_INFORMATION pPerformanceInformation, | |
23 DWORD cb); | |
24 | |
25 static BOOL InternalGetPerformanceInfo( | |
26 PPERFORMANCE_INFORMATION pPerformanceInformation, DWORD cb) { | |
27 static GetPerformanceInfoFunction GetPerformanceInfo_func = NULL; | |
28 if (!GetPerformanceInfo_func) { | |
29 HMODULE psapi_dll = ::GetModuleHandle(kPsapiDllName); | |
30 if (psapi_dll) | |
31 GetPerformanceInfo_func = reinterpret_cast<GetPerformanceInfoFunction>( | |
32 GetProcAddress(psapi_dll, "GetPerformanceInfo")); | |
33 | |
34 if (!GetPerformanceInfo_func) { | |
35 // The function could be loaded! | |
36 memset(pPerformanceInformation, 0, cb); | |
37 return FALSE; | |
38 } | |
39 } | |
40 return GetPerformanceInfo_func(pPerformanceInformation, cb); | |
41 } | |
42 | |
43 | |
44 size_t GetSystemCommitCharge() { | |
45 // Get the System Page Size. | |
46 SYSTEM_INFO system_info; | |
47 GetSystemInfo(&system_info); | |
48 | |
49 PERFORMANCE_INFORMATION info; | |
50 if (InternalGetPerformanceInfo(&info, sizeof(info))) | |
51 return info.CommitTotal * system_info.dwPageSize; | |
52 return -1; | |
53 } | |
54 | |
55 void PrintChromeMemoryUsageInfo() { | |
56 printf("\n"); | |
57 | |
58 FilePath data_dir; | |
59 PathService::Get(chrome::DIR_USER_DATA, &data_dir); | |
60 int browser_process_pid = ChromeBrowserProcessId(data_dir); | |
61 ChromeProcessList chrome_processes(GetRunningChromeProcesses(data_dir)); | |
62 | |
63 ChromeProcessList::const_iterator it; | |
64 for (it = chrome_processes.begin(); it != chrome_processes.end(); ++it) { | |
65 base::ProcessHandle process_handle; | |
66 if (!base::OpenPrivilegedProcessHandle(*it, &process_handle)) { | |
67 NOTREACHED(); | |
68 } | |
69 | |
70 // TODO(sgk): if/when base::ProcessMetrics can return real memory | |
71 // stats on mac, convert to: | |
72 // | |
73 // scoped_ptr<base::ProcessMetrics> process_metrics; | |
74 // process_metrics.reset( | |
75 // base::ProcessMetrics::CreateProcessMetrics(process_handle)); | |
76 scoped_ptr<ChromeTestProcessMetrics> process_metrics; | |
77 process_metrics.reset( | |
78 ChromeTestProcessMetrics::CreateProcessMetrics(process_handle)); | |
79 | |
80 size_t peak_virtual_size = process_metrics->GetPeakPagefileUsage(); | |
81 size_t current_virtual_size = process_metrics->GetPagefileUsage(); | |
82 size_t peak_working_set_size = process_metrics->GetPeakWorkingSetSize(); | |
83 size_t current_working_set_size = process_metrics->GetWorkingSetSize(); | |
84 | |
85 if (*it == browser_process_pid) { | |
86 wprintf(L"browser_vm_peak = %d\n", peak_virtual_size); | |
87 wprintf(L"browser_vm_current = %d\n", current_virtual_size); | |
88 wprintf(L"browser_ws_peak = %d\n", peak_working_set_size); | |
89 wprintf(L"browser_ws_final = %d\n", current_working_set_size); | |
90 } else { | |
91 wprintf(L"render_vm_peak = %d\n", peak_virtual_size); | |
92 wprintf(L"render_vm_current = %d\n", current_virtual_size); | |
93 wprintf(L"render_ws_peak = %d\n", peak_working_set_size); | |
94 wprintf(L"render_ws_final = %d\n", current_working_set_size); | |
95 } | |
96 }; | |
97 } | |
OLD | NEW |