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 #ifndef CHROME_BROWSER_MEMORY_DETAILS_H_
6 #define CHROME_BROWSER_MEMORY_DETAILS_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/process_util.h"
12 #include "base/string16.h"
13 #include "content/public/common/process_type.h"
15 // We collect data about each browser process. A browser may
16 // have multiple processes (of course!). Even IE has multiple
17 // processes these days.
18 struct ProcessMemoryInformation
{
19 // NOTE: Do not remove or reorder the elements in this enum, and only add new
20 // items at the end. We depend on these specific values in a histogram.
21 enum RendererProcessType
{
24 RENDERER_CHROME
, // WebUI (chrome:// URL)
25 RENDERER_EXTENSION
, // chrome-extension://
26 RENDERER_DEVTOOLS
, // Web inspector
27 RENDERER_INTERSTITIAL
, // malware/phishing interstitial
28 RENDERER_NOTIFICATION
, // HTML notification bubble
29 RENDERER_BACKGROUND_APP
// hosted app background page
32 static std::string
GetRendererTypeNameInEnglish(RendererProcessType type
);
33 static std::string
GetFullTypeNameInEnglish(
34 content::ProcessType type
,
35 RendererProcessType rtype
);
37 ProcessMemoryInformation();
38 ~ProcessMemoryInformation();
40 // Default ordering is by private memory consumption.
41 bool operator<(const ProcessMemoryInformation
& rhs
) const;
45 // The working set information.
46 base::WorkingSetKBytes working_set
;
47 // The committed bytes.
48 base::CommittedKBytes committed
;
49 // The process version
51 // The process product name.
52 string16 product_name
;
53 // The number of processes which this memory represents.
55 // A process is a diagnostics process if it is rendering about:memory.
56 // Mark this specially so that it can avoid counting it in its own
59 // If this is a child process of Chrome, what type (i.e. plugin) it is.
60 content::ProcessType type
;
61 // If this is a renderer process, what type it is.
62 RendererProcessType renderer_type
;
63 // A collection of titles used, i.e. for a tab it'll show all the page titles.
64 std::vector
<string16
> titles
;
67 typedef std::vector
<ProcessMemoryInformation
> ProcessMemoryInformationList
;
69 // Browser Process Information.
72 ProcessData(const ProcessData
& rhs
);
74 ProcessData
& operator=(const ProcessData
& rhs
);
77 string16 process_name
;
78 ProcessMemoryInformationList processes
;
81 #if defined(OS_MACOSX)
82 class ProcessInfoSnapshot
;
85 // MemoryDetails fetches memory details about current running browsers.
86 // Because this data can only be fetched asynchronously, callers use
87 // this class via a callback.
91 // class MyMemoryDetailConsumer : public MemoryDetails {
93 // MyMemoryDetailConsumer() {
94 // // Anything but |StartFetch()|.
97 // // (Or just call |StartFetch()| explicitly if there's nothing else to
99 // void StartDoingStuff() {
100 // StartFetch(); // Starts fetching details.
104 // // Your other class stuff here
106 // virtual void OnDetailsAvailable() {
107 // // do work with memory info here
110 class MemoryDetails
: public base::RefCountedThreadSafe
<MemoryDetails
> {
112 enum UserMetricsMode
{
113 UPDATE_USER_METRICS
, // Update UMA memory histograms with results.
120 // Access to the process detail information. This data is only available
121 // after OnDetailsAvailable() has been called.
122 const std::vector
<ProcessData
>& processes() { return process_data_
; }
124 // Initiate updating the current memory details. These are fetched
125 // asynchronously because data must be collected from multiple threads.
126 // Updates UMA memory histograms if |mode| is UPDATE_USER_METRICS.
127 // OnDetailsAvailable will be called when this process is complete.
128 void StartFetch(UserMetricsMode user_metrics_mode
);
130 virtual void OnDetailsAvailable() = 0;
132 // Returns a string summarizing memory usage of the Chrome browser process
133 // and all sub-processes, suitable for logging.
134 std::string
ToLogString();
137 friend class base::RefCountedThreadSafe
<MemoryDetails
>;
139 virtual ~MemoryDetails();
142 // Collect child process information on the IO thread. This is needed because
143 // information about some child process types (i.e. plugins) can only be taken
144 // on that thread. The data will be used by about:memory. When finished,
145 // invokes back to the file thread to run the rest of the about:memory
147 void CollectChildInfoOnIOThread();
149 // Collect current process information from the OS and store it
150 // for processing. If data has already been collected, clears old
151 // data and re-collects the data.
152 // Note - this function enumerates memory details from many processes
153 // and is fairly expensive to run, hence it's run on the file thread.
154 // The parameter holds information about processes from the IO thread.
155 void CollectProcessData(const std::vector
<ProcessMemoryInformation
>&);
157 #if defined(OS_MACOSX)
158 // A helper for |CollectProcessData()|, collecting data on the Chrome/Chromium
159 // process with PID |pid|. The collected data is added to the state of the
160 // object (in |process_data_|).
161 void CollectProcessDataChrome(
162 const std::vector
<ProcessMemoryInformation
>& child_info
,
164 const ProcessInfoSnapshot
& process_info
);
167 // Collect child process information on the UI thread. Information about
168 // renderer processes is only available there.
169 void CollectChildInfoOnUIThread();
171 // Updates the global histograms for tracking memory usage.
172 void UpdateHistograms();
174 // Returns a pointer to the ProcessData structure for Chrome.
175 ProcessData
* ChromeBrowser();
177 std::vector
<ProcessData
> process_data_
;
179 UserMetricsMode user_metrics_mode_
;
181 DISALLOW_COPY_AND_ASSIGN(MemoryDetails
);
184 #endif // CHROME_BROWSER_MEMORY_DETAILS_H_