1 // Copyright 2012 The Chromium Authors
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 "build/build_config.h"
16 #include "build/chromeos_buildflags.h"
17 #include "content/public/common/process_type.h"
19 namespace memory_instrumentation {
20 class GlobalMemoryDump;
21 } // namespace memory_instrumentation
23 // We collect data about each browser process. A browser may
24 // have multiple processes (of course!). Even IE has multiple
25 // processes these days.
26 struct ProcessMemoryInformation {
27 // NOTE: Do not remove or reorder the elements in this enum, and only add new
28 // items at the end. We depend on these specific values in a histogram.
29 enum RendererProcessType {
32 RENDERER_CHROME, // WebUI (chrome:// URL)
33 RENDERER_EXTENSION, // chrome-extension://
34 RENDERER_DEVTOOLS, // Web inspector
35 RENDERER_INTERSTITIAL, // malware/phishing interstitial
36 RENDERER_BACKGROUND_APP // hosted app background page
39 static std::string GetRendererTypeNameInEnglish(RendererProcessType type);
40 static std::string GetFullTypeNameInEnglish(
42 RendererProcessType rtype);
44 ProcessMemoryInformation();
45 ProcessMemoryInformation(const ProcessMemoryInformation& other);
46 ~ProcessMemoryInformation();
48 // Default ordering is by private memory consumption.
49 bool operator<(const ProcessMemoryInformation& rhs) const;
53 // The process version
54 std::u16string version;
55 // The process product name.
56 std::u16string product_name;
57 // The number of processes which this memory represents.
59 // If this is a child process of Chrome, what type (i.e. plugin) it is.
61 // Number of open file descriptors in this process.
63 // Maximum number of file descriptors that can be opened in this process.
64 int open_fds_soft_limit;
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<std::u16string> titles;
69 // Consistent memory metric for all platforms.
70 size_t private_memory_footprint_kb;
73 typedef std::vector<ProcessMemoryInformation> ProcessMemoryInformationList;
75 // Browser Process Information.
78 ProcessData(const ProcessData& rhs);
80 ProcessData& operator=(const ProcessData& rhs);
83 std::u16string process_name;
84 ProcessMemoryInformationList processes;
87 // MemoryDetails fetches memory details about current running browsers.
88 // Because this data can only be fetched asynchronously, callers use
89 // this class via a callback.
93 // class MyMemoryDetailConsumer : public MemoryDetails {
95 // MyMemoryDetailConsumer() {
96 // // Anything but |StartFetch()|.
99 // // (Or just call |StartFetch()| explicitly if there's nothing else to
101 // void StartDoingStuff() {
102 // StartFetch(); // Starts fetching details.
106 // // Your other class stuff here
108 // virtual void OnDetailsAvailable() {
109 // // do work with memory info here
112 class MemoryDetails : public base::RefCountedThreadSafe<MemoryDetails> {
117 MemoryDetails(const MemoryDetails&) = delete;
118 MemoryDetails& operator=(const MemoryDetails&) = delete;
120 // Initiate updating the current memory details. These are fetched
121 // asynchronously because data must be collected from multiple threads.
122 // OnDetailsAvailable will be called when this process is complete.
125 virtual void OnDetailsAvailable() = 0;
127 // Returns a string summarizing memory usage of the Chrome browser process
128 // and all sub-processes, suitable for logging. Tab title may contain PII,
129 // set |include_tab_title| to false to exclude tab titles when there are
131 std::string ToLogString(bool include_tab_title);
134 friend class base::RefCountedThreadSafe<MemoryDetails>;
136 virtual ~MemoryDetails();
138 // Access to the process detail information. This data is only available
139 // after OnDetailsAvailable() has been called.
140 const std::vector<ProcessData>& processes() { return process_data_; }
142 // Returns a pointer to the ProcessData structure for Chrome.
143 ProcessData* ChromeBrowser();
145 #if BUILDFLAG(IS_CHROMEOS_ASH)
146 const base::SwapInfo& swap_info() const { return swap_info_; }
150 // Collect current process information from the OS and store it
151 // for processing. If data has already been collected, clears old
152 // data and re-collects the data.
153 // Note - this function enumerates memory details from many processes
154 // and is fairly expensive to run, hence it's run on the blocking pool.
155 // The parameter holds information about processes from the IO thread.
156 void CollectProcessData(
157 const std::vector<ProcessMemoryInformation>& child_info);
159 // Collect child process information on the UI thread. Information about
160 // renderer processes is only available there.
161 void CollectChildInfoOnUIThread();
163 void DidReceiveMemoryDump(
165 std::unique_ptr<memory_instrumentation::GlobalMemoryDump> dump);
167 std::vector<ProcessData> process_data_;
169 #if BUILDFLAG(IS_CHROMEOS_ASH)
170 base::SwapInfo swap_info_;
174 #endif // CHROME_BROWSER_MEMORY_DETAILS_H_