[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 | |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 9 | #include <set> |
| 10 | #include <string> |
| 11 | |
[email protected] | 24d6969 | 2011-10-21 18:26:51 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 13 | #include "base/file_version_info.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 14 | #include "base/files/file_path.h" |
avi | 69fed02 | 2014-12-21 01:02:52 | [diff] [blame] | 15 | #include "base/mac/foundation_util.h" |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 16 | #include "base/memory/scoped_ptr.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" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 20 | #include "base/threading/thread.h" |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 21 | #include "chrome/common/chrome_constants.h" |
| 22 | #include "chrome/common/url_constants.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 23 | #include "chrome/grit/chromium_strings.h" |
sdefresne | 9fb6769 | 2015-08-03 18:48:22 | [diff] [blame] | 24 | #include "components/version_info/version_info.h" |
asvitkine | 58409e4c | 2015-01-15 01:25:45 | [diff] [blame] | 25 | #include "content/public/browser/browser_child_process_host.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 26 | #include "content/public/browser/browser_thread.h" |
[email protected] | bd5d6cf | 2011-12-01 00:39:12 | [diff] [blame] | 27 | #include "content/public/common/process_type.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 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 30 | using content::BrowserThread; |
| 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 | |
| 50 | // Check if this is one of the child processes whose data was already |
| 51 | // collected and exists in |child_data|. |
| 52 | for (const ProcessMemoryInformation& child : child_info) { |
| 53 | if (child.pid == info.pid) { |
| 54 | info.titles = child.titles; |
| 55 | info.process_type = child.process_type; |
| 56 | break; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | scoped_ptr<base::ProcessMetrics> metrics; |
| 61 | metrics.reset(base::ProcessMetrics::CreateProcessMetrics( |
| 62 | pid, content::BrowserChildProcessHost::GetPortProvider())); |
asvitkine | 57e53d315 | 2015-01-23 01:42:50 | [diff] [blame] | 63 | metrics->GetCommittedAndWorkingSetKBytes(&info.committed, &info.working_set); |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 64 | |
| 65 | processes->push_back(info); |
| 66 | } |
| 67 | |
asvitkine | 6f5f359 | 2015-01-21 20:50:37 | [diff] [blame] | 68 | } // namespace |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 69 | |
asvitkine | 89406d1f | 2015-01-17 06:57:10 | [diff] [blame] | 70 | MemoryDetails::MemoryDetails() { |
asvitkine | 58409e4c | 2015-01-15 01:25:45 | [diff] [blame] | 71 | const base::FilePath browser_process_path = |
| 72 | base::GetProcessExecutablePath(base::GetCurrentProcessHandle()); |
asvitkine | 58409e4c | 2015-01-15 01:25:45 | [diff] [blame] | 73 | |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame^] | 74 | ProcessData process; |
| 75 | process.name = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); |
| 76 | process.process_name = |
| 77 | base::UTF8ToUTF16(browser_process_path.BaseName().value()); |
| 78 | process_data_.push_back(process); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | ProcessData* MemoryDetails::ChromeBrowser() { |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame^] | 82 | return &process_data_[0]; |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void MemoryDetails::CollectProcessData( |
[email protected] | 4df3ac6 | 2011-03-11 04:38:52 | [diff] [blame] | 86 | const std::vector<ProcessMemoryInformation>& child_info) { |
ellyjones | cd6e449d | 2016-04-13 19:31:15 | [diff] [blame^] | 87 | // TODO(ellyjones): Does this still need to be run in the blocking pool? |
| 88 | // It used to need to be because it ran /bin/ps, but it might not need to any |
| 89 | // more. |
hashimoto | a8ea28d | 2015-04-11 02:50:48 | [diff] [blame] | 90 | DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
[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, |
| 116 | NULL); |
| 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. |
[email protected] | f8b3ef8 | 2010-10-11 02:45:52 | [diff] [blame] | 129 | BrowserThread::PostTask( |
| 130 | BrowserThread::UI, FROM_HERE, |
[email protected] | 24d6969 | 2011-10-21 18:26:51 | [diff] [blame] | 131 | base::Bind(&MemoryDetails::CollectChildInfoOnUIThread, this)); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 132 | } |