blob: d01ed46bb17ecc0c2cc0dbd36ed888ce5b0957a7 [file] [log] [blame]
[email protected]4306df72012-04-20 18:58:571// Copyright (c) 2012 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_
initial.commit09911bf2008-07-26 23:55:297
initial.commit09911bf2008-07-26 23:55:298#include <vector>
9
[email protected]3b63f8f42011-03-28 01:54:1510#include "base/memory/ref_counted.h"
initial.commit09911bf2008-07-26 23:55:2911#include "base/process_util.h"
[email protected]4f260d02010-12-23 18:35:4212#include "base/string16.h"
[email protected]bd5d6cf2011-12-01 00:39:1213#include "content/public/common/process_type.h"
initial.commit09911bf2008-07-26 23:55:2914
initial.commit09911bf2008-07-26 23:55:2915// We collect data about each browser process. A browser may
16// have multiple processes (of course!). Even IE has multiple
17// processes these days.
18struct ProcessMemoryInformation {
[email protected]2c1978a2011-11-29 17:02:3919 // 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 {
22 RENDERER_UNKNOWN = 0,
23 RENDERER_NORMAL,
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
30 };
31
32 static std::string GetRendererTypeNameInEnglish(RendererProcessType type);
33 static std::string GetFullTypeNameInEnglish(
[email protected]f3b357692013-03-22 05:16:1334 int process_type,
[email protected]2c1978a2011-11-29 17:02:3935 RendererProcessType rtype);
36
[email protected]8e383412010-10-19 16:57:0337 ProcessMemoryInformation();
38 ~ProcessMemoryInformation();
initial.commit09911bf2008-07-26 23:55:2939
[email protected]8e23c882012-05-05 01:14:1140 // Default ordering is by private memory consumption.
41 bool operator<(const ProcessMemoryInformation& rhs) const;
42
initial.commit09911bf2008-07-26 23:55:2943 // The process id.
[email protected]a4dc33f2009-10-20 15:09:5544 base::ProcessId pid;
initial.commit09911bf2008-07-26 23:55:2945 // The working set information.
[email protected]176aa482008-11-14 03:25:1546 base::WorkingSetKBytes working_set;
initial.commit09911bf2008-07-26 23:55:2947 // The committed bytes.
[email protected]176aa482008-11-14 03:25:1548 base::CommittedKBytes committed;
initial.commit09911bf2008-07-26 23:55:2949 // The process version
[email protected]4f260d02010-12-23 18:35:4250 string16 version;
initial.commit09911bf2008-07-26 23:55:2951 // The process product name.
[email protected]4f260d02010-12-23 18:35:4252 string16 product_name;
initial.commit09911bf2008-07-26 23:55:2953 // The number of processes which this memory represents.
54 int num_processes;
[email protected]fcf79352010-12-28 20:13:2055 // 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
57 // results.
initial.commit09911bf2008-07-26 23:55:2958 bool is_diagnostics;
[email protected]a27a9382009-02-11 23:55:1059 // If this is a child process of Chrome, what type (i.e. plugin) it is.
[email protected]f3b357692013-03-22 05:16:1360 int process_type;
[email protected]fcf79352010-12-28 20:13:2061 // If this is a renderer process, what type it is.
[email protected]2c1978a2011-11-29 17:02:3962 RendererProcessType renderer_type;
[email protected]a27a9382009-02-11 23:55:1063 // A collection of titles used, i.e. for a tab it'll show all the page titles.
[email protected]4f260d02010-12-23 18:35:4264 std::vector<string16> titles;
initial.commit09911bf2008-07-26 23:55:2965};
66
67typedef std::vector<ProcessMemoryInformation> ProcessMemoryInformationList;
68
initial.commit09911bf2008-07-26 23:55:2969// Browser Process Information.
70struct ProcessData {
[email protected]93aa89c72010-10-20 21:32:0471 ProcessData();
72 ProcessData(const ProcessData& rhs);
73 ~ProcessData();
74 ProcessData& operator=(const ProcessData& rhs);
75
[email protected]4f260d02010-12-23 18:35:4276 string16 name;
77 string16 process_name;
initial.commit09911bf2008-07-26 23:55:2978 ProcessMemoryInformationList processes;
79};
80
[email protected]f164cea2009-11-05 23:37:4081#if defined(OS_MACOSX)
82class ProcessInfoSnapshot;
83#endif
84
initial.commit09911bf2008-07-26 23:55:2985// 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.
88//
89// Example usage:
90//
91// class MyMemoryDetailConsumer : public MemoryDetails {
92//
[email protected]bfa5cf82009-11-20 21:48:0293// MyMemoryDetailConsumer() {
94// // Anything but |StartFetch()|.
initial.commit09911bf2008-07-26 23:55:2995// }
96//
[email protected]bfa5cf82009-11-20 21:48:0297// // (Or just call |StartFetch()| explicitly if there's nothing else to
98// // do.)
99// void StartDoingStuff() {
100// StartFetch(); // Starts fetching details.
101// // Etc.
102// }
103//
104// // Your other class stuff here
initial.commit09911bf2008-07-26 23:55:29105//
106// virtual void OnDetailsAvailable() {
[email protected]bfa5cf82009-11-20 21:48:02107// // do work with memory info here
initial.commit09911bf2008-07-26 23:55:29108// }
109// }
110class MemoryDetails : public base::RefCountedThreadSafe<MemoryDetails> {
111 public:
[email protected]4306df72012-04-20 18:58:57112 enum UserMetricsMode {
113 UPDATE_USER_METRICS, // Update UMA memory histograms with results.
114 SKIP_USER_METRICS
115 };
116
initial.commit09911bf2008-07-26 23:55:29117 // Constructor.
118 MemoryDetails();
initial.commit09911bf2008-07-26 23:55:29119
[email protected]54fd1d32009-09-01 00:12:58120 // Access to the process detail information. This data is only available
initial.commit09911bf2008-07-26 23:55:29121 // after OnDetailsAvailable() has been called.
[email protected]54fd1d32009-09-01 00:12:58122 const std::vector<ProcessData>& processes() { return process_data_; }
initial.commit09911bf2008-07-26 23:55:29123
initial.commit09911bf2008-07-26 23:55:29124 // Initiate updating the current memory details. These are fetched
125 // asynchronously because data must be collected from multiple threads.
[email protected]4306df72012-04-20 18:58:57126 // Updates UMA memory histograms if |mode| is UPDATE_USER_METRICS.
initial.commit09911bf2008-07-26 23:55:29127 // OnDetailsAvailable will be called when this process is complete.
[email protected]4306df72012-04-20 18:58:57128 void StartFetch(UserMetricsMode user_metrics_mode);
initial.commit09911bf2008-07-26 23:55:29129
[email protected]4306df72012-04-20 18:58:57130 virtual void OnDetailsAvailable() = 0;
131
132 // Returns a string summarizing memory usage of the Chrome browser process
133 // and all sub-processes, suitable for logging.
134 std::string ToLogString();
initial.commit09911bf2008-07-26 23:55:29135
[email protected]e6e6ba42009-11-07 01:56:19136 protected:
137 friend class base::RefCountedThreadSafe<MemoryDetails>;
138
[email protected]8e383412010-10-19 16:57:03139 virtual ~MemoryDetails();
[email protected]e6e6ba42009-11-07 01:56:19140
initial.commit09911bf2008-07-26 23:55:29141 private:
[email protected]a27a9382009-02-11 23:55:10142 // 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
146 // functionality.
147 void CollectChildInfoOnIOThread();
initial.commit09911bf2008-07-26 23:55:29148
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
[email protected]a27a9382009-02-11 23:55:10153 // 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.
[email protected]4df3ac62011-03-11 04:38:52155 void CollectProcessData(const std::vector<ProcessMemoryInformation>&);
initial.commit09911bf2008-07-26 23:55:29156
[email protected]f164cea2009-11-05 23:37:40157#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,
163 base::ProcessId pid,
164 const ProcessInfoSnapshot& process_info);
165#endif
166
[email protected]a27a9382009-02-11 23:55:10167 // Collect child process information on the UI thread. Information about
168 // renderer processes is only available there.
169 void CollectChildInfoOnUIThread();
initial.commit09911bf2008-07-26 23:55:29170
[email protected]4306df72012-04-20 18:58:57171 // Updates the global histograms for tracking memory usage.
initial.commit09911bf2008-07-26 23:55:29172 void UpdateHistograms();
173
[email protected]54fd1d32009-09-01 00:12:58174 // Returns a pointer to the ProcessData structure for Chrome.
175 ProcessData* ChromeBrowser();
176
177 std::vector<ProcessData> process_data_;
initial.commit09911bf2008-07-26 23:55:29178
[email protected]4306df72012-04-20 18:58:57179 UserMetricsMode user_metrics_mode_;
180
[email protected]4d818fee2010-06-06 13:32:27181 DISALLOW_COPY_AND_ASSIGN(MemoryDetails);
initial.commit09911bf2008-07-26 23:55:29182};
183
184#endif // CHROME_BROWSER_MEMORY_DETAILS_H_