[email protected] | b0b67cf | 2012-01-18 21:59:57 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | f164cea | 2009-11-05 23:37:40 | [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" |
| 6 | |
| 7 | #include <set> |
| 8 | #include <string> |
| 9 | |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 10 | #include "base/basictypes.h" |
[email protected] | 24d6969 | 2011-10-21 18:26:51 | [diff] [blame] | 11 | #include "base/bind.h" |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 12 | #include "base/file_version_info.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
[email protected] | 0378bf4 | 2011-01-01 18:20:14 | [diff] [blame] | 14 | #include "base/mac/mac_util.h" |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 15 | #include "base/process_util.h" |
[email protected] | f9b29436 | 2013-06-10 20:22:31 | [diff] [blame^] | 16 | #include "base/strings/string_util.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 17 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 18 | #include "base/threading/thread.h" |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 19 | #include "chrome/browser/process_info_snapshot.h" |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 20 | #include "chrome/common/chrome_constants.h" |
[email protected] | 1eeb5e0 | 2010-07-20 23:02:11 | [diff] [blame] | 21 | #include "chrome/common/chrome_version_info.h" |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 22 | #include "chrome/common/url_constants.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 23 | #include "content/public/browser/browser_thread.h" |
[email protected] | bd5d6cf | 2011-12-01 00:39:12 | [diff] [blame] | 24 | #include "content/public/common/process_type.h" |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 25 | #include "grit/chromium_strings.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 26 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 27 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 28 | using content::BrowserThread; |
| 29 | |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 30 | // TODO(viettrungluu): Many of the TODOs below are subsumed by a general need to |
| 31 | // refactor the about:memory code (not just on Mac, but probably on other |
| 32 | // platforms as well). I've filed crbug.com/25456. |
| 33 | |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 34 | // Known browsers which we collect details for. |CHROME_BROWSER| *must* be the |
| 35 | // first browser listed. The order here must match those in |process_template| |
| 36 | // (in |MemoryDetails::MemoryDetails()| below). |
| 37 | // TODO(viettrungluu): In the big refactoring (see above), get rid of this order |
| 38 | // dependence. |
| 39 | enum BrowserType { |
| 40 | // TODO(viettrungluu): possibly add more? |
| 41 | CHROME_BROWSER = 0, |
| 42 | SAFARI_BROWSER, |
| 43 | FIREFOX_BROWSER, |
| 44 | CAMINO_BROWSER, |
| 45 | OPERA_BROWSER, |
| 46 | OMNIWEB_BROWSER, |
| 47 | MAX_BROWSERS |
| 48 | } BrowserProcess; |
| 49 | |
| 50 | |
[email protected] | 4306df7 | 2012-04-20 18:58:57 | [diff] [blame] | 51 | MemoryDetails::MemoryDetails() |
| 52 | : user_metrics_mode_(UPDATE_USER_METRICS) { |
[email protected] | e4de6b5 | 2011-11-17 06:06:15 | [diff] [blame] | 53 | const std::string google_browser_name = |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 54 | l10n_util::GetStringUTF8(IDS_PRODUCT_NAME); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 55 | // (Human and process) names of browsers; should match the ordering for |
| 56 | // |BrowserProcess| (i.e., |BrowserType|). |
| 57 | // TODO(viettrungluu): The current setup means that we can't detect both |
| 58 | // Chrome and Chromium at the same time! |
| 59 | // TODO(viettrungluu): Get localized browser names for other browsers |
| 60 | // (crbug.com/25779). |
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 61 | struct { |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 62 | const char* name; |
| 63 | const char* process_name; |
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 64 | } process_template[MAX_BROWSERS] = { |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 65 | { google_browser_name.c_str(), chrome::kBrowserProcessExecutableName, }, |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 66 | { "Safari", "Safari", }, |
| 67 | { "Firefox", "firefox-bin", }, |
| 68 | { "Camino", "Camino", }, |
| 69 | { "Opera", "Opera", }, |
| 70 | { "OmniWeb", "OmniWeb", }, |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 71 | }; |
| 72 | |
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 73 | for (size_t index = 0; index < MAX_BROWSERS; ++index) { |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 74 | ProcessData process; |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 75 | process.name = UTF8ToUTF16(process_template[index].name); |
| 76 | process.process_name = UTF8ToUTF16(process_template[index].process_name); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 77 | process_data_.push_back(process); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | ProcessData* MemoryDetails::ChromeBrowser() { |
| 82 | return &process_data_[CHROME_BROWSER]; |
| 83 | } |
| 84 | |
| 85 | void MemoryDetails::CollectProcessData( |
[email protected] | 4df3ac6 | 2011-03-11 04:38:52 | [diff] [blame] | 86 | const std::vector<ProcessMemoryInformation>& child_info) { |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 87 | // This must be run on the file thread to avoid jank (|ProcessInfoSnapshot| |
| 88 | // runs /bin/ps, which isn't instantaneous). |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 89 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 90 | |
| 91 | // Clear old data. |
| 92 | for (size_t index = 0; index < MAX_BROWSERS; index++) |
| 93 | process_data_[index].processes.clear(); |
| 94 | |
| 95 | // First, we use |NamedProcessIterator| to get the PIDs of the processes we're |
| 96 | // interested in; we save our results to avoid extra calls to |
| 97 | // |NamedProcessIterator| (for performance reasons) and to avoid additional |
| 98 | // inconsistencies caused by racing. Then we run |/bin/ps| *once* to get |
| 99 | // information on those PIDs. Then we used our saved information to iterate |
| 100 | // over browsers, then over PIDs. |
| 101 | |
| 102 | // Get PIDs of main browser processes. |
| 103 | std::vector<base::ProcessId> pids_by_browser[MAX_BROWSERS]; |
| 104 | std::vector<base::ProcessId> all_pids; |
| 105 | for (size_t index = CHROME_BROWSER; index < MAX_BROWSERS; index++) { |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 106 | base::NamedProcessIterator process_it( |
| 107 | UTF16ToUTF8(process_data_[index].process_name), NULL); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 108 | |
[email protected] | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 109 | while (const base::ProcessEntry* entry = process_it.NextProcessEntry()) { |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 110 | pids_by_browser[index].push_back(entry->pid()); |
| 111 | all_pids.push_back(entry->pid()); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 115 | // The helper might show up as these different flavors depending on the |
| 116 | // executable flags required. |
| 117 | std::vector<std::string> helper_names; |
| 118 | helper_names.push_back(chrome::kHelperProcessExecutableName); |
| 119 | for (const char* const* suffix = chrome::kHelperFlavorSuffixes; |
| 120 | *suffix; |
| 121 | ++suffix) { |
| 122 | std::string helper_name = chrome::kHelperProcessExecutableName; |
| 123 | helper_name.append(1, ' '); |
| 124 | helper_name.append(*suffix); |
| 125 | helper_names.push_back(helper_name); |
| 126 | } |
| 127 | |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 128 | // Get PIDs of helpers. |
| 129 | std::vector<base::ProcessId> helper_pids; |
[email protected] | 8c40f32 | 2011-08-24 03:33:36 | [diff] [blame] | 130 | for (size_t i = 0; i < helper_names.size(); ++i) { |
| 131 | std::string helper_name = helper_names[i]; |
| 132 | base::NamedProcessIterator helper_it(helper_name, NULL); |
[email protected] | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 133 | while (const base::ProcessEntry* entry = helper_it.NextProcessEntry()) { |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 134 | helper_pids.push_back(entry->pid()); |
| 135 | all_pids.push_back(entry->pid()); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | // Capture information about the processes we're interested in. |
| 140 | ProcessInfoSnapshot process_info; |
| 141 | process_info.Sample(all_pids); |
| 142 | |
| 143 | // Handle the other processes first. |
| 144 | for (size_t index = CHROME_BROWSER + 1; index < MAX_BROWSERS; index++) { |
| 145 | for (std::vector<base::ProcessId>::const_iterator it = |
| 146 | pids_by_browser[index].begin(); |
| 147 | it != pids_by_browser[index].end(); ++it) { |
| 148 | ProcessMemoryInformation info; |
| 149 | info.pid = *it; |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 150 | info.process_type = content::PROCESS_TYPE_UNKNOWN; |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 151 | |
| 152 | // Try to get version information. To do this, we need first to get the |
| 153 | // executable's name (we can only believe |proc_info.command| if it looks |
| 154 | // like an absolute path). Then we need strip the executable's name back |
| 155 | // to the bundle's name. And only then can we try to get the version. |
| 156 | scoped_ptr<FileVersionInfo> version_info; |
| 157 | ProcessInfoSnapshot::ProcInfoEntry proc_info; |
| 158 | if (process_info.GetProcInfo(info.pid, &proc_info)) { |
| 159 | if (proc_info.command.length() > 1 && proc_info.command[0] == '/') { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 160 | base::FilePath bundle_name = |
| 161 | base::mac::GetAppBundlePath(base::FilePath(proc_info.command)); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 162 | if (!bundle_name.empty()) { |
| 163 | version_info.reset(FileVersionInfo::CreateFileVersionInfo( |
| 164 | bundle_name)); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | if (version_info.get()) { |
| 169 | info.product_name = version_info->product_name(); |
| 170 | info.version = version_info->product_version(); |
| 171 | } else { |
| 172 | info.product_name = process_data_[index].name; |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 173 | info.version = string16(); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // Memory info. |
| 177 | process_info.GetCommittedKBytesOfPID(info.pid, &info.committed); |
| 178 | process_info.GetWorkingSetKBytesOfPID(info.pid, &info.working_set); |
| 179 | |
| 180 | // Add the process info to our list. |
| 181 | process_data_[index].processes.push_back(info); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // Collect data about Chrome/Chromium. |
| 186 | for (std::vector<base::ProcessId>::const_iterator it = |
| 187 | pids_by_browser[CHROME_BROWSER].begin(); |
| 188 | it != pids_by_browser[CHROME_BROWSER].end(); ++it) { |
| 189 | CollectProcessDataChrome(child_info, *it, process_info); |
| 190 | } |
| 191 | |
| 192 | // And collect data about the helpers. |
| 193 | for (std::vector<base::ProcessId>::const_iterator it = helper_pids.begin(); |
| 194 | it != helper_pids.end(); ++it) { |
| 195 | CollectProcessDataChrome(child_info, *it, process_info); |
| 196 | } |
| 197 | |
| 198 | // Finally return to the browser thread. |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 199 | BrowserThread::PostTask( |
| 200 | BrowserThread::UI, FROM_HERE, |
[email protected] | 24d6969 | 2011-10-21 18:26:51 | [diff] [blame] | 201 | base::Bind(&MemoryDetails::CollectChildInfoOnUIThread, this)); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void MemoryDetails::CollectProcessDataChrome( |
| 205 | const std::vector<ProcessMemoryInformation>& child_info, |
| 206 | base::ProcessId pid, |
| 207 | const ProcessInfoSnapshot& process_info) { |
| 208 | ProcessMemoryInformation info; |
| 209 | info.pid = pid; |
| 210 | if (info.pid == base::GetCurrentProcId()) |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 211 | info.process_type = content::PROCESS_TYPE_BROWSER; |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 212 | else |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 213 | info.process_type = content::PROCESS_TYPE_UNKNOWN; |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 214 | |
[email protected] | 0211f57e | 2010-08-27 20:28:42 | [diff] [blame] | 215 | chrome::VersionInfo version_info; |
[email protected] | 30c9180 | 2010-12-18 00:40:17 | [diff] [blame] | 216 | if (version_info.is_valid()) { |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 217 | info.product_name = ASCIIToUTF16(version_info.Name()); |
| 218 | info.version = ASCIIToUTF16(version_info.Version()); |
[email protected] | 30c9180 | 2010-12-18 00:40:17 | [diff] [blame] | 219 | } else { |
| 220 | info.product_name = process_data_[CHROME_BROWSER].name; |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 221 | info.version = string16(); |
[email protected] | 30c9180 | 2010-12-18 00:40:17 | [diff] [blame] | 222 | } |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 223 | |
| 224 | // Check if this is one of the child processes whose data we collected |
| 225 | // on the IO thread, and if so copy over that data. |
| 226 | for (size_t child = 0; child < child_info.size(); child++) { |
| 227 | if (child_info[child].pid == info.pid) { |
| 228 | info.titles = child_info[child].titles; |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 229 | info.process_type = child_info[child].process_type; |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 230 | break; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // Memory info. |
| 235 | process_info.GetCommittedKBytesOfPID(info.pid, &info.committed); |
| 236 | process_info.GetWorkingSetKBytesOfPID(info.pid, &info.working_set); |
| 237 | |
| 238 | // Add the process info to our list. |
| 239 | process_data_[CHROME_BROWSER].processes.push_back(info); |
| 240 | } |