Home | History | Annotate | Download | only in jni
      1 // Copyright 2017 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 PROCESS_INFO_H_
      6 #define PROCESS_INFO_H_
      7 
      8 #include <map>
      9 #include <memory>
     10 
     11 #include "process_memory_stats.h"
     12 
     13 // Reads various process stats and details from /proc/pid/.
     14 class ProcessInfo {
     15  public:
     16   struct ThreadInfo {
     17     char name[128] = {};
     18   };
     19   using ThreadInfoMap = std::map<int, std::unique_ptr<ThreadInfo>>;
     20 
     21   // Returns true if |pid| is a process (|pid| == TGID), false if it's just a
     22   // thread of another process, or if |pid| doesn't exist at all.
     23   static bool IsProcess(int pid);
     24 
     25   explicit ProcessInfo(int pid);
     26 
     27   bool ReadProcessName();
     28   bool ReadThreadNames();
     29   bool ReadOOMStats();
     30   bool ReadPageFaultsAndCPUTimeStats();
     31 
     32   ProcessMemoryStats* memory() { return &memory_; }
     33   const ProcessMemoryStats* memory() const { return &memory_; }
     34   const ThreadInfoMap* threads() const { return &threads_; }
     35   const char* name() const { return name_; }
     36   const char* exe() const { return exe_; }
     37 
     38   int oom_adj() const { return oom_adj_; }
     39   int oom_score_adj() const { return oom_score_adj_; }
     40   int oom_score() const { return oom_score_; }
     41 
     42   unsigned long minflt() const { return minflt_; }
     43   unsigned long majflt() const { return majflt_; }
     44   unsigned long utime() const { return utime_; }
     45   unsigned long stime() const { return stime_; }
     46   unsigned long long start_time() const { return start_time_; }
     47 
     48  private:
     49   ProcessInfo(const ProcessInfo&) = delete;
     50   void operator=(const ProcessInfo&) = delete;
     51 
     52   ProcessMemoryStats memory_;
     53 
     54   ThreadInfoMap threads_;
     55   char name_[128] = {};
     56   char exe_[128] = {};
     57 
     58   int oom_adj_ = 0;
     59   int oom_score_adj_ = 0;
     60   int oom_score_ = 0;
     61 
     62   unsigned long minflt_ = 0;
     63   unsigned long majflt_ = 0;
     64   unsigned long utime_ = 0;            // CPU time in user mode.
     65   unsigned long stime_ = 0;            // CPU time in kernel mode.
     66   unsigned long long start_time_ = 0;  // CPU time in kernel mode.
     67 
     68   const int pid_;
     69 };
     70 
     71 #endif  // PROCESS_INFO_H_
     72