• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static constexpr const char kDmabufHeapRoot[] = "/dev/dma_heap";
32 
33 class SysMemInfo final {
34     // System or Global memory accounting
35   public:
36     static constexpr const char kMemTotal[] = "MemTotal:";
37     static constexpr const char kMemFree[] = "MemFree:";
38     static constexpr const char kMemBuffers[] = "Buffers:";
39     static constexpr const char kMemCached[] = "Cached:";
40     static constexpr const char kMemShmem[] = "Shmem:";
41     static constexpr const char kMemSlab[] = "Slab:";
42     static constexpr const char kMemSReclaim[] = "SReclaimable:";
43     static constexpr const char kMemSUnreclaim[] = "SUnreclaim:";
44     static constexpr const char kMemSwapTotal[] = "SwapTotal:";
45     static constexpr const char kMemSwapFree[] = "SwapFree:";
46     static constexpr const char kMemMapped[] = "Mapped:";
47     static constexpr const char kMemVmallocUsed[] = "VmallocUsed:";
48     static constexpr const char kMemPageTables[] = "PageTables:";
49     static constexpr const char kMemKernelStack[] = "KernelStack:";
50     static constexpr const char kMemKReclaimable[] = "KReclaimable:";
51     static constexpr const char kMemActive[] = "Active:";
52     static constexpr const char kMemInactive[] = "Inactive:";
53     static constexpr const char kMemUnevictable[] = "Unevictable:";
54 
55 
56     static constexpr std::initializer_list<std::string_view> kDefaultSysMemInfoTags = {
57             SysMemInfo::kMemTotal,      SysMemInfo::kMemFree,        SysMemInfo::kMemBuffers,
58             SysMemInfo::kMemCached,     SysMemInfo::kMemShmem,       SysMemInfo::kMemSlab,
59             SysMemInfo::kMemSReclaim,   SysMemInfo::kMemSUnreclaim,  SysMemInfo::kMemSwapTotal,
60             SysMemInfo::kMemSwapFree,   SysMemInfo::kMemMapped,      SysMemInfo::kMemVmallocUsed,
61             SysMemInfo::kMemPageTables, SysMemInfo::kMemKernelStack, SysMemInfo::kMemKReclaimable,
62             SysMemInfo::kMemActive,     SysMemInfo::kMemInactive,    SysMemInfo::kMemUnevictable,
63     };
64 
65     SysMemInfo() = default;
66 
67     // Parse /proc/meminfo and read values that are needed
68     bool ReadMemInfo(const char* path = "/proc/meminfo");
69     bool ReadMemInfo(size_t ntags, const std::string_view* tags, uint64_t* out,
70                      const char* path = "/proc/meminfo");
71     bool ReadMemInfo(std::vector<uint64_t>* out, const char* path = "/proc/meminfo");
72 
73     // Parse /proc/vmallocinfo and return total physical memory mapped
74     // in vmalloc area by the kernel.
75     // Note that this deliberately ignores binder buffers. They are _always_
76     // mapped in a process and are counted for in each process.
77     uint64_t ReadVmallocInfo();
78 
79     // getters
mem_total_kb()80     uint64_t mem_total_kb() { return mem_in_kb_[kMemTotal]; }
mem_free_kb()81     uint64_t mem_free_kb() { return mem_in_kb_[kMemFree]; }
mem_buffers_kb()82     uint64_t mem_buffers_kb() { return mem_in_kb_[kMemBuffers]; }
mem_cached_kb()83     uint64_t mem_cached_kb() { return mem_in_kb_[kMemCached]; }
mem_shmem_kb()84     uint64_t mem_shmem_kb() { return mem_in_kb_[kMemShmem]; }
mem_slab_kb()85     uint64_t mem_slab_kb() { return mem_in_kb_[kMemSlab]; }
mem_slab_reclaimable_kb()86     uint64_t mem_slab_reclaimable_kb() { return mem_in_kb_[kMemSReclaim]; }
mem_slab_unreclaimable_kb()87     uint64_t mem_slab_unreclaimable_kb() { return mem_in_kb_[kMemSUnreclaim]; }
mem_swap_kb()88     uint64_t mem_swap_kb() { return mem_in_kb_[kMemSwapTotal]; }
mem_swap_free_kb()89     uint64_t mem_swap_free_kb() { return mem_in_kb_[kMemSwapFree]; }
mem_mapped_kb()90     uint64_t mem_mapped_kb() { return mem_in_kb_[kMemMapped]; }
mem_vmalloc_used_kb()91     uint64_t mem_vmalloc_used_kb() { return mem_in_kb_[kMemVmallocUsed]; }
mem_page_tables_kb()92     uint64_t mem_page_tables_kb() { return mem_in_kb_[kMemPageTables]; }
mem_kernel_stack_kb()93     uint64_t mem_kernel_stack_kb() { return mem_in_kb_[kMemKernelStack]; }
mem_kreclaimable_kb()94     uint64_t mem_kreclaimable_kb() { return mem_in_kb_[kMemKReclaimable]; }
mem_active_kb()95     uint64_t mem_active_kb() { return mem_in_kb_[kMemActive]; }
mem_inactive_kb()96     uint64_t mem_inactive_kb() { return mem_in_kb_[kMemInactive]; }
mem_unevictable_kb()97     uint64_t mem_unevictable_kb() { return mem_in_kb_[kMemUnevictable]; }
98     uint64_t mem_zram_kb(const char* zram_dev = nullptr);
99 
100   private:
101     std::map<std::string_view, uint64_t> mem_in_kb_;
102     bool MemZramDevice(const char* zram_dev, uint64_t* mem_zram_dev);
103     bool ReadMemInfo(const char* path, size_t ntags, const std::string_view* tags,
104                      std::function<void(std::string_view, uint64_t)> store_val);
105 };
106 
107 // Parse /proc/vmallocinfo and return total physical memory mapped
108 // in vmalloc area by the kernel. Note that this deliberately ignores binder buffers. They are
109 // _always_ mapped in a process and are counted for in each process.
110 uint64_t ReadVmallocInfo(const char* path = "/proc/vmallocinfo");
111 
112 // Read ION heaps allocation size in kb
113 bool ReadIonHeapsSizeKb(
114     uint64_t* size, const std::string& path = "/sys/kernel/ion/total_heaps_kb");
115 
116 // Read ION pools allocation size in kb
117 bool ReadIonPoolsSizeKb(
118     uint64_t* size, const std::string& path = "/sys/kernel/ion/total_pools_kb");
119 
120 // Read DMA-BUF heap pools allocation size in kb
121 bool ReadDmabufHeapPoolsSizeKb(uint64_t* size,
122                             const std::string& path = "/sys/kernel/dma_heap/total_pools_kb");
123 
124 // Read GPU total usage size in kb
125 bool ReadGpuTotalUsageKb(uint64_t* size);
126 
127 // Read total size of DMA-BUFs exported from the DMA-BUF heap framework in kb
128 bool ReadDmabufHeapTotalExportedKb(
129         uint64_t* size, const std::string& dma_heap_root = kDmabufHeapRoot,
130         const std::string& dma_buf_sysfs_path = "/sys/kernel/dmabuf/buffers");
131 
132 }  // namespace meminfo
133 }  // namespace android
134