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