| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/memory_details.h" | 5 #include "chrome/browser/memory_details.h" |
| 6 | 6 |
| 7 #include <psapi.h> | 7 #include <psapi.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <TlHelp32.h> | 9 #include <TlHelp32.h> |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "components/version_info/version_info.h" | 22 #include "components/version_info/version_info.h" |
| 23 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/common/process_type.h" | 24 #include "content/public/common/process_type.h" |
| 25 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
| 26 | 26 |
| 27 using content::BrowserThread; | 27 using content::BrowserThread; |
| 28 | 28 |
| 29 // Known browsers which we collect details for. | 29 // Known browsers which we collect details for. |
| 30 enum BrowserProcess { | 30 enum BrowserProcess { |
| 31 CHROME_BROWSER = 0, | 31 CHROME_BROWSER = 0, |
| 32 CHROME_NACL_PROCESS, | |
| 33 IE_BROWSER, | |
| 34 FIREFOX_BROWSER, | |
| 35 OPERA_BROWSER, | |
| 36 SAFARI_BROWSER, | |
| 37 IE_64BIT_BROWSER, | |
| 38 KONQUEROR_BROWSER, | |
| 39 MAX_BROWSERS | |
| 40 }; | 32 }; |
| 41 | 33 |
| 42 MemoryDetails::MemoryDetails() { | 34 MemoryDetails::MemoryDetails() { |
| 43 base::FilePath browser_process_path; | 35 base::FilePath browser_process_path; |
| 44 PathService::Get(base::FILE_EXE, &browser_process_path); | 36 PathService::Get(base::FILE_EXE, &browser_process_path); |
| 45 const base::string16 browser_process_name = | |
| 46 browser_process_path.BaseName().value(); | |
| 47 const base::string16 google_browser_name = | |
| 48 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); | |
| 49 | 37 |
| 50 struct { | 38 ProcessData process; |
| 51 const wchar_t* name; | 39 process.name = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); |
| 52 const wchar_t* process_name; | 40 process.process_name = browser_process_path.BaseName().value(); |
| 53 } process_template[MAX_BROWSERS] = { | 41 process_data_.push_back(process); |
| 54 { google_browser_name.c_str(), browser_process_name.c_str(), }, | |
| 55 { google_browser_name.c_str(), L"nacl64.exe", }, | |
| 56 { L"IE", L"iexplore.exe", }, | |
| 57 { L"Firefox", L"firefox.exe", }, | |
| 58 { L"Opera", L"opera.exe", }, | |
| 59 { L"Safari", L"safari.exe", }, | |
| 60 { L"IE (64bit)", L"iexplore.exe", }, | |
| 61 { L"Konqueror", L"konqueror.exe", }, | |
| 62 }; | |
| 63 | |
| 64 for (int index = 0; index < MAX_BROWSERS; ++index) { | |
| 65 ProcessData process; | |
| 66 process.name = process_template[index].name; | |
| 67 process.process_name = process_template[index].process_name; | |
| 68 process_data_.push_back(process); | |
| 69 } | |
| 70 } | 42 } |
| 71 | 43 |
| 72 ProcessData* MemoryDetails::ChromeBrowser() { | 44 ProcessData* MemoryDetails::ChromeBrowser() { |
| 73 return &process_data_[CHROME_BROWSER]; | 45 return &process_data_[0]; |
| 74 } | 46 } |
| 75 | 47 |
| 76 void MemoryDetails::CollectProcessData( | 48 void MemoryDetails::CollectProcessData( |
| 77 CollectionMode mode, | |
| 78 const std::vector<ProcessMemoryInformation>& child_info) { | 49 const std::vector<ProcessMemoryInformation>& child_info) { |
| 79 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 50 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 80 | 51 |
| 81 // Clear old data. | 52 // Clear old data. |
| 82 for (unsigned int index = 0; index < process_data_.size(); index++) | 53 process_data_[0].processes.clear(); |
| 83 process_data_[index].processes.clear(); | |
| 84 | |
| 85 base::win::OSInfo::WindowsArchitecture windows_architecture = | |
| 86 base::win::OSInfo::GetInstance()->architecture(); | |
| 87 | 54 |
| 88 base::win::ScopedHandle snapshot( | 55 base::win::ScopedHandle snapshot( |
| 89 ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)); | 56 ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)); |
| 90 PROCESSENTRY32 process_entry = {sizeof(PROCESSENTRY32)}; | 57 PROCESSENTRY32 process_entry = {sizeof(PROCESSENTRY32)}; |
| 91 if (!snapshot.Get()) { | 58 if (!snapshot.Get()) { |
| 92 LOG(ERROR) << "CreateToolhelp32Snaphot failed: " << GetLastError(); | 59 LOG(ERROR) << "CreateToolhelp32Snaphot failed: " << GetLastError(); |
| 93 return; | 60 return; |
| 94 } | 61 } |
| 95 if (!::Process32First(snapshot.Get(), &process_entry)) { | 62 if (!::Process32First(snapshot.Get(), &process_entry)) { |
| 96 LOG(ERROR) << "Process32First failed: " << GetLastError(); | 63 LOG(ERROR) << "Process32First failed: " << GetLastError(); |
| 97 return; | 64 return; |
| 98 } | 65 } |
| 99 do { | 66 do { |
| 100 base::ProcessId pid = process_entry.th32ProcessID; | 67 base::ProcessId pid = process_entry.th32ProcessID; |
| 101 base::win::ScopedHandle process_handle(::OpenProcess( | 68 base::win::ScopedHandle process_handle(::OpenProcess( |
| 102 PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid)); | 69 PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid)); |
| 103 if (!process_handle.IsValid()) | 70 if (!process_handle.IsValid()) |
| 104 continue; | 71 continue; |
| 105 bool is_64bit_process = | 72 if (_wcsicmp(process_data_[0].process_name.c_str(), |
| 106 ((windows_architecture == base::win::OSInfo::X64_ARCHITECTURE) || | 73 process_entry.szExeFile) != 0) |
| 107 (windows_architecture == base::win::OSInfo::IA64_ARCHITECTURE)) && | 74 continue; |
| 108 (base::win::OSInfo::GetWOW64StatusForProcess(process_handle.Get()) == | 75 // Get Memory Information. |
| 109 base::win::OSInfo::WOW64_DISABLED); | 76 ProcessMemoryInformation info; |
| 110 const size_t browser_list_size = | 77 info.pid = pid; |
| 111 (mode == FROM_CHROME_ONLY ? 1 : process_data_.size()); | 78 if (info.pid == GetCurrentProcessId()) |
| 112 for (size_t index2 = 0; index2 < browser_list_size; ++index2) { | 79 info.process_type = content::PROCESS_TYPE_BROWSER; |
| 113 if (_wcsicmp(process_data_[index2].process_name.c_str(), | 80 else |
| 114 process_entry.szExeFile) != 0) | 81 info.process_type = content::PROCESS_TYPE_UNKNOWN; |
| 82 |
| 83 scoped_ptr<base::ProcessMetrics> metrics; |
| 84 metrics.reset( |
| 85 base::ProcessMetrics::CreateProcessMetrics(process_handle.Get())); |
| 86 metrics->GetCommittedKBytes(&info.committed); |
| 87 metrics->GetWorkingSetKBytes(&info.working_set); |
| 88 |
| 89 // Get Version Information. |
| 90 info.version = base::ASCIIToUTF16(version_info::GetVersionNumber()); |
| 91 // Check if this is one of the child processes whose data we collected |
| 92 // on the IO thread, and if so copy over that data. |
| 93 for (size_t child = 0; child < child_info.size(); child++) { |
| 94 if (child_info[child].pid != info.pid) |
| 115 continue; | 95 continue; |
| 116 if (index2 == IE_BROWSER && is_64bit_process) | 96 info.titles = child_info[child].titles; |
| 117 continue; // Should use IE_64BIT_BROWSER | 97 info.process_type = child_info[child].process_type; |
| 118 // Get Memory Information. | |
| 119 ProcessMemoryInformation info; | |
| 120 info.pid = pid; | |
| 121 if (info.pid == GetCurrentProcessId()) | |
| 122 info.process_type = content::PROCESS_TYPE_BROWSER; | |
| 123 else | |
| 124 info.process_type = content::PROCESS_TYPE_UNKNOWN; | |
| 125 | |
| 126 scoped_ptr<base::ProcessMetrics> metrics; | |
| 127 metrics.reset(base::ProcessMetrics::CreateProcessMetrics( | |
| 128 process_handle.Get())); | |
| 129 metrics->GetCommittedKBytes(&info.committed); | |
| 130 metrics->GetWorkingSetKBytes(&info.working_set); | |
| 131 | |
| 132 // Get Version Information. | |
| 133 TCHAR name[MAX_PATH]; | |
| 134 if (index2 == CHROME_BROWSER || index2 == CHROME_NACL_PROCESS) { | |
| 135 info.version = base::ASCIIToUTF16(version_info::GetVersionNumber()); | |
| 136 // Check if this is one of the child processes whose data we collected | |
| 137 // on the IO thread, and if so copy over that data. | |
| 138 for (size_t child = 0; child < child_info.size(); child++) { | |
| 139 if (child_info[child].pid != info.pid) | |
| 140 continue; | |
| 141 info.titles = child_info[child].titles; | |
| 142 info.process_type = child_info[child].process_type; | |
| 143 break; | |
| 144 } | |
| 145 } else if (GetModuleFileNameEx(process_handle.Get(), NULL, name, | |
| 146 MAX_PATH - 1)) { | |
| 147 std::wstring str_name(name); | |
| 148 scoped_ptr<FileVersionInfo> version_info( | |
| 149 FileVersionInfo::CreateFileVersionInfo(base::FilePath(str_name))); | |
| 150 if (version_info != NULL) { | |
| 151 info.version = version_info->product_version(); | |
| 152 info.product_name = version_info->product_name(); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 // Add the process info to our list. | |
| 157 if (index2 == CHROME_NACL_PROCESS) { | |
| 158 // Add NaCl processes to Chrome's list | |
| 159 process_data_[CHROME_BROWSER].processes.push_back(info); | |
| 160 } else { | |
| 161 process_data_[index2].processes.push_back(info); | |
| 162 } | |
| 163 break; | 98 break; |
| 164 } | 99 } |
| 100 |
| 101 // Add the process info to our list. |
| 102 process_data_[0].processes.push_back(info); |
| 165 } while (::Process32Next(snapshot.Get(), &process_entry)); | 103 } while (::Process32Next(snapshot.Get(), &process_entry)); |
| 166 | 104 |
| 167 // Finally return to the browser thread. | 105 // Finally return to the browser thread. |
| 168 BrowserThread::PostTask( | 106 BrowserThread::PostTask( |
| 169 BrowserThread::UI, FROM_HERE, | 107 BrowserThread::UI, FROM_HERE, |
| 170 base::Bind(&MemoryDetails::CollectChildInfoOnUIThread, this)); | 108 base::Bind(&MemoryDetails::CollectChildInfoOnUIThread, this)); |
| 171 } | 109 } |
| OLD | NEW |