• 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 #include <ctype.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <inttypes.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 #include <algorithm>
27 #include <cctype>
28 #include <cstdio>
29 #include <fstream>
30 #include <iterator>
31 #if defined(__ANDROID__) && !defined(__ANDROID_APEX__) && !defined(__ANDROID_VNDK__)
32 #include "bpf/BpfMap.h"
33 #endif
34 #include <sstream>
35 #include <string>
36 #include <unordered_set>
37 #include <utility>
38 #include <vector>
39 
40 #include <android-base/file.h>
41 #include <android-base/logging.h>
42 #include <android-base/parseint.h>
43 #include <android-base/stringprintf.h>
44 #include <android-base/strings.h>
45 #include <android-base/unique_fd.h>
46 #include <dmabufinfo/dmabuf_sysfs_stats.h>
47 
48 #include "meminfo_private.h"
49 
50 namespace android {
51 namespace meminfo {
52 
ReadMemInfo(const char * path)53 bool SysMemInfo::ReadMemInfo(const char* path) {
54     return ReadMemInfo(path, SysMemInfo::kDefaultSysMemInfoTags.size(),
55                        &*SysMemInfo::kDefaultSysMemInfoTags.begin(),
56                        [&](std::string_view tag, uint64_t val) {
57                            // Safe to store the string_view in the map
58                            // because the tags from
59                            // kDefaultSysMemInfoTags are all
60                            // statically-allocated.
61                            mem_in_kb_[tag] = val;
62                        });
63 }
64 
ReadMemInfo(std::vector<uint64_t> * out,const char * path)65 bool SysMemInfo::ReadMemInfo(std::vector<uint64_t>* out, const char* path) {
66     out->clear();
67     out->resize(SysMemInfo::kDefaultSysMemInfoTags.size());
68     return ReadMemInfo(SysMemInfo::kDefaultSysMemInfoTags.size(),
69                        &*SysMemInfo::kDefaultSysMemInfoTags.begin(), out->data(), path);
70 }
71 
ReadMemInfo(size_t ntags,const std::string_view * tags,uint64_t * out,const char * path)72 bool SysMemInfo::ReadMemInfo(size_t ntags, const std::string_view* tags, uint64_t* out,
73                              const char* path) {
74     return ReadMemInfo(path, ntags, tags, [&]([[maybe_unused]] std::string_view tag, uint64_t val) {
75         auto it = std::find(tags, tags + ntags, tag);
76         if (it == tags + ntags) {
77             LOG(ERROR) << "Tried to store invalid tag: " << tag;
78             return;
79         }
80         auto index = std::distance(tags, it);
81         // store the values in the same order as the tags
82         out[index] = val;
83     });
84 }
85 
ReadVmallocInfo()86 uint64_t SysMemInfo::ReadVmallocInfo() {
87     return ::android::meminfo::ReadVmallocInfo();
88 }
89 
ReadMemInfo(const char * path,size_t ntags,const std::string_view * tags,std::function<void (std::string_view,uint64_t)> store_val)90 bool SysMemInfo::ReadMemInfo(const char* path, size_t ntags, const std::string_view* tags,
91                              std::function<void(std::string_view, uint64_t)> store_val) {
92     char buffer[4096];
93     int fd = open(path, O_RDONLY | O_CLOEXEC);
94     if (fd < 0) {
95         PLOG(ERROR) << "Failed to open file :" << path;
96         return false;
97     }
98 
99     const int len = read(fd, buffer, sizeof(buffer) - 1);
100     close(fd);
101     if (len < 0) {
102         return false;
103     }
104 
105     buffer[len] = '\0';
106     char* p = buffer;
107     uint32_t found = 0;
108     uint32_t lineno = 0;
109     bool zram_tag_found = false;
110     while (*p && found < ntags) {
111         for (size_t tagno = 0; tagno < ntags; ++tagno) {
112             const std::string_view& tag = tags[tagno];
113             // Special case for "Zram:" tag that android_os_Debug and friends look
114             // up along with the rest of the numbers from /proc/meminfo
115             if (!zram_tag_found && tag == "Zram:") {
116                 store_val(tag, mem_zram_kb());
117                 zram_tag_found = true;
118                 found++;
119                 continue;
120             }
121 
122             if (strncmp(p, tag.data(), tag.size()) == 0) {
123                 p += tag.size();
124                 while (*p == ' ') p++;
125                 char* endptr = nullptr;
126                 uint64_t val = strtoull(p, &endptr, 10);
127                 if (p == endptr) {
128                     PLOG(ERROR) << "Failed to parse line:" << lineno + 1 << " in file: " << path;
129                     return false;
130                 }
131                 store_val(tag, val);
132                 p = endptr;
133                 found++;
134                 break;
135             }
136         }
137 
138         while (*p && *p != '\n') {
139             p++;
140         }
141         if (*p) p++;
142         lineno++;
143     }
144 
145     return true;
146 }
147 
mem_zram_kb(const char * zram_dev_cstr)148 uint64_t SysMemInfo::mem_zram_kb(const char* zram_dev_cstr) {
149     uint64_t mem_zram_total = 0;
150     if (zram_dev_cstr) {
151         if (!MemZramDevice(zram_dev_cstr, &mem_zram_total)) {
152             return 0;
153         }
154         return mem_zram_total / 1024;
155     }
156 
157     constexpr uint32_t kMaxZramDevices = 256;
158     for (uint32_t i = 0; i < kMaxZramDevices; i++) {
159         std::string zram_dev_abspath = ::android::base::StringPrintf("/sys/block/zram%u/", i);
160         if (access(zram_dev_abspath.c_str(), F_OK)) {
161             // We assume zram devices appear in range 0-255 and appear always in sequence
162             // under /sys/block. So, stop looking for them once we find one is missing.
163             break;
164         }
165 
166         uint64_t mem_zram_dev;
167         if (!MemZramDevice(zram_dev_abspath.c_str(), &mem_zram_dev)) {
168             return 0;
169         }
170 
171         mem_zram_total += mem_zram_dev;
172     }
173 
174     return mem_zram_total / 1024;
175 }
176 
MemZramDevice(const char * zram_dev,uint64_t * mem_zram_dev)177 bool SysMemInfo::MemZramDevice(const char* zram_dev, uint64_t* mem_zram_dev) {
178     std::string mmstat = ::android::base::StringPrintf("%s/%s", zram_dev, "mm_stat");
179     auto mmstat_fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(mmstat.c_str(), "re"), fclose};
180     if (mmstat_fp != nullptr) {
181         // only if we do have mmstat, use it. Otherwise, fall through to trying out the old
182         // 'mem_used_total'
183         if (fscanf(mmstat_fp.get(), "%*" SCNu64 " %*" SCNu64 " %" SCNu64, mem_zram_dev) != 1) {
184             PLOG(ERROR) << "Malformed mm_stat file in: " << zram_dev;
185             return false;
186         }
187         return true;
188     }
189 
190     std::string content;
191     if (::android::base::ReadFileToString(
192                 ::android::base::StringPrintf("%s/mem_used_total", zram_dev), &content)) {
193         *mem_zram_dev = strtoull(content.c_str(), NULL, 10);
194         if (*mem_zram_dev == ULLONG_MAX) {
195             PLOG(ERROR) << "Malformed mem_used_total file for zram dev: " << zram_dev
196                         << " content: " << content;
197             return false;
198         }
199 
200         return true;
201     }
202 
203     LOG(ERROR) << "Can't find memory status under: " << zram_dev;
204     return false;
205 }
206 
207 // Public methods
ReadVmallocInfo(const char * path)208 uint64_t ReadVmallocInfo(const char* path) {
209     uint64_t vmalloc_total = 0;
210     auto fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(path, "re"), fclose};
211     if (fp == nullptr) {
212         return vmalloc_total;
213     }
214 
215     char* line = nullptr;
216     size_t line_alloc = 0;
217     while (getline(&line, &line_alloc, fp.get()) > 0) {
218         // We are looking for lines like
219         //
220         // 0x0000000000000000-0x0000000000000000   12288 drm_property_create_blob+0x44/0xec pages=2 vmalloc
221         // 0x0000000000000000-0x0000000000000000    8192 wlan_logging_sock_init_svc+0xf8/0x4f0 [wlan] pages=1 vmalloc
222         //
223         // Notice that if the caller is coming from a module, the kernel prints and extra
224         // "[module_name]" after the address and the symbol of the call site. This means we can't
225         // use the old sscanf() method of getting the # of pages.
226         char* p_start = strstr(line, "pages=");
227         if (p_start == nullptr) {
228             // we didn't find anything
229             continue;
230         }
231 
232         uint64_t nr_pages;
233         if (sscanf(p_start, "pages=%" SCNu64 "", &nr_pages) == 1) {
234             vmalloc_total += (nr_pages * getpagesize());
235         }
236     }
237 
238     free(line);
239 
240     return vmalloc_total;
241 }
242 
ReadSysfsFile(const std::string & path,uint64_t * value)243 static bool ReadSysfsFile(const std::string& path, uint64_t* value) {
244     std::string content;
245     if (!::android::base::ReadFileToString(path, &content)) {
246         LOG(ERROR) << "Can't open file: " << path;
247         return false;
248     }
249 
250     *value = strtoull(content.c_str(), NULL, 10);
251     if (*value == ULLONG_MAX) {
252         PLOG(ERROR) << "Invalid file format: " << path;
253         return false;
254     }
255 
256     return true;
257 }
258 
ReadIonHeapsSizeKb(uint64_t * size,const std::string & path)259 bool ReadIonHeapsSizeKb(uint64_t* size, const std::string& path) {
260     return ReadSysfsFile(path, size);
261 }
262 
ReadIonPoolsSizeKb(uint64_t * size,const std::string & path)263 bool ReadIonPoolsSizeKb(uint64_t* size, const std::string& path) {
264     return ReadSysfsFile(path, size);
265 }
266 
ReadDmabufHeapPoolsSizeKb(uint64_t * size,const std::string & dma_heap_pool_size_path)267 bool ReadDmabufHeapPoolsSizeKb(uint64_t* size, const std::string& dma_heap_pool_size_path) {
268     static bool support_dmabuf_heap_pool_size = [dma_heap_pool_size_path]() -> bool {
269         bool ret = (access(dma_heap_pool_size_path.c_str(), R_OK) == 0);
270         if (!ret)
271             LOG(ERROR) << "Unable to read DMA-BUF heap total pool size, read ION total pool "
272                           "size instead.";
273         return ret;
274     }();
275 
276     if (!support_dmabuf_heap_pool_size) return ReadIonPoolsSizeKb(size);
277 
278     return ReadSysfsFile(dma_heap_pool_size_path, size);
279 }
280 
ReadDmabufHeapTotalExportedKb(uint64_t * size,const std::string & dma_heap_root_path,const std::string & dmabuf_sysfs_stats_path)281 bool ReadDmabufHeapTotalExportedKb(uint64_t* size, const std::string& dma_heap_root_path,
282                                    const std::string& dmabuf_sysfs_stats_path) {
283     static bool support_dmabuf_heaps = [dma_heap_root_path]() -> bool {
284         bool ret = (access(dma_heap_root_path.c_str(), R_OK) == 0);
285         if (!ret) LOG(ERROR) << "DMA-BUF heaps not supported, read ION heap total instead.";
286         return ret;
287     }();
288 
289     if (!support_dmabuf_heaps) return ReadIonHeapsSizeKb(size);
290 
291     std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(dma_heap_root_path.c_str()), closedir);
292 
293     if (!dir) {
294         return false;
295     }
296 
297     std::unordered_set<std::string> heap_list;
298     struct dirent* dent;
299     while ((dent = readdir(dir.get()))) {
300         if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) continue;
301 
302         heap_list.insert(dent->d_name);
303     }
304 
305     if (heap_list.empty()) return false;
306 
307     android::dmabufinfo::DmabufSysfsStats stats;
308     if (!android::dmabufinfo::GetDmabufSysfsStats(&stats, dmabuf_sysfs_stats_path)) return false;
309 
310     auto exporter_info = stats.exporter_info();
311 
312     *size = 0;
313     for (const auto& heap : heap_list) {
314         auto iter = exporter_info.find(heap);
315         if (iter != exporter_info.end()) *size += iter->second.size;
316     }
317 
318     *size = *size / 1024;
319 
320     return true;
321 }
322 
ReadGpuTotalUsageKb(uint64_t * size)323 bool ReadGpuTotalUsageKb(uint64_t* size) {
324 #if defined(__ANDROID__) && !defined(__ANDROID_APEX__) && !defined(__ANDROID_VNDK__)
325     static constexpr const char kBpfGpuMemTotalMap[] =
326         "/sys/fs/bpf/map_gpu_mem_gpu_mem_total_map";
327     static constexpr uint64_t kBpfKeyGpuTotalUsage = 0;
328 
329     // Use the read-only wrapper BpfMapRO to properly retrieve the read-only map.
330     auto map = bpf::BpfMapRO<uint64_t, uint64_t>(kBpfGpuMemTotalMap);
331     if (!map.isValid()) {
332         LOG(ERROR) << "Can't open file: " << kBpfGpuMemTotalMap;
333         return false;
334     }
335 
336     auto res = map.readValue(kBpfKeyGpuTotalUsage);
337     if (!res.ok()) {
338         LOG(ERROR) << "Invalid file format: " << kBpfGpuMemTotalMap;
339         return false;
340     }
341 
342     if (size) {
343         *size = res.value() / 1024;
344     }
345     return true;
346 #else
347     if (size) {
348         *size = 0;
349     }
350     return false;
351 #endif
352 }
353 
354 }  // namespace meminfo
355 }  // namespace android
356