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     static constexpr const char* kMemKReclaimable = "KReclaimable:";
47 
48     static const std::vector<std::string> kDefaultSysMemInfoTags;
49 
50     SysMemInfo() = default;
51 
52     // Parse /proc/meminfo and read values that are needed
53     bool ReadMemInfo(const std::string& path = "/proc/meminfo");
54     bool ReadMemInfo(const std::vector<std::string>& tags, std::vector<uint64_t>* out,
55                      const std::string& path = "/proc/meminfo");
56     bool ReadMemInfo(std::vector<uint64_t>* out, const std::string& path = "/proc/meminfo");
57 
58     // Parse /proc/vmallocinfo and return total physical memory mapped
59     // in vmalloc area by the kernel.
60     // Note that this deliberately ignores binder buffers. They are _always_
61     // mapped in a process and are counted for in each process.
62     uint64_t ReadVmallocInfo();
63 
64     // getters
mem_total_kb()65     uint64_t mem_total_kb() { return mem_in_kb_[kMemTotal]; }
mem_free_kb()66     uint64_t mem_free_kb() { return mem_in_kb_[kMemFree]; }
mem_buffers_kb()67     uint64_t mem_buffers_kb() { return mem_in_kb_[kMemBuffers]; }
mem_cached_kb()68     uint64_t mem_cached_kb() { return mem_in_kb_[kMemCached]; }
mem_shmem_kb()69     uint64_t mem_shmem_kb() { return mem_in_kb_[kMemShmem]; }
mem_slab_kb()70     uint64_t mem_slab_kb() { return mem_in_kb_[kMemSlab]; }
mem_slab_reclaimable_kb()71     uint64_t mem_slab_reclaimable_kb() { return mem_in_kb_[kMemSReclaim]; }
mem_slab_unreclaimable_kb()72     uint64_t mem_slab_unreclaimable_kb() { return mem_in_kb_[kMemSUnreclaim]; }
mem_swap_kb()73     uint64_t mem_swap_kb() { return mem_in_kb_[kMemSwapTotal]; }
mem_swap_free_kb()74     uint64_t mem_swap_free_kb() { return mem_in_kb_[kMemSwapFree]; }
mem_mapped_kb()75     uint64_t mem_mapped_kb() { return mem_in_kb_[kMemMapped]; }
mem_vmalloc_used_kb()76     uint64_t mem_vmalloc_used_kb() { return mem_in_kb_[kMemVmallocUsed]; }
mem_page_tables_kb()77     uint64_t mem_page_tables_kb() { return mem_in_kb_[kMemPageTables]; }
mem_kernel_stack_kb()78     uint64_t mem_kernel_stack_kb() { return mem_in_kb_[kMemKernelStack]; }
mem_kreclaimable_kb()79     uint64_t mem_kreclaimable_kb() { return mem_in_kb_[kMemKReclaimable]; }
80     uint64_t mem_zram_kb(const std::string& zram_dev = "");
81 
82   private:
83     std::map<std::string, uint64_t> mem_in_kb_;
84     bool MemZramDevice(const std::string& zram_dev, uint64_t* mem_zram_dev);
85     bool ReadMemInfo(const std::vector<std::string>& tags, const std::string& path,
86                      std::function<void(const std::string&, uint64_t)> store_val);
87 };
88 
89 // Parse /proc/vmallocinfo and return total physical memory mapped
90 // in vmalloc area by the kernel. Note that this deliberately ignores binder buffers. They are
91 // _always_ mapped in a process and are counted for in each process.
92 uint64_t ReadVmallocInfo(const std::string& path = "/proc/vmallocinfo");
93 
94 // Read ION heaps allocation size in kb
95 bool ReadIonHeapsSizeKb(
96     uint64_t* size, const std::string& path = "/sys/kernel/ion/total_heaps_kb");
97 
98 // Read ION pools allocation size in kb
99 bool ReadIonPoolsSizeKb(
100     uint64_t* size, const std::string& path = "/sys/kernel/ion/total_pools_kb");
101 
102 }  // namespace meminfo
103 }  // namespace android
104