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