blob: 21af097c741c806d6c523a9c011fc62284786b5c [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commit09911bf2008-07-26 23:55:294
5#ifndef CHROME_BROWSER_MEMORY_DETAILS_H_
6#define CHROME_BROWSER_MEMORY_DETAILS_H_
7
initial.commit09911bf2008-07-26 23:55:298#include <vector>
9
10#include "base/process_util.h"
11#include "base/ref_counted.h"
[email protected]a27a9382009-02-11 23:55:1012#include "chrome/common/child_process_info.h"
initial.commit09911bf2008-07-26 23:55:2913
initial.commit09911bf2008-07-26 23:55:2914// We collect data about each browser process. A browser may
15// have multiple processes (of course!). Even IE has multiple
16// processes these days.
17struct ProcessMemoryInformation {
[email protected]54fd1d32009-09-01 00:12:5818 ProcessMemoryInformation()
19 : pid(0),
20 num_processes(0),
21 is_diagnostics(false),
22 type(ChildProcessInfo::UNKNOWN_PROCESS) {
initial.commit09911bf2008-07-26 23:55:2923 }
24
25 // The process id.
[email protected]a4dc33f2009-10-20 15:09:5526 base::ProcessId pid;
initial.commit09911bf2008-07-26 23:55:2927 // The working set information.
[email protected]176aa482008-11-14 03:25:1528 base::WorkingSetKBytes working_set;
initial.commit09911bf2008-07-26 23:55:2929 // The committed bytes.
[email protected]176aa482008-11-14 03:25:1530 base::CommittedKBytes committed;
initial.commit09911bf2008-07-26 23:55:2931 // The process version
32 std::wstring version;
33 // The process product name.
34 std::wstring product_name;
35 // The number of processes which this memory represents.
36 int num_processes;
37 // A process is a diagnostics process if it is rendering
38 // about:xxx information.
39 bool is_diagnostics;
[email protected]a27a9382009-02-11 23:55:1040 // If this is a child process of Chrome, what type (i.e. plugin) it is.
41 ChildProcessInfo::ProcessType type;
42 // A collection of titles used, i.e. for a tab it'll show all the page titles.
43 std::vector<std::wstring> titles;
initial.commit09911bf2008-07-26 23:55:2944};
45
46typedef std::vector<ProcessMemoryInformation> ProcessMemoryInformationList;
47
initial.commit09911bf2008-07-26 23:55:2948// Browser Process Information.
49struct ProcessData {
[email protected]54fd1d32009-09-01 00:12:5850 std::wstring name;
51 std::wstring process_name;
initial.commit09911bf2008-07-26 23:55:2952 ProcessMemoryInformationList processes;
53};
54
[email protected]f164cea2009-11-05 23:37:4055#if defined(OS_MACOSX)
56class ProcessInfoSnapshot;
57#endif
58
initial.commit09911bf2008-07-26 23:55:2959// MemoryDetails fetches memory details about current running browsers.
60// Because this data can only be fetched asynchronously, callers use
61// this class via a callback.
62//
63// Example usage:
64//
65// class MyMemoryDetailConsumer : public MemoryDetails {
66//
[email protected]bfa5cf82009-11-20 21:48:0267// MyMemoryDetailConsumer() {
68// // Anything but |StartFetch()|.
initial.commit09911bf2008-07-26 23:55:2969// }
70//
[email protected]bfa5cf82009-11-20 21:48:0271// // (Or just call |StartFetch()| explicitly if there's nothing else to
72// // do.)
73// void StartDoingStuff() {
74// StartFetch(); // Starts fetching details.
75// // Etc.
76// }
77//
78// // Your other class stuff here
initial.commit09911bf2008-07-26 23:55:2979//
80// virtual void OnDetailsAvailable() {
[email protected]bfa5cf82009-11-20 21:48:0281// // do work with memory info here
initial.commit09911bf2008-07-26 23:55:2982// }
83// }
84class MemoryDetails : public base::RefCountedThreadSafe<MemoryDetails> {
85 public:
initial.commit09911bf2008-07-26 23:55:2986 // Constructor.
87 MemoryDetails();
initial.commit09911bf2008-07-26 23:55:2988
[email protected]54fd1d32009-09-01 00:12:5889 // Access to the process detail information. This data is only available
initial.commit09911bf2008-07-26 23:55:2990 // after OnDetailsAvailable() has been called.
[email protected]54fd1d32009-09-01 00:12:5891 const std::vector<ProcessData>& processes() { return process_data_; }
initial.commit09911bf2008-07-26 23:55:2992
initial.commit09911bf2008-07-26 23:55:2993 // Initiate updating the current memory details. These are fetched
94 // asynchronously because data must be collected from multiple threads.
95 // OnDetailsAvailable will be called when this process is complete.
96 void StartFetch();
97
98 virtual void OnDetailsAvailable() {}
99
[email protected]e6e6ba42009-11-07 01:56:19100 protected:
101 friend class base::RefCountedThreadSafe<MemoryDetails>;
102
103 virtual ~MemoryDetails() {}
104
initial.commit09911bf2008-07-26 23:55:29105 private:
[email protected]a27a9382009-02-11 23:55:10106 // Collect child process information on the IO thread. This is needed because
107 // information about some child process types (i.e. plugins) can only be taken
108 // on that thread. The data will be used by about:memory. When finished,
109 // invokes back to the file thread to run the rest of the about:memory
110 // functionality.
111 void CollectChildInfoOnIOThread();
initial.commit09911bf2008-07-26 23:55:29112
113 // Collect current process information from the OS and store it
114 // for processing. If data has already been collected, clears old
115 // data and re-collects the data.
116 // Note - this function enumerates memory details from many processes
[email protected]a27a9382009-02-11 23:55:10117 // and is fairly expensive to run, hence it's run on the file thread.
118 // The parameter holds information about processes from the IO thread.
119 void CollectProcessData(std::vector<ProcessMemoryInformation>);
initial.commit09911bf2008-07-26 23:55:29120
[email protected]f164cea2009-11-05 23:37:40121#if defined(OS_MACOSX)
122 // A helper for |CollectProcessData()|, collecting data on the Chrome/Chromium
123 // process with PID |pid|. The collected data is added to the state of the
124 // object (in |process_data_|).
125 void CollectProcessDataChrome(
126 const std::vector<ProcessMemoryInformation>& child_info,
127 base::ProcessId pid,
128 const ProcessInfoSnapshot& process_info);
129#endif
130
[email protected]a27a9382009-02-11 23:55:10131 // Collect child process information on the UI thread. Information about
132 // renderer processes is only available there.
133 void CollectChildInfoOnUIThread();
initial.commit09911bf2008-07-26 23:55:29134
135 // Each time we take a memory sample, we do a little work to update
136 // the global histograms for tracking memory usage.
137 void UpdateHistograms();
138
[email protected]54fd1d32009-09-01 00:12:58139 // Returns a pointer to the ProcessData structure for Chrome.
140 ProcessData* ChromeBrowser();
141
142 std::vector<ProcessData> process_data_;
initial.commit09911bf2008-07-26 23:55:29143
144 DISALLOW_EVIL_CONSTRUCTORS(MemoryDetails);
145};
146
147#endif // CHROME_BROWSER_MEMORY_DETAILS_H_