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 <functional> 22 #include <map> 23 #include <string> 24 #include <vector> 25 26 namespace android { 27 namespace meminfo { 28 29 class SysMemInfo final { 30 // System or Global memory accounting 31 public: 32 static constexpr const char* kMemTotal = "MemTotal:"; 33 static constexpr const char* kMemFree = "MemFree:"; 34 static constexpr const char* kMemBuffers = "Buffers:"; 35 static constexpr const char* kMemCached = "Cached:"; 36 static constexpr const char* kMemShmem = "Shmem:"; 37 static constexpr const char* kMemSlab = "Slab:"; 38 static constexpr const char* kMemSReclaim = "SReclaimable:"; 39 static constexpr const char* kMemSUnreclaim = "SUnreclaim:"; 40 static constexpr const char* kMemSwapTotal = "SwapTotal:"; 41 static constexpr const char* kMemSwapFree = "SwapFree:"; 42 static constexpr const char* kMemMapped = "Mapped:"; 43 static constexpr const char* kMemVmallocUsed = "VmallocUsed:"; 44 static constexpr const char* kMemPageTables = "PageTables:"; 45 static constexpr const char* kMemKernelStack = "KernelStack:"; 46 47 static const std::vector<std::string> kDefaultSysMemInfoTags; 48 49 SysMemInfo() = default; 50 51 // Parse /proc/meminfo and read values that are needed 52 bool ReadMemInfo(const std::string& path = "/proc/meminfo"); 53 bool ReadMemInfo(const std::vector<std::string>& tags, std::vector<uint64_t>* out, 54 const std::string& path = "/proc/meminfo"); 55 bool ReadMemInfo(std::vector<uint64_t>* out, const std::string& path = "/proc/meminfo"); 56 57 // Parse /proc/vmallocinfo and return total physical memory mapped 58 // in vmalloc area by the kernel. 59 // Note that this deliberately ignores binder buffers. They are _always_ 60 // mapped in a process and are counted for in each process. 61 uint64_t ReadVmallocInfo(); 62 63 // getters mem_total_kb()64 uint64_t mem_total_kb() { return mem_in_kb_[kMemTotal]; } mem_free_kb()65 uint64_t mem_free_kb() { return mem_in_kb_[kMemFree]; } mem_buffers_kb()66 uint64_t mem_buffers_kb() { return mem_in_kb_[kMemBuffers]; } mem_cached_kb()67 uint64_t mem_cached_kb() { return mem_in_kb_[kMemCached]; } mem_shmem_kb()68 uint64_t mem_shmem_kb() { return mem_in_kb_[kMemShmem]; } mem_slab_kb()69 uint64_t mem_slab_kb() { return mem_in_kb_[kMemSlab]; } mem_slab_reclaimable_kb()70 uint64_t mem_slab_reclaimable_kb() { return mem_in_kb_[kMemSReclaim]; } mem_slab_unreclaimable_kb()71 uint64_t mem_slab_unreclaimable_kb() { return mem_in_kb_[kMemSUnreclaim]; } mem_swap_kb()72 uint64_t mem_swap_kb() { return mem_in_kb_[kMemSwapTotal]; } mem_swap_free_kb()73 uint64_t mem_swap_free_kb() { return mem_in_kb_[kMemSwapFree]; } mem_mapped_kb()74 uint64_t mem_mapped_kb() { return mem_in_kb_[kMemMapped]; } mem_vmalloc_used_kb()75 uint64_t mem_vmalloc_used_kb() { return mem_in_kb_[kMemVmallocUsed]; } mem_page_tables_kb()76 uint64_t mem_page_tables_kb() { return mem_in_kb_[kMemPageTables]; } mem_kernel_stack_kb()77 uint64_t mem_kernel_stack_kb() { return mem_in_kb_[kMemKernelStack]; } 78 uint64_t mem_zram_kb(const std::string& zram_dev = ""); 79 80 private: 81 std::map<std::string, uint64_t> mem_in_kb_; 82 bool MemZramDevice(const std::string& zram_dev, uint64_t* mem_zram_dev); 83 bool ReadMemInfo(const std::vector<std::string>& tags, const std::string& path, 84 std::function<void(const std::string&, uint64_t)> store_val); 85 }; 86 87 // Parse /proc/vmallocinfo and return total physical memory mapped 88 // in vmalloc area by the kernel. Note that this deliberately ignores binder buffers. They are 89 // _always_ mapped in a process and are counted for in each process. 90 uint64_t ReadVmallocInfo(const std::string& path = "/proc/vmallocinfo"); 91 92 } // namespace meminfo 93 } // namespace android 94