| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_PROCESS_INFO_SNAPSHOT_H_ | 5 #ifndef CHROME_BROWSER_PROCESS_INFO_SNAPSHOT_H_ |
| 6 #define CHROME_BROWSER_PROCESS_INFO_SNAPSHOT_H_ | 6 #define CHROME_BROWSER_PROCESS_INFO_SNAPSHOT_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <sys/types.h> | 9 #include <sys/types.h> |
| 10 | 10 |
| 11 #include <map> | 11 #include <map> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/process_util.h" | 15 #include "base/process_util.h" |
| 16 | 16 |
| 17 // A class which captures process information at a given point in time when its | 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. | 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 | 19 // |Sample()| may take a while to complete, so if calling from the browser |
| 20 // process, only do so from the file thread. The current implementation, only on | 20 // process, only do so from the file thread. |
| 21 // Mac, pulls information from /bin/ps. /usr/bin/top provides much more | |
| 22 // information about memory, but it has changed greatly from Mac OS 10.5.x to | |
| 23 // 10.6.x, thereby raising future compatibility concerns. Moreover, the 10.6.x | |
| 24 // version is less capable in terms of configuring output and its output is | |
| 25 // harder to parse. | |
| 26 // TODO(viettrungluu): This is currently only implemented and used on Mac, so | 21 // TODO(viettrungluu): This is currently only implemented and used on Mac, so |
| 27 // things are very Mac-specific. If this is ever implemented for other | 22 // things are very Mac-specific. If this is ever implemented for other |
| 28 // platforms, we should subclass and add opaqueness (probably |ProcInfoEntry| | 23 // platforms, we should subclass and add opaqueness (probably |ProcInfoEntry| |
| 29 // should be considered opaque). | 24 // should be considered opaque). |
| 30 class ProcessInfoSnapshot { | 25 class ProcessInfoSnapshot { |
| 31 public: | 26 public: |
| 32 ProcessInfoSnapshot(); | 27 ProcessInfoSnapshot(); |
| 33 ~ProcessInfoSnapshot(); | 28 ~ProcessInfoSnapshot(); |
| 34 | 29 |
| 35 // Maximum size of lists of PIDs which this class will accept; used in | 30 // Maximum size of lists of PIDs which this class will accept; used in |
| 36 // |Sample()| below. | 31 // |Sample()| below. |
| 37 static const size_t kMaxPidListSize; | 32 static const size_t kMaxPidListSize; |
| 38 | 33 |
| 39 // Capture a snapshot of process memory information (by running ps) for the | 34 // Capture a snapshot of process memory information for the |
| 40 // given list of PIDs. Call only from the file thread. | 35 // given list of PIDs. Call only from the file thread. |
| 41 // |pid_list| - list of |ProcessId|s on which to capture information; must | 36 // |pid_list| - list of |ProcessId|s on which to capture information; must |
| 42 // have no more than |kMaxPidListSize| (above) entries, | 37 // have no more than |kMaxPidListSize| (above) entries, |
| 43 // returns - |true| if okay, |false| on error. | 38 // returns - |true| if okay, |false| on error. |
| 44 bool Sample(std::vector<base::ProcessId> pid_list); | 39 bool Sample(std::vector<base::ProcessId> pid_list); |
| 45 | 40 |
| 46 // Reset all statistics (deallocating any memory allocated). | 41 // Reset all statistics (deallocating any memory allocated). |
| 47 void Reset(); | 42 void Reset(); |
| 48 | 43 |
| 49 // Our basic structure for storing information about a process (the names are | 44 // Our basic structure for storing information about a process (the names are |
| 50 // mostly self-explanatory). Note that |command| may not actually reflect the | 45 // mostly self-explanatory). Note that |command| may not actually reflect the |
| 51 // actual executable name; never trust it absolutely, and only take it | 46 // actual executable name; never trust it absolutely, and only take it |
| 52 // half-seriously when it begins with '/'. | 47 // half-seriously when it begins with '/'. |
| 53 struct ProcInfoEntry { | 48 struct ProcInfoEntry { |
| 54 base::ProcessId pid; | 49 base::ProcessId pid; |
| 55 base::ProcessId ppid; | 50 base::ProcessId ppid; |
| 56 uid_t uid; | 51 uid_t uid; |
| 57 uid_t euid; | 52 uid_t euid; |
| 58 size_t rss; | 53 // Explicitly use uint64_t instead of size_t in case this code is running |
| 59 size_t vsize; | 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; |
| 60 std::string command; | 59 std::string command; |
| 60 |
| 61 ProcInfoEntry() |
| 62 : pid(0), |
| 63 ppid(0), |
| 64 uid(0), |
| 65 euid(0), |
| 66 rss(0), |
| 67 rshrd(0), |
| 68 rprvt(0), |
| 69 vsize(0) { |
| 70 } |
| 61 }; | 71 }; |
| 62 | 72 |
| 63 // Get process information for a given PID. | 73 // Get process information for a given PID. |
| 64 // |pid| - self-explanatory. | 74 // |pid| - self-explanatory. |
| 65 // |proc_info| - place to put the process information. | 75 // |proc_info| - place to put the process information. |
| 66 // returns - |true| if okay, |false| on error (including PID not found). | 76 // returns - |true| if okay, |false| on error (including PID not found). |
| 67 bool GetProcInfo(int pid, | 77 bool GetProcInfo(int pid, |
| 68 ProcInfoEntry* proc_info) const; | 78 ProcInfoEntry* proc_info) const; |
| 69 | 79 |
| 70 // Fills a |CommittedKBytes| with both resident and paged memory usage, as per | 80 // 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) | 81 // its definition (or as close as we can manage). In the current (Mac) |
| 72 // implementation, we map: | 82 // implementation, we map: |
| 73 // vsize --> comm_priv, | 83 // vsize --> comm_priv, |
| 74 // 0 --> comm_mapped, | 84 // 0 --> comm_mapped, |
| 75 // 0 --> comm_image; | 85 // 0 --> comm_image; |
| 76 // in about:memory: virtual:private = comm_priv, | 86 // in about:memory: virtual:private = comm_priv, |
| 77 // virtual:mapped = comm_mapped. | 87 // virtual:mapped = comm_mapped. |
| 78 // TODO(viettrungluu): Doing such a mapping is kind of ugly. | 88 // TODO(viettrungluu): Doing such a mapping is kind of ugly. |
| 79 // |pid| - self-explanatory. | 89 // |pid| - self-explanatory. |
| 80 // |usage| - pointer to |CommittedBytes| to fill; zero-ed on error. | 90 // |usage| - pointer to |CommittedBytes| to fill; zero-ed on error. |
| 81 // returns - |true| on success, |false| on error (including PID not found). | 91 // returns - |true| on success, |false| on error (including PID not found). |
| 82 bool GetCommittedKBytesOfPID(int pid, | 92 bool GetCommittedKBytesOfPID(int pid, |
| 83 base::CommittedKBytes* usage) const; | 93 base::CommittedKBytes* usage) const; |
| 84 | 94 |
| 85 // Fills a |WorkingSetKBytes| containing resident private and shared memory, | 95 // 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) | 96 // as per its definition (or as close as we can manage). In the current (Mac) |
| 87 // implementation, we map: | 97 // implementation, we map: |
| 88 // 0 --> ws_priv, | 98 // rprvt --> ws_priv, |
| 89 // rss --> ws_shareable, | 99 // rss --> ws_shareable, |
| 90 // 0 --> ws_shared; | 100 // rshrd --> ws_shared; |
| 91 // in about:memory: res:private = ws_priv + ws_shareable - ws_shared, | 101 // in about:memory: res:private = ws_priv + ws_shareable - ws_shared, |
| 92 // res:shared = ws_shared / num_procs, | 102 // res:shared = ws_shared / num_procs, |
| 93 // res:total = res:private + res:shared. | 103 // res:total = res:private + res:shared. |
| 94 // TODO(viettrungluu): Doing such a mapping is kind of ugly. | 104 // TODO(viettrungluu): Doing such a mapping is kind of ugly. |
| 95 // |pid| - self-explanatory. | 105 // |pid| - self-explanatory. |
| 96 // |ws_usage| - pointer to |WorkingSetKBytes| to fill; zero-ed on error. | 106 // |ws_usage| - pointer to |WorkingSetKBytes| to fill; zero-ed on error. |
| 97 // returns - |true| on success, |false| on error (including PID not found). | 107 // returns - |true| on success, |false| on error (including PID not found). |
| 98 bool GetWorkingSetKBytesOfPID(int pid, | 108 bool GetWorkingSetKBytesOfPID(int pid, |
| 99 base::WorkingSetKBytes* ws_usage) const; | 109 base::WorkingSetKBytes* ws_usage) const; |
| 100 | 110 |
| 101 // TODO(viettrungluu): Maybe we should also have the following (again, for | 111 // TODO(viettrungluu): Maybe we should also have the following (again, for |
| 102 // "compatibility"): | 112 // "compatibility"): |
| 103 // size_t GetWorkingSetSizeOfPID(int pid) const; | 113 // size_t GetWorkingSetSizeOfPID(int pid) const; |
| 104 // size_t GetPeakWorkingSetSizeOfPID(int pid) const; | 114 // size_t GetPeakWorkingSetSizeOfPID(int pid) const; |
| 105 // size_t GetPrivateBytesOfPID(int pid) const; | 115 // size_t GetPrivateBytesOfPID(int pid) const; |
| 106 | 116 |
| 107 private: | 117 private: |
| 108 // map from |int| (PID) to |ProcInfoEntry| | 118 // map from |int| (PID) to |ProcInfoEntry| |
| 109 std::map<int,ProcInfoEntry> proc_info_entries_; | 119 std::map<int,ProcInfoEntry> proc_info_entries_; |
| 110 }; | 120 }; |
| 111 | 121 |
| 112 #endif // CHROME_BROWSER_PROCESS_INFO_SNAPSHOT_H_ | 122 #endif // CHROME_BROWSER_PROCESS_INFO_SNAPSHOT_H_ |
| OLD | NEW |