Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[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 | |
avi | 6846aef | 2015-12-26 01:09:38 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 9 | #include <memory> |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 10 | #include <set> |
| 11 | #include <string> |
| 12 | |
Avi Drissman | eac566b0 | 2023-08-18 02:56:21 | [diff] [blame] | 13 | #include "base/apple/foundation_util.h" |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 14 | #include "base/file_version_info.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 15 | #include "base/files/file_path.h" |
Avi Drissman | 9269d4ed | 2023-01-07 01:38:06 | [diff] [blame] | 16 | #include "base/functional/bind.h" |
[email protected] | d09a4ce1c | 2013-07-24 17:37:02 | [diff] [blame] | 17 | #include "base/process/process_iterator.h" |
[email protected] | f9b29436 | 2013-06-10 20:22:31 | [diff] [blame] | 18 | #include "base/strings/string_util.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 19 | #include "base/strings/utf_string_conversions.h" |
Etienne Pierre-doray | 0fbce43 | 2018-08-27 20:27:51 | [diff] [blame] | 20 | #include "base/threading/scoped_blocking_call.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 21 | #include "base/threading/thread.h" |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 22 | #include "chrome/common/chrome_constants.h" |
| 23 | #include "chrome/common/url_constants.h" |
Henrique Ferreiro | d78ab26 | 2023-09-13 11:21:43 | [diff] [blame] | 24 | #include "chrome/grit/branded_strings.h" |
sdefresne | 9fb6769 | 2015-08-03 18:48:22 | [diff] [blame] | 25 | #include "components/version_info/version_info.h" |
asvitkine | 58409e4c | 2015-01-15 01:25:45 | [diff] [blame] | 26 | #include "content/public/browser/browser_child_process_host.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] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 30 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 31 | |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | // A helper for |CollectProcessData()|, collecting data on the Chrome/Chromium |
| 35 | // process with PID |pid|. The collected data is added to |processes|. |
| 36 | void CollectProcessDataForChromeProcess( |
| 37 | const std::vector<ProcessMemoryInformation>& child_info, |
| 38 | base::ProcessId pid, |
| 39 | ProcessMemoryInformationList* processes) { |
| 40 | ProcessMemoryInformation info; |
| 41 | info.pid = pid; |
| 42 | if (info.pid == base::GetCurrentProcId()) |
| 43 | info.process_type = content::PROCESS_TYPE_BROWSER; |
| 44 | else |
| 45 | info.process_type = content::PROCESS_TYPE_UNKNOWN; |
| 46 | |
sdefresne | 9fb6769 | 2015-08-03 18:48:22 | [diff] [blame] | 47 | info.product_name = base::ASCIIToUTF16(version_info::GetProductName()); |
| 48 | info.version = base::ASCIIToUTF16(version_info::GetVersionNumber()); |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 49 | |
Robert Sesek | 3aff336 | 2019-01-23 20:16:16 | [diff] [blame] | 50 | // A PortProvider is not necessary to acquire information about the number |
| 51 | // of open file descriptors. |
| 52 | std::unique_ptr<base::ProcessMetrics> metrics( |
| 53 | base::ProcessMetrics::CreateProcessMetrics(pid, nullptr)); |
| 54 | info.num_open_fds = metrics->GetOpenFdCount(); |
| 55 | info.open_fds_soft_limit = metrics->GetOpenFdSoftLimit(); |
| 56 | |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 57 | // Check if this is one of the child processes whose data was already |
| 58 | // collected and exists in |child_data|. |
| 59 | for (const ProcessMemoryInformation& child : child_info) { |
| 60 | if (child.pid == info.pid) { |
| 61 | info.titles = child.titles; |
| 62 | info.process_type = child.process_type; |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 67 | processes->push_back(info); |
| 68 | } |
| 69 | |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 70 | } // namespace |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 71 | |
asvitkine | 89406d1f | 2015-01-17 06:57:10 | [diff] [blame] | 72 | MemoryDetails::MemoryDetails() { |
asvitkine | 58409e4c | 2015-01-15 01:25:45 | [diff] [blame] | 73 | const base::FilePath browser_process_path = |
| 74 | base::GetProcessExecutablePath(base::GetCurrentProcessHandle()); |
asvitkine | 58409e4c | 2015-01-15 01:25:45 | [diff] [blame] | 75 | |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 76 | ProcessData process; |
| 77 | process.name = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); |
| 78 | process.process_name = |
| 79 | base::UTF8ToUTF16(browser_process_path.BaseName().value()); |
| 80 | process_data_.push_back(process); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | ProcessData* MemoryDetails::ChromeBrowser() { |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 84 | return &process_data_[0]; |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void MemoryDetails::CollectProcessData( |
[email protected] | 4df3ac6 | 2011-03-11 04:38:52 | [diff] [blame] | 88 | const std::vector<ProcessMemoryInformation>& child_info) { |
Etienne Bergeron | 436d4221 | 2019-02-26 17:15:12 | [diff] [blame] | 89 | base::ScopedBlockingCall scoped_blocking_call(FROM_HERE, |
| 90 | base::BlockingType::MAY_BLOCK); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 91 | |
| 92 | // Clear old data. |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 93 | process_data_[0].processes.clear(); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 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. |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 103 | std::vector<base::ProcessId> all_pids; |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 104 | { |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 105 | base::NamedProcessIterator process_it( |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 106 | base::UTF16ToUTF8(process_data_[0].process_name), NULL); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 107 | |
[email protected] | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 108 | while (const base::ProcessEntry* entry = process_it.NextProcessEntry()) { |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 109 | all_pids.push_back(entry->pid()); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
kerrnel | 0c61964 | 2015-09-21 18:39:54 | [diff] [blame] | 113 | // Get PIDs of the helper. |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 114 | { |
| 115 | base::NamedProcessIterator helper_it(chrome::kHelperProcessExecutableName, |
Leonard Grey | 77c540e | 2022-10-28 20:04:15 | [diff] [blame] | 116 | NULL, /*use_prefix_match=*/true); |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 117 | while (const base::ProcessEntry* entry = helper_it.NextProcessEntry()) { |
| 118 | all_pids.push_back(entry->pid()); |
| 119 | } |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 120 | } |
| 121 | |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 122 | ProcessMemoryInformationList* chrome_processes = &process_data_[0].processes; |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 123 | |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 124 | // Collect data about Chrome/Chromium. |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 125 | for (const base::ProcessId& pid : all_pids) |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 126 | CollectProcessDataForChromeProcess(child_info, pid, chrome_processes); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 127 | |
| 128 | // Finally return to the browser thread. |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 129 | content::GetUIThreadTaskRunner({})->PostTask( |
| 130 | FROM_HERE, |
kylechar | 99ef904 | 2019-02-25 18:09:43 | [diff] [blame] | 131 | base::BindOnce(&MemoryDetails::CollectChildInfoOnUIThread, this)); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 132 | } |