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