1 // Copyright (c) 2009 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.
5 #ifndef CHROME_BROWSER_PROCESS_INFO_SNAPSHOT_H_
6 #define CHROME_BROWSER_PROCESS_INFO_SNAPSHOT_H_
14 #include "base/process_util.h"
16 // A class which captures process information at a given point in time when its
17 // |Sample()| method is called. This information can then be probed by PID.
18 // |Sample()| may take a while to complete, so if calling from the browser
19 // process, only do so from the file thread.
20 // TODO(viettrungluu): This is currently only implemented and used on Mac, so
21 // things are very Mac-specific. If this is ever implemented for other
22 // platforms, we should subclass and add opaqueness (probably |ProcInfoEntry|
23 // should be considered opaque).
24 class ProcessInfoSnapshot
{
26 ProcessInfoSnapshot();
27 ~ProcessInfoSnapshot();
29 // Maximum size of lists of PIDs which this class will accept; used in
31 static const size_t kMaxPidListSize
;
33 // Capture a snapshot of process memory information for the
34 // given list of PIDs. Call only from the file thread.
35 // |pid_list| - list of |ProcessId|s on which to capture information; must
36 // have no more than |kMaxPidListSize| (above) entries,
37 // returns - |true| if okay, |false| on error.
38 bool Sample(std::vector
<base::ProcessId
> pid_list
);
40 // Reset all statistics (deallocating any memory allocated).
43 // Our basic structure for storing information about a process (the names are
44 // mostly self-explanatory). Note that |command| may not actually reflect the
45 // actual executable name; never trust it absolutely, and only take it
46 // half-seriously when it begins with '/'.
47 struct ProcInfoEntry
{
52 // Explicitly use uint64_t instead of size_t in case this code is running
53 // in a 32 bit process and the target process is 64 bit.
63 // Get process information for a given PID.
64 // |pid| - self-explanatory.
65 // |proc_info| - place to put the process information.
66 // returns - |true| if okay, |false| on error (including PID not found).
67 bool GetProcInfo(int pid
,
68 ProcInfoEntry
* proc_info
) const;
70 // Fills a |CommittedKBytes| with both resident and paged memory usage, as per
71 // its definition (or as close as we can manage). In the current (Mac)
72 // implementation, we map:
73 // vsize --> comm_priv,
76 // in about:memory: virtual:private = comm_priv,
77 // virtual:mapped = comm_mapped.
78 // TODO(viettrungluu): Doing such a mapping is kind of ugly.
79 // |pid| - self-explanatory.
80 // |usage| - pointer to |CommittedBytes| to fill; zero-ed on error.
81 // returns - |true| on success, |false| on error (including PID not found).
82 bool GetCommittedKBytesOfPID(int pid
,
83 base::CommittedKBytes
* usage
) const;
85 // Fills a |WorkingSetKBytes| containing resident private and shared memory,
86 // as per its definition (or as close as we can manage). In the current (Mac)
87 // implementation, we map:
89 // rss --> ws_shareable,
90 // rshrd --> ws_shared;
91 // in about:memory: res:private = ws_priv + ws_shareable - ws_shared,
92 // res:shared = ws_shared / num_procs,
93 // res:total = res:private + res:shared.
94 // TODO(viettrungluu): Doing such a mapping is kind of ugly.
95 // |pid| - self-explanatory.
96 // |ws_usage| - pointer to |WorkingSetKBytes| to fill; zero-ed on error.
97 // returns - |true| on success, |false| on error (including PID not found).
98 bool GetWorkingSetKBytesOfPID(int pid
,
99 base::WorkingSetKBytes
* ws_usage
) const;
101 // TODO(viettrungluu): Maybe we should also have the following (again, for
103 // size_t GetWorkingSetSizeOfPID(int pid) const;
104 // size_t GetPeakWorkingSetSizeOfPID(int pid) const;
105 // size_t GetPrivateBytesOfPID(int pid) const;
108 // map from |int| (PID) to |ProcInfoEntry|
109 std::map
<int,ProcInfoEntry
> proc_info_entries_
;
112 #endif // CHROME_BROWSER_PROCESS_INFO_SNAPSHOT_H_