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_MEMORY_STATS_H_ 6 #define PROCESS_MEMORY_STATS_H_ 7 8 #include <stdint.h> 9 10 #include <memory> 11 #include <vector> 12 13 // Reads process memory stats from /proc/pid/{statm,smaps}. 14 class ProcessMemoryStats { 15 public: 16 struct MmapInfo { 17 char mapped_file[128] = {}; 18 char prot_flags[5] = {}; 19 uint64_t start_addr = 0; 20 uint64_t end_addr = 0; 21 uint64_t virt_kb = 0; 22 uint64_t pss_kb = 0; // Proportional Set Size. 23 uint64_t rss_kb = 0; // Resident Set Size. 24 uint64_t private_clean_kb = 0; 25 uint64_t private_dirty_kb = 0; 26 uint64_t shared_clean_kb = 0; 27 uint64_t shared_dirty_kb = 0; 28 uint64_t swapped_kb = 0; 29 }; 30 31 ProcessMemoryStats() {} 32 33 bool ReadLightStats(int pid); 34 bool ReadFullStats(int pid); 35 bool ReadGpuStats(int pid); 36 37 // Available after ReadLightStats(). 38 uint64_t virt_kb() const { return virt_kb_; } 39 uint64_t rss_kb() const { return rss_kb_; } 40 41 // Available after ReadFullStats(). 42 bool full_stats_available() const { return full_stats_; } 43 uint64_t pss_kb() const { return pss_kb_; } 44 uint64_t private_clean_kb() const { return private_clean_kb_; } 45 uint64_t private_dirty_kb() const { return private_dirty_kb_; } 46 uint64_t shared_clean_kb() const { return shared_clean_kb_; } 47 uint64_t shared_dirty_kb() const { return shared_dirty_kb_; } 48 uint64_t swapped_kb() const { return swapped_kb_; } 49 50 // Available after ReadMemtrackStats(). 51 bool gpu_stats_available() const { return gpu_stats_; } 52 uint64_t gpu_graphics_kb() const { return gpu_graphics_kb_; } 53 uint64_t gpu_graphics_pss_kb() const { return gpu_graphics_pss_kb_; } 54 uint64_t gpu_gl_kb() const { return gpu_gl_kb_; } 55 uint64_t gpu_gl_pss_kb() const { return gpu_gl_pss_kb_; } 56 uint64_t gpu_other_kb() const { return gpu_other_kb_; } 57 uint64_t gpu_other_pss_kb() const { return gpu_other_pss_kb_; } 58 59 size_t mmaps_count() const { return mmaps_.size(); } 60 const MmapInfo* mmap(size_t index) const { return mmaps_[index].get(); } 61 62 private: 63 ProcessMemoryStats(const ProcessMemoryStats&) = delete; 64 void operator=(const ProcessMemoryStats&) = delete; 65 66 // Light stats. 67 uint64_t virt_kb_ = 0; 68 uint64_t rss_kb_ = 0; 69 70 // Full stats. 71 bool full_stats_ = false; 72 uint64_t pss_kb_ = 0; 73 uint64_t private_clean_kb_ = 0; 74 uint64_t private_dirty_kb_ = 0; 75 uint64_t shared_clean_kb_ = 0; 76 uint64_t shared_dirty_kb_ = 0; 77 uint64_t swapped_kb_ = 0; 78 79 // Graphics stats. 80 bool gpu_stats_ = false; 81 uint64_t gpu_graphics_kb_ = 0; 82 uint64_t gpu_graphics_pss_kb_ = 0; 83 uint64_t gpu_gl_kb_ = 0; 84 uint64_t gpu_gl_pss_kb_ = 0; 85 uint64_t gpu_other_kb_ = 0; 86 uint64_t gpu_other_pss_kb_ = 0; 87 88 std::vector<std::unique_ptr<const MmapInfo>> mmaps_; 89 }; 90 91 #endif // PROCESS_MEMORY_STATS_H_ 92