memory_details.h revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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/process_util.h"
12#include "base/ref_counted.h"
13#include "base/string16.h"
14#include "chrome/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.
19struct 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
36  // about:xxx information.
37  bool is_diagnostics;
38  // If this is a child process of Chrome, what type (i.e. plugin) it is.
39  ChildProcessInfo::ProcessType type;
40  // A collection of titles used, i.e. for a tab it'll show all the page titles.
41  std::vector<string16> titles;
42};
43
44typedef std::vector<ProcessMemoryInformation> ProcessMemoryInformationList;
45
46// Browser Process Information.
47struct ProcessData {
48  ProcessData();
49  ProcessData(const ProcessData& rhs);
50  ~ProcessData();
51  ProcessData& operator=(const ProcessData& rhs);
52
53  string16 name;
54  string16 process_name;
55  ProcessMemoryInformationList processes;
56};
57
58#if defined(OS_MACOSX)
59class ProcessInfoSnapshot;
60#endif
61
62// MemoryDetails fetches memory details about current running browsers.
63// Because this data can only be fetched asynchronously, callers use
64// this class via a callback.
65//
66// Example usage:
67//
68//    class MyMemoryDetailConsumer : public MemoryDetails {
69//
70//      MyMemoryDetailConsumer() {
71//        // Anything but |StartFetch()|.
72//      }
73//
74//      // (Or just call |StartFetch()| explicitly if there's nothing else to
75//      // do.)
76//      void StartDoingStuff() {
77//        StartFetch();  // Starts fetching details.
78//        // Etc.
79//      }
80//
81//      // Your other class stuff here
82//
83//      virtual void OnDetailsAvailable() {
84//        // do work with memory info here
85//      }
86//    }
87class MemoryDetails : public base::RefCountedThreadSafe<MemoryDetails> {
88 public:
89  // Constructor.
90  MemoryDetails();
91
92  // Access to the process detail information.  This data is only available
93  // after OnDetailsAvailable() has been called.
94  const std::vector<ProcessData>& processes() { return process_data_; }
95
96  // Initiate updating the current memory details.  These are fetched
97  // asynchronously because data must be collected from multiple threads.
98  // OnDetailsAvailable will be called when this process is complete.
99  void StartFetch();
100
101  virtual void OnDetailsAvailable() {}
102
103 protected:
104  friend class base::RefCountedThreadSafe<MemoryDetails>;
105
106  virtual ~MemoryDetails();
107
108 private:
109  // Collect child process information on the IO thread.  This is needed because
110  // information about some child process types (i.e. plugins) can only be taken
111  // on that thread.  The data will be used by about:memory.  When finished,
112  // invokes back to the file thread to run the rest of the about:memory
113  // functionality.
114  void CollectChildInfoOnIOThread();
115
116  // Collect current process information from the OS and store it
117  // for processing.  If data has already been collected, clears old
118  // data and re-collects the data.
119  // Note - this function enumerates memory details from many processes
120  // and is fairly expensive to run, hence it's run on the file thread.
121  // The parameter holds information about processes from the IO thread.
122  void CollectProcessData(std::vector<ProcessMemoryInformation>);
123
124#if defined(OS_MACOSX)
125  // A helper for |CollectProcessData()|, collecting data on the Chrome/Chromium
126  // process with PID |pid|. The collected data is added to the state of the
127  // object (in |process_data_|).
128  void CollectProcessDataChrome(
129      const std::vector<ProcessMemoryInformation>& child_info,
130      base::ProcessId pid,
131      const ProcessInfoSnapshot& process_info);
132#endif
133
134  // Collect child process information on the UI thread.  Information about
135  // renderer processes is only available there.
136  void CollectChildInfoOnUIThread();
137
138  // Each time we take a memory sample, we do a little work to update
139  // the global histograms for tracking memory usage.
140  void UpdateHistograms();
141
142  // Returns a pointer to the ProcessData structure for Chrome.
143  ProcessData* ChromeBrowser();
144
145  std::vector<ProcessData> process_data_;
146
147  DISALLOW_COPY_AND_ASSIGN(MemoryDetails);
148};
149
150#endif  // CHROME_BROWSER_MEMORY_DETAILS_H_
151