• 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 #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     static constexpr const char kMemAvailable[] = "MemAvailable:";
56     static constexpr const char kMemActiveAnon[] = "Active(anon):";
57     static constexpr const char kMemInactiveAnon[] = "Inactive(anon):";
58     static constexpr const char kMemActiveFile[] = "Active(file):";
59     static constexpr const char kMemInactiveFile[] = "Inactive(file):";
60     static constexpr const char kMemCmaTotal[] = "CmaTotal:";
61     static constexpr const char kMemCmaFree[] = "CmaFree:";
62 
63     static constexpr std::initializer_list<std::string_view> kDefaultSysMemInfoTags = {
64             SysMemInfo::kMemTotal,      SysMemInfo::kMemFree,         SysMemInfo::kMemBuffers,
65             SysMemInfo::kMemCached,     SysMemInfo::kMemShmem,        SysMemInfo::kMemSlab,
66             SysMemInfo::kMemSReclaim,   SysMemInfo::kMemSUnreclaim,   SysMemInfo::kMemSwapTotal,
67             SysMemInfo::kMemSwapFree,   SysMemInfo::kMemMapped,       SysMemInfo::kMemVmallocUsed,
68             SysMemInfo::kMemPageTables, SysMemInfo::kMemKernelStack,  SysMemInfo::kMemKReclaimable,
69             SysMemInfo::kMemActive,     SysMemInfo::kMemInactive,     SysMemInfo::kMemUnevictable,
70             SysMemInfo::kMemAvailable,  SysMemInfo::kMemActiveAnon,   SysMemInfo::kMemInactiveAnon,
71             SysMemInfo::kMemActiveFile, SysMemInfo::kMemInactiveFile, SysMemInfo::kMemCmaTotal,
72             SysMemInfo::kMemCmaFree,
73     };
74 
75     SysMemInfo() = default;
76 
77     // Parse /proc/meminfo and read values that are needed
78     bool ReadMemInfo(const char* path = "/proc/meminfo");
79     bool ReadMemInfo(size_t ntags, const std::string_view* tags, uint64_t* out,
80                      const char* path = "/proc/meminfo");
81     bool ReadMemInfo(std::vector<uint64_t>* out, const char* path = "/proc/meminfo");
82 
83     // Parse /proc/vmallocinfo and return total physical memory mapped
84     // in vmalloc area by the kernel.
85     // Note that this deliberately ignores binder buffers. They are _always_
86     // mapped in a process and are counted for in each process.
87     uint64_t ReadVmallocInfo();
88 
89     // getters
mem_total_kb()90     uint64_t mem_total_kb() const { return find_mem_by_tag(kMemTotal); }
mem_free_kb()91     uint64_t mem_free_kb() const { return find_mem_by_tag(kMemFree); }
mem_buffers_kb()92     uint64_t mem_buffers_kb() const { return find_mem_by_tag(kMemBuffers); }
mem_cached_kb()93     uint64_t mem_cached_kb() const { return find_mem_by_tag(kMemCached); }
mem_shmem_kb()94     uint64_t mem_shmem_kb() const { return find_mem_by_tag(kMemShmem); }
mem_slab_kb()95     uint64_t mem_slab_kb() const { return find_mem_by_tag(kMemSlab); }
mem_slab_reclaimable_kb()96     uint64_t mem_slab_reclaimable_kb() const { return find_mem_by_tag(kMemSReclaim); }
mem_slab_unreclaimable_kb()97     uint64_t mem_slab_unreclaimable_kb() const { return find_mem_by_tag(kMemSUnreclaim); }
mem_swap_kb()98     uint64_t mem_swap_kb() const { return find_mem_by_tag(kMemSwapTotal); }
mem_swap_free_kb()99     uint64_t mem_swap_free_kb() const { return find_mem_by_tag(kMemSwapFree); }
mem_mapped_kb()100     uint64_t mem_mapped_kb() const { return find_mem_by_tag(kMemMapped); }
mem_vmalloc_used_kb()101     uint64_t mem_vmalloc_used_kb() const { return find_mem_by_tag(kMemVmallocUsed); }
mem_page_tables_kb()102     uint64_t mem_page_tables_kb() const { return find_mem_by_tag(kMemPageTables); }
mem_kernel_stack_kb()103     uint64_t mem_kernel_stack_kb() const { return find_mem_by_tag(kMemKernelStack); }
mem_kreclaimable_kb()104     uint64_t mem_kreclaimable_kb() const { return find_mem_by_tag(kMemKReclaimable); }
mem_active_kb()105     uint64_t mem_active_kb() const { return find_mem_by_tag(kMemActive); }
mem_inactive_kb()106     uint64_t mem_inactive_kb() const { return find_mem_by_tag(kMemInactive); }
mem_unevictable_kb()107     uint64_t mem_unevictable_kb() const { return find_mem_by_tag(kMemUnevictable); }
mem_available_kb()108     uint64_t mem_available_kb() const { return find_mem_by_tag(kMemAvailable); }
mem_active_anon_kb()109     uint64_t mem_active_anon_kb() const { return find_mem_by_tag(kMemActiveAnon); }
mem_inactive_anon_kb()110     uint64_t mem_inactive_anon_kb() const { return find_mem_by_tag(kMemInactiveAnon); }
mem_active_file_kb()111     uint64_t mem_active_file_kb() const { return find_mem_by_tag(kMemActiveFile); }
mem_inactive_file_kb()112     uint64_t mem_inactive_file_kb() const { return find_mem_by_tag(kMemInactiveFile); }
mem_cma_total_kb()113     uint64_t mem_cma_total_kb() const { return find_mem_by_tag(kMemCmaTotal); }
mem_cma_free_kb()114     uint64_t mem_cma_free_kb() const { return find_mem_by_tag(kMemCmaFree); }
115     uint64_t mem_zram_kb(const char* zram_dev = nullptr) const;
116     uint64_t mem_compacted_kb(const char* zram_dev = nullptr);
117 
118   private:
119     std::map<std::string_view, uint64_t> mem_in_kb_;
120     bool MemZramDevice(const char* zram_dev, uint64_t* mem_zram_dev) const;
121     bool GetTotalMemCompacted(const char* zram_dev, uint64_t* out_mem_compacted);
122     bool ReadMemInfo(const char* path, size_t ntags, const std::string_view* tags,
123                      std::function<void(std::string_view, uint64_t)> store_val);
124     // Convenience function to avoid duplicating code for each memory category.
find_mem_by_tag(const char kTag[])125     uint64_t find_mem_by_tag(const char kTag[]) const {
126         auto it = mem_in_kb_.find(kTag);
127         if (it != mem_in_kb_.end()) {
128             return it->second;
129         }
130         return 0;
131     }
132 };
133 
134 // Parse /proc/vmallocinfo and return total physical memory mapped
135 // in vmalloc area by the kernel. Note that this deliberately ignores binder buffers. They are
136 // _always_ mapped in a process and are counted for in each process.
137 uint64_t ReadVmallocInfo(const char* path = "/proc/vmallocinfo");
138 
139 // Read ION heaps allocation size in kb
140 bool ReadIonHeapsSizeKb(
141     uint64_t* size, const std::string& path = "/sys/kernel/ion/total_heaps_kb");
142 
143 // Read ION pools allocation size in kb
144 bool ReadIonPoolsSizeKb(
145     uint64_t* size, const std::string& path = "/sys/kernel/ion/total_pools_kb");
146 
147 // Read DMA-BUF heap pools allocation size in kb
148 bool ReadDmabufHeapPoolsSizeKb(uint64_t* size,
149                             const std::string& path = "/sys/kernel/dma_heap/total_pools_kb");
150 
151 // Read per-process GPU memory usage. Returns a map of pid -> GPU Mem in kilobytes.
152 bool ReadPerProcessGpuMem(std::unordered_map<uint32_t, uint64_t>* out);
153 
154 // Read GPU usage of the specified process in kb.
155 bool ReadProcessGpuUsageKb(uint32_t pid, uint32_t gpu_id, uint64_t* size);
156 
157 // Read GPU total usage size in kb
158 bool ReadGpuTotalUsageKb(uint64_t* size);
159 
160 // Read total size of DMA-BUFs exported from the DMA-BUF heap framework in kb
161 bool ReadDmabufHeapTotalExportedKb(
162         uint64_t* size, const std::string& dma_heap_root = kDmabufHeapRoot,
163         const std::string& dma_buf_sysfs_path = "/sys/kernel/dmabuf/buffers");
164 
165 }  // namespace meminfo
166 }  // namespace android
167