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