[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_PROCESS_INFO_SNAPSHOT_H_ |
| 6 | #define CHROME_BROWSER_PROCESS_INFO_SNAPSHOT_H_ |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 7 | |
| 8 | #include <sys/types.h> |
| 9 | |
| 10 | #include <map> |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
[email protected] | d09a4ce1c | 2013-07-24 17:37:02 | [diff] [blame] | 14 | #include "base/process/process_handle.h" |
| 15 | #include "base/process/process_metrics.h" |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 16 | |
| 17 | // A class which captures process information at a given point in time when its |
| 18 | // |Sample()| method is called. This information can then be probed by PID. |
| 19 | // |Sample()| may take a while to complete, so if calling from the browser |
[email protected] | b23d013 | 2011-01-12 00:20:45 | [diff] [blame] | 20 | // process, only do so from the file thread. |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 21 | // TODO(viettrungluu): This is currently only implemented and used on Mac, so |
| 22 | // things are very Mac-specific. If this is ever implemented for other |
| 23 | // platforms, we should subclass and add opaqueness (probably |ProcInfoEntry| |
| 24 | // should be considered opaque). |
| 25 | class ProcessInfoSnapshot { |
| 26 | public: |
| 27 | ProcessInfoSnapshot(); |
| 28 | ~ProcessInfoSnapshot(); |
| 29 | |
[email protected] | 983ef7f | 2010-01-04 16:17:13 | [diff] [blame] | 30 | // Maximum size of lists of PIDs which this class will accept; used in |
| 31 | // |Sample()| below. |
| 32 | static const size_t kMaxPidListSize; |
| 33 | |
[email protected] | b23d013 | 2011-01-12 00:20:45 | [diff] [blame] | 34 | // Capture a snapshot of process memory information for the |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 35 | // given list of PIDs. Call only from the file thread. |
[email protected] | 983ef7f | 2010-01-04 16:17:13 | [diff] [blame] | 36 | // |pid_list| - list of |ProcessId|s on which to capture information; must |
| 37 | // have no more than |kMaxPidListSize| (above) entries, |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 38 | // returns - |true| if okay, |false| on error. |
| 39 | bool Sample(std::vector<base::ProcessId> pid_list); |
| 40 | |
| 41 | // Reset all statistics (deallocating any memory allocated). |
| 42 | void Reset(); |
| 43 | |
| 44 | // Our basic structure for storing information about a process (the names are |
| 45 | // mostly self-explanatory). Note that |command| may not actually reflect the |
| 46 | // actual executable name; never trust it absolutely, and only take it |
| 47 | // half-seriously when it begins with '/'. |
| 48 | struct ProcInfoEntry { |
| 49 | base::ProcessId pid; |
| 50 | base::ProcessId ppid; |
| 51 | uid_t uid; |
| 52 | uid_t euid; |
[email protected] | b23d013 | 2011-01-12 00:20:45 | [diff] [blame] | 53 | // Explicitly use uint64_t instead of size_t in case this code is running |
| 54 | // in a 32 bit process and the target process is 64 bit. |
| 55 | uint64_t rss; |
| 56 | uint64_t rshrd; |
| 57 | uint64_t rprvt; |
| 58 | uint64_t vsize; |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 59 | std::string command; |
[email protected] | b23d013 | 2011-01-12 00:20:45 | [diff] [blame] | 60 | |
[email protected] | 23827ec | 2012-08-10 22:08:08 | [diff] [blame] | 61 | ProcInfoEntry(); |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | // Get process information for a given PID. |
| 65 | // |pid| - self-explanatory. |
| 66 | // |proc_info| - place to put the process information. |
| 67 | // returns - |true| if okay, |false| on error (including PID not found). |
| 68 | bool GetProcInfo(int pid, |
| 69 | ProcInfoEntry* proc_info) const; |
| 70 | |
| 71 | // Fills a |CommittedKBytes| with both resident and paged memory usage, as per |
| 72 | // its definition (or as close as we can manage). In the current (Mac) |
| 73 | // implementation, we map: |
| 74 | // vsize --> comm_priv, |
| 75 | // 0 --> comm_mapped, |
| 76 | // 0 --> comm_image; |
| 77 | // in about:memory: virtual:private = comm_priv, |
| 78 | // virtual:mapped = comm_mapped. |
| 79 | // TODO(viettrungluu): Doing such a mapping is kind of ugly. |
| 80 | // |pid| - self-explanatory. |
| 81 | // |usage| - pointer to |CommittedBytes| to fill; zero-ed on error. |
| 82 | // returns - |true| on success, |false| on error (including PID not found). |
| 83 | bool GetCommittedKBytesOfPID(int pid, |
| 84 | base::CommittedKBytes* usage) const; |
| 85 | |
| 86 | // Fills a |WorkingSetKBytes| containing resident private and shared memory, |
| 87 | // as per its definition (or as close as we can manage). In the current (Mac) |
| 88 | // implementation, we map: |
[email protected] | b23d013 | 2011-01-12 00:20:45 | [diff] [blame] | 89 | // rprvt --> ws_priv, |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 90 | // rss --> ws_shareable, |
[email protected] | b23d013 | 2011-01-12 00:20:45 | [diff] [blame] | 91 | // rshrd --> ws_shared; |
[email protected] | f164cea | 2009-11-05 23:37:40 | [diff] [blame] | 92 | // in about:memory: res:private = ws_priv + ws_shareable - ws_shared, |
| 93 | // res:shared = ws_shared / num_procs, |
| 94 | // res:total = res:private + res:shared. |
| 95 | // TODO(viettrungluu): Doing such a mapping is kind of ugly. |
| 96 | // |pid| - self-explanatory. |
| 97 | // |ws_usage| - pointer to |WorkingSetKBytes| to fill; zero-ed on error. |
| 98 | // returns - |true| on success, |false| on error (including PID not found). |
| 99 | bool GetWorkingSetKBytesOfPID(int pid, |
| 100 | base::WorkingSetKBytes* ws_usage) const; |
| 101 | |
| 102 | // TODO(viettrungluu): Maybe we should also have the following (again, for |
| 103 | // "compatibility"): |
| 104 | // size_t GetWorkingSetSizeOfPID(int pid) const; |
| 105 | // size_t GetPeakWorkingSetSizeOfPID(int pid) const; |
| 106 | // size_t GetPrivateBytesOfPID(int pid) const; |
| 107 | |
| 108 | private: |
| 109 | // map from |int| (PID) to |ProcInfoEntry| |
| 110 | std::map<int,ProcInfoEntry> proc_info_entries_; |
| 111 | }; |
| 112 | |
| 113 | #endif // CHROME_BROWSER_PROCESS_INFO_SNAPSHOT_H_ |