[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame^] | 1 | // Copyright (c) 2011 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_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame^] | 11 | #include "base/memory/ref_counted.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 12 | #include "base/process_util.h" |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 13 | #include "base/string16.h" |
[email protected] | 6faf7b1 | 2011-03-09 19:24:14 | [diff] [blame] | 14 | #include "content/common/child_process_info.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 { |
[email protected] | 8e38341 | 2010-10-19 16:57:03 | [diff] [blame] | 20 | ProcessMemoryInformation(); |
| 21 | ~ProcessMemoryInformation(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | |
| 23 | // The process id. |
[email protected] | a4dc33f | 2009-10-20 15:09:55 | [diff] [blame] | 24 | base::ProcessId pid; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 25 | // The working set information. |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 26 | base::WorkingSetKBytes working_set; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 27 | // The committed bytes. |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 28 | base::CommittedKBytes committed; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | // The process version |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 30 | string16 version; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | // The process product name. |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 32 | string16 product_name; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 33 | // The number of processes which this memory represents. |
| 34 | int num_processes; |
[email protected] | fcf7935 | 2010-12-28 20:13:20 | [diff] [blame] | 35 | // A process is a diagnostics process if it is rendering about:memory. |
| 36 | // Mark this specially so that it can avoid counting it in its own |
| 37 | // results. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 38 | bool is_diagnostics; |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 39 | // If this is a child process of Chrome, what type (i.e. plugin) it is. |
| 40 | ChildProcessInfo::ProcessType type; |
[email protected] | fcf7935 | 2010-12-28 20:13:20 | [diff] [blame] | 41 | // If this is a renderer process, what type it is. |
| 42 | ChildProcessInfo::RendererProcessType renderer_type; |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 43 | // 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] | 44 | std::vector<string16> titles; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | typedef std::vector<ProcessMemoryInformation> ProcessMemoryInformationList; |
| 48 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 49 | // Browser Process Information. |
| 50 | struct ProcessData { |
[email protected] | 93aa89c7 | 2010-10-20 21:32:04 | [diff] [blame] | 51 | ProcessData(); |
| 52 | ProcessData(const ProcessData& rhs); |
| 53 | ~ProcessData(); |
| 54 | ProcessData& operator=(const ProcessData& rhs); |
| 55 | |
[email protected] | 4f260d0 | 2010-12-23 18:35:42 | [diff] [blame] | 56 | string16 name; |
| 57 | string16 process_name; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 58 | ProcessMemoryInformationList processes; |
| 59 | }; |
| 60 | |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 61 | #if defined(OS_MACOSX) |
| 62 | class ProcessInfoSnapshot; |
| 63 | #endif |
| 64 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | // MemoryDetails fetches memory details about current running browsers. |
| 66 | // Because this data can only be fetched asynchronously, callers use |
| 67 | // this class via a callback. |
| 68 | // |
| 69 | // Example usage: |
| 70 | // |
| 71 | // class MyMemoryDetailConsumer : public MemoryDetails { |
| 72 | // |
[email protected] | bfa5cf8 | 2009-11-20 21:48:02 | [diff] [blame] | 73 | // MyMemoryDetailConsumer() { |
| 74 | // // Anything but |StartFetch()|. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | // } |
| 76 | // |
[email protected] | bfa5cf8 | 2009-11-20 21:48:02 | [diff] [blame] | 77 | // // (Or just call |StartFetch()| explicitly if there's nothing else to |
| 78 | // // do.) |
| 79 | // void StartDoingStuff() { |
| 80 | // StartFetch(); // Starts fetching details. |
| 81 | // // Etc. |
| 82 | // } |
| 83 | // |
| 84 | // // Your other class stuff here |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 85 | // |
| 86 | // virtual void OnDetailsAvailable() { |
[email protected] | bfa5cf8 | 2009-11-20 21:48:02 | [diff] [blame] | 87 | // // do work with memory info here |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 88 | // } |
| 89 | // } |
| 90 | class MemoryDetails : public base::RefCountedThreadSafe<MemoryDetails> { |
| 91 | public: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 92 | // Constructor. |
| 93 | MemoryDetails(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 94 | |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 95 | // Access to the process detail information. This data is only available |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 96 | // after OnDetailsAvailable() has been called. |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 97 | const std::vector<ProcessData>& processes() { return process_data_; } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 98 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 99 | // Initiate updating the current memory details. These are fetched |
| 100 | // asynchronously because data must be collected from multiple threads. |
| 101 | // OnDetailsAvailable will be called when this process is complete. |
| 102 | void StartFetch(); |
| 103 | |
| 104 | virtual void OnDetailsAvailable() {} |
| 105 | |
[email protected] | e6e6ba4 | 2009-11-07 01:56:19 | [diff] [blame] | 106 | protected: |
| 107 | friend class base::RefCountedThreadSafe<MemoryDetails>; |
| 108 | |
[email protected] | 8e38341 | 2010-10-19 16:57:03 | [diff] [blame] | 109 | virtual ~MemoryDetails(); |
[email protected] | e6e6ba4 | 2009-11-07 01:56:19 | [diff] [blame] | 110 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | private: |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 112 | // Collect child process information on the IO thread. This is needed because |
| 113 | // information about some child process types (i.e. plugins) can only be taken |
| 114 | // on that thread. The data will be used by about:memory. When finished, |
| 115 | // invokes back to the file thread to run the rest of the about:memory |
| 116 | // functionality. |
| 117 | void CollectChildInfoOnIOThread(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 118 | |
| 119 | // Collect current process information from the OS and store it |
| 120 | // for processing. If data has already been collected, clears old |
| 121 | // data and re-collects the data. |
| 122 | // Note - this function enumerates memory details from many processes |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 123 | // and is fairly expensive to run, hence it's run on the file thread. |
| 124 | // The parameter holds information about processes from the IO thread. |
[email protected] | 4df3ac6 | 2011-03-11 04:38:52 | [diff] [blame] | 125 | void CollectProcessData(const std::vector<ProcessMemoryInformation>&); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 126 | |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 127 | #if defined(OS_MACOSX) |
| 128 | // A helper for |CollectProcessData()|, collecting data on the Chrome/Chromium |
| 129 | // process with PID |pid|. The collected data is added to the state of the |
| 130 | // object (in |process_data_|). |
| 131 | void CollectProcessDataChrome( |
| 132 | const std::vector<ProcessMemoryInformation>& child_info, |
| 133 | base::ProcessId pid, |
| 134 | const ProcessInfoSnapshot& process_info); |
| 135 | #endif |
| 136 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 137 | // Collect child process information on the UI thread. Information about |
| 138 | // renderer processes is only available there. |
| 139 | void CollectChildInfoOnUIThread(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 140 | |
| 141 | // Each time we take a memory sample, we do a little work to update |
| 142 | // the global histograms for tracking memory usage. |
| 143 | void UpdateHistograms(); |
| 144 | |
[email protected] | 54fd1d3 | 2009-09-01 00:12:58 | [diff] [blame] | 145 | // Returns a pointer to the ProcessData structure for Chrome. |
| 146 | ProcessData* ChromeBrowser(); |
| 147 | |
| 148 | std::vector<ProcessData> process_data_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 149 | |
[email protected] | 4d818fee | 2010-06-06 13:32:27 | [diff] [blame] | 150 | DISALLOW_COPY_AND_ASSIGN(MemoryDetails); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | #endif // CHROME_BROWSER_MEMORY_DETAILS_H_ |