Merge "libmeminfo: Add ReadSysMemInfo variants"

This commit is contained in:
Treehugger Robot 2018-12-12 01:25:51 +00:00 committed by Gerrit Code Review
commit 556fc786c6
5 changed files with 314 additions and 37 deletions

View file

@ -18,6 +18,7 @@
#include <sys/types.h>
#include <functional>
#include <map>
#include <string>
#include <vector>
@ -51,6 +52,9 @@ class SysMemInfo final {
bool ReadMemInfo(const std::string& path = "/proc/meminfo");
bool ReadMemInfo(const std::vector<std::string>& tags,
const std::string& path = "/proc/meminfo");
bool ReadMemInfo(const std::vector<std::string>& tags, std::vector<uint64_t>* out,
const std::string& path = "/proc/meminfo");
bool ReadMemInfo(std::vector<uint64_t>* out, const std::string& path = "/proc/meminfo");
// getters
uint64_t mem_total_kb() { return mem_in_kb_[kMemTotal]; }
@ -59,19 +63,21 @@ class SysMemInfo final {
uint64_t mem_cached_kb() { return mem_in_kb_[kMemCached]; }
uint64_t mem_shmem_kb() { return mem_in_kb_[kMemShmem]; }
uint64_t mem_slab_kb() { return mem_in_kb_[kMemSlab]; }
uint64_t mem_slab_reclailmable_kb() { return mem_in_kb_[kMemSReclaim]; }
uint64_t mem_slab_reclaimable_kb() { return mem_in_kb_[kMemSReclaim]; }
uint64_t mem_slab_unreclaimable_kb() { return mem_in_kb_[kMemSUnreclaim]; }
uint64_t mem_swap_kb() { return mem_in_kb_[kMemSwapTotal]; }
uint64_t mem_swap_free_kb() { return mem_in_kb_[kMemSwapFree]; }
uint64_t mem_mapped_kb() { return mem_in_kb_[kMemMapped]; }
uint64_t mem_vmalloc_used_kb() { return mem_in_kb_[kMemVmallocUsed]; }
uint64_t mem_page_tables_kb() { return mem_in_kb_[kMemPageTables]; }
uint64_t mem_kernel_stack_kb() { return mem_in_kb_[kMemPageTables]; }
uint64_t mem_kernel_stack_kb() { return mem_in_kb_[kMemKernelStack]; }
uint64_t mem_zram_kb(const std::string& zram_dev = "");
private:
std::map<std::string, uint64_t> mem_in_kb_;
bool MemZramDevice(const std::string& zram_dev, uint64_t* mem_zram_dev);
bool ReadMemInfo(const std::vector<std::string>& tags, const std::string& path,
std::function<void(const std::string&, uint64_t)> store_val);
};
} // namespace meminfo

View file

@ -29,6 +29,8 @@
#include <benchmark/benchmark.h>
using ::android::meminfo::SysMemInfo;
enum {
MEMINFO_TOTAL,
MEMINFO_FREE,
@ -71,8 +73,7 @@ static void get_mem_info(uint64_t mem[], const char* file) {
static const char* const tags[] = {
"MemTotal:", "MemFree:", "Buffers:", "Cached:", "Shmem:", "Slab:",
"SReclaimable:", "SUnreclaim:", "SwapTotal:", "SwapFree:", "ZRam:", "Mapped:",
"VmallocUsed:", "PageTables:", "KernelStack:", NULL
};
"VmallocUsed:", "PageTables:", "KernelStack:", NULL};
static const int tagsLen[] = {9, 8, 8, 7, 6, 5, 13, 11, 10, 9, 5, 7, 12, 11, 12, 0};
@ -105,7 +106,7 @@ static void get_mem_info(uint64_t mem[], const char* file) {
}
}
static void BM_ParseSysMemInfo(benchmark::State& state) {
static void BM_ReadMemInfo_old(benchmark::State& state) {
std::string meminfo = R"meminfo(MemTotal: 3019740 kB
MemFree: 1809728 kB
MemAvailable: 2546560 kB
@ -159,9 +160,9 @@ Hugepagesize: 2048 kB)meminfo";
get_mem_info(mem, tf.path);
}
}
BENCHMARK(BM_ParseSysMemInfo);
BENCHMARK(BM_ReadMemInfo_old);
static void BM_ReadMemInfo(benchmark::State& state) {
static void BM_ReadMemInfo_new(benchmark::State& state) {
std::string meminfo = R"meminfo(MemTotal: 3019740 kB
MemFree: 1809728 kB
MemAvailable: 2546560 kB
@ -211,12 +212,21 @@ Hugepagesize: 2048 kB)meminfo";
android::base::WriteStringToFd(meminfo, tf.fd);
std::string file = std::string(tf.path);
::android::meminfo::SysMemInfo mi;
std::vector<uint64_t> mem(MEMINFO_COUNT);
const std::vector<std::string> tags = {
SysMemInfo::kMemTotal, SysMemInfo::kMemFree, SysMemInfo::kMemBuffers,
SysMemInfo::kMemCached, SysMemInfo::kMemShmem, SysMemInfo::kMemSlab,
SysMemInfo::kMemSReclaim, SysMemInfo::kMemSUnreclaim, SysMemInfo::kMemSwapTotal,
SysMemInfo::kMemSwapFree, SysMemInfo::kMemMapped, SysMemInfo::kMemVmallocUsed,
SysMemInfo::kMemPageTables, SysMemInfo::kMemKernelStack,
};
SysMemInfo smi;
for (auto _ : state) {
mi.ReadMemInfo(file);
smi.ReadMemInfo(tags, &mem, file);
}
}
BENCHMARK(BM_ReadMemInfo);
BENCHMARK(BM_ReadMemInfo_new);
static uint64_t get_zram_mem_used(const std::string& zram_dir) {
FILE* f = fopen((zram_dir + "mm_stat").c_str(), "r");
@ -246,23 +256,145 @@ static uint64_t get_zram_mem_used(const std::string& zram_dir) {
return 0;
}
static void BM_OldReadZramTotal(benchmark::State& state) {
static void BM_ZramTotal_old(benchmark::State& state) {
std::string exec_dir = ::android::base::GetExecutableDirectory();
std::string zram_mmstat_dir = exec_dir + "/testdata1/";
for (auto _ : state) {
uint64_t zram_total __attribute__((unused)) = get_zram_mem_used(zram_mmstat_dir) / 1024;
}
}
BENCHMARK(BM_OldReadZramTotal);
BENCHMARK(BM_ZramTotal_old);
static void BM_NewReadZramTotal(benchmark::State& state) {
static void BM_ZramTotal_new(benchmark::State& state) {
std::string exec_dir = ::android::base::GetExecutableDirectory();
std::string zram_mmstat_dir = exec_dir + "/testdata1/";
::android::meminfo::SysMemInfo mi;
SysMemInfo smi;
for (auto _ : state) {
uint64_t zram_total __attribute__((unused)) = mi.mem_zram_kb(zram_mmstat_dir);
uint64_t zram_total __attribute__((unused)) = smi.mem_zram_kb(zram_mmstat_dir);
}
}
BENCHMARK(BM_NewReadZramTotal);
BENCHMARK(BM_ZramTotal_new);
static void BM_MemInfoWithZram_old(benchmark::State& state) {
std::string meminfo = R"meminfo(MemTotal: 3019740 kB
MemFree: 1809728 kB
MemAvailable: 2546560 kB
Buffers: 54736 kB
Cached: 776052 kB
SwapCached: 0 kB
Active: 445856 kB
Inactive: 459092 kB
Active(anon): 78492 kB
Inactive(anon): 2240 kB
Active(file): 367364 kB
Inactive(file): 456852 kB
Unevictable: 3096 kB
Mlocked: 3096 kB
SwapTotal: 0 kB
SwapFree: 0 kB
Dirty: 32 kB
Writeback: 0 kB
AnonPages: 74988 kB
Mapped: 62624 kB
Shmem: 4020 kB
Slab: 86464 kB
SReclaimable: 44432 kB
SUnreclaim: 42032 kB
KernelStack: 4880 kB
PageTables: 2900 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 1509868 kB
Committed_AS: 80296 kB
VmallocTotal: 263061440 kB
VmallocUsed: 0 kB
VmallocChunk: 0 kB
AnonHugePages: 6144 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
CmaTotal: 131072 kB
CmaFree: 130380 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB)meminfo";
TemporaryFile tf;
::android::base::WriteStringToFd(meminfo, tf.fd);
std::string exec_dir = ::android::base::GetExecutableDirectory();
std::string zram_mmstat_dir = exec_dir + "/testdata1/";
uint64_t mem[MEMINFO_COUNT];
for (auto _ : state) {
get_mem_info(mem, tf.path);
mem[MEMINFO_ZRAM_TOTAL] = get_zram_mem_used("/sys/block/zram0/") / 1024;
CHECK_EQ(mem[MEMINFO_KERNEL_STACK], 4880u);
}
}
BENCHMARK(BM_MemInfoWithZram_old);
static void BM_MemInfoWithZram_new(benchmark::State& state) {
std::string meminfo = R"meminfo(MemTotal: 3019740 kB
MemFree: 1809728 kB
MemAvailable: 2546560 kB
Buffers: 54736 kB
Cached: 776052 kB
SwapCached: 0 kB
Active: 445856 kB
Inactive: 459092 kB
Active(anon): 78492 kB
Inactive(anon): 2240 kB
Active(file): 367364 kB
Inactive(file): 456852 kB
Unevictable: 3096 kB
Mlocked: 3096 kB
SwapTotal: 0 kB
SwapFree: 0 kB
Dirty: 32 kB
Writeback: 0 kB
AnonPages: 74988 kB
Mapped: 62624 kB
Shmem: 4020 kB
Slab: 86464 kB
SReclaimable: 44432 kB
SUnreclaim: 42032 kB
KernelStack: 4880 kB
PageTables: 2900 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 1509868 kB
Committed_AS: 80296 kB
VmallocTotal: 263061440 kB
VmallocUsed: 0 kB
VmallocChunk: 0 kB
AnonHugePages: 6144 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
CmaTotal: 131072 kB
CmaFree: 130380 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB)meminfo";
TemporaryFile tf;
android::base::WriteStringToFd(meminfo, tf.fd);
std::string file = std::string(tf.path);
std::vector<uint64_t> mem(MEMINFO_COUNT);
std::vector<std::string> tags(SysMemInfo::kDefaultSysMemInfoTags);
auto it = tags.begin();
tags.insert(it + MEMINFO_ZRAM_TOTAL, "Zram:");
SysMemInfo smi;
for (auto _ : state) {
smi.ReadMemInfo(tags, &mem, file);
CHECK_EQ(mem[MEMINFO_KERNEL_STACK], 4880u);
}
}
BENCHMARK(BM_MemInfoWithZram_new);
BENCHMARK_MAIN();

View file

@ -347,8 +347,8 @@ Active(file): 367364 kB
Inactive(file): 456852 kB
Unevictable: 3096 kB
Mlocked: 3096 kB
SwapTotal: 0 kB
SwapFree: 0 kB
SwapTotal: 32768 kB
SwapFree: 4096 kB
Dirty: 32 kB
Writeback: 0 kB
AnonPages: 74988 kB
@ -365,7 +365,7 @@ WritebackTmp: 0 kB
CommitLimit: 1509868 kB
Committed_AS: 80296 kB
VmallocTotal: 263061440 kB
VmallocUsed: 0 kB
VmallocUsed: 65536 kB
VmallocChunk: 0 kB
AnonHugePages: 6144 kB
ShmemHugePages: 0 kB
@ -385,7 +385,19 @@ Hugepagesize: 2048 kB)meminfo";
SysMemInfo mi;
ASSERT_TRUE(mi.ReadMemInfo(tf.path));
EXPECT_EQ(mi.mem_total_kb(), 3019740);
EXPECT_EQ(mi.mem_free_kb(), 1809728);
EXPECT_EQ(mi.mem_buffers_kb(), 54736);
EXPECT_EQ(mi.mem_cached_kb(), 776052);
EXPECT_EQ(mi.mem_shmem_kb(), 4020);
EXPECT_EQ(mi.mem_slab_kb(), 86464);
EXPECT_EQ(mi.mem_slab_reclaimable_kb(), 44432);
EXPECT_EQ(mi.mem_slab_unreclaimable_kb(), 42032);
EXPECT_EQ(mi.mem_swap_kb(), 32768);
EXPECT_EQ(mi.mem_swap_free_kb(), 4096);
EXPECT_EQ(mi.mem_mapped_kb(), 62624);
EXPECT_EQ(mi.mem_vmalloc_used_kb(), 65536);
EXPECT_EQ(mi.mem_page_tables_kb(), 2900);
EXPECT_EQ(mi.mem_kernel_stack_kb(), 4880);
}
TEST(SysMemInfoParser, TestEmptyFile) {
@ -399,7 +411,7 @@ TEST(SysMemInfoParser, TestEmptyFile) {
EXPECT_EQ(mi.mem_total_kb(), 0);
}
TEST(SysMemInfoParse, TestZramTotal) {
TEST(SysMemInfoParser, TestZramTotal) {
std::string exec_dir = ::android::base::GetExecutableDirectory();
SysMemInfo mi;
@ -410,6 +422,100 @@ TEST(SysMemInfoParse, TestZramTotal) {
EXPECT_EQ(mi.mem_zram_kb(zram_memused_dir), 30504);
}
enum {
MEMINFO_TOTAL,
MEMINFO_FREE,
MEMINFO_BUFFERS,
MEMINFO_CACHED,
MEMINFO_SHMEM,
MEMINFO_SLAB,
MEMINFO_SLAB_RECLAIMABLE,
MEMINFO_SLAB_UNRECLAIMABLE,
MEMINFO_SWAP_TOTAL,
MEMINFO_SWAP_FREE,
MEMINFO_ZRAM_TOTAL,
MEMINFO_MAPPED,
MEMINFO_VMALLOC_USED,
MEMINFO_PAGE_TABLES,
MEMINFO_KERNEL_STACK,
MEMINFO_COUNT
};
TEST(SysMemInfoParser, TestZramWithTags) {
std::string meminfo = R"meminfo(MemTotal: 3019740 kB
MemFree: 1809728 kB
MemAvailable: 2546560 kB
Buffers: 54736 kB
Cached: 776052 kB
SwapCached: 0 kB
Active: 445856 kB
Inactive: 459092 kB
Active(anon): 78492 kB
Inactive(anon): 2240 kB
Active(file): 367364 kB
Inactive(file): 456852 kB
Unevictable: 3096 kB
Mlocked: 3096 kB
SwapTotal: 32768 kB
SwapFree: 4096 kB
Dirty: 32 kB
Writeback: 0 kB
AnonPages: 74988 kB
Mapped: 62624 kB
Shmem: 4020 kB
Slab: 86464 kB
SReclaimable: 44432 kB
SUnreclaim: 42032 kB
KernelStack: 4880 kB
PageTables: 2900 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 1509868 kB
Committed_AS: 80296 kB
VmallocTotal: 263061440 kB
VmallocUsed: 65536 kB
VmallocChunk: 0 kB
AnonHugePages: 6144 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
CmaTotal: 131072 kB
CmaFree: 130380 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB)meminfo";
TemporaryFile tf;
ASSERT_TRUE(tf.fd != -1);
ASSERT_TRUE(::android::base::WriteStringToFd(meminfo, tf.fd));
std::string file = std::string(tf.path);
std::vector<uint64_t> mem(MEMINFO_COUNT);
std::vector<std::string> tags(SysMemInfo::kDefaultSysMemInfoTags);
auto it = tags.begin();
tags.insert(it + MEMINFO_ZRAM_TOTAL, "Zram:");
SysMemInfo mi;
// Read system memory info
EXPECT_TRUE(mi.ReadMemInfo(tags, &mem, file));
EXPECT_EQ(mem[MEMINFO_TOTAL], 3019740);
EXPECT_EQ(mem[MEMINFO_FREE], 1809728);
EXPECT_EQ(mem[MEMINFO_BUFFERS], 54736);
EXPECT_EQ(mem[MEMINFO_CACHED], 776052);
EXPECT_EQ(mem[MEMINFO_SHMEM], 4020);
EXPECT_EQ(mem[MEMINFO_SLAB], 86464);
EXPECT_EQ(mem[MEMINFO_SLAB_RECLAIMABLE], 44432);
EXPECT_EQ(mem[MEMINFO_SLAB_UNRECLAIMABLE], 42032);
EXPECT_EQ(mem[MEMINFO_SWAP_TOTAL], 32768);
EXPECT_EQ(mem[MEMINFO_SWAP_FREE], 4096);
EXPECT_EQ(mem[MEMINFO_MAPPED], 62624);
EXPECT_EQ(mem[MEMINFO_VMALLOC_USED], 65536);
EXPECT_EQ(mem[MEMINFO_PAGE_TABLES], 2900);
EXPECT_EQ(mem[MEMINFO_KERNEL_STACK], 4880);
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
if (argc <= 1) {

View file

@ -18,12 +18,15 @@
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <fstream>
#include <iterator>
#include <string>
#include <utility>
#include <vector>
@ -49,7 +52,29 @@ const std::vector<std::string> SysMemInfo::kDefaultSysMemInfoTags = {
};
bool SysMemInfo::ReadMemInfo(const std::string& path) {
return ReadMemInfo(SysMemInfo::kDefaultSysMemInfoTags, path);
return ReadMemInfo(SysMemInfo::kDefaultSysMemInfoTags, path,
[&](const std::string& tag, uint64_t val) { mem_in_kb_[tag] = val; });
}
bool SysMemInfo::ReadMemInfo(std::vector<uint64_t>* out, const std::string& path) {
return ReadMemInfo(SysMemInfo::kDefaultSysMemInfoTags, out, path);
}
bool SysMemInfo::ReadMemInfo(const std::vector<std::string>& tags, std::vector<uint64_t>* out,
const std::string& path) {
out->clear();
out->resize(tags.size());
return ReadMemInfo(tags, path, [&]([[maybe_unused]] const std::string& tag, uint64_t val) {
auto it = std::find(tags.begin(), tags.end(), tag);
if (it == tags.end()) {
LOG(ERROR) << "Tried to store invalid tag: " << tag;
return;
}
auto index = std::distance(tags.begin(), it);
// store the values in the same order as the tags
out->at(index) = val;
});
}
// TODO: Delete this function if it can't match up with the c-like implementation below.
@ -88,7 +113,8 @@ bool SysMemInfo::ReadMemInfo(const std::vector<std::string>& tags, const std::st
}
#else
bool SysMemInfo::ReadMemInfo(const std::vector<std::string>& tags, const std::string& path) {
bool SysMemInfo::ReadMemInfo(const std::vector<std::string>& tags, const std::string& path,
std::function<void(const std::string&, uint64_t)> store_val) {
char buffer[4096];
int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
if (fd < 0) {
@ -106,22 +132,34 @@ bool SysMemInfo::ReadMemInfo(const std::vector<std::string>& tags, const std::st
char* p = buffer;
uint32_t found = 0;
uint32_t lineno = 0;
bool zram_tag_found = false;
while (*p && found < tags.size()) {
for (auto& tag : tags) {
// Special case for "Zram:" tag that android_os_Debug and friends look
// up along with the rest of the numbers from /proc/meminfo
if (!zram_tag_found && tag == "Zram:") {
store_val(tag, mem_zram_kb());
zram_tag_found = true;
found++;
continue;
}
if (strncmp(p, tag.c_str(), tag.size()) == 0) {
p += tag.size();
while (*p == ' ') p++;
char* endptr = nullptr;
mem_in_kb_[tag] = strtoull(p, &endptr, 10);
uint64_t val = strtoull(p, &endptr, 10);
if (p == endptr) {
PLOG(ERROR) << "Failed to parse line:" << lineno + 1 << " in file: " << path;
return false;
}
store_val(tag, val);
p = endptr;
found++;
break;
}
}
while (*p && *p != '\n') {
p++;
}
@ -163,24 +201,19 @@ uint64_t SysMemInfo::mem_zram_kb(const std::string& zram_dev) {
}
bool SysMemInfo::MemZramDevice(const std::string& zram_dev, uint64_t* mem_zram_dev) {
std::string content;
if (android::base::ReadFileToString(zram_dev + "mm_stat", &content)) {
std::vector<std::string> values = ::android::base::Split(content, " ");
if (values.size() < 3) {
LOG(ERROR) << "Malformed mm_stat file for zram dev: " << zram_dev
<< " content: " << content;
std::string mmstat = ::android::base::StringPrintf("%s/%s", zram_dev.c_str(), "mm_stat");
auto mmstat_fp = std::unique_ptr<FILE, decltype(&fclose)>{fopen(mmstat.c_str(), "re"), fclose};
if (mmstat_fp != nullptr) {
// only if we do have mmstat, use it. Otherwise, fall through to trying out the old
// 'mem_used_total'
if (fscanf(mmstat_fp.get(), "%*" SCNu64 " %*" SCNu64 " %" SCNu64, mem_zram_dev) != 1) {
PLOG(ERROR) << "Malformed mm_stat file in: " << zram_dev;
return false;
}
if (!::android::base::ParseUint(values[2], mem_zram_dev)) {
LOG(ERROR) << "Malformed mm_stat file for zram dev: " << zram_dev
<< " value: " << values[2];
return false;
}
return true;
}
std::string content;
if (::android::base::ReadFileToString(zram_dev + "mem_used_total", &content)) {
*mem_zram_dev = strtoull(content.c_str(), NULL, 10);
if (*mem_zram_dev == ULLONG_MAX) {

View file

@ -1 +1 @@
145674240 26801454 31236096 0 45772800 3042 1887 517
145674240 26801454 31236096 0 45772800 3042 1887 517