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. 4 5 #ifndef CHROME_BROWSER_MEMORY_DETAILS_H_ 6 #define CHROME_BROWSER_MEMORY_DETAILS_H_ 7 8 #include <vector> 9 10 #include "base/memory/ref_counted.h" 11 #include "base/process/process_metrics.h" 12 #include "base/strings/string16.h" 13 #include "chrome/browser/site_details.h" 14 #include "content/public/common/process_type.h" 15 16 // We collect data about each browser process. A browser may 17 // have multiple processes (of course!). Even IE has multiple 18 // processes these days. 19 struct ProcessMemoryInformation { 20 // NOTE: Do not remove or reorder the elements in this enum, and only add new 21 // items at the end. We depend on these specific values in a histogram. 22 enum RendererProcessType { 23 RENDERER_UNKNOWN = 0, 24 RENDERER_NORMAL, 25 RENDERER_CHROME, // WebUI (chrome:// URL) 26 RENDERER_EXTENSION, // chrome-extension:// 27 RENDERER_DEVTOOLS, // Web inspector 28 RENDERER_INTERSTITIAL, // malware/phishing interstitial 29 RENDERER_BACKGROUND_APP // hosted app background page 30 }; 31 32 static std::string GetRendererTypeNameInEnglish(RendererProcessType type); 33 static std::string GetFullTypeNameInEnglish( 34 int process_type, 35 RendererProcessType rtype); 36 37 ProcessMemoryInformation(); 38 ~ProcessMemoryInformation(); 39 40 // Default ordering is by private memory consumption. 41 bool operator<(const ProcessMemoryInformation& rhs) const; 42 43 // The process id. 44 base::ProcessId pid; 45 // The working set information. 46 base::WorkingSetKBytes working_set; 47 // The committed bytes. 48 base::CommittedKBytes committed; 49 // The process version 50 base::string16 version; 51 // The process product name. 52 base::string16 product_name; 53 // The number of processes which this memory represents. 54 int num_processes; 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. 58 bool is_diagnostics; 59 // If this is a child process of Chrome, what type (i.e. plugin) it is. 60 int process_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<base::string16> titles; 65 }; 66 67 typedef std::vector<ProcessMemoryInformation> ProcessMemoryInformationList; 68 69 // Browser Process Information. 70 struct ProcessData { 71 ProcessData(); 72 ProcessData(const ProcessData& rhs); 73 ~ProcessData(); 74 ProcessData& operator=(const ProcessData& rhs); 75 76 base::string16 name; 77 base::string16 process_name; 78 ProcessMemoryInformationList processes; 79 80 // Track site data for predicting process counts with out-of-process iframes. 81 // See site_details.h. 82 BrowserContextSiteDataMap site_data; 83 }; 84 85 #if defined(OS_MACOSX) 86 class ProcessInfoSnapshot; 87 #endif 88 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. 92 // 93 // Example usage: 94 // 95 // class MyMemoryDetailConsumer : public MemoryDetails { 96 // 97 // MyMemoryDetailConsumer() { 98 // // Anything but |StartFetch()|. 99 // } 100 // 101 // // (Or just call |StartFetch()| explicitly if there's nothing else to 102 // // do.) 103 // void StartDoingStuff() { 104 // StartFetch(); // Starts fetching details. 105 // // Etc. 106 // } 107 // 108 // // Your other class stuff here 109 // 110 // virtual void OnDetailsAvailable() { 111 // // do work with memory info here 112 // } 113 // } 114 class MemoryDetails : public base::RefCountedThreadSafe<MemoryDetails> { 115 public: 116 enum UserMetricsMode { 117 UPDATE_USER_METRICS, // Update UMA memory histograms with results. 118 SKIP_USER_METRICS 119 }; 120 121 // Constructor. 122 MemoryDetails(); 123 124 // Access to the process detail information. This data is only available 125 // after OnDetailsAvailable() has been called. processes()126 const std::vector<ProcessData>& processes() { return process_data_; } 127 128 // Initiate updating the current memory details. These are fetched 129 // asynchronously because data must be collected from multiple threads. 130 // Updates UMA memory histograms if |mode| is UPDATE_USER_METRICS. 131 // OnDetailsAvailable will be called when this process is complete. 132 void StartFetch(UserMetricsMode user_metrics_mode); 133 134 virtual void OnDetailsAvailable() = 0; 135 136 // Returns a string summarizing memory usage of the Chrome browser process 137 // and all sub-processes, suitable for logging. 138 std::string ToLogString(); 139 140 protected: 141 friend class base::RefCountedThreadSafe<MemoryDetails>; 142 143 virtual ~MemoryDetails(); 144 145 private: 146 // Collect child process information on the IO thread. This is needed because 147 // information about some child process types (i.e. plugins) can only be taken 148 // on that thread. The data will be used by about:memory. When finished, 149 // invokes back to the file thread to run the rest of the about:memory 150 // functionality. 151 void CollectChildInfoOnIOThread(); 152 153 // Collect current process information from the OS and store it 154 // for processing. If data has already been collected, clears old 155 // data and re-collects the data. 156 // Note - this function enumerates memory details from many processes 157 // and is fairly expensive to run, hence it's run on the file thread. 158 // The parameter holds information about processes from the IO thread. 159 void CollectProcessData(const std::vector<ProcessMemoryInformation>&); 160 161 #if defined(OS_MACOSX) 162 // A helper for |CollectProcessData()|, collecting data on the Chrome/Chromium 163 // process with PID |pid|. The collected data is added to the state of the 164 // object (in |process_data_|). 165 void CollectProcessDataChrome( 166 const std::vector<ProcessMemoryInformation>& child_info, 167 base::ProcessId pid, 168 const ProcessInfoSnapshot& process_info); 169 #endif 170 171 // Collect child process information on the UI thread. Information about 172 // renderer processes is only available there. 173 void CollectChildInfoOnUIThread(); 174 175 // Updates the global histograms for tracking memory usage. 176 void UpdateHistograms(); 177 178 #if defined(OS_CHROMEOS) 179 void UpdateSwapHistograms(); 180 #endif 181 182 // Returns a pointer to the ProcessData structure for Chrome. 183 ProcessData* ChromeBrowser(); 184 185 std::vector<ProcessData> process_data_; 186 187 UserMetricsMode user_metrics_mode_; 188 189 #if defined(OS_CHROMEOS) 190 base::SwapInfo swap_info_; 191 #endif 192 193 DISALLOW_COPY_AND_ASSIGN(MemoryDetails); 194 }; 195 196 #endif // CHROME_BROWSER_MEMORY_DETAILS_H_ 197