[email protected] | 4306df7 | 2012-04-20 18:58:57 | [diff] [blame] | 1 | // Copyright (c) 2012 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> |
[email protected] | d09a4ce1c | 2013-07-24 17:37:02 | [diff] [blame] | 8 | #include <TlHelp32.h> |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 9 | |
[email protected] | 24d6969 | 2011-10-21 18:26:51 | [diff] [blame] | 10 | #include "base/bind.h" |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 11 | #include "base/file_version_info.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
asvitkine | 58409e4c | 2015-01-15 01:25:45 | [diff] [blame] | 13 | #include "base/path_service.h" |
[email protected] | f9b29436 | 2013-06-10 20:22:31 | [diff] [blame] | 14 | #include "base/strings/string_util.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 15 | #include "base/strings/utf_string_conversions.h" |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 16 | #include "base/win/scoped_handle.h" |
[email protected] | 1e67c2b | 2011-03-04 01:17:37 | [diff] [blame] | 17 | #include "base/win/windows_version.h" |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 18 | #include "chrome/common/url_constants.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 19 | #include "chrome/grit/chromium_strings.h" |
sdefresne | 9fb6769 | 2015-08-03 18:48:22 | [diff] [blame^] | 20 | #include "components/version_info/version_info.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 21 | #include "content/public/browser/browser_thread.h" |
[email protected] | bd5d6cf | 2011-12-01 00:39:12 | [diff] [blame] | 22 | #include "content/public/common/process_type.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 23 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 24 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 25 | using content::BrowserThread; |
| 26 | |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 27 | // Known browsers which we collect details for. |
mgiuca | 88867d3 | 2015-07-08 04:08:24 | [diff] [blame] | 28 | enum BrowserProcess { |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 29 | CHROME_BROWSER = 0, |
[email protected] | aef8d5ae | 2010-03-17 22:40:52 | [diff] [blame] | 30 | CHROME_NACL_PROCESS, |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 31 | IE_BROWSER, |
| 32 | FIREFOX_BROWSER, |
| 33 | OPERA_BROWSER, |
| 34 | SAFARI_BROWSER, |
| 35 | IE_64BIT_BROWSER, |
| 36 | KONQUEROR_BROWSER, |
| 37 | MAX_BROWSERS |
mgiuca | 88867d3 | 2015-07-08 04:08:24 | [diff] [blame] | 38 | }; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 39 | |
asvitkine | 89406d1f | 2015-01-17 06:57:10 | [diff] [blame] | 40 | MemoryDetails::MemoryDetails() { |
asvitkine | 58409e4c | 2015-01-15 01:25:45 | [diff] [blame] | 41 | base::FilePath browser_process_path; |
| 42 | PathService::Get(base::FILE_EXE, &browser_process_path); |
| 43 | const base::string16 browser_process_name = |
| 44 | browser_process_path.BaseName().value(); |
| 45 | const base::string16 google_browser_name = |
[email protected] | 299e3d00 | 2014-06-03 08:46:49 | [diff] [blame] | 46 | l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); |
asvitkine | 58409e4c | 2015-01-15 01:25:45 | [diff] [blame] | 47 | |
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 48 | struct { |
| 49 | const wchar_t* name; |
| 50 | const wchar_t* process_name; |
| 51 | } process_template[MAX_BROWSERS] = { |
asvitkine | 58409e4c | 2015-01-15 01:25:45 | [diff] [blame] | 52 | { google_browser_name.c_str(), browser_process_name.c_str(), }, |
[email protected] | aef8d5ae | 2010-03-17 22:40:52 | [diff] [blame] | 53 | { google_browser_name.c_str(), L"nacl64.exe", }, |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 54 | { L"IE", L"iexplore.exe", }, |
| 55 | { L"Firefox", L"firefox.exe", }, |
| 56 | { L"Opera", L"opera.exe", }, |
| 57 | { L"Safari", L"safari.exe", }, |
| 58 | { L"IE (64bit)", L"iexplore.exe", }, |
| 59 | { L"Konqueror", L"konqueror.exe", }, |
| 60 | }; |
| 61 | |
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 62 | for (int index = 0; index < MAX_BROWSERS; ++index) { |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 63 | ProcessData process; |
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 64 | process.name = process_template[index].name; |
| 65 | process.process_name = process_template[index].process_name; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 66 | process_data_.push_back(process); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | ProcessData* MemoryDetails::ChromeBrowser() { |
| 71 | return &process_data_[CHROME_BROWSER]; |
| 72 | } |
| 73 | |
| 74 | void MemoryDetails::CollectProcessData( |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 75 | CollectionMode mode, |
[email protected] | 4df3ac6 | 2011-03-11 04:38:52 | [diff] [blame] | 76 | const std::vector<ProcessMemoryInformation>& child_info) { |
hashimoto | a8ea28d | 2015-04-11 02:50:48 | [diff] [blame] | 77 | DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 78 | |
| 79 | // Clear old data. |
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 80 | for (unsigned int index = 0; index < process_data_.size(); index++) |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 81 | process_data_[index].processes.clear(); |
| 82 | |
[email protected] | f48122119 | 2011-04-07 22:15:34 | [diff] [blame] | 83 | base::win::OSInfo::WindowsArchitecture windows_architecture = |
| 84 | base::win::OSInfo::GetInstance()->architecture(); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 85 | |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 86 | base::win::ScopedHandle snapshot( |
| 87 | ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 88 | PROCESSENTRY32 process_entry = {sizeof(PROCESSENTRY32)}; |
| 89 | if (!snapshot.Get()) { |
| 90 | LOG(ERROR) << "CreateToolhelp32Snaphot failed: " << GetLastError(); |
| 91 | return; |
| 92 | } |
rvargas | d7743ba | 2014-09-25 01:35:28 | [diff] [blame] | 93 | if (!::Process32First(snapshot.Get(), &process_entry)) { |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 94 | LOG(ERROR) << "Process32First failed: " << GetLastError(); |
| 95 | return; |
| 96 | } |
| 97 | do { |
[email protected] | a4dc33f | 2009-10-20 15:09:55 | [diff] [blame] | 98 | base::ProcessId pid = process_entry.th32ProcessID; |
[email protected] | 1e67c2b | 2011-03-04 01:17:37 | [diff] [blame] | 99 | base::win::ScopedHandle process_handle(::OpenProcess( |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 100 | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid)); |
rvargas | d7743ba | 2014-09-25 01:35:28 | [diff] [blame] | 101 | if (!process_handle.IsValid()) |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 102 | continue; |
[email protected] | af67f20 | 2011-03-04 19:12:29 | [diff] [blame] | 103 | bool is_64bit_process = |
[email protected] | f48122119 | 2011-04-07 22:15:34 | [diff] [blame] | 104 | ((windows_architecture == base::win::OSInfo::X64_ARCHITECTURE) || |
| 105 | (windows_architecture == base::win::OSInfo::IA64_ARCHITECTURE)) && |
rvargas | d7743ba | 2014-09-25 01:35:28 | [diff] [blame] | 106 | (base::win::OSInfo::GetWOW64StatusForProcess(process_handle.Get()) == |
[email protected] | f48122119 | 2011-04-07 22:15:34 | [diff] [blame] | 107 | base::win::OSInfo::WOW64_DISABLED); |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 108 | const size_t browser_list_size = |
| 109 | (mode == FROM_CHROME_ONLY ? 1 : process_data_.size()); |
| 110 | for (size_t index2 = 0; index2 < browser_list_size; ++index2) { |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 111 | if (_wcsicmp(process_data_[index2].process_name.c_str(), |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 112 | process_entry.szExeFile) != 0) |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 113 | continue; |
| 114 | if (index2 == IE_BROWSER && is_64bit_process) |
| 115 | continue; // Should use IE_64BIT_BROWSER |
| 116 | // Get Memory Information. |
| 117 | ProcessMemoryInformation info; |
| 118 | info.pid = pid; |
| 119 | if (info.pid == GetCurrentProcessId()) |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 120 | info.process_type = content::PROCESS_TYPE_BROWSER; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 121 | else |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 122 | info.process_type = content::PROCESS_TYPE_UNKNOWN; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 123 | |
| 124 | scoped_ptr<base::ProcessMetrics> metrics; |
rvargas | d7743ba | 2014-09-25 01:35:28 | [diff] [blame] | 125 | metrics.reset(base::ProcessMetrics::CreateProcessMetrics( |
| 126 | process_handle.Get())); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 127 | metrics->GetCommittedKBytes(&info.committed); |
| 128 | metrics->GetWorkingSetKBytes(&info.working_set); |
| 129 | |
| 130 | // Get Version Information. |
| 131 | TCHAR name[MAX_PATH]; |
[email protected] | aef8d5ae | 2010-03-17 22:40:52 | [diff] [blame] | 132 | if (index2 == CHROME_BROWSER || index2 == CHROME_NACL_PROCESS) { |
sdefresne | 9fb6769 | 2015-08-03 18:48:22 | [diff] [blame^] | 133 | info.version = base::ASCIIToUTF16(version_info::GetVersionNumber()); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 134 | // Check if this is one of the child processes whose data we collected |
| 135 | // on the IO thread, and if so copy over that data. |
| 136 | for (size_t child = 0; child < child_info.size(); child++) { |
| 137 | if (child_info[child].pid != info.pid) |
| 138 | continue; |
| 139 | info.titles = child_info[child].titles; |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 140 | info.process_type = child_info[child].process_type; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 141 | break; |
| 142 | } |
rvargas | d7743ba | 2014-09-25 01:35:28 | [diff] [blame] | 143 | } else if (GetModuleFileNameEx(process_handle.Get(), NULL, name, |
[email protected] | 1e67c2b | 2011-03-04 01:17:37 | [diff] [blame] | 144 | MAX_PATH - 1)) { |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 145 | std::wstring str_name(name); |
| 146 | scoped_ptr<FileVersionInfo> version_info( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 147 | FileVersionInfo::CreateFileVersionInfo(base::FilePath(str_name))); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 148 | if (version_info != NULL) { |
| 149 | info.version = version_info->product_version(); |
| 150 | info.product_name = version_info->product_name(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // Add the process info to our list. |
[email protected] | aef8d5ae | 2010-03-17 22:40:52 | [diff] [blame] | 155 | if (index2 == CHROME_NACL_PROCESS) { |
| 156 | // Add NaCl processes to Chrome's list |
| 157 | process_data_[CHROME_BROWSER].processes.push_back(info); |
| 158 | } else { |
| 159 | process_data_[index2].processes.push_back(info); |
| 160 | } |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 161 | break; |
| 162 | } |
rvargas | d7743ba | 2014-09-25 01:35:28 | [diff] [blame] | 163 | } while (::Process32Next(snapshot.Get(), &process_entry)); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 164 | |
| 165 | // Finally return to the browser thread. |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 166 | BrowserThread::PostTask( |
| 167 | BrowserThread::UI, FROM_HERE, |
[email protected] | 24d6969 | 2011-10-21 18:26:51 | [diff] [blame] | 168 | base::Bind(&MemoryDetails::CollectChildInfoOnUIThread, this)); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 169 | } |