OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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. |
| 4 |
| 5 #include "chrome/browser/memory_details.h" |
| 6 |
| 7 #include <set> |
| 8 #include <string> |
| 9 |
| 10 #include "app/l10n_util.h" |
| 11 #include "base/basictypes.h" |
| 12 #include "base/file_path.h" |
| 13 #include "base/file_version_info.h" |
| 14 #include "base/mac_util.h" |
| 15 #include "base/string_util.h" |
| 16 #include "base/process_util.h" |
| 17 #include "base/thread.h" |
| 18 #include "chrome/browser/browser_process.h" |
| 19 #include "chrome/browser/chrome_thread.h" |
| 20 #include "chrome/browser/process_info_snapshot.h" |
| 21 #include "chrome/browser/renderer_host/backing_store_manager.h" |
| 22 #include "chrome/browser/renderer_host/render_process_host.h" |
| 23 #include "chrome/browser/tab_contents/navigation_entry.h" |
| 24 #include "chrome/browser/tab_contents/tab_contents.h" |
| 25 #include "chrome/common/child_process_host.h" |
| 26 #include "chrome/common/chrome_constants.h" |
| 27 #include "chrome/common/url_constants.h" |
| 28 #include "grit/chromium_strings.h" |
| 29 |
| 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. |
| 33 |
| 34 class RenderViewHostDelegate; |
| 35 |
| 36 // Known browsers which we collect details for. |CHROME_BROWSER| *must* be the |
| 37 // first browser listed. The order here must match those in |process_template| |
| 38 // (in |MemoryDetails::MemoryDetails()| below). |
| 39 // TODO(viettrungluu): In the big refactoring (see above), get rid of this order |
| 40 // dependence. |
| 41 enum BrowserType { |
| 42 // TODO(viettrungluu): possibly add more? |
| 43 CHROME_BROWSER = 0, |
| 44 SAFARI_BROWSER, |
| 45 FIREFOX_BROWSER, |
| 46 CAMINO_BROWSER, |
| 47 OPERA_BROWSER, |
| 48 OMNIWEB_BROWSER, |
| 49 MAX_BROWSERS |
| 50 } BrowserProcess; |
| 51 |
| 52 |
| 53 MemoryDetails::MemoryDetails() { |
| 54 static const std::wstring google_browser_name = |
| 55 l10n_util::GetString(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 ProcessData process_template[MAX_BROWSERS] = { |
| 63 { google_browser_name.c_str(), chrome::kBrowserProcessExecutableName, }, |
| 64 { L"Safari", L"Safari", }, |
| 65 { L"Firefox", L"firefox-bin", }, |
| 66 { L"Camino", L"Camino", }, |
| 67 { L"Opera", L"Opera", }, |
| 68 { L"OmniWeb", L"OmniWeb", }, |
| 69 }; |
| 70 |
| 71 for (size_t index = 0; index < arraysize(process_template); ++index) { |
| 72 ProcessData process; |
| 73 process.name = process_template[index].name; |
| 74 process.process_name = process_template[index].process_name; |
| 75 process_data_.push_back(process); |
| 76 } |
| 77 } |
| 78 |
| 79 ProcessData* MemoryDetails::ChromeBrowser() { |
| 80 return &process_data_[CHROME_BROWSER]; |
| 81 } |
| 82 |
| 83 void MemoryDetails::CollectProcessData( |
| 84 std::vector<ProcessMemoryInformation> child_info) { |
| 85 // This must be run on the file thread to avoid jank (|ProcessInfoSnapshot| |
| 86 // runs /bin/ps, which isn't instantaneous). |
| 87 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
| 88 |
| 89 // Clear old data. |
| 90 for (size_t index = 0; index < MAX_BROWSERS; index++) |
| 91 process_data_[index].processes.clear(); |
| 92 |
| 93 // First, we use |NamedProcessIterator| to get the PIDs of the processes we're |
| 94 // interested in; we save our results to avoid extra calls to |
| 95 // |NamedProcessIterator| (for performance reasons) and to avoid additional |
| 96 // inconsistencies caused by racing. Then we run |/bin/ps| *once* to get |
| 97 // information on those PIDs. Then we used our saved information to iterate |
| 98 // over browsers, then over PIDs. |
| 99 |
| 100 // Get PIDs of main browser processes. |
| 101 std::vector<base::ProcessId> pids_by_browser[MAX_BROWSERS]; |
| 102 std::vector<base::ProcessId> all_pids; |
| 103 for (size_t index = CHROME_BROWSER; index < MAX_BROWSERS; index++) { |
| 104 base::NamedProcessIterator process_it(process_data_[index].process_name, |
| 105 NULL); |
| 106 |
| 107 while (const ProcessEntry* process_entry = process_it.NextProcessEntry()) { |
| 108 pids_by_browser[index].push_back(process_entry->pid); |
| 109 all_pids.push_back(process_entry->pid); |
| 110 } |
| 111 } |
| 112 |
| 113 // Get PIDs of helpers. |
| 114 std::vector<base::ProcessId> helper_pids; |
| 115 { |
| 116 base::NamedProcessIterator helper_it(chrome::kHelperProcessExecutableName, |
| 117 NULL); |
| 118 while (const ProcessEntry* process_entry = helper_it.NextProcessEntry()) { |
| 119 helper_pids.push_back(process_entry->pid); |
| 120 all_pids.push_back(process_entry->pid); |
| 121 } |
| 122 } |
| 123 |
| 124 // Capture information about the processes we're interested in. |
| 125 ProcessInfoSnapshot process_info; |
| 126 process_info.Sample(all_pids); |
| 127 |
| 128 // Handle the other processes first. |
| 129 for (size_t index = CHROME_BROWSER + 1; index < MAX_BROWSERS; index++) { |
| 130 for (std::vector<base::ProcessId>::const_iterator it = |
| 131 pids_by_browser[index].begin(); |
| 132 it != pids_by_browser[index].end(); ++it) { |
| 133 ProcessMemoryInformation info; |
| 134 info.pid = *it; |
| 135 info.type = ChildProcessInfo::UNKNOWN_PROCESS; |
| 136 |
| 137 // Try to get version information. To do this, we need first to get the |
| 138 // executable's name (we can only believe |proc_info.command| if it looks |
| 139 // like an absolute path). Then we need strip the executable's name back |
| 140 // to the bundle's name. And only then can we try to get the version. |
| 141 scoped_ptr<FileVersionInfo> version_info; |
| 142 ProcessInfoSnapshot::ProcInfoEntry proc_info; |
| 143 if (process_info.GetProcInfo(info.pid, &proc_info)) { |
| 144 if (proc_info.command.length() > 1 && proc_info.command[0] == '/') { |
| 145 FilePath bundle_name = |
| 146 mac_util::GetAppBundlePath(FilePath(proc_info.command)); |
| 147 if (!bundle_name.empty()) { |
| 148 version_info.reset(FileVersionInfo::CreateFileVersionInfo( |
| 149 bundle_name)); |
| 150 } |
| 151 } |
| 152 } |
| 153 if (version_info.get()) { |
| 154 info.product_name = version_info->product_name(); |
| 155 info.version = version_info->product_version(); |
| 156 } else { |
| 157 info.product_name = process_data_[index].name; |
| 158 info.version = L""; |
| 159 } |
| 160 |
| 161 // Memory info. |
| 162 process_info.GetCommittedKBytesOfPID(info.pid, &info.committed); |
| 163 process_info.GetWorkingSetKBytesOfPID(info.pid, &info.working_set); |
| 164 |
| 165 // Add the process info to our list. |
| 166 process_data_[index].processes.push_back(info); |
| 167 } |
| 168 } |
| 169 |
| 170 // Collect data about Chrome/Chromium. |
| 171 for (std::vector<base::ProcessId>::const_iterator it = |
| 172 pids_by_browser[CHROME_BROWSER].begin(); |
| 173 it != pids_by_browser[CHROME_BROWSER].end(); ++it) { |
| 174 CollectProcessDataChrome(child_info, *it, process_info); |
| 175 } |
| 176 |
| 177 // And collect data about the helpers. |
| 178 for (std::vector<base::ProcessId>::const_iterator it = helper_pids.begin(); |
| 179 it != helper_pids.end(); ++it) { |
| 180 CollectProcessDataChrome(child_info, *it, process_info); |
| 181 } |
| 182 |
| 183 // Finally return to the browser thread. |
| 184 ChromeThread::PostTask( |
| 185 ChromeThread::UI, FROM_HERE, |
| 186 NewRunnableMethod(this, &MemoryDetails::CollectChildInfoOnUIThread)); |
| 187 } |
| 188 |
| 189 void MemoryDetails::CollectProcessDataChrome( |
| 190 const std::vector<ProcessMemoryInformation>& child_info, |
| 191 base::ProcessId pid, |
| 192 const ProcessInfoSnapshot& process_info) { |
| 193 ProcessMemoryInformation info; |
| 194 info.pid = pid; |
| 195 if (info.pid == base::GetCurrentProcId()) |
| 196 info.type = ChildProcessInfo::BROWSER_PROCESS; |
| 197 else |
| 198 info.type = ChildProcessInfo::UNKNOWN_PROCESS; |
| 199 |
| 200 scoped_ptr<FileVersionInfo> version_info( |
| 201 FileVersionInfo::CreateFileVersionInfoForCurrentModule()); |
| 202 if (version_info.get()) { |
| 203 info.product_name = version_info->product_name(); |
| 204 info.version = version_info->product_version(); |
| 205 } else { |
| 206 info.product_name = process_data_[CHROME_BROWSER].name; |
| 207 info.version = L""; |
| 208 } |
| 209 |
| 210 // Check if this is one of the child processes whose data we collected |
| 211 // on the IO thread, and if so copy over that data. |
| 212 for (size_t child = 0; child < child_info.size(); child++) { |
| 213 if (child_info[child].pid == info.pid) { |
| 214 info.titles = child_info[child].titles; |
| 215 info.type = child_info[child].type; |
| 216 break; |
| 217 } |
| 218 } |
| 219 |
| 220 // Memory info. |
| 221 process_info.GetCommittedKBytesOfPID(info.pid, &info.committed); |
| 222 process_info.GetWorkingSetKBytesOfPID(info.pid, &info.working_set); |
| 223 |
| 224 // Add the process info to our list. |
| 225 process_data_[CHROME_BROWSER].processes.push_back(info); |
| 226 } |
OLD | NEW |