• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
memory()32   ProcessMemoryStats* memory() { return &memory_; }
memory()33   const ProcessMemoryStats* memory() const { return &memory_; }
threads()34   const ThreadInfoMap* threads() const { return &threads_; }
name()35   const char* name() const { return name_; }
exe()36   const char* exe() const { return exe_; }
37 
oom_adj()38   int oom_adj() const { return oom_adj_; }
oom_score_adj()39   int oom_score_adj() const { return oom_score_adj_; }
oom_score()40   int oom_score() const { return oom_score_; }
41 
minflt()42   unsigned long minflt() const { return minflt_; }
majflt()43   unsigned long majflt() const { return majflt_; }
utime()44   unsigned long utime() const { return utime_; }
stime()45   unsigned long stime() const { return stime_; }
start_time()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