1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
// Copyright (c) 2010 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 "chrome/browser/memory_details.h"
#include <psapi.h>
#include "app/l10n_util.h"
#include "base/file_version_info.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/browser_child_process_host.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/renderer_host/backing_store_manager.h"
#include "chrome/browser/renderer_host/render_process_host.h"
#include "chrome/browser/tab_contents/navigation_entry.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/url_constants.h"
#include "grit/chromium_strings.h"
// Known browsers which we collect details for.
enum {
CHROME_BROWSER = 0,
CHROME_NACL_PROCESS,
IE_BROWSER,
FIREFOX_BROWSER,
OPERA_BROWSER,
SAFARI_BROWSER,
IE_64BIT_BROWSER,
KONQUEROR_BROWSER,
MAX_BROWSERS
} BrowserProcess;
// Template of static data we use for finding browser process information.
// These entries must match the ordering for MemoryDetails::BrowserProcess.
static ProcessData g_process_template[MAX_BROWSERS];
MemoryDetails::MemoryDetails() {
static const std::wstring google_browser_name =
l10n_util::GetString(IDS_PRODUCT_NAME);
ProcessData g_process_template[MAX_BROWSERS] = {
{ google_browser_name.c_str(), L"chrome.exe", },
{ google_browser_name.c_str(), L"nacl64.exe", },
{ L"IE", L"iexplore.exe", },
{ L"Firefox", L"firefox.exe", },
{ L"Opera", L"opera.exe", },
{ L"Safari", L"safari.exe", },
{ L"IE (64bit)", L"iexplore.exe", },
{ L"Konqueror", L"konqueror.exe", },
};
for (int index = 0; index < arraysize(g_process_template); ++index) {
ProcessData process;
process.name = g_process_template[index].name;
process.process_name = g_process_template[index].process_name;
process_data_.push_back(process);
}
}
ProcessData* MemoryDetails::ChromeBrowser() {
return &process_data_[CHROME_BROWSER];
}
void MemoryDetails::CollectProcessData(
std::vector<ProcessMemoryInformation> child_info) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
// Clear old data.
for (int index = 0; index < arraysize(g_process_template); index++)
process_data_[index].processes.clear();
SYSTEM_INFO system_info;
GetNativeSystemInfo(&system_info);
bool is_64bit_os =
system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64;
ScopedHandle snapshot(::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
PROCESSENTRY32 process_entry = {sizeof(PROCESSENTRY32)};
if (!snapshot.Get()) {
LOG(ERROR) << "CreateToolhelp32Snaphot failed: " << GetLastError();
return;
}
if (!::Process32First(snapshot, &process_entry)) {
LOG(ERROR) << "Process32First failed: " << GetLastError();
return;
}
do {
base::ProcessId pid = process_entry.th32ProcessID;
ScopedHandle handle(::OpenProcess(
PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid));
if (!handle.Get())
continue;
bool is_64bit_process = false;
// IsWow64Process() returns FALSE for a 32bit process on a 32bit OS.
// We need to check if the real OS is 64bit.
if (is_64bit_os) {
BOOL is_wow64 = FALSE;
// IsWow64Process() is supported by Windows XP SP2 or later.
IsWow64Process(handle, &is_wow64);
is_64bit_process = !is_wow64;
}
for (int index2 = 0; index2 < arraysize(g_process_template); index2++) {
if (_wcsicmp(process_data_[index2].process_name.c_str(),
process_entry.szExeFile) != 0)
continue;
if (index2 == IE_BROWSER && is_64bit_process)
continue; // Should use IE_64BIT_BROWSER
// Get Memory Information.
ProcessMemoryInformation info;
info.pid = pid;
if (info.pid == GetCurrentProcessId())
info.type = ChildProcessInfo::BROWSER_PROCESS;
else
info.type = ChildProcessInfo::UNKNOWN_PROCESS;
scoped_ptr<base::ProcessMetrics> metrics;
metrics.reset(base::ProcessMetrics::CreateProcessMetrics(handle));
metrics->GetCommittedKBytes(&info.committed);
metrics->GetWorkingSetKBytes(&info.working_set);
// Get Version Information.
TCHAR name[MAX_PATH];
if (index2 == CHROME_BROWSER || index2 == CHROME_NACL_PROCESS) {
chrome::VersionInfo version_info;
if (version_info.is_valid())
info.version = ASCIIToWide(version_info.Version());
// Check if this is one of the child processes whose data we collected
// on the IO thread, and if so copy over that data.
for (size_t child = 0; child < child_info.size(); child++) {
if (child_info[child].pid != info.pid)
continue;
info.titles = child_info[child].titles;
info.type = child_info[child].type;
break;
}
} else if (GetModuleFileNameEx(handle, NULL, name, MAX_PATH - 1)) {
std::wstring str_name(name);
scoped_ptr<FileVersionInfo> version_info(
FileVersionInfo::CreateFileVersionInfo(str_name));
if (version_info != NULL) {
info.version = version_info->product_version();
info.product_name = version_info->product_name();
}
}
// Add the process info to our list.
if (index2 == CHROME_NACL_PROCESS) {
// Add NaCl processes to Chrome's list
process_data_[CHROME_BROWSER].processes.push_back(info);
} else {
process_data_[index2].processes.push_back(info);
}
break;
}
} while (::Process32Next(snapshot, &process_entry));
// Finally return to the browser thread.
ChromeThread::PostTask(
ChromeThread::UI, FROM_HERE,
NewRunnableMethod(this, &MemoryDetails::CollectChildInfoOnUIThread));
}
|