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_
12 #include "base/memory/ref_counted.h"
13 #include "base/process/process_handle.h"
14 #include "base/process/process_metrics.h"
15 #include "base/strings/string16.h"
16 #include "base/time/time.h"
17 #include "chrome/browser/site_details.h"
18 #include "content/public/common/process_type.h"
20 // We collect data about each browser process. A browser may
21 // have multiple processes (of course!). Even IE has multiple
22 // processes these days.
23 struct ProcessMemoryInformation
{
24 // NOTE: Do not remove or reorder the elements in this enum, and only add new
25 // items at the end. We depend on these specific values in a histogram.
26 enum RendererProcessType
{
29 RENDERER_CHROME
, // WebUI (chrome:// URL)
30 RENDERER_EXTENSION
, // chrome-extension://
31 RENDERER_DEVTOOLS
, // Web inspector
32 RENDERER_INTERSTITIAL
, // malware/phishing interstitial
33 RENDERER_BACKGROUND_APP
// hosted app background page
36 static std::string
GetRendererTypeNameInEnglish(RendererProcessType type
);
37 static std::string
GetFullTypeNameInEnglish(
39 RendererProcessType rtype
);
41 ProcessMemoryInformation();
42 ~ProcessMemoryInformation();
44 // Default ordering is by private memory consumption.
45 bool operator<(const ProcessMemoryInformation
& rhs
) const;
49 // The working set information.
50 base::WorkingSetKBytes working_set
;
51 // The committed bytes.
52 base::CommittedKBytes committed
;
53 // The process version
54 base::string16 version
;
55 // The process product name.
56 base::string16 product_name
;
57 // The number of processes which this memory represents.
59 // A process is a diagnostics process if it is rendering about:memory.
60 // Mark this specially so that it can avoid counting it in its own
63 // If this is a child process of Chrome, what type (i.e. plugin) it is.
65 // If this is a renderer process, what type it is.
66 RendererProcessType renderer_type
;
67 // A collection of titles used, i.e. for a tab it'll show all the page titles.
68 std::vector
<base::string16
> titles
;
71 typedef std::vector
<ProcessMemoryInformation
> ProcessMemoryInformationList
;
73 // Browser Process Information.
76 ProcessData(const ProcessData
& rhs
);
78 ProcessData
& operator=(const ProcessData
& rhs
);
81 base::string16 process_name
;
82 ProcessMemoryInformationList processes
;
84 // Track site data for predicting process counts with out-of-process iframes.
85 // See site_details.h.
86 BrowserContextSiteDataMap site_data
;
89 // MemoryDetails fetches memory details about current running browsers.
90 // Because this data can only be fetched asynchronously, callers use
91 // this class via a callback.
95 // class MyMemoryDetailConsumer : public MemoryDetails {
97 // MyMemoryDetailConsumer() {
98 // // Anything but |StartFetch()|.
101 // // (Or just call |StartFetch()| explicitly if there's nothing else to
103 // void StartDoingStuff() {
104 // StartFetch(); // Starts fetching details.
108 // // Your other class stuff here
110 // virtual void OnDetailsAvailable() {
111 // // do work with memory info here
114 class MemoryDetails
: public base::RefCountedThreadSafe
<MemoryDetails
> {
116 enum CollectionMode
{
117 // Collect metrics from Chrome and other running browsers.
119 // Collect metrics from Chrome processes only.
126 // Initiate updating the current memory details (based on |mode|). These are
127 // fetched asynchronously because data must be collected from multiple
128 // threads. OnDetailsAvailable will be called when this process is complete.
129 void StartFetch(CollectionMode mode
);
131 virtual void OnDetailsAvailable() = 0;
133 // Returns a string summarizing memory usage of the Chrome browser process
134 // and all sub-processes, suitable for logging.
135 std::string
ToLogString();
138 friend class base::RefCountedThreadSafe
<MemoryDetails
>;
140 virtual ~MemoryDetails();
142 // Access to the process detail information. This data is only available
143 // after OnDetailsAvailable() has been called.
144 const std::vector
<ProcessData
>& processes() { return process_data_
; }
146 // Returns a pointer to the ProcessData structure for Chrome.
147 ProcessData
* ChromeBrowser();
149 #if defined(OS_CHROMEOS)
150 const base::SwapInfo
& swap_info() const { return swap_info_
; }
154 // Collect child process information on the IO thread. This is needed because
155 // information about some child process types (i.e. plugins) can only be taken
156 // on that thread. The data will be used by about:memory. When finished,
157 // invokes back to the file thread to run the rest of the about:memory
159 void CollectChildInfoOnIOThread(CollectionMode mode
);
161 // Collect current process information from the OS and store it
162 // for processing. If data has already been collected, clears old
163 // data and re-collects the data.
164 // Note - this function enumerates memory details from many processes
165 // and is fairly expensive to run, hence it's run on the blocking pool.
166 // The parameter holds information about processes from the IO thread.
167 void CollectProcessData(
169 const std::vector
<ProcessMemoryInformation
>& child_info
);
171 // Collect child process information on the UI thread. Information about
172 // renderer processes is only available there.
173 void CollectChildInfoOnUIThread();
175 std::vector
<ProcessData
> process_data_
;
177 #if defined(OS_CHROMEOS)
178 base::SwapInfo swap_info_
;
181 DISALLOW_COPY_AND_ASSIGN(MemoryDetails
);
184 #endif // CHROME_BROWSER_MEMORY_DETAILS_H_