[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> |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 12 | #include <memory> |
[email protected] | 8e2b647 | 2010-12-15 22:19:48 | [diff] [blame] | 13 | #include <set> |
| 14 | |
[email protected] | 24d6969 | 2011-10-21 18:26:51 | [diff] [blame] | 15 | #include "base/bind.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 16 | #include "base/files/file_util.h" |
[email protected] | d09a4ce1c | 2013-07-24 17:37:02 | [diff] [blame] | 17 | #include "base/process/process_iterator.h" |
| 18 | #include "base/process/process_metrics.h" |
[email protected] | 48e30344 | 2013-07-18 19:13:15 | [diff] [blame] | 19 | #include "base/strings/string_number_conversions.h" |
[email protected] | f9b29436 | 2013-06-10 20:22:31 | [diff] [blame] | 20 | #include "base/strings/string_util.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 21 | #include "base/strings/utf_string_conversions.h" |
Etienne Pierre-doray | 0fbce43 | 2018-08-27 20:27:51 | [diff] [blame] | 22 | #include "base/threading/scoped_blocking_call.h" |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 23 | #include "build/build_config.h" |
Yuta Hijikata | 235fc62b | 2020-12-08 03:48:32 | [diff] [blame] | 24 | #include "build/chromeos_buildflags.h" |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 25 | #include "chrome/common/chrome_constants.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 26 | #include "chrome/grit/chromium_strings.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 27 | #include "content/public/browser/browser_task_traits.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 28 | #include "content/public/browser/browser_thread.h" |
[email protected] | bd5d6cf | 2011-12-01 00:39:12 | [diff] [blame] | 29 | #include "content/public/common/process_type.h" |
[email protected] | b432074 | 2013-02-06 02:57:31 | [diff] [blame] | 30 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 31 | |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 32 | using base::ProcessEntry; |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 33 | namespace { |
| 34 | |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 35 | struct Process { |
| 36 | pid_t pid; |
| 37 | pid_t parent; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 38 | }; |
| 39 | |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 40 | typedef std::map<pid_t, Process> ProcessMap; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 41 | |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 42 | // Get information on all the processes running on the system. |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 43 | ProcessMap GetProcesses() { |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 44 | ProcessMap map; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 45 | |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 46 | base::ProcessIterator process_iter(NULL); |
| 47 | while (const ProcessEntry* process_entry = process_iter.NextProcessEntry()) { |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 48 | Process process; |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 49 | process.pid = process_entry->pid(); |
| 50 | process.parent = process_entry->parent_pid(); |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 51 | map.insert(std::make_pair(process.pid, process)); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 52 | } |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 53 | return map; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 54 | } |
| 55 | |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 56 | // For each of a list of pids, collect memory information about that process. |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 57 | ProcessData GetProcessDataMemoryInformation( |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 58 | const std::vector<pid_t>& pids) { |
| 59 | ProcessData process_data; |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 60 | for (pid_t pid : pids) { |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 61 | ProcessMemoryInformation pmi; |
| 62 | |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 63 | pmi.pid = pid; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 64 | pmi.num_processes = 1; |
| 65 | |
| 66 | if (pmi.pid == base::GetCurrentProcId()) |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 67 | pmi.process_type = content::PROCESS_TYPE_BROWSER; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 68 | else |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 69 | pmi.process_type = content::PROCESS_TYPE_UNKNOWN; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 70 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 71 | std::unique_ptr<base::ProcessMetrics> metrics( |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 72 | base::ProcessMetrics::CreateProcessMetrics(pid)); |
dcastagna | f454af4e | 2017-03-15 23:34:14 | [diff] [blame] | 73 | pmi.num_open_fds = metrics->GetOpenFdCount(); |
| 74 | pmi.open_fds_soft_limit = metrics->GetOpenFdSoftLimit(); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 75 | |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 76 | process_data.processes.push_back(pmi); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 77 | } |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 78 | return process_data; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 79 | } |
| 80 | |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 81 | // Find all children of the given process with pid |root|. |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 82 | std::vector<pid_t> GetAllChildren(const ProcessMap& processes, pid_t root) { |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 83 | std::vector<pid_t> children; |
| 84 | children.push_back(root); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 85 | |
| 86 | std::set<pid_t> wavefront, next_wavefront; |
| 87 | wavefront.insert(root); |
| 88 | |
| 89 | while (wavefront.size()) { |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 90 | for (const auto& entry : processes) { |
| 91 | const Process& process = entry.second; |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 92 | if (wavefront.count(process.parent)) { |
| 93 | children.push_back(process.pid); |
| 94 | next_wavefront.insert(process.pid); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | wavefront.clear(); |
| 99 | wavefront.swap(next_wavefront); |
| 100 | } |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 101 | return children; |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 102 | } |
| 103 | |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 104 | } // namespace |
| 105 | |
| 106 | MemoryDetails::MemoryDetails() { |
| 107 | } |
| 108 | |
| 109 | ProcessData* MemoryDetails::ChromeBrowser() { |
| 110 | return &process_data_[0]; |
| 111 | } |
| 112 | |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 113 | void MemoryDetails::CollectProcessData( |
[email protected] | 4df3ac6 | 2011-03-11 04:38:52 | [diff] [blame] | 114 | const std::vector<ProcessMemoryInformation>& child_info) { |
Etienne Bergeron | 436d4221 | 2019-02-26 17:15:12 | [diff] [blame] | 115 | base::ScopedBlockingCall scoped_blocking_call(FROM_HERE, |
| 116 | base::BlockingType::MAY_BLOCK); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 117 | |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 118 | ProcessMap process_map = GetProcesses(); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 119 | std::set<pid_t> browsers_found; |
| 120 | |
[email protected] | e9720e82 | 2012-06-07 00:07:48 | [diff] [blame] | 121 | ProcessData current_browser = |
| 122 | GetProcessDataMemoryInformation(GetAllChildren(process_map, getpid())); |
[email protected] | b432074 | 2013-02-06 02:57:31 | [diff] [blame] | 123 | current_browser.name = l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME); |
Jan Wilken Dörrie | 78e88d82e | 2021-03-23 15:24:22 | [diff] [blame] | 124 | current_browser.process_name = u"chrome"; |
[email protected] | e1873b9 | 2011-05-13 19:22:22 | [diff] [blame] | 125 | |
jdoerrie | 2f1af51 | 2018-10-03 00:59:37 | [diff] [blame] | 126 | for (auto i = current_browser.processes.begin(); |
[email protected] | e1873b9 | 2011-05-13 19:22:22 | [diff] [blame] | 127 | i != current_browser.processes.end(); ++i) { |
| 128 | // Check if this is one of the child processes whose data we collected |
| 129 | // on the IO thread, and if so copy over that data. |
| 130 | for (size_t child = 0; child < child_info.size(); child++) { |
| 131 | if (child_info[child].pid != i->pid) |
| 132 | continue; |
| 133 | i->titles = child_info[child].titles; |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 134 | i->process_type = child_info[child].process_type; |
[email protected] | e1873b9 | 2011-05-13 19:22:22 | [diff] [blame] | 135 | break; |
| 136 | } |
| 137 | } |
| 138 | |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 139 | process_data_.push_back(current_browser); |
| 140 | |
Yuta Hijikata | 235fc62b | 2020-12-08 03:48:32 | [diff] [blame] | 141 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
[email protected] | f413478 | 2013-08-29 21:25:20 | [diff] [blame] | 142 | base::GetSwapInfo(&swap_info_); |
[email protected] | 48e30344 | 2013-07-18 19:13:15 | [diff] [blame] | 143 | #endif |
| 144 | |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 145 | // Finally return to the browser thread. |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 146 | content::GetUIThreadTaskRunner({})->PostTask( |
| 147 | FROM_HERE, |
tzik | 3f7781d | 2017-04-20 17:09:33 | [diff] [blame] | 148 | base::BindOnce(&MemoryDetails::CollectChildInfoOnUIThread, this)); |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 149 | } |