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