[email protected] | 4306df7 | 2012-04-20 18:58:57 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #ifndef CHROME_BROWSER_MEMORY_DETAILS_H_ |
| 6 | #define CHROME_BROWSER_MEMORY_DETAILS_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 10 | #include "base/memory/ref_counted.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | #include "base/process_util.h" |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 12 | #include "base/string16.h" |
[email protected] | bd5d6cf | 2011-12-01 00:39:12 | [diff] [blame] | 13 | #include "content/public/common/process_type.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 { |
[email protected] | 2c1978a | 2011-11-29 17:02:39 | [diff] [blame] | 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 { |
| 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] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 34 | int process_type, |
[email protected] | 2c1978a | 2011-11-29 17:02:39 | [diff] [blame] | 35 | RendererProcessType rtype); |
| 36 | |
[email protected] | 8e38341 | 2010-10-19 16:57:03 | [diff] [blame] | 37 | ProcessMemoryInformation(); |
| 38 | ~ProcessMemoryInformation(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 39 | |
[email protected] | 8e23c88 | 2012-05-05 01:14:11 | [diff] [blame] | 40 | // Default ordering is by private memory consumption. |
| 41 | bool operator<(const ProcessMemoryInformation& rhs) const; |
| 42 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | // The process id. |
[email protected] | a4dc33f | 2009-10-20 15:09:55 | [diff] [blame] | 44 | base::ProcessId pid; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | // The working set information. |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 46 | base::WorkingSetKBytes working_set; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 47 | // The committed bytes. |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 48 | base::CommittedKBytes committed; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 49 | // The process version |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 50 | string16 version; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 51 | // The process product name. |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 52 | string16 product_name; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 53 | // The number of processes which this memory represents. |
| 54 | int num_processes; |
[email protected] | fcf7935 | 2010-12-28 20:13:20 | [diff] [blame] | 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 |
| 57 | // results. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 58 | bool is_diagnostics; |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 59 | // If this is a child process of Chrome, what type (i.e. plugin) it is. |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 60 | int process_type; |
[email protected] | fcf7935 | 2010-12-28 20:13:20 | [diff] [blame] | 61 | // If this is a renderer process, what type it is. |
[email protected] | 2c1978a | 2011-11-29 17:02:39 | [diff] [blame] | 62 | RendererProcessType renderer_type; |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 63 | // A collection of titles used, i.e. for a tab it'll show all the page titles. |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 64 | std::vector<string16> titles; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | typedef std::vector<ProcessMemoryInformation> ProcessMemoryInformationList; |
| 68 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 69 | // Browser Process Information. |
| 70 | struct ProcessData { |
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 71 | ProcessData(); |
| 72 | ProcessData(const ProcessData& rhs); |
| 73 | ~ProcessData(); |
| 74 | ProcessData& operator=(const ProcessData& rhs); |
| 75 | |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 76 | string16 name; |
| 77 | string16 process_name; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 78 | ProcessMemoryInformationList processes; |
| 79 | }; |
| 80 | |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 81 | #if defined(OS_MACOSX) |
| 82 | class ProcessInfoSnapshot; |
| 83 | #endif |
| 84 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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. |
| 88 | // |
| 89 | // Example usage: |
| 90 | // |
| 91 | // class MyMemoryDetailConsumer : public MemoryDetails { |
| 92 | // |
[email protected] | bfa5cf8 | 2009-11-20 21:48:02 | [diff] [blame] | 93 | // MyMemoryDetailConsumer() { |
| 94 | // // Anything but |StartFetch()|. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 95 | // } |
| 96 | // |
[email protected] | bfa5cf8 | 2009-11-20 21:48:02 | [diff] [blame] | 97 | // // (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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 105 | // |
| 106 | // virtual void OnDetailsAvailable() { |
[email protected] | bfa5cf8 | 2009-11-20 21:48:02 | [diff] [blame] | 107 | // // do work with memory info here |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | // } |
| 109 | // } |
| 110 | class MemoryDetails : public base::RefCountedThreadSafe<MemoryDetails> { |
| 111 | public: |
[email protected] | 4306df7 | 2012-04-20 18:58:57 | [diff] [blame] | 112 | enum UserMetricsMode { |
| 113 | UPDATE_USER_METRICS, // Update UMA memory histograms with results. |
| 114 | SKIP_USER_METRICS |
| 115 | }; |
| 116 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 117 | // Constructor. |
| 118 | MemoryDetails(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 119 | |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 120 | // Access to the process detail information. This data is only available |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 121 | // after OnDetailsAvailable() has been called. |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 122 | const std::vector<ProcessData>& processes() { return process_data_; } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 123 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | // Initiate updating the current memory details. These are fetched |
| 125 | // asynchronously because data must be collected from multiple threads. |
[email protected] | 4306df7 | 2012-04-20 18:58:57 | [diff] [blame] | 126 | // Updates UMA memory histograms if |mode| is UPDATE_USER_METRICS. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 127 | // OnDetailsAvailable will be called when this process is complete. |
[email protected] | 4306df7 | 2012-04-20 18:58:57 | [diff] [blame] | 128 | void StartFetch(UserMetricsMode user_metrics_mode); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 129 | |
[email protected] | 4306df7 | 2012-04-20 18:58:57 | [diff] [blame] | 130 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 135 | |
[email protected] | e6e6ba4 | 2009-11-07 01:56:19 | [diff] [blame] | 136 | protected: |
| 137 | friend class base::RefCountedThreadSafe<MemoryDetails>; |
| 138 | |
[email protected] | 8e38341 | 2010-10-19 16:57:03 | [diff] [blame] | 139 | virtual ~MemoryDetails(); |
[email protected] | e6e6ba4 | 2009-11-07 01:56:19 | [diff] [blame] | 140 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 141 | private: |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 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 |
| 146 | // functionality. |
| 147 | void CollectChildInfoOnIOThread(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 148 | |
| 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] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 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. |
[email protected] | 4df3ac6 | 2011-03-11 04:38:52 | [diff] [blame] | 155 | void CollectProcessData(const std::vector<ProcessMemoryInformation>&); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 156 | |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 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, |
| 163 | base::ProcessId pid, |
| 164 | const ProcessInfoSnapshot& process_info); |
| 165 | #endif |
| 166 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 167 | // Collect child process information on the UI thread. Information about |
| 168 | // renderer processes is only available there. |
| 169 | void CollectChildInfoOnUIThread(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 170 | |
[email protected] | 4306df7 | 2012-04-20 18:58:57 | [diff] [blame] | 171 | // Updates the global histograms for tracking memory usage. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 172 | void UpdateHistograms(); |
| 173 | |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 174 | // Returns a pointer to the ProcessData structure for Chrome. |
| 175 | ProcessData* ChromeBrowser(); |
| 176 | |
| 177 | std::vector<ProcessData> process_data_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 178 | |
[email protected] | 4306df7 | 2012-04-20 18:58:57 | [diff] [blame] | 179 | UserMetricsMode user_metrics_mode_; |
| 180 | |
[email protected] | 4d818fee | 2010-06-06 13:32:27 | [diff] [blame] | 181 | DISALLOW_COPY_AND_ASSIGN(MemoryDetails); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | #endif // CHROME_BROWSER_MEMORY_DETAILS_H_ |