• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 #pragma once
8 
9 #include <vector>
10 
11 #include "base/memory/ref_counted.h"
12 #include "base/process_util.h"
13 #include "base/string16.h"
14 #include "content/common/child_process_info.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   ProcessMemoryInformation();
21   ~ProcessMemoryInformation();
22 
23   // The process id.
24   base::ProcessId pid;
25   // The working set information.
26   base::WorkingSetKBytes working_set;
27   // The committed bytes.
28   base::CommittedKBytes committed;
29   // The process version
30   string16 version;
31   // The process product name.
32   string16 product_name;
33   // The number of processes which this memory represents.
34   int num_processes;
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.
38   bool is_diagnostics;
39   // If this is a child process of Chrome, what type (i.e. plugin) it is.
40   ChildProcessInfo::ProcessType type;
41   // If this is a renderer process, what type it is.
42   ChildProcessInfo::RendererProcessType renderer_type;
43   // A collection of titles used, i.e. for a tab it'll show all the page titles.
44   std::vector<string16> titles;
45 };
46 
47 typedef std::vector<ProcessMemoryInformation> ProcessMemoryInformationList;
48 
49 // Browser Process Information.
50 struct ProcessData {
51   ProcessData();
52   ProcessData(const ProcessData& rhs);
53   ~ProcessData();
54   ProcessData& operator=(const ProcessData& rhs);
55 
56   string16 name;
57   string16 process_name;
58   ProcessMemoryInformationList processes;
59 };
60 
61 #if defined(OS_MACOSX)
62 class ProcessInfoSnapshot;
63 #endif
64 
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 //
73 //      MyMemoryDetailConsumer() {
74 //        // Anything but |StartFetch()|.
75 //      }
76 //
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
85 //
86 //      virtual void OnDetailsAvailable() {
87 //        // do work with memory info here
88 //      }
89 //    }
90 class MemoryDetails : public base::RefCountedThreadSafe<MemoryDetails> {
91  public:
92   // Constructor.
93   MemoryDetails();
94 
95   // Access to the process detail information.  This data is only available
96   // after OnDetailsAvailable() has been called.
processes()97   const std::vector<ProcessData>& processes() { return process_data_; }
98 
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 
OnDetailsAvailable()104   virtual void OnDetailsAvailable() {}
105 
106  protected:
107   friend class base::RefCountedThreadSafe<MemoryDetails>;
108 
109   virtual ~MemoryDetails();
110 
111  private:
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();
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
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.
125   void CollectProcessData(const std::vector<ProcessMemoryInformation>&);
126 
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 
137   // Collect child process information on the UI thread.  Information about
138   // renderer processes is only available there.
139   void CollectChildInfoOnUIThread();
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 
145   // Returns a pointer to the ProcessData structure for Chrome.
146   ProcessData* ChromeBrowser();
147 
148   std::vector<ProcessData> process_data_;
149 
150   DISALLOW_COPY_AND_ASSIGN(MemoryDetails);
151 };
152 
153 #endif  // CHROME_BROWSER_MEMORY_DETAILS_H_
154