Home | History | Annotate | Download | only in browser
      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_
      7 #pragma once
      8 
      9 #include <sys/types.h>
     10 
     11 #include <map>
     12 #include <string>
     13 #include <vector>
     14 
     15 #include "base/process_util.h"
     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
     20 // process, only do so from the file thread.
     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 
     30   // Maximum size of lists of PIDs which this class will accept; used in
     31   // |Sample()| below.
     32   static const size_t kMaxPidListSize;
     33 
     34   // Capture a snapshot of process memory information for the
     35   // given list of PIDs. Call only from the file thread.
     36   //   |pid_list| - list of |ProcessId|s on which to capture information; must
     37   //     have no more than |kMaxPidListSize| (above) entries,
     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;
     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;
     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     }
     71   };
     72 
     73   // Get process information for a given PID.
     74   //   |pid| - self-explanatory.
     75   //   |proc_info| - place to put the process information.
     76   //   returns - |true| if okay, |false| on error (including PID not found).
     77   bool GetProcInfo(int pid,
     78                    ProcInfoEntry* proc_info) const;
     79 
     80   // Fills a |CommittedKBytes| with both resident and paged memory usage, as per
     81   // its definition (or as close as we can manage). In the current (Mac)
     82   // implementation, we map:
     83   //                              vsize --> comm_priv,
     84   //                                  0 --> comm_mapped,
     85   //                                  0 --> comm_image;
     86   //   in about:memory: virtual:private  =  comm_priv,
     87   //                     virtual:mapped  =  comm_mapped.
     88   // TODO(viettrungluu): Doing such a mapping is kind of ugly.
     89   //   |pid| - self-explanatory.
     90   //   |usage| - pointer to |CommittedBytes| to fill; zero-ed on error.
     91   //   returns - |true| on success, |false| on error (including PID not found).
     92   bool GetCommittedKBytesOfPID(int pid,
     93                                base::CommittedKBytes* usage) const;
     94 
     95   // Fills a |WorkingSetKBytes| containing resident private and shared memory,
     96   // as per its definition (or as close as we can manage). In the current (Mac)
     97   // implementation, we map:
     98   //                          rprvt --> ws_priv,
     99   //                            rss --> ws_shareable,
    100   //                          rshrd --> ws_shared;
    101   //   in about:memory: res:private  =  ws_priv + ws_shareable - ws_shared,
    102   //                     res:shared  =  ws_shared / num_procs,
    103   //                      res:total  =  res:private + res:shared.
    104   // TODO(viettrungluu): Doing such a mapping is kind of ugly.
    105   //   |pid| - self-explanatory.
    106   //   |ws_usage| - pointer to |WorkingSetKBytes| to fill; zero-ed on error.
    107   //   returns - |true| on success, |false| on error (including PID not found).
    108   bool GetWorkingSetKBytesOfPID(int pid,
    109                                 base::WorkingSetKBytes* ws_usage) const;
    110 
    111   // TODO(viettrungluu): Maybe we should also have the following (again, for
    112   // "compatibility"):
    113   //    size_t GetWorkingSetSizeOfPID(int pid) const;
    114   //    size_t GetPeakWorkingSetSizeOfPID(int pid) const;
    115   //    size_t GetPrivateBytesOfPID(int pid) const;
    116 
    117  private:
    118   // map from |int| (PID) to |ProcInfoEntry|
    119   std::map<int,ProcInfoEntry> proc_info_entries_;
    120 };
    121 
    122 #endif  // CHROME_BROWSER_PROCESS_INFO_SNAPSHOT_H_
    123