| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/file_version_info.h" | 13 #include "base/file_version_info.h" |
| 14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/mac/foundation_util.h" | 15 #include "base/mac/foundation_util.h" |
| 16 #include "base/process/process_iterator.h" | 16 #include "base/process/process_iterator.h" |
| 17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 19 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
| 20 #include "chrome/browser/process_info_snapshot.h" | |
| 21 #include "chrome/common/chrome_constants.h" | 20 #include "chrome/common/chrome_constants.h" |
| 22 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
| 23 #include "chrome/grit/chromium_strings.h" | 22 #include "chrome/grit/chromium_strings.h" |
| 24 #include "components/version_info/version_info.h" | 23 #include "components/version_info/version_info.h" |
| 25 #include "content/public/browser/browser_child_process_host.h" | 24 #include "content/public/browser/browser_child_process_host.h" |
| 26 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
| 27 #include "content/public/common/process_type.h" | 26 #include "content/public/common/process_type.h" |
| 28 #include "ui/base/l10n/l10n_util.h" | 27 #include "ui/base/l10n/l10n_util.h" |
| 29 | 28 |
| 30 using content::BrowserThread; | 29 using content::BrowserThread; |
| 31 | 30 |
| 32 // TODO(viettrungluu): Many of the TODOs below are subsumed by a general need to | |
| 33 // refactor the about:memory code (not just on Mac, but probably on other | |
| 34 // platforms as well). I've filed crbug.com/25456. | |
| 35 | |
| 36 // Known browsers which we collect details for. |CHROME_BROWSER| *must* be the | |
| 37 // first browser listed. The order here must match those in |process_template| | |
| 38 // (in |MemoryDetails::MemoryDetails()| below). | |
| 39 // TODO(viettrungluu): In the big refactoring (see above), get rid of this order | |
| 40 // dependence. | |
| 41 enum BrowserType { | |
| 42 // TODO(viettrungluu): possibly add more? | |
| 43 CHROME_BROWSER = 0, | |
| 44 SAFARI_BROWSER, | |
| 45 FIREFOX_BROWSER, | |
| 46 CAMINO_BROWSER, | |
| 47 OPERA_BROWSER, | |
| 48 OMNIWEB_BROWSER, | |
| 49 MAX_BROWSERS | |
| 50 }; | |
| 51 | |
| 52 namespace { | 31 namespace { |
| 53 | 32 |
| 54 // A helper for |CollectProcessData()|, collecting data on the Chrome/Chromium | 33 // A helper for |CollectProcessData()|, collecting data on the Chrome/Chromium |
| 55 // process with PID |pid|. The collected data is added to |processes|. | 34 // process with PID |pid|. The collected data is added to |processes|. |
| 56 void CollectProcessDataForChromeProcess( | 35 void CollectProcessDataForChromeProcess( |
| 57 const std::vector<ProcessMemoryInformation>& child_info, | 36 const std::vector<ProcessMemoryInformation>& child_info, |
| 58 base::ProcessId pid, | 37 base::ProcessId pid, |
| 59 ProcessMemoryInformationList* processes) { | 38 ProcessMemoryInformationList* processes) { |
| 60 ProcessMemoryInformation info; | 39 ProcessMemoryInformation info; |
| 61 info.pid = pid; | 40 info.pid = pid; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 78 } | 57 } |
| 79 | 58 |
| 80 scoped_ptr<base::ProcessMetrics> metrics; | 59 scoped_ptr<base::ProcessMetrics> metrics; |
| 81 metrics.reset(base::ProcessMetrics::CreateProcessMetrics( | 60 metrics.reset(base::ProcessMetrics::CreateProcessMetrics( |
| 82 pid, content::BrowserChildProcessHost::GetPortProvider())); | 61 pid, content::BrowserChildProcessHost::GetPortProvider())); |
| 83 metrics->GetCommittedAndWorkingSetKBytes(&info.committed, &info.working_set); | 62 metrics->GetCommittedAndWorkingSetKBytes(&info.committed, &info.working_set); |
| 84 | 63 |
| 85 processes->push_back(info); | 64 processes->push_back(info); |
| 86 } | 65 } |
| 87 | 66 |
| 88 // Collects memory information from non-Chrome browser processes, using the | |
| 89 // ProcessInfoSnapshot helper (which runs an external command - PS or TOP). | |
| 90 // Updates |process_data| with the collected information. | |
| 91 void CollectProcessDataAboutNonChromeProcesses( | |
| 92 const std::vector<base::ProcessId>& all_pids, | |
| 93 const std::vector<base::ProcessId> pids_by_browser[MAX_BROWSERS], | |
| 94 std::vector<ProcessData>* process_data) { | |
| 95 DCHECK_EQ(MAX_BROWSERS, process_data->size()); | |
| 96 | |
| 97 // Capture information about the processes we're interested in. | |
| 98 ProcessInfoSnapshot process_info; | |
| 99 process_info.Sample(all_pids); | |
| 100 | |
| 101 // Handle the other processes first. | |
| 102 for (size_t index = CHROME_BROWSER + 1; index < MAX_BROWSERS; ++index) { | |
| 103 for (const base::ProcessId& pid : pids_by_browser[index]) { | |
| 104 ProcessMemoryInformation info; | |
| 105 info.pid = pid; | |
| 106 info.process_type = content::PROCESS_TYPE_UNKNOWN; | |
| 107 | |
| 108 // Try to get version information. To do this, we need first to get the | |
| 109 // executable's name (we can only believe |proc_info.command| if it looks | |
| 110 // like an absolute path). Then we need strip the executable's name back | |
| 111 // to the bundle's name. And only then can we try to get the version. | |
| 112 scoped_ptr<FileVersionInfo> version_info; | |
| 113 ProcessInfoSnapshot::ProcInfoEntry proc_info; | |
| 114 if (process_info.GetProcInfo(info.pid, &proc_info)) { | |
| 115 if (proc_info.command.length() > 1 && proc_info.command[0] == '/') { | |
| 116 base::FilePath bundle_name = | |
| 117 base::mac::GetAppBundlePath(base::FilePath(proc_info.command)); | |
| 118 if (!bundle_name.empty()) { | |
| 119 version_info.reset( | |
| 120 FileVersionInfo::CreateFileVersionInfo(bundle_name)); | |
| 121 } | |
| 122 } | |
| 123 } | |
| 124 if (version_info) { | |
| 125 info.product_name = version_info->product_name(); | |
| 126 info.version = version_info->product_version(); | |
| 127 } else { | |
| 128 info.product_name = (*process_data)[index].name; | |
| 129 info.version.clear(); | |
| 130 } | |
| 131 | |
| 132 // Memory info. | |
| 133 process_info.GetCommittedKBytesOfPID(info.pid, &info.committed); | |
| 134 process_info.GetWorkingSetKBytesOfPID(info.pid, &info.working_set); | |
| 135 | |
| 136 // Add the process info to our list. | |
| 137 (*process_data)[index].processes.push_back(info); | |
| 138 } | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 } // namespace | 67 } // namespace |
| 143 | 68 |
| 144 MemoryDetails::MemoryDetails() { | 69 MemoryDetails::MemoryDetails() { |
| 145 const base::FilePath browser_process_path = | 70 const base::FilePath browser_process_path = |
| 146 base::GetProcessExecutablePath(base::GetCurrentProcessHandle()); | 71 base::GetProcessExecutablePath(base::GetCurrentProcessHandle()); |
| 147 const std::string browser_process_name = | |
| 148 browser_process_path.BaseName().value(); | |
| 149 const std::string google_browser_name = | |
| 150 l10n_util::GetStringUTF8(IDS_PRODUCT_NAME); | |
| 151 | 72 |
| 152 // (Human and process) names of browsers; should match the ordering for | 73 ProcessData process; |
| 153 // |BrowserType| enum. | 74 process.name = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); |
| 154 // TODO(viettrungluu): The current setup means that we can't detect both | 75 process.process_name = base::UTF8ToUTF16( |
| 155 // Chrome and Chromium at the same time! | 76 browser_process_path.BaseName().value()); |
| 156 // TODO(viettrungluu): Get localized browser names for other browsers | 77 process_data_.push_back(process); |
| 157 // (crbug.com/25779). | |
| 158 struct { | |
| 159 const char* name; | |
| 160 const char* process_name; | |
| 161 } process_template[MAX_BROWSERS] = { | |
| 162 { google_browser_name.c_str(), browser_process_name.c_str(), }, | |
| 163 { "Safari", "Safari", }, | |
| 164 { "Firefox", "firefox-bin", }, | |
| 165 { "Camino", "Camino", }, | |
| 166 { "Opera", "Opera", }, | |
| 167 { "OmniWeb", "OmniWeb", }, | |
| 168 }; | |
| 169 | |
| 170 for (size_t index = 0; index < MAX_BROWSERS; ++index) { | |
| 171 ProcessData process; | |
| 172 process.name = base::UTF8ToUTF16(process_template[index].name); | |
| 173 process.process_name = | |
| 174 base::UTF8ToUTF16(process_template[index].process_name); | |
| 175 process_data_.push_back(process); | |
| 176 } | |
| 177 } | 78 } |
| 178 | 79 |
| 179 ProcessData* MemoryDetails::ChromeBrowser() { | 80 ProcessData* MemoryDetails::ChromeBrowser() { |
| 180 return &process_data_[CHROME_BROWSER]; | 81 return &process_data_[0]; |
| 181 } | 82 } |
| 182 | 83 |
| 183 void MemoryDetails::CollectProcessData( | 84 void MemoryDetails::CollectProcessData( |
| 184 CollectionMode mode, | |
| 185 const std::vector<ProcessMemoryInformation>& child_info) { | 85 const std::vector<ProcessMemoryInformation>& child_info) { |
| 186 // This must be run on the blocking pool to avoid jank (|ProcessInfoSnapshot| | 86 // TODO(ellyjones): Does this still need to be run in the blocking pool? |
| 187 // runs /bin/ps, which isn't instantaneous). | 87 // It used to need to be because it ran /bin/ps, but it might not need to any |
| 88 // more. |
| 188 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 89 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 189 | 90 |
| 190 // Clear old data. | 91 // Clear old data. |
| 191 for (size_t index = 0; index < MAX_BROWSERS; index++) | 92 process_data_[0].processes.clear(); |
| 192 process_data_[index].processes.clear(); | |
| 193 | 93 |
| 194 // First, we use |NamedProcessIterator| to get the PIDs of the processes we're | 94 // First, we use |NamedProcessIterator| to get the PIDs of the processes we're |
| 195 // interested in; we save our results to avoid extra calls to | 95 // interested in; we save our results to avoid extra calls to |
| 196 // |NamedProcessIterator| (for performance reasons) and to avoid additional | 96 // |NamedProcessIterator| (for performance reasons) and to avoid additional |
| 197 // inconsistencies caused by racing. Then we run |/bin/ps| *once* to get | 97 // inconsistencies caused by racing. Then we run |/bin/ps| *once* to get |
| 198 // information on those PIDs. Then we used our saved information to iterate | 98 // information on those PIDs. Then we used our saved information to iterate |
| 199 // over browsers, then over PIDs. | 99 // over browsers, then over PIDs. |
| 200 | 100 |
| 201 // Get PIDs of main browser processes. | 101 // Get PIDs of main browser processes. |
| 202 std::vector<base::ProcessId> pids_by_browser[MAX_BROWSERS]; | |
| 203 std::vector<base::ProcessId> all_pids; | 102 std::vector<base::ProcessId> all_pids; |
| 204 for (size_t index = CHROME_BROWSER; index < MAX_BROWSERS; index++) { | 103 { |
| 205 base::NamedProcessIterator process_it( | 104 base::NamedProcessIterator process_it( |
| 206 base::UTF16ToUTF8(process_data_[index].process_name), NULL); | 105 base::UTF16ToUTF8(process_data_[0].process_name), NULL); |
| 207 | 106 |
| 208 while (const base::ProcessEntry* entry = process_it.NextProcessEntry()) { | 107 while (const base::ProcessEntry* entry = process_it.NextProcessEntry()) { |
| 209 pids_by_browser[index].push_back(entry->pid()); | |
| 210 all_pids.push_back(entry->pid()); | 108 all_pids.push_back(entry->pid()); |
| 211 } | 109 } |
| 212 } | 110 } |
| 213 | 111 |
| 214 // Get PIDs of the helper. | 112 // Get PIDs of the helper. |
| 215 std::vector<base::ProcessId> helper_pids; | 113 { |
| 216 base::NamedProcessIterator helper_it(chrome::kHelperProcessExecutableName, | 114 base::NamedProcessIterator helper_it(chrome::kHelperProcessExecutableName, |
| 217 NULL); | 115 NULL); |
| 218 while (const base::ProcessEntry* entry = helper_it.NextProcessEntry()) { | 116 while (const base::ProcessEntry* entry = helper_it.NextProcessEntry()) { |
| 219 helper_pids.push_back(entry->pid()); | 117 all_pids.push_back(entry->pid()); |
| 220 all_pids.push_back(entry->pid()); | 118 } |
| 221 } | |
| 222 | |
| 223 if (mode == FROM_ALL_BROWSERS) { | |
| 224 CollectProcessDataAboutNonChromeProcesses(all_pids, pids_by_browser, | |
| 225 &process_data_); | |
| 226 } | 119 } |
| 227 | 120 |
| 228 ProcessMemoryInformationList* chrome_processes = | 121 ProcessMemoryInformationList* chrome_processes = |
| 229 &process_data_[CHROME_BROWSER].processes; | 122 &process_data_[0].processes; |
| 230 | 123 |
| 231 // Collect data about Chrome/Chromium. | 124 // Collect data about Chrome/Chromium. |
| 232 for (const base::ProcessId& pid : pids_by_browser[CHROME_BROWSER]) | 125 for (const base::ProcessId& pid : all_pids) |
| 233 CollectProcessDataForChromeProcess(child_info, pid, chrome_processes); | |
| 234 | |
| 235 // And collect data about the helpers. | |
| 236 for (const base::ProcessId& pid : helper_pids) | |
| 237 CollectProcessDataForChromeProcess(child_info, pid, chrome_processes); | 126 CollectProcessDataForChromeProcess(child_info, pid, chrome_processes); |
| 238 | 127 |
| 239 // Finally return to the browser thread. | 128 // Finally return to the browser thread. |
| 240 BrowserThread::PostTask( | 129 BrowserThread::PostTask( |
| 241 BrowserThread::UI, FROM_HERE, | 130 BrowserThread::UI, FROM_HERE, |
| 242 base::Bind(&MemoryDetails::CollectChildInfoOnUIThread, this)); | 131 base::Bind(&MemoryDetails::CollectChildInfoOnUIThread, this)); |
| 243 } | 132 } |
| OLD | NEW |