[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 | |
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 | |
[email protected] | 24d6969 | 2011-10-21 18:26:51 | [diff] [blame] | 13 | #include "base/bind.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 | 69fed02 | 2014-12-21 01:02:52 | [diff] [blame] | 16 | #include "base/mac/foundation_util.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" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 24 | #include "chrome/grit/chromium_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" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 27 | #include "content/public/browser/browser_thread.h" |
[email protected] | bd5d6cf | 2011-12-01 00:39:12 | [diff] [blame] | 28 | #include "content/public/common/process_type.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 29 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 30 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 31 | using content::BrowserThread; |
| 32 | |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 33 | namespace { |
| 34 | |
| 35 | // A helper for |CollectProcessData()|, collecting data on the Chrome/Chromium |
| 36 | // process with PID |pid|. The collected data is added to |processes|. |
| 37 | void CollectProcessDataForChromeProcess( |
| 38 | const std::vector<ProcessMemoryInformation>& child_info, |
| 39 | base::ProcessId pid, |
| 40 | ProcessMemoryInformationList* processes) { |
| 41 | ProcessMemoryInformation info; |
| 42 | info.pid = pid; |
| 43 | if (info.pid == base::GetCurrentProcId()) |
| 44 | info.process_type = content::PROCESS_TYPE_BROWSER; |
| 45 | else |
| 46 | info.process_type = content::PROCESS_TYPE_UNKNOWN; |
| 47 | |
sdefresne | 9fb6769 | 2015-08-03 18:48:22 | [diff] [blame] | 48 | info.product_name = base::ASCIIToUTF16(version_info::GetProductName()); |
| 49 | info.version = base::ASCIIToUTF16(version_info::GetVersionNumber()); |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 50 | |
| 51 | // Check if this is one of the child processes whose data was already |
| 52 | // collected and exists in |child_data|. |
| 53 | for (const ProcessMemoryInformation& child : child_info) { |
| 54 | if (child.pid == info.pid) { |
| 55 | info.titles = child.titles; |
| 56 | info.process_type = child.process_type; |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 61 | processes->push_back(info); |
| 62 | } |
| 63 | |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 64 | } // namespace |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 65 | |
asvitkine | 89406d1f | 2015-01-17 06:57:10 | [diff] [blame] | 66 | MemoryDetails::MemoryDetails() { |
asvitkine | 58409e4c | 2015-01-15 01:25:45 | [diff] [blame] | 67 | const base::FilePath browser_process_path = |
| 68 | base::GetProcessExecutablePath(base::GetCurrentProcessHandle()); |
asvitkine | 58409e4c | 2015-01-15 01:25:45 | [diff] [blame] | 69 | |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 70 | ProcessData process; |
| 71 | process.name = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); |
| 72 | process.process_name = |
| 73 | base::UTF8ToUTF16(browser_process_path.BaseName().value()); |
| 74 | process_data_.push_back(process); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | ProcessData* MemoryDetails::ChromeBrowser() { |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 78 | return &process_data_[0]; |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void MemoryDetails::CollectProcessData( |
[email protected] | 4df3ac6 | 2011-03-11 04:38:52 | [diff] [blame] | 82 | const std::vector<ProcessMemoryInformation>& child_info) { |
Etienne Pierre-doray | 0fbce43 | 2018-08-27 20:27:51 | [diff] [blame^] | 83 | base::ScopedBlockingCall scoped_blocking_call(base::BlockingType::MAY_BLOCK); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 84 | |
| 85 | // Clear old data. |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 86 | process_data_[0].processes.clear(); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 87 | |
| 88 | // First, we use |NamedProcessIterator| to get the PIDs of the processes we're |
| 89 | // interested in; we save our results to avoid extra calls to |
| 90 | // |NamedProcessIterator| (for performance reasons) and to avoid additional |
| 91 | // inconsistencies caused by racing. Then we run |/bin/ps| *once* to get |
| 92 | // information on those PIDs. Then we used our saved information to iterate |
| 93 | // over browsers, then over PIDs. |
| 94 | |
| 95 | // Get PIDs of main browser processes. |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 96 | std::vector<base::ProcessId> all_pids; |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 97 | { |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 98 | base::NamedProcessIterator process_it( |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 99 | base::UTF16ToUTF8(process_data_[0].process_name), NULL); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 100 | |
[email protected] | a5a00b1d | 2010-04-08 15:52:45 | [diff] [blame] | 101 | while (const base::ProcessEntry* entry = process_it.NextProcessEntry()) { |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 102 | all_pids.push_back(entry->pid()); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
kerrnel | 0c61964 | 2015-09-21 18:39:54 | [diff] [blame] | 106 | // Get PIDs of the helper. |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 107 | { |
| 108 | base::NamedProcessIterator helper_it(chrome::kHelperProcessExecutableName, |
| 109 | NULL); |
| 110 | while (const base::ProcessEntry* entry = helper_it.NextProcessEntry()) { |
| 111 | all_pids.push_back(entry->pid()); |
| 112 | } |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 113 | } |
| 114 | |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 115 | ProcessMemoryInformationList* chrome_processes = &process_data_[0].processes; |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 116 | |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 117 | // Collect data about Chrome/Chromium. |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame] | 118 | for (const base::ProcessId& pid : all_pids) |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 119 | CollectProcessDataForChromeProcess(child_info, pid, chrome_processes); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 120 | |
| 121 | // Finally return to the browser thread. |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 122 | BrowserThread::PostTask( |
| 123 | BrowserThread::UI, FROM_HERE, |
[email protected] | 24d6969 | 2011-10-21 18:26:51 | [diff] [blame] | 124 | base::Bind(&MemoryDetails::CollectChildInfoOnUIThread, this)); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 125 | } |