seccomp-bpf: Remove legacy SandboxBPFPolicy class
[chromium-blink-merge.git] / chrome / browser / memory_details_mac.cc
blobf63635a85c20d671c20f1e560855896bfe3d9876
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/memory_details.h"
7 #include <set>
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/file_version_info.h"
13 #include "base/files/file_path.h"
14 #include "base/mac/mac_util.h"
15 #include "base/process/process_iterator.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/threading/thread.h"
19 #include "chrome/browser/process_info_snapshot.h"
20 #include "chrome/common/chrome_constants.h"
21 #include "chrome/common/chrome_version_info.h"
22 #include "chrome/common/url_constants.h"
23 #include "chrome/grit/chromium_strings.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "content/public/common/process_type.h"
26 #include "ui/base/l10n/l10n_util.h"
28 using content::BrowserThread;
30 // TODO(viettrungluu): Many of the TODOs below are subsumed by a general need to
31 // refactor the about:memory code (not just on Mac, but probably on other
32 // platforms as well). I've filed crbug.com/25456.
34 // Known browsers which we collect details for. |CHROME_BROWSER| *must* be the
35 // first browser listed. The order here must match those in |process_template|
36 // (in |MemoryDetails::MemoryDetails()| below).
37 // TODO(viettrungluu): In the big refactoring (see above), get rid of this order
38 // dependence.
39 enum BrowserType {
40 // TODO(viettrungluu): possibly add more?
41 CHROME_BROWSER = 0,
42 SAFARI_BROWSER,
43 FIREFOX_BROWSER,
44 CAMINO_BROWSER,
45 OPERA_BROWSER,
46 OMNIWEB_BROWSER,
47 MAX_BROWSERS
48 } BrowserProcess;
51 MemoryDetails::MemoryDetails()
52 : user_metrics_mode_(UPDATE_USER_METRICS),
53 memory_growth_tracker_(NULL) {
54 const std::string google_browser_name =
55 l10n_util::GetStringUTF8(IDS_PRODUCT_NAME);
56 // (Human and process) names of browsers; should match the ordering for
57 // |BrowserProcess| (i.e., |BrowserType|).
58 // TODO(viettrungluu): The current setup means that we can't detect both
59 // Chrome and Chromium at the same time!
60 // TODO(viettrungluu): Get localized browser names for other browsers
61 // (crbug.com/25779).
62 struct {
63 const char* name;
64 const char* process_name;
65 } process_template[MAX_BROWSERS] = {
66 { google_browser_name.c_str(), chrome::kBrowserProcessExecutableName, },
67 { "Safari", "Safari", },
68 { "Firefox", "firefox-bin", },
69 { "Camino", "Camino", },
70 { "Opera", "Opera", },
71 { "OmniWeb", "OmniWeb", },
74 for (size_t index = 0; index < MAX_BROWSERS; ++index) {
75 ProcessData process;
76 process.name = base::UTF8ToUTF16(process_template[index].name);
77 process.process_name =
78 base::UTF8ToUTF16(process_template[index].process_name);
79 process_data_.push_back(process);
83 ProcessData* MemoryDetails::ChromeBrowser() {
84 return &process_data_[CHROME_BROWSER];
87 void MemoryDetails::CollectProcessData(
88 const std::vector<ProcessMemoryInformation>& child_info) {
89 // This must be run on the file thread to avoid jank (|ProcessInfoSnapshot|
90 // runs /bin/ps, which isn't instantaneous).
91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
93 // Clear old data.
94 for (size_t index = 0; index < MAX_BROWSERS; index++)
95 process_data_[index].processes.clear();
97 // First, we use |NamedProcessIterator| to get the PIDs of the processes we're
98 // interested in; we save our results to avoid extra calls to
99 // |NamedProcessIterator| (for performance reasons) and to avoid additional
100 // inconsistencies caused by racing. Then we run |/bin/ps| *once* to get
101 // information on those PIDs. Then we used our saved information to iterate
102 // over browsers, then over PIDs.
104 // Get PIDs of main browser processes.
105 std::vector<base::ProcessId> pids_by_browser[MAX_BROWSERS];
106 std::vector<base::ProcessId> all_pids;
107 for (size_t index = CHROME_BROWSER; index < MAX_BROWSERS; index++) {
108 base::NamedProcessIterator process_it(
109 base::UTF16ToUTF8(process_data_[index].process_name), NULL);
111 while (const base::ProcessEntry* entry = process_it.NextProcessEntry()) {
112 pids_by_browser[index].push_back(entry->pid());
113 all_pids.push_back(entry->pid());
117 // The helper might show up as these different flavors depending on the
118 // executable flags required.
119 std::vector<std::string> helper_names;
120 helper_names.push_back(chrome::kHelperProcessExecutableName);
121 for (const char* const* suffix = chrome::kHelperFlavorSuffixes;
122 *suffix;
123 ++suffix) {
124 std::string helper_name = chrome::kHelperProcessExecutableName;
125 helper_name.append(1, ' ');
126 helper_name.append(*suffix);
127 helper_names.push_back(helper_name);
130 // Get PIDs of helpers.
131 std::vector<base::ProcessId> helper_pids;
132 for (size_t i = 0; i < helper_names.size(); ++i) {
133 std::string helper_name = helper_names[i];
134 base::NamedProcessIterator helper_it(helper_name, NULL);
135 while (const base::ProcessEntry* entry = helper_it.NextProcessEntry()) {
136 helper_pids.push_back(entry->pid());
137 all_pids.push_back(entry->pid());
141 // Capture information about the processes we're interested in.
142 ProcessInfoSnapshot process_info;
143 process_info.Sample(all_pids);
145 // Handle the other processes first.
146 for (size_t index = CHROME_BROWSER + 1; index < MAX_BROWSERS; index++) {
147 for (std::vector<base::ProcessId>::const_iterator it =
148 pids_by_browser[index].begin();
149 it != pids_by_browser[index].end(); ++it) {
150 ProcessMemoryInformation info;
151 info.pid = *it;
152 info.process_type = content::PROCESS_TYPE_UNKNOWN;
154 // Try to get version information. To do this, we need first to get the
155 // executable's name (we can only believe |proc_info.command| if it looks
156 // like an absolute path). Then we need strip the executable's name back
157 // to the bundle's name. And only then can we try to get the version.
158 scoped_ptr<FileVersionInfo> version_info;
159 ProcessInfoSnapshot::ProcInfoEntry proc_info;
160 if (process_info.GetProcInfo(info.pid, &proc_info)) {
161 if (proc_info.command.length() > 1 && proc_info.command[0] == '/') {
162 base::FilePath bundle_name =
163 base::mac::GetAppBundlePath(base::FilePath(proc_info.command));
164 if (!bundle_name.empty()) {
165 version_info.reset(FileVersionInfo::CreateFileVersionInfo(
166 bundle_name));
170 if (version_info.get()) {
171 info.product_name = version_info->product_name();
172 info.version = version_info->product_version();
173 } else {
174 info.product_name = process_data_[index].name;
175 info.version = base::string16();
178 // Memory info.
179 process_info.GetCommittedKBytesOfPID(info.pid, &info.committed);
180 process_info.GetWorkingSetKBytesOfPID(info.pid, &info.working_set);
182 // Add the process info to our list.
183 process_data_[index].processes.push_back(info);
187 // Collect data about Chrome/Chromium.
188 for (std::vector<base::ProcessId>::const_iterator it =
189 pids_by_browser[CHROME_BROWSER].begin();
190 it != pids_by_browser[CHROME_BROWSER].end(); ++it) {
191 CollectProcessDataChrome(child_info, *it, process_info);
194 // And collect data about the helpers.
195 for (std::vector<base::ProcessId>::const_iterator it = helper_pids.begin();
196 it != helper_pids.end(); ++it) {
197 CollectProcessDataChrome(child_info, *it, process_info);
200 // Finally return to the browser thread.
201 BrowserThread::PostTask(
202 BrowserThread::UI, FROM_HERE,
203 base::Bind(&MemoryDetails::CollectChildInfoOnUIThread, this));
206 void MemoryDetails::CollectProcessDataChrome(
207 const std::vector<ProcessMemoryInformation>& child_info,
208 base::ProcessId pid,
209 const ProcessInfoSnapshot& process_info) {
210 ProcessMemoryInformation info;
211 info.pid = pid;
212 if (info.pid == base::GetCurrentProcId())
213 info.process_type = content::PROCESS_TYPE_BROWSER;
214 else
215 info.process_type = content::PROCESS_TYPE_UNKNOWN;
217 chrome::VersionInfo version_info;
218 info.product_name = base::ASCIIToUTF16(version_info.Name());
219 info.version = base::ASCIIToUTF16(version_info.Version());
221 // Check if this is one of the child processes whose data we collected
222 // on the IO thread, and if so copy over that data.
223 for (size_t child = 0; child < child_info.size(); child++) {
224 if (child_info[child].pid == info.pid) {
225 info.titles = child_info[child].titles;
226 info.process_type = child_info[child].process_type;
227 break;
231 // Memory info.
232 process_info.GetCommittedKBytesOfPID(info.pid, &info.committed);
233 process_info.GetWorkingSetKBytesOfPID(info.pid, &info.working_set);
235 // Add the process info to our list.
236 process_data_[CHROME_BROWSER].processes.push_back(info);