[email protected] | 4306df7 | 2012-04-20 18:58:57 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [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 | |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame^] | 7 | #include <stddef.h> |
[email protected] | 500c872 | 2012-06-18 17:34:23 | [diff] [blame] | 8 | #include <sys/types.h> |
| 9 | #include <unistd.h> |
| 10 | |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 11 | #include <map> |
[email protected] | 8e2b647 | 2010-12-15 22:19:48 | [diff] [blame] | 12 | #include <set> |
| 13 | |
[email protected] | 24d6969 | 2011-10-21 18:26:51 | [diff] [blame] | 14 | #include "base/bind.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 15 | #include "base/files/file_util.h" |
[email protected] | d09a4ce1c | 2013-07-24 17:37:02 | [diff] [blame] | 16 | #include "base/process/process_iterator.h" |
| 17 | #include "base/process/process_metrics.h" |
[email protected] | 48e30344 | 2013-07-18 19:13:15 | [diff] [blame] | 18 | #include "base/strings/string_number_conversions.h" |
[email protected] | f9b29436 | 2013-06-10 20:22:31 | [diff] [blame] | 19 | #include "base/strings/string_util.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 20 | #include "base/strings/utf_string_conversions.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame^] | 21 | #include "build/build_config.h" |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 22 | #include "chrome/common/chrome_constants.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 23 | #include "chrome/grit/chromium_strings.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 24 | #include "content/public/browser/browser_thread.h" |
[email protected] | bd5d6cf | 2011-12-01 00:39:12 | [diff] [blame] | 25 | #include "content/public/common/process_type.h" |
[email protected] | b432074 | 2013-02-06 02:57:31 | [diff] [blame] | 26 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 27 | |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 28 | using base::ProcessEntry; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 29 | using content::BrowserThread; |
| 30 | |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 31 | namespace { |
| 32 | |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 33 | // Known browsers which we collect details for. |
| 34 | enum BrowserType { |
| 35 | CHROME = 0, |
| 36 | FIREFOX, |
[email protected] | f3f032f8 | 2010-07-07 17:32:53 | [diff] [blame] | 37 | ICEWEASEL, |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 38 | OPERA, |
| 39 | KONQUEROR, |
| 40 | EPIPHANY, |
| 41 | MIDORI, |
| 42 | MAX_BROWSERS |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 43 | }; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 44 | |
| 45 | // The pretty printed names of those browsers. Matches up with enum |
| 46 | // BrowserType. |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 47 | const char kBrowserPrettyNames[][10] = { |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 48 | "Chrome", |
| 49 | "Firefox", |
[email protected] | f3f032f8 | 2010-07-07 17:32:53 | [diff] [blame] | 50 | "Iceweasel", |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 51 | "Opera", |
| 52 | "Konqueror", |
| 53 | "Epiphany", |
| 54 | "Midori", |
| 55 | }; |
| 56 | |
| 57 | // A mapping from process name to the type of browser. |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 58 | const struct { |
p.forysiuk | 52f6146b | 2015-01-21 14:01:09 | [diff] [blame] | 59 | const char process_name[17]; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 60 | BrowserType browser; |
[email protected] | b432074 | 2013-02-06 02:57:31 | [diff] [blame] | 61 | } kBrowserBinaryNames[] = { |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 62 | { "firefox", FIREFOX }, |
| 63 | { "firefox-3.5", FIREFOX }, |
| 64 | { "firefox-3.0", FIREFOX }, |
[email protected] | 57678648 | 2010-04-05 18:54:32 | [diff] [blame] | 65 | { "firefox-bin", FIREFOX }, |
[email protected] | f3f032f8 | 2010-07-07 17:32:53 | [diff] [blame] | 66 | { "iceweasel", ICEWEASEL }, |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 67 | { "opera", OPERA }, |
| 68 | { "konqueror", KONQUEROR }, |
p.forysiuk | 52f6146b | 2015-01-21 14:01:09 | [diff] [blame] | 69 | { "epiphany-browser", EPIPHANY }, |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 70 | { "epiphany", EPIPHANY }, |
| 71 | { "midori", MIDORI }, |
| 72 | { "", MAX_BROWSERS }, |
| 73 | }; |
| 74 | |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 75 | struct Process { |
| 76 | pid_t pid; |
| 77 | pid_t parent; |
| 78 | std::string name; |
| 79 | }; |
| 80 | |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 81 | typedef std::map<pid_t, Process> ProcessMap; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 82 | |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 83 | // Get information on all the processes running on the system. |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 84 | ProcessMap GetProcesses() { |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 85 | ProcessMap map; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 86 | |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 87 | base::ProcessIterator process_iter(NULL); |
| 88 | while (const ProcessEntry* process_entry = process_iter.NextProcessEntry()) { |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 89 | Process process; |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 90 | process.pid = process_entry->pid(); |
| 91 | process.parent = process_entry->parent_pid(); |
| 92 | process.name = process_entry->exe_file(); |
| 93 | map.insert(std::make_pair(process.pid, process)); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 94 | } |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 95 | return map; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Given a process name, return the type of the browser which created that |
| 99 | // process, or |MAX_BROWSERS| if we don't know about it. |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 100 | BrowserType GetBrowserType(const std::string& process_name) { |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 101 | for (unsigned i = 0; kBrowserBinaryNames[i].process_name[0]; ++i) { |
| 102 | if (strcmp(process_name.c_str(), kBrowserBinaryNames[i].process_name) == 0) |
| 103 | return kBrowserBinaryNames[i].browser; |
| 104 | } |
| 105 | |
| 106 | return MAX_BROWSERS; |
| 107 | } |
| 108 | |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 109 | // For each of a list of pids, collect memory information about that process. |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 110 | ProcessData GetProcessDataMemoryInformation( |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 111 | const std::vector<pid_t>& pids) { |
| 112 | ProcessData process_data; |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 113 | for (pid_t pid : pids) { |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 114 | ProcessMemoryInformation pmi; |
| 115 | |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 116 | pmi.pid = pid; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 117 | pmi.num_processes = 1; |
| 118 | |
| 119 | if (pmi.pid == base::GetCurrentProcId()) |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 120 | pmi.process_type = content::PROCESS_TYPE_BROWSER; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 121 | else |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 122 | pmi.process_type = content::PROCESS_TYPE_UNKNOWN; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 123 | |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 124 | scoped_ptr<base::ProcessMetrics> metrics( |
| 125 | base::ProcessMetrics::CreateProcessMetrics(pid)); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 126 | metrics->GetWorkingSetKBytes(&pmi.working_set); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 127 | |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 128 | process_data.processes.push_back(pmi); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 129 | } |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 130 | return process_data; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 131 | } |
| 132 | |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 133 | // Find all children of the given process with pid |root|. |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 134 | std::vector<pid_t> GetAllChildren(const ProcessMap& processes, pid_t root) { |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 135 | std::vector<pid_t> children; |
| 136 | children.push_back(root); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 137 | |
| 138 | std::set<pid_t> wavefront, next_wavefront; |
| 139 | wavefront.insert(root); |
| 140 | |
| 141 | while (wavefront.size()) { |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 142 | for (const auto& entry : processes) { |
| 143 | const Process& process = entry.second; |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 144 | if (wavefront.count(process.parent)) { |
| 145 | children.push_back(process.pid); |
| 146 | next_wavefront.insert(process.pid); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | wavefront.clear(); |
| 151 | wavefront.swap(next_wavefront); |
| 152 | } |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 153 | return children; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 154 | } |
| 155 | |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 156 | } // namespace |
| 157 | |
| 158 | MemoryDetails::MemoryDetails() { |
| 159 | } |
| 160 | |
| 161 | ProcessData* MemoryDetails::ChromeBrowser() { |
| 162 | return &process_data_[0]; |
| 163 | } |
| 164 | |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 165 | void MemoryDetails::CollectProcessData( |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 166 | CollectionMode mode, |
[email protected] | 4df3ac6 | 2011-03-11 04:38:52 | [diff] [blame] | 167 | const std::vector<ProcessMemoryInformation>& child_info) { |
hashimoto | a8ea28d | 2015-04-11 02:50:48 | [diff] [blame] | 168 | DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 169 | |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 170 | ProcessMap process_map = GetProcesses(); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 171 | std::set<pid_t> browsers_found; |
| 172 | |
| 173 | // For each process on the system, if it appears to be a browser process and |
| 174 | // it's parent isn't a browser process, then record it in |browsers_found|. |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 175 | for (const auto& entry : process_map) { |
| 176 | const Process& current_process = entry.second; |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 177 | const BrowserType type = GetBrowserType(current_process.name); |
| 178 | if (type == MAX_BROWSERS) |
| 179 | continue; |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 180 | if (type != CHROME && mode == FROM_CHROME_ONLY) |
| 181 | continue; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 182 | |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 183 | ProcessMap::const_iterator parent_iter = |
| 184 | process_map.find(current_process.parent); |
| 185 | if (parent_iter == process_map.end()) { |
| 186 | browsers_found.insert(current_process.pid); |
| 187 | continue; |
| 188 | } |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 189 | |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 190 | if (GetBrowserType(parent_iter->second.name) != type) { |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 191 | // We found a process whose type is different from its parent's type. |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 192 | // That means it is the root process of the browser. |
| 193 | browsers_found.insert(current_process.pid); |
p.forysiuk | dd7ed9cd | 2015-01-22 21:28:52 | [diff] [blame] | 194 | continue; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 198 | ProcessData current_browser = |
| 199 | GetProcessDataMemoryInformation(GetAllChildren(process_map, getpid())); |
[email protected] | b432074 | 2013-02-06 02:57:31 | [diff] [blame] | 200 | current_browser.name = l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME); |
[email protected] | 6778fed | 2013-12-24 20:09:37 | [diff] [blame] | 201 | current_browser.process_name = base::ASCIIToUTF16("chrome"); |
[email protected] | e1873b9 | 2011-05-13 19:22:22 | [diff] [blame] | 202 | |
| 203 | for (std::vector<ProcessMemoryInformation>::iterator |
| 204 | i = current_browser.processes.begin(); |
| 205 | i != current_browser.processes.end(); ++i) { |
| 206 | // Check if this is one of the child processes whose data we collected |
| 207 | // on the IO thread, and if so copy over that data. |
| 208 | for (size_t child = 0; child < child_info.size(); child++) { |
| 209 | if (child_info[child].pid != i->pid) |
| 210 | continue; |
| 211 | i->titles = child_info[child].titles; |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 212 | i->process_type = child_info[child].process_type; |
[email protected] | e1873b9 | 2011-05-13 19:22:22 | [diff] [blame] | 213 | break; |
| 214 | } |
| 215 | } |
| 216 | |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 217 | process_data_.push_back(current_browser); |
| 218 | |
| 219 | // For each browser process, collect a list of its children and get the |
| 220 | // memory usage of each. |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 221 | for (pid_t pid : browsers_found) { |
| 222 | std::vector<pid_t> browser_processes = GetAllChildren(process_map, pid); |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 223 | ProcessData browser = GetProcessDataMemoryInformation(browser_processes); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 224 | |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 225 | ProcessMap::const_iterator process_iter = process_map.find(pid); |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 226 | if (process_iter == process_map.end()) |
| 227 | continue; |
| 228 | BrowserType type = GetBrowserType(process_iter->second.name); |
| 229 | if (type != MAX_BROWSERS) |
[email protected] | 6778fed | 2013-12-24 20:09:37 | [diff] [blame] | 230 | browser.name = base::ASCIIToUTF16(kBrowserPrettyNames[type]); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 231 | process_data_.push_back(browser); |
| 232 | } |
| 233 | |
[email protected] | 48e30344 | 2013-07-18 19:13:15 | [diff] [blame] | 234 | #if defined(OS_CHROMEOS) |
[email protected] | f413478 | 2013-08-29 21:25:20 | [diff] [blame] | 235 | base::GetSwapInfo(&swap_info_); |
[email protected] | 48e30344 | 2013-07-18 19:13:15 | [diff] [blame] | 236 | #endif |
| 237 | |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 238 | // Finally return to the browser thread. |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 239 | BrowserThread::PostTask( |
| 240 | BrowserThread::UI, FROM_HERE, |
[email protected] | 24d6969 | 2011-10-21 18:26:51 | [diff] [blame] | 241 | base::Bind(&MemoryDetails::CollectChildInfoOnUIThread, this)); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 242 | } |