blob: e3e2204d0bb8d30f12da6bec3b750f2d1ac1ff36 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// 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_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
initial.commit09911bf2008-07-26 23:55:298
initial.commit09911bf2008-07-26 23:55:299#include <vector>
10
[email protected]3b63f8f42011-03-28 01:54:1511#include "base/memory/ref_counted.h"
initial.commit09911bf2008-07-26 23:55:2912#include "base/process_util.h"
[email protected]4f260d02010-12-23 18:35:4213#include "base/string16.h"
[email protected]6faf7b12011-03-09 19:24:1414#include "content/common/child_process_info.h"
initial.commit09911bf2008-07-26 23:55:2915
initial.commit09911bf2008-07-26 23:55:2916// We collect data about each browser process. A browser may
17// have multiple processes (of course!). Even IE has multiple
18// processes these days.
19struct ProcessMemoryInformation {
[email protected]8e383412010-10-19 16:57:0320 ProcessMemoryInformation();
21 ~ProcessMemoryInformation();
initial.commit09911bf2008-07-26 23:55:2922
23 // The process id.
[email protected]a4dc33f2009-10-20 15:09:5524 base::ProcessId pid;
initial.commit09911bf2008-07-26 23:55:2925 // The working set information.
[email protected]176aa482008-11-14 03:25:1526 base::WorkingSetKBytes working_set;
initial.commit09911bf2008-07-26 23:55:2927 // The committed bytes.
[email protected]176aa482008-11-14 03:25:1528 base::CommittedKBytes committed;
initial.commit09911bf2008-07-26 23:55:2929 // The process version
[email protected]4f260d02010-12-23 18:35:4230 string16 version;
initial.commit09911bf2008-07-26 23:55:2931 // The process product name.
[email protected]4f260d02010-12-23 18:35:4232 string16 product_name;
initial.commit09911bf2008-07-26 23:55:2933 // The number of processes which this memory represents.
34 int num_processes;
[email protected]fcf79352010-12-28 20:13:2035 // A process is a diagnostics process if it is rendering about:memory.
36 // Mark this specially so that it can avoid counting it in its own
37 // results.
initial.commit09911bf2008-07-26 23:55:2938 bool is_diagnostics;
[email protected]a27a9382009-02-11 23:55:1039 // If this is a child process of Chrome, what type (i.e. plugin) it is.
40 ChildProcessInfo::ProcessType type;
[email protected]fcf79352010-12-28 20:13:2041 // If this is a renderer process, what type it is.
42 ChildProcessInfo::RendererProcessType renderer_type;
[email protected]a27a9382009-02-11 23:55:1043 // A collection of titles used, i.e. for a tab it'll show all the page titles.
[email protected]4f260d02010-12-23 18:35:4244 std::vector<string16> titles;
initial.commit09911bf2008-07-26 23:55:2945};
46
47typedef std::vector<ProcessMemoryInformation> ProcessMemoryInformationList;
48
initial.commit09911bf2008-07-26 23:55:2949// Browser Process Information.
50struct ProcessData {
[email protected]93aa89c72010-10-20 21:32:0451 ProcessData();
52 ProcessData(const ProcessData& rhs);
53 ~ProcessData();
54 ProcessData& operator=(const ProcessData& rhs);
55
[email protected]4f260d02010-12-23 18:35:4256 string16 name;
57 string16 process_name;
initial.commit09911bf2008-07-26 23:55:2958 ProcessMemoryInformationList processes;
59};
60
[email protected]f164cea2009-11-05 23:37:4061#if defined(OS_MACOSX)
62class ProcessInfoSnapshot;
63#endif
64
initial.commit09911bf2008-07-26 23:55:2965// MemoryDetails fetches memory details about current running browsers.
66// Because this data can only be fetched asynchronously, callers use
67// this class via a callback.
68//
69// Example usage:
70//
71// class MyMemoryDetailConsumer : public MemoryDetails {
72//
[email protected]bfa5cf82009-11-20 21:48:0273// MyMemoryDetailConsumer() {
74// // Anything but |StartFetch()|.
initial.commit09911bf2008-07-26 23:55:2975// }
76//
[email protected]bfa5cf82009-11-20 21:48:0277// // (Or just call |StartFetch()| explicitly if there's nothing else to
78// // do.)
79// void StartDoingStuff() {
80// StartFetch(); // Starts fetching details.
81// // Etc.
82// }
83//
84// // Your other class stuff here
initial.commit09911bf2008-07-26 23:55:2985//
86// virtual void OnDetailsAvailable() {
[email protected]bfa5cf82009-11-20 21:48:0287// // do work with memory info here
initial.commit09911bf2008-07-26 23:55:2988// }
89// }
90class MemoryDetails : public base::RefCountedThreadSafe<MemoryDetails> {
91 public:
initial.commit09911bf2008-07-26 23:55:2992 // Constructor.
93 MemoryDetails();
initial.commit09911bf2008-07-26 23:55:2994
[email protected]54fd1d32009-09-01 00:12:5895 // Access to the process detail information. This data is only available
initial.commit09911bf2008-07-26 23:55:2996 // after OnDetailsAvailable() has been called.
[email protected]54fd1d32009-09-01 00:12:5897 const std::vector<ProcessData>& processes() { return process_data_; }
initial.commit09911bf2008-07-26 23:55:2998
initial.commit09911bf2008-07-26 23:55:2999 // Initiate updating the current memory details. These are fetched
100 // asynchronously because data must be collected from multiple threads.
101 // OnDetailsAvailable will be called when this process is complete.
102 void StartFetch();
103
104 virtual void OnDetailsAvailable() {}
105
[email protected]e6e6ba42009-11-07 01:56:19106 protected:
107 friend class base::RefCountedThreadSafe<MemoryDetails>;
108
[email protected]8e383412010-10-19 16:57:03109 virtual ~MemoryDetails();
[email protected]e6e6ba42009-11-07 01:56:19110
initial.commit09911bf2008-07-26 23:55:29111 private:
[email protected]a27a9382009-02-11 23:55:10112 // Collect child process information on the IO thread. This is needed because
113 // information about some child process types (i.e. plugins) can only be taken
114 // on that thread. The data will be used by about:memory. When finished,
115 // invokes back to the file thread to run the rest of the about:memory
116 // functionality.
117 void CollectChildInfoOnIOThread();
initial.commit09911bf2008-07-26 23:55:29118
119 // Collect current process information from the OS and store it
120 // for processing. If data has already been collected, clears old
121 // data and re-collects the data.
122 // Note - this function enumerates memory details from many processes
[email protected]a27a9382009-02-11 23:55:10123 // and is fairly expensive to run, hence it's run on the file thread.
124 // The parameter holds information about processes from the IO thread.
[email protected]4df3ac62011-03-11 04:38:52125 void CollectProcessData(const std::vector<ProcessMemoryInformation>&);
initial.commit09911bf2008-07-26 23:55:29126
[email protected]f164cea2009-11-05 23:37:40127#if defined(OS_MACOSX)
128 // A helper for |CollectProcessData()|, collecting data on the Chrome/Chromium
129 // process with PID |pid|. The collected data is added to the state of the
130 // object (in |process_data_|).
131 void CollectProcessDataChrome(
132 const std::vector<ProcessMemoryInformation>& child_info,
133 base::ProcessId pid,
134 const ProcessInfoSnapshot& process_info);
135#endif
136
[email protected]a27a9382009-02-11 23:55:10137 // Collect child process information on the UI thread. Information about
138 // renderer processes is only available there.
139 void CollectChildInfoOnUIThread();
initial.commit09911bf2008-07-26 23:55:29140
141 // Each time we take a memory sample, we do a little work to update
142 // the global histograms for tracking memory usage.
143 void UpdateHistograms();
144
[email protected]54fd1d32009-09-01 00:12:58145 // Returns a pointer to the ProcessData structure for Chrome.
146 ProcessData* ChromeBrowser();
147
148 std::vector<ProcessData> process_data_;
initial.commit09911bf2008-07-26 23:55:29149
[email protected]4d818fee2010-06-06 13:32:27150 DISALLOW_COPY_AND_ASSIGN(MemoryDetails);
initial.commit09911bf2008-07-26 23:55:29151};
152
153#endif // CHROME_BROWSER_MEMORY_DETAILS_H_