[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 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/browser/memory_details.h" |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 6 | |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 7 | #include <psapi.h> |
| 8 | |
[email protected] | 24d6969 | 2011-10-21 18:26:51 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 10 | #include "base/file_path.h" |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 11 | #include "base/file_version_info.h" |
| 12 | #include "base/string_util.h" |
[email protected] | 0211f57e | 2010-08-27 20:28:42 | [diff] [blame] | 13 | #include "base/utf_string_conversions.h" |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 14 | #include "base/win/scoped_handle.h" |
[email protected] | 1e67c2b | 2011-03-04 01:17:37 | [diff] [blame] | 15 | #include "base/win/windows_version.h" |
[email protected] | 1eeb5e0 | 2010-07-20 23:02:11 | [diff] [blame] | 16 | #include "chrome/common/chrome_version_info.h" |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 17 | #include "chrome/common/url_constants.h" |
[email protected] | a01efd2 | 2011-03-01 00:38:32 | [diff] [blame] | 18 | #include "content/browser/browser_child_process_host.h" |
| 19 | #include "content/browser/browser_thread.h" |
| 20 | #include "content/browser/renderer_host/backing_store_manager.h" |
| 21 | #include "content/browser/renderer_host/render_process_host.h" |
| 22 | #include "content/browser/tab_contents/navigation_entry.h" |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 23 | #include "grit/chromium_strings.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 24 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 25 | |
| 26 | // Known browsers which we collect details for. |
| 27 | enum { |
| 28 | CHROME_BROWSER = 0, |
[email protected] | aef8d5ae | 2010-03-17 22:40:52 | [diff] [blame] | 29 | CHROME_NACL_PROCESS, |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 30 | IE_BROWSER, |
| 31 | FIREFOX_BROWSER, |
| 32 | OPERA_BROWSER, |
| 33 | SAFARI_BROWSER, |
| 34 | IE_64BIT_BROWSER, |
| 35 | KONQUEROR_BROWSER, |
| 36 | MAX_BROWSERS |
| 37 | } BrowserProcess; |
| 38 | |
[email protected] | 6fad263 | 2009-11-02 05:59:37 | [diff] [blame] | 39 | MemoryDetails::MemoryDetails() { |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 40 | static const std::wstring google_browser_name = |
[email protected] | 0f26d7b | 2011-01-05 19:10:44 | [diff] [blame] | 41 | UTF16ToWide(l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)); |
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 42 | struct { |
| 43 | const wchar_t* name; |
| 44 | const wchar_t* process_name; |
| 45 | } process_template[MAX_BROWSERS] = { |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 46 | { google_browser_name.c_str(), L"chrome.exe", }, |
[email protected] | aef8d5ae | 2010-03-17 22:40:52 | [diff] [blame] | 47 | { google_browser_name.c_str(), L"nacl64.exe", }, |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 48 | { L"IE", L"iexplore.exe", }, |
| 49 | { L"Firefox", L"firefox.exe", }, |
| 50 | { L"Opera", L"opera.exe", }, |
| 51 | { L"Safari", L"safari.exe", }, |
| 52 | { L"IE (64bit)", L"iexplore.exe", }, |
| 53 | { L"Konqueror", L"konqueror.exe", }, |
| 54 | }; |
| 55 | |
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 56 | for (int index = 0; index < MAX_BROWSERS; ++index) { |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 57 | ProcessData process; |
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 58 | process.name = process_template[index].name; |
| 59 | process.process_name = process_template[index].process_name; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 60 | process_data_.push_back(process); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | ProcessData* MemoryDetails::ChromeBrowser() { |
| 65 | return &process_data_[CHROME_BROWSER]; |
| 66 | } |
| 67 | |
| 68 | void MemoryDetails::CollectProcessData( |
[email protected] | 4df3ac6 | 2011-03-11 04:38:52 | [diff] [blame] | 69 | const std::vector<ProcessMemoryInformation>& child_info) { |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 70 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 71 | |
| 72 | // Clear old data. |
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 73 | for (unsigned int index = 0; index < process_data_.size(); index++) |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 74 | process_data_[index].processes.clear(); |
| 75 | |
[email protected] | f48122119 | 2011-04-07 22:15:34 | [diff] [blame] | 76 | base::win::OSInfo::WindowsArchitecture windows_architecture = |
| 77 | base::win::OSInfo::GetInstance()->architecture(); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 78 | |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 79 | base::win::ScopedHandle snapshot( |
| 80 | ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 81 | PROCESSENTRY32 process_entry = {sizeof(PROCESSENTRY32)}; |
| 82 | if (!snapshot.Get()) { |
| 83 | LOG(ERROR) << "CreateToolhelp32Snaphot failed: " << GetLastError(); |
| 84 | return; |
| 85 | } |
| 86 | if (!::Process32First(snapshot, &process_entry)) { |
| 87 | LOG(ERROR) << "Process32First failed: " << GetLastError(); |
| 88 | return; |
| 89 | } |
| 90 | do { |
[email protected] | a4dc33f | 2009-10-20 15:09:55 | [diff] [blame] | 91 | base::ProcessId pid = process_entry.th32ProcessID; |
[email protected] | 1e67c2b | 2011-03-04 01:17:37 | [diff] [blame] | 92 | base::win::ScopedHandle process_handle(::OpenProcess( |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 93 | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid)); |
[email protected] | 1e67c2b | 2011-03-04 01:17:37 | [diff] [blame] | 94 | if (!process_handle.Get()) |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 95 | continue; |
[email protected] | af67f20 | 2011-03-04 19:12:29 | [diff] [blame] | 96 | bool is_64bit_process = |
[email protected] | f48122119 | 2011-04-07 22:15:34 | [diff] [blame] | 97 | ((windows_architecture == base::win::OSInfo::X64_ARCHITECTURE) || |
| 98 | (windows_architecture == base::win::OSInfo::IA64_ARCHITECTURE)) && |
| 99 | (base::win::OSInfo::GetWOW64StatusForProcess(process_handle) == |
| 100 | base::win::OSInfo::WOW64_DISABLED); |
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 101 | for (unsigned int index2 = 0; index2 < process_data_.size(); index2++) { |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 102 | if (_wcsicmp(process_data_[index2].process_name.c_str(), |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 103 | process_entry.szExeFile) != 0) |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 104 | continue; |
| 105 | if (index2 == IE_BROWSER && is_64bit_process) |
| 106 | continue; // Should use IE_64BIT_BROWSER |
| 107 | // Get Memory Information. |
| 108 | ProcessMemoryInformation info; |
| 109 | info.pid = pid; |
| 110 | if (info.pid == GetCurrentProcessId()) |
| 111 | info.type = ChildProcessInfo::BROWSER_PROCESS; |
| 112 | else |
| 113 | info.type = ChildProcessInfo::UNKNOWN_PROCESS; |
| 114 | |
| 115 | scoped_ptr<base::ProcessMetrics> metrics; |
[email protected] | 1e67c2b | 2011-03-04 01:17:37 | [diff] [blame] | 116 | metrics.reset(base::ProcessMetrics::CreateProcessMetrics(process_handle)); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 117 | metrics->GetCommittedKBytes(&info.committed); |
| 118 | metrics->GetWorkingSetKBytes(&info.working_set); |
| 119 | |
| 120 | // Get Version Information. |
| 121 | TCHAR name[MAX_PATH]; |
[email protected] | aef8d5ae | 2010-03-17 22:40:52 | [diff] [blame] | 122 | if (index2 == CHROME_BROWSER || index2 == CHROME_NACL_PROCESS) { |
[email protected] | 0211f57e | 2010-08-27 20:28:42 | [diff] [blame] | 123 | chrome::VersionInfo version_info; |
[email protected] | 30c9180 | 2010-12-18 00:40:17 | [diff] [blame] | 124 | if (version_info.is_valid()) |
| 125 | info.version = ASCIIToWide(version_info.Version()); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 126 | // Check if this is one of the child processes whose data we collected |
| 127 | // on the IO thread, and if so copy over that data. |
| 128 | for (size_t child = 0; child < child_info.size(); child++) { |
| 129 | if (child_info[child].pid != info.pid) |
| 130 | continue; |
| 131 | info.titles = child_info[child].titles; |
| 132 | info.type = child_info[child].type; |
| 133 | break; |
| 134 | } |
[email protected] | 1e67c2b | 2011-03-04 01:17:37 | [diff] [blame] | 135 | } else if (GetModuleFileNameEx(process_handle, NULL, name, |
| 136 | MAX_PATH - 1)) { |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 137 | std::wstring str_name(name); |
| 138 | scoped_ptr<FileVersionInfo> version_info( |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 139 | FileVersionInfo::CreateFileVersionInfo(FilePath(str_name))); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 140 | if (version_info != NULL) { |
| 141 | info.version = version_info->product_version(); |
| 142 | info.product_name = version_info->product_name(); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // Add the process info to our list. |
[email protected] | aef8d5ae | 2010-03-17 22:40:52 | [diff] [blame] | 147 | if (index2 == CHROME_NACL_PROCESS) { |
| 148 | // Add NaCl processes to Chrome's list |
| 149 | process_data_[CHROME_BROWSER].processes.push_back(info); |
| 150 | } else { |
| 151 | process_data_[index2].processes.push_back(info); |
| 152 | } |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 153 | break; |
| 154 | } |
| 155 | } while (::Process32Next(snapshot, &process_entry)); |
| 156 | |
| 157 | // Finally return to the browser thread. |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 158 | BrowserThread::PostTask( |
| 159 | BrowserThread::UI, FROM_HERE, |
[email protected] | 24d6969 | 2011-10-21 18:26:51 | [diff] [blame] | 160 | base::Bind(&MemoryDetails::CollectChildInfoOnUIThread, this)); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 161 | } |