1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <sys/types.h> 20 21 #include <array> 22 #include <functional> 23 #include <initializer_list> 24 #include <map> 25 #include <string> 26 #include <string_view> 27 #include <unordered_map> 28 29 namespace android { 30 namespace meminfo { 31 32 static constexpr const char kDmabufHeapRoot[] = "/dev/dma_heap"; 33 34 class SysMemInfo final { 35 // System or Global memory accounting 36 public: 37 static constexpr const char kMemTotal[] = "MemTotal:"; 38 static constexpr const char kMemFree[] = "MemFree:"; 39 static constexpr const char kMemBuffers[] = "Buffers:"; 40 static constexpr const char kMemCached[] = "Cached:"; 41 static constexpr const char kMemShmem[] = "Shmem:"; 42 static constexpr const char kMemSlab[] = "Slab:"; 43 static constexpr const char kMemSReclaim[] = "SReclaimable:"; 44 static constexpr const char kMemSUnreclaim[] = "SUnreclaim:"; 45 static constexpr const char kMemSwapTotal[] = "SwapTotal:"; 46 static constexpr const char kMemSwapFree[] = "SwapFree:"; 47 static constexpr const char kMemMapped[] = "Mapped:"; 48 static constexpr const char kMemVmallocUsed[] = "VmallocUsed:"; 49 static constexpr const char kMemPageTables[] = "PageTables:"; 50 static constexpr const char kMemKernelStack[] = "KernelStack:"; 51 static constexpr const char kMemKReclaimable[] = "KReclaimable:"; 52 static constexpr const char kMemActive[] = "Active:"; 53 static constexpr const char kMemInactive[] = "Inactive:"; 54 static constexpr const char kMemUnevictable[] = "Unevictable:"; 55 56 57 static constexpr std::initializer_list<std::string_view> kDefaultSysMemInfoTags = { 58 SysMemInfo::kMemTotal, SysMemInfo::kMemFree, SysMemInfo::kMemBuffers, 59 SysMemInfo::kMemCached, SysMemInfo::kMemShmem, SysMemInfo::kMemSlab, 60 SysMemInfo::kMemSReclaim, SysMemInfo::kMemSUnreclaim, SysMemInfo::kMemSwapTotal, 61 SysMemInfo::kMemSwapFree, SysMemInfo::kMemMapped, SysMemInfo::kMemVmallocUsed, 62 SysMemInfo::kMemPageTables, SysMemInfo::kMemKernelStack, SysMemInfo::kMemKReclaimable, 63 SysMemInfo::kMemActive, SysMemInfo::kMemInactive, SysMemInfo::kMemUnevictable, 64 }; 65 66 SysMemInfo() = default; 67 68 // Parse /proc/meminfo and read values that are needed 69 bool ReadMemInfo(const char* path = "/proc/meminfo"); 70 bool ReadMemInfo(size_t ntags, const std::string_view* tags, uint64_t* out, 71 const char* path = "/proc/meminfo"); 72 bool ReadMemInfo(std::vector<uint64_t>* out, const char* path = "/proc/meminfo"); 73 74 // Parse /proc/vmallocinfo and return total physical memory mapped 75 // in vmalloc area by the kernel. 76 // Note that this deliberately ignores binder buffers. They are _always_ 77 // mapped in a process and are counted for in each process. 78 uint64_t ReadVmallocInfo(); 79 80 // getters mem_total_kb()81 uint64_t mem_total_kb() { return mem_in_kb_[kMemTotal]; } mem_free_kb()82 uint64_t mem_free_kb() { return mem_in_kb_[kMemFree]; } mem_buffers_kb()83 uint64_t mem_buffers_kb() { return mem_in_kb_[kMemBuffers]; } mem_cached_kb()84 uint64_t mem_cached_kb() { return mem_in_kb_[kMemCached]; } mem_shmem_kb()85 uint64_t mem_shmem_kb() { return mem_in_kb_[kMemShmem]; } mem_slab_kb()86 uint64_t mem_slab_kb() { return mem_in_kb_[kMemSlab]; } mem_slab_reclaimable_kb()87 uint64_t mem_slab_reclaimable_kb() { return mem_in_kb_[kMemSReclaim]; } mem_slab_unreclaimable_kb()88 uint64_t mem_slab_unreclaimable_kb() { return mem_in_kb_[kMemSUnreclaim]; } mem_swap_kb()89 uint64_t mem_swap_kb() { return mem_in_kb_[kMemSwapTotal]; } mem_swap_free_kb()90 uint64_t mem_swap_free_kb() { return mem_in_kb_[kMemSwapFree]; } mem_mapped_kb()91 uint64_t mem_mapped_kb() { return mem_in_kb_[kMemMapped]; } mem_vmalloc_used_kb()92 uint64_t mem_vmalloc_used_kb() { return mem_in_kb_[kMemVmallocUsed]; } mem_page_tables_kb()93 uint64_t mem_page_tables_kb() { return mem_in_kb_[kMemPageTables]; } mem_kernel_stack_kb()94 uint64_t mem_kernel_stack_kb() { return mem_in_kb_[kMemKernelStack]; } mem_kreclaimable_kb()95 uint64_t mem_kreclaimable_kb() { return mem_in_kb_[kMemKReclaimable]; } mem_active_kb()96 uint64_t mem_active_kb() { return mem_in_kb_[kMemActive]; } mem_inactive_kb()97 uint64_t mem_inactive_kb() { return mem_in_kb_[kMemInactive]; } mem_unevictable_kb()98 uint64_t mem_unevictable_kb() { return mem_in_kb_[kMemUnevictable]; } 99 uint64_t mem_zram_kb(const char* zram_dev = nullptr); 100 101 private: 102 std::map<std::string_view, uint64_t> mem_in_kb_; 103 bool MemZramDevice(const char* zram_dev, uint64_t* mem_zram_dev); 104 bool ReadMemInfo(const char* path, size_t ntags, const std::string_view* tags, 105 std::function<void(std::string_view, uint64_t)> store_val); 106 }; 107 108 // Parse /proc/vmallocinfo and return total physical memory mapped 109 // in vmalloc area by the kernel. Note that this deliberately ignores binder buffers. They are 110 // _always_ mapped in a process and are counted for in each process. 111 uint64_t ReadVmallocInfo(const char* path = "/proc/vmallocinfo"); 112 113 // Read ION heaps allocation size in kb 114 bool ReadIonHeapsSizeKb( 115 uint64_t* size, const std::string& path = "/sys/kernel/ion/total_heaps_kb"); 116 117 // Read ION pools allocation size in kb 118 bool ReadIonPoolsSizeKb( 119 uint64_t* size, const std::string& path = "/sys/kernel/ion/total_pools_kb"); 120 121 // Read DMA-BUF heap pools allocation size in kb 122 bool ReadDmabufHeapPoolsSizeKb(uint64_t* size, 123 const std::string& path = "/sys/kernel/dma_heap/total_pools_kb"); 124 125 // Read per-process GPU memory usage. Returns a map of pid -> GPU Mem in kilobytes. 126 bool ReadPerProcessGpuMem(std::unordered_map<uint32_t, uint64_t>* out); 127 128 // Read GPU usage of the specified process in kb. 129 bool ReadProcessGpuUsageKb(uint32_t pid, uint32_t gpu_id, uint64_t* size); 130 131 // Read GPU total usage size in kb 132 bool ReadGpuTotalUsageKb(uint64_t* size); 133 134 // Read total size of DMA-BUFs exported from the DMA-BUF heap framework in kb 135 bool ReadDmabufHeapTotalExportedKb( 136 uint64_t* size, const std::string& dma_heap_root = kDmabufHeapRoot, 137 const std::string& dma_buf_sysfs_path = "/sys/kernel/dmabuf/buffers"); 138 139 } // namespace meminfo 140 } // namespace android 141