blob: 82605b681c8c4a7905c4a949719fcdf66e699155 [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 = {
Sandeep Patil2259fdf2018-11-09 16:42:45 -080040 SysMemInfo::kMemTotal, SysMemInfo::kMemFree, SysMemInfo::kMemBuffers,
41 SysMemInfo::kMemCached, SysMemInfo::kMemShmem, SysMemInfo::kMemSlab,
42 SysMemInfo::kMemSReclaim, SysMemInfo::kMemSUnreclaim, SysMemInfo::kMemSwapTotal,
43 SysMemInfo::kMemSwapFree, SysMemInfo::kMemZram, SysMemInfo::kMemMapped,
44 SysMemInfo::kMemVmallocUsed, SysMemInfo::kMemPageTables, SysMemInfo::kMemKernelStack,
Sandeep Patil54d87212018-08-29 17:10:47 -070045};
46
47bool SysMemInfo::ReadMemInfo(const std::string& path) {
48 return ReadMemInfo(SysMemInfo::kDefaultSysMemInfoTags, path);
49}
50
51// TODO: Delete this function if it can't match up with the c-like implementation below.
52// Currently, this added about 50 % extra overhead on hikey.
53#if 0
54bool SysMemInfo::ReadMemInfo(const std::vector<std::string>& tags, const std::string& path) {
55 std::string buffer;
56 if (!::android::base::ReadFileToString(path, &buffer)) {
57 PLOG(ERROR) << "Failed to read : " << path;
58 return false;
59 }
60
61 uint32_t total_found = 0;
62 for (auto s = buffer.begin(); s < buffer.end() && total_found < tags.size();) {
63 for (auto& tag : tags) {
64 if (tag == std::string(s, s + tag.size())) {
65 s += tag.size();
66 while (isspace(*s)) s++;
67 auto num_start = s;
68 while (std::isdigit(*s)) s++;
69
70 std::string number(num_start, num_start + (s - num_start));
71 if (!::android::base::ParseUint(number, &mem_in_kb_[tag])) {
72 LOG(ERROR) << "Failed to parse uint";
73 return false;
74 }
75 total_found++;
76 break;
77 }
78 }
79 while (s < buffer.end() && *s != '\n') s++;
80 if (s < buffer.end()) s++;
81 }
82
83 return true;
84}
85
86#else
87bool SysMemInfo::ReadMemInfo(const std::vector<std::string>& tags, const std::string& path) {
88 char buffer[4096];
89 int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
90 if (fd < 0) {
91 PLOG(ERROR) << "Failed to open file :" << path;
92 return false;
93 }
94
95 const int len = read(fd, buffer, sizeof(buffer) - 1);
96 close(fd);
97 if (len < 0) {
98 return false;
99 }
100
101 buffer[len] = '\0';
102 char* p = buffer;
103 uint32_t found = 0;
Sandeep Patil2259fdf2018-11-09 16:42:45 -0800104 uint32_t lineno = 0;
Sandeep Patil54d87212018-08-29 17:10:47 -0700105 while (*p && found < tags.size()) {
106 for (auto& tag : tags) {
107 if (strncmp(p, tag.c_str(), tag.size()) == 0) {
108 p += tag.size();
109 while (*p == ' ') p++;
110 char* endptr = nullptr;
111 mem_in_kb_[tag] = strtoull(p, &endptr, 10);
112 if (p == endptr) {
Sandeep Patil2259fdf2018-11-09 16:42:45 -0800113 PLOG(ERROR) << "Failed to parse line:" << lineno + 1 << " in file: " << path;
Sandeep Patil54d87212018-08-29 17:10:47 -0700114 return false;
115 }
116 p = endptr;
117 found++;
118 break;
119 }
120 }
121 while (*p && *p != '\n') {
122 p++;
123 }
124 if (*p) p++;
Sandeep Patil2259fdf2018-11-09 16:42:45 -0800125 lineno++;
Sandeep Patil54d87212018-08-29 17:10:47 -0700126 }
127
128 return true;
129}
130#endif
131
132} // namespace meminfo
133} // namespace android