From 16ba8bc7733e5cd9a6d88331499cd8f2d764e4cd Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Tue, 15 Feb 2022 23:39:33 +0000 Subject: [PATCH] statslog: Make this library compatible with the cgroups v2 hierarchy Call memcg_apps_dir() instead of hard-coding the top-level directory of the memcg controller. Bug: 213617178 Test: Verified lmkd operation inside Cuttlefish. Change-Id: I659610ebdabe3b69b0b1097aa5cf248d6f3273c5 Signed-off-by: Bart Van Assche --- Android.bp | 1 + statslog.cpp | 18 ++++++++++++------ statslog.h | 1 - 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Android.bp b/Android.bp index 5a079c9..0481c96 100644 --- a/Android.bp +++ b/Android.bp @@ -55,6 +55,7 @@ cc_library_static { defaults: ["stats_defaults"], shared_libs: [ "liblog", + "libprocessgroup", ], } diff --git a/statslog.cpp b/statslog.cpp index 6568f73..26a6d86 100644 --- a/statslog.cpp +++ b/statslog.cpp @@ -30,6 +30,10 @@ #include #include +#include + +#include + #ifdef LMKD_LOG_STATS #define STRINGIFY(x) STRINGIFY_INTERNAL(x) @@ -85,18 +89,20 @@ static void memory_stat_parse_line(char* line, struct memory_stat* mem_st) { mem_st->swap_in_bytes = value; } -static int memory_stat_from_cgroup(struct memory_stat* mem_st, int pid, uid_t uid) { - FILE *fp; - char buf[PATH_MAX]; +static int memory_stat_from_cgroup(struct memory_stat* mem_st, int pid, uid_t uid __unused) { + std::string path; + if (!CgroupGetAttributePathForTask("MemStats", pid, &path)) { + ALOGE("Querying MemStats path failed"); + return -1; + } - snprintf(buf, sizeof(buf), MEMCG_PROCESS_MEMORY_STAT_PATH, uid, pid); - - fp = fopen(buf, "r"); + FILE* fp = fopen(path.c_str(), "r"); if (fp == NULL) { return -1; } + char buf[PAGE_SIZE]; while (fgets(buf, PAGE_SIZE, fp) != NULL) { memory_stat_parse_line(buf, mem_st); } diff --git a/statslog.h b/statslog.h index 89e4d2e..e3f8b72 100644 --- a/statslog.h +++ b/statslog.h @@ -92,7 +92,6 @@ enum lmk_state { #ifdef LMKD_LOG_STATS -#define MEMCG_PROCESS_MEMORY_STAT_PATH "/dev/memcg/apps/uid_%u/pid_%d/memory.stat" #define PROC_STAT_FILE_PATH "/proc/%d/stat" #define PROC_STAT_BUFFER_SIZE 1024 #define BYTES_IN_KILOBYTE 1024