Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 PROCESS_INFO_H_ | 5 #ifndef PROCESS_INFO_H_ |
| 6 #define PROCESS_INFO_H_ | 6 #define PROCESS_INFO_H_ |
| 7 | 7 |
| 8 #include <functional> | |
| 8 #include <map> | 9 #include <map> |
| 9 #include <memory> | 10 #include <memory> |
| 11 #include <string> | |
| 10 | 12 |
| 11 #include "process_memory_stats.h" | 13 #include "process_memory_stats.h" |
| 12 | 14 |
| 13 // Reads various process stats and details from /proc/pid/. | 15 // Contains time-indefinite process information from multiple procfs dumps. |
| 14 class ProcessInfo { | 16 class PersistentProcessInfo { |
| 15 public: | 17 public: |
| 16 struct ThreadInfo { | 18 struct ThreadInfo { |
| 17 char name[128] = {}; | 19 int tid; |
| 20 std::string name; | |
|
Primiano Tucci (use gerrit)
2017/06/22 07:55:52
isn't this going to create all sort of (slower) he
kraynov
2017/06/22 09:19:45
It takes O(n_processes) to allocate all the string
| |
| 18 }; | 21 }; |
| 19 using ThreadInfoMap = std::map<int, std::unique_ptr<ThreadInfo>>; | 22 using ThreadMap = std::map<int, std::unique_ptr<ThreadInfo>>; |
| 20 | 23 |
| 21 // Returns true if |pid| is a process (|pid| == TGID), false if it's just a | 24 struct ProcessInfo { |
| 22 // thread of another process, or if |pid| doesn't exist at all. | 25 int pid; |
| 23 static bool IsProcess(int pid); | 26 bool in_kernel; |
| 24 | 27 bool is_app; |
| 25 explicit ProcessInfo(int pid); | 28 std::string name; |
| 26 | 29 std::string exe; |
| 27 bool ReadProcessName(); | 30 ThreadMap threads; |
| 28 bool ReadThreadNames(); | 31 }; |
| 29 bool ReadOOMStats(); | 32 using ProcessMap = std::map<int, std::unique_ptr<ProcessInfo>>; |
| 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 | 33 |
| 48 private: | 34 private: |
| 49 ProcessInfo(const ProcessInfo&) = delete; | 35 bool UpdateProcessInfo(int pid); |
| 50 void operator=(const ProcessInfo&) = delete; | |
| 51 | 36 |
| 52 ProcessMemoryStats memory_; | 37 ProcessMap processes_; |
| 38 friend class ProcessDumpManager; | |
| 39 }; | |
| 53 | 40 |
| 54 ThreadInfoMap threads_; | 41 // Contains process information captured at specific moment. |
| 55 char name_[128] = {}; | 42 class InstantProcessInfo { |
| 56 char exe_[128] = {}; | 43 public: |
| 44 struct ProcessSnapshot { | |
| 45 int pid; | |
| 46 ProcessMemoryStats memory; | |
| 47 // OOM badness and tolerance (oom_adj is deprecated). | |
| 48 int oom_score; | |
| 49 int oom_score_adj; | |
| 50 // Page faults. | |
| 51 unsigned long minor_faults; | |
| 52 unsigned long major_faults; | |
| 53 // Time spent in userspace and in the kernel. | |
| 54 unsigned long utime; | |
| 55 unsigned long stime; | |
| 56 }; | |
| 57 using ProcessSnapshotMap = | |
| 58 std::map<int, std::unique_ptr<ProcessSnapshot>>; | |
| 57 | 59 |
| 58 int oom_adj_ = 0; | 60 uint64_t timestamp() const { return timestamp_; }; |
| 59 int oom_score_adj_ = 0; | 61 const ProcessSnapshotMap* processes() const { return &processes_; } |
| 60 int oom_score_ = 0; | |
| 61 | 62 |
| 62 unsigned long minflt_ = 0; | 63 private: |
| 63 unsigned long majflt_ = 0; | 64 uint64_t timestamp_; |
| 64 unsigned long utime_ = 0; // CPU time in user mode. | 65 ProcessSnapshotMap processes_; |
| 65 unsigned long stime_ = 0; // CPU time in kernel mode. | 66 friend class ProcessDumpManager; |
| 66 unsigned long long start_time_ = 0; // CPU time in kernel mode. | 67 }; |
| 67 | 68 |
| 68 const int pid_; | 69 class ProcessDumpManager { |
| 70 public: | |
| 71 using DumpPredicate = | |
| 72 std::function<bool(const PersistentProcessInfo::ProcessInfo*)>; | |
| 73 | |
| 74 void SetFullDumpPredicate(const DumpPredicate& predicate); | |
|
Primiano Tucci (use gerrit)
2017/06/22 07:55:51
honestly I don't see the need of passing a predica
kraynov
2017/06/22 09:19:45
The logic which process gets which dump is the sco
Primiano Tucci (use gerrit)
2017/06/26 10:07:19
the dump predicate seems to be still here, but I t
| |
| 75 void SetGraphicsDumpPredicate(const DumpPredicate& predicate); | |
| 76 | |
| 77 bool TakeSnapshot(); | |
| 78 | |
| 79 const InstantProcessInfo* last_snapshot() const { | |
| 80 return &global_snapshot_; | |
| 81 } | |
| 82 | |
| 83 const PersistentProcessInfo::ProcessMap* all_processes() const { | |
| 84 return &persistent_.processes_; | |
| 85 } | |
| 86 | |
| 87 private: | |
| 88 PersistentProcessInfo persistent_; | |
| 89 InstantProcessInfo global_snapshot_; | |
| 90 | |
| 91 std::unique_ptr<const DumpPredicate> full_dump_predicate_; | |
| 92 std::unique_ptr<const DumpPredicate> graphics_dump_predicate_; | |
| 69 }; | 93 }; |
| 70 | 94 |
| 71 #endif // PROCESS_INFO_H_ | 95 #endif // PROCESS_INFO_H_ |
| OLD | NEW |