blob: b29fa705449b4200a5157bd6a0b3d870b9d5ffe0 [file] [log] [blame]
[email protected]4306df72012-04-20 18:58:571// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]54fd1d32009-09-01 00:12:582// 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"
[email protected]b90d7e802011-01-09 16:32:206
[email protected]54fd1d32009-09-01 00:12:587#include <psapi.h>
avi6846aef2015-12-26 01:09:388#include <stddef.h>
[email protected]d09a4ce1c2013-07-24 17:37:029#include <TlHelp32.h>
[email protected]54fd1d32009-09-01 00:12:5810
[email protected]24d69692011-10-21 18:26:5111#include "base/bind.h"
[email protected]54fd1d32009-09-01 00:12:5812#include "base/file_version_info.h"
[email protected]57999812013-02-24 05:40:5213#include "base/files/file_path.h"
dcheng6003e0b2016-04-09 18:42:3414#include "base/memory/scoped_ptr.h"
asvitkine58409e4c2015-01-15 01:25:4515#include "base/path_service.h"
[email protected]f9b294362013-06-10 20:22:3116#include "base/strings/string_util.h"
[email protected]112158af2013-06-07 23:46:1817#include "base/strings/utf_string_conversions.h"
[email protected]b90d7e802011-01-09 16:32:2018#include "base/win/scoped_handle.h"
[email protected]1e67c2b2011-03-04 01:17:3719#include "base/win/windows_version.h"
[email protected]54fd1d32009-09-01 00:12:5820#include "chrome/common/url_constants.h"
[email protected]af39f002014-08-22 10:18:1821#include "chrome/grit/chromium_strings.h"
sdefresne9fb67692015-08-03 18:48:2222#include "components/version_info/version_info.h"
[email protected]c38831a12011-10-28 12:44:4923#include "content/public/browser/browser_thread.h"
[email protected]bd5d6cf2011-12-01 00:39:1224#include "content/public/common/process_type.h"
[email protected]c051a1b2011-01-21 23:30:1725#include "ui/base/l10n/l10n_util.h"
[email protected]54fd1d32009-09-01 00:12:5826
[email protected]631bb742011-11-02 11:29:3927using content::BrowserThread;
28
[email protected]54fd1d32009-09-01 00:12:5829// Known browsers which we collect details for.
mgiuca88867d32015-07-08 04:08:2430enum BrowserProcess {
[email protected]54fd1d32009-09-01 00:12:5831 CHROME_BROWSER = 0,
mgiuca88867d32015-07-08 04:08:2432};
[email protected]54fd1d32009-09-01 00:12:5833
asvitkine89406d1f2015-01-17 06:57:1034MemoryDetails::MemoryDetails() {
asvitkine58409e4c2015-01-15 01:25:4535 base::FilePath browser_process_path;
36 PathService::Get(base::FILE_EXE, &browser_process_path);
asvitkine58409e4c2015-01-15 01:25:4537
ellyjonescd6e449d2016-04-13 19:31:1538 ProcessData process;
39 process.name = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
40 process.process_name = browser_process_path.BaseName().value();
41 process_data_.push_back(process);
[email protected]54fd1d32009-09-01 00:12:5842}
43
44ProcessData* MemoryDetails::ChromeBrowser() {
ellyjonescd6e449d2016-04-13 19:31:1545 return &process_data_[0];
[email protected]54fd1d32009-09-01 00:12:5846}
47
48void MemoryDetails::CollectProcessData(
[email protected]4df3ac62011-03-11 04:38:5249 const std::vector<ProcessMemoryInformation>& child_info) {
hashimotoa8ea28d2015-04-11 02:50:4850 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
[email protected]54fd1d32009-09-01 00:12:5851
52 // Clear old data.
ellyjonescd6e449d2016-04-13 19:31:1553 process_data_[0].processes.clear();
[email protected]54fd1d32009-09-01 00:12:5854
[email protected]b90d7e802011-01-09 16:32:2055 base::win::ScopedHandle snapshot(
56 ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
[email protected]54fd1d32009-09-01 00:12:5857 PROCESSENTRY32 process_entry = {sizeof(PROCESSENTRY32)};
58 if (!snapshot.Get()) {
59 LOG(ERROR) << "CreateToolhelp32Snaphot failed: " << GetLastError();
60 return;
61 }
rvargasd7743ba2014-09-25 01:35:2862 if (!::Process32First(snapshot.Get(), &process_entry)) {
[email protected]54fd1d32009-09-01 00:12:5863 LOG(ERROR) << "Process32First failed: " << GetLastError();
64 return;
65 }
66 do {
[email protected]a4dc33f2009-10-20 15:09:5567 base::ProcessId pid = process_entry.th32ProcessID;
[email protected]1e67c2b2011-03-04 01:17:3768 base::win::ScopedHandle process_handle(::OpenProcess(
[email protected]54fd1d32009-09-01 00:12:5869 PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid));
rvargasd7743ba2014-09-25 01:35:2870 if (!process_handle.IsValid())
[email protected]54fd1d32009-09-01 00:12:5871 continue;
ellyjonescd6e449d2016-04-13 19:31:1572 if (_wcsicmp(process_data_[0].process_name.c_str(),
73 process_entry.szExeFile) != 0)
74 continue;
75 // Get Memory Information.
76 ProcessMemoryInformation info;
77 info.pid = pid;
78 if (info.pid == GetCurrentProcessId())
79 info.process_type = content::PROCESS_TYPE_BROWSER;
80 else
81 info.process_type = content::PROCESS_TYPE_UNKNOWN;
82
83 scoped_ptr<base::ProcessMetrics> metrics;
84 metrics.reset(
85 base::ProcessMetrics::CreateProcessMetrics(process_handle.Get()));
86 metrics->GetCommittedKBytes(&info.committed);
87 metrics->GetWorkingSetKBytes(&info.working_set);
88
89 // Get Version Information.
90 info.version = base::ASCIIToUTF16(version_info::GetVersionNumber());
91 // Check if this is one of the child processes whose data we collected
92 // on the IO thread, and if so copy over that data.
93 for (size_t child = 0; child < child_info.size(); child++) {
94 if (child_info[child].pid != info.pid)
[email protected]54fd1d32009-09-01 00:12:5895 continue;
ellyjonescd6e449d2016-04-13 19:31:1596 info.titles = child_info[child].titles;
97 info.process_type = child_info[child].process_type;
[email protected]54fd1d32009-09-01 00:12:5898 break;
99 }
ellyjonescd6e449d2016-04-13 19:31:15100
101 // Add the process info to our list.
102 process_data_[0].processes.push_back(info);
rvargasd7743ba2014-09-25 01:35:28103 } while (::Process32Next(snapshot.Get(), &process_entry));
[email protected]54fd1d32009-09-01 00:12:58104
105 // Finally return to the browser thread.
[email protected]f8b3ef82010-10-11 02:45:52106 BrowserThread::PostTask(
107 BrowserThread::UI, FROM_HERE,
[email protected]24d69692011-10-21 18:26:51108 base::Bind(&MemoryDetails::CollectChildInfoOnUIThread, this));
[email protected]54fd1d32009-09-01 00:12:58109}