Linux Memory Stats

In order to impliment the functions in bug 16251, we need to work out what the stats map to under Linux.

References are /proc/<pid>/stat, /proc/<pid>/status, the kernel source that populates those files and ps mem

GetPeakWorkingSetSize

  // Returns the peak working set size, in bytes.
  size_t GetPeakWorkingSetSize() const;
/proc/pid/status gives us VmHWM (kB), which is the max value of RSS (get_mm_rss()) reached.

There is code that has already tokenised /proc/<pid>/stat, which gives the value of RSS - we could do the caching ($$if (max_val < current_val) max_val = current_val;$$) in Chrome instead of opening and tokenising another file. This probably isn't a solution as the chrome code will be called intermittantly, whereas the kernel value will be accurate for the lifetime of the process.

GetPrivateBytes

  // Returns private usage, in bytes. Private bytes is the amount
  // of memory currently allocated to a process that cannot be shared.
  // Note: returns 0 on unsupported OSes: prior to XP SP2.
  size_t GetPrivateBytes() const;
ps_mem.py uses the sum of all private mappings in /proc/pid/smaps.

GetWorkingSetKBytes

  // Fills a WorkingSetKBytes containing resident private and shared memory
  // usage in bytes, as per definition of WorkingSetBytes.
  bool GetWorkingSetKBytes(WorkingSetKBytes* ws_usage) const;
the struct we have to fill out looks like this:

// Working Set (resident) memory usage broken down by
// priv (private): These pages (kbytes) cannot be shared with any other process.
// shareable:      These pages (kbytes) can be shared with other processes under
//                 the right circumstances.
// shared :        These pages (kbytes) are currently shared with at least one
//                 other process.
struct WorkingSetKBytes {
  size_t priv;
  size_t shareable;
  size_t shared;
};

priv = GetPrivateBytes();

sharable = (could this be Private_Clean? Does copy on write mean that private clean could be shared with the parent?)

shared =

ps_mem.py uses the sum of all shared mappings in /proc/pid/smaps (as with private), except when there's PSS, in which case Shared = Pss - Private.

when statm does not exist, the fallback is:

 Shared=int(open("/proc/"+str(pid)+"/statm").readline().split()[2])
 Shared*=PAGESIZE
 Private = Rss - Shared
However, this has huge margins of error when tested on chrome with many tabs open, so is not an option:
pid:  13557
 Shared:        4850    20048   313.36%
 Private:       37224   31636   -15.01%

pid:  13567
 Shared:        1432    1660    15.88%
 Private:       2932    5280    80.08%

pid:  13568
 Shared:        1482    5904    298.25%
 Private:       276     956     246.38%

pid:  13582
 Shared:        1051    10564   904.66%
 Private:       98852   99452   0.61%

pid:  13883
 Shared:        1124    10984   877.22%
 Private:       21660   22296   2.94%

pid:  13902
 Shared:        1099    10868   888.45%
 Private:       37520   38160   1.71%

pid:  14125
 Shared:        565     6624    1071.35%
 Private:       1148    1832    59.58%

pid:  14129
 Shared:        703     8224    1069.01%
 Private:       10392   11044   6.27%

pid:  14135
 Shared:        921     9944    979.70%
 Private:       21304   21944   3.00%