blob: 50fa2136d39b9048a099cf3d24f000a57c7fc708 [file] [log] [blame]
Sandeep Patil54d87212018-08-29 17:10:47 -07001/*
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 <stdlib.h>
21#include <unistd.h>
22
23#include <cctype>
24#include <fstream>
25#include <string>
26#include <utility>
27#include <vector>
28
29#include <android-base/file.h>
30#include <android-base/logging.h>
31#include <android-base/parseint.h>
32#include <android-base/strings.h>
33
34#include "meminfo_private.h"
35
36namespace android {
37namespace meminfo {
38
39const std::vector<std::string> SysMemInfo::kDefaultSysMemInfoTags = {
40 "MemTotal:", "MemFree:", "Buffers:", "Cached:", "Shmem:",
41 "Slab:", "SReclaimable:", "SUnreclaim:", "SwapTotal:", "SwapFree:",
42 "ZRam:", "Mapped:", "VmallocUsed:", "PageTables:", "KernelStack:",
43};
44
45bool SysMemInfo::ReadMemInfo(const std::string& path) {
46 return ReadMemInfo(SysMemInfo::kDefaultSysMemInfoTags, path);
47}
48
49// TODO: Delete this function if it can't match up with the c-like implementation below.
50// Currently, this added about 50 % extra overhead on hikey.
51#if 0
52bool SysMemInfo::ReadMemInfo(const std::vector<std::string>& tags, const std::string& path) {
53 std::string buffer;
54 if (!::android::base::ReadFileToString(path, &buffer)) {
55 PLOG(ERROR) << "Failed to read : " << path;
56 return false;
57 }
58
59 uint32_t total_found = 0;
60 for (auto s = buffer.begin(); s < buffer.end() && total_found < tags.size();) {
61 for (auto& tag : tags) {
62 if (tag == std::string(s, s + tag.size())) {
63 s += tag.size();
64 while (isspace(*s)) s++;
65 auto num_start = s;
66 while (std::isdigit(*s)) s++;
67
68 std::string number(num_start, num_start + (s - num_start));
69 if (!::android::base::ParseUint(number, &mem_in_kb_[tag])) {
70 LOG(ERROR) << "Failed to parse uint";
71 return false;
72 }
73 total_found++;
74 break;
75 }
76 }
77 while (s < buffer.end() && *s != '\n') s++;
78 if (s < buffer.end()) s++;
79 }
80
81 return true;
82}
83
84#else
85bool SysMemInfo::ReadMemInfo(const std::vector<std::string>& tags, const std::string& path) {
86 char buffer[4096];
87 int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
88 if (fd < 0) {
89 PLOG(ERROR) << "Failed to open file :" << path;
90 return false;
91 }
92
93 const int len = read(fd, buffer, sizeof(buffer) - 1);
94 close(fd);
95 if (len < 0) {
96 return false;
97 }
98
99 buffer[len] = '\0';
100 char* p = buffer;
101 uint32_t found = 0;
102 while (*p && found < tags.size()) {
103 for (auto& tag : tags) {
104 if (strncmp(p, tag.c_str(), tag.size()) == 0) {
105 p += tag.size();
106 while (*p == ' ') p++;
107 char* endptr = nullptr;
108 mem_in_kb_[tag] = strtoull(p, &endptr, 10);
109 if (p == endptr) {
110 PLOG(ERROR) << "Failed to parse line in file: " << path;
111 return false;
112 }
113 p = endptr;
114 found++;
115 break;
116 }
117 }
118 while (*p && *p != '\n') {
119 p++;
120 }
121 if (*p) p++;
122 }
123
124 return true;
125}
126#endif
127
128} // namespace meminfo
129} // namespace android