lmkd: Look up cgroup attribute paths instead of hardcoding these

Retrieve cgroup attribute paths from task_profiles.json instead of
hardcoding these paths.

Bug: 213617178
Test: Tested lmkd in Cuttlefish.
Change-Id: I03f40ac8ccd4635f21432214e1acf997c505d1e9
Signed-off-by: Bart Van Assche <bvanassche@google.com>
diff --git a/lmkd.cpp b/lmkd.cpp
index 7f0ca58..6133938 100644
--- a/lmkd.cpp
+++ b/lmkd.cpp
@@ -46,6 +46,7 @@
 #include <log/log_event_list.h>
 #include <log/log_time.h>
 #include <private/android_filesystem_config.h>
+#include <processgroup/processgroup.h>
 #include <psi/psi.h>
 
 #include "reaper.h"
@@ -85,9 +86,6 @@
 #define __unused __attribute__((__unused__))
 #endif
 
-#define MEMCG_SYSFS_PATH "/dev/memcg/"
-#define MEMCG_MEMORY_USAGE "/dev/memcg/memory.usage_in_bytes"
-#define MEMCG_MEMORYSW_USAGE "/dev/memcg/memory.memsw.usage_in_bytes"
 #define ZONEINFO_PATH "/proc/zoneinfo"
 #define MEMINFO_PATH "/proc/meminfo"
 #define VMSTAT_PATH "/proc/vmstat"
@@ -1182,9 +1180,12 @@
             soft_limit_mult = 64;
         }
 
-        snprintf(path, sizeof(path), MEMCG_SYSFS_PATH
-                 "apps/uid_%d/pid_%d/memory.soft_limit_in_bytes",
-                 params.uid, params.pid);
+        std::string path;
+        if (!CgroupGetAttributePathForTask("MemSoftLimit", params.pid, &path)) {
+            ALOGE("Querying MemSoftLimit path failed");
+            return;
+        }
+
         snprintf(val, sizeof(val), "%d", soft_limit_mult * EIGHT_MEGA);
 
         /*
@@ -1194,7 +1195,7 @@
         is_system_server = (params.oomadj == SYSTEM_ADJ &&
                             (pwdrec = getpwnam("system")) != NULL &&
                             params.uid == pwdrec->pw_uid);
-        writefilestring(path, val, !is_system_server);
+        writefilestring(path.c_str(), val, !is_system_server);
     }
 
     procp = pid_lookup(params.pid);
@@ -2861,6 +2862,16 @@
     }
 }
 
+static std::string GetCgroupAttributePath(const char* attr) {
+    std::string path;
+    if (!CgroupGetAttributePath(attr, &path)) {
+        ALOGE("Unknown cgroup attribute %s", attr);
+    }
+    return path;
+}
+
+// The implementation of this function relies on memcg statistics that are only available in the
+// v1 cgroup hierarchy.
 static void mp_event_common(int data, uint32_t events, struct polling_params *poll_params) {
     unsigned long long evcount;
     int64_t mem_usage, memsw_usage;
@@ -2873,12 +2884,14 @@
     long other_free = 0, other_file = 0;
     int min_score_adj;
     int minfree = 0;
+    static const std::string mem_usage_path = GetCgroupAttributePath("MemUsage");
     static struct reread_data mem_usage_file_data = {
-        .filename = MEMCG_MEMORY_USAGE,
+        .filename = mem_usage_path.c_str(),
         .fd = -1,
     };
+    static const std::string memsw_usage_path = GetCgroupAttributePath("MemAndSwapUsage");
     static struct reread_data memsw_usage_file_data = {
-        .filename = MEMCG_MEMORYSW_USAGE,
+        .filename = memsw_usage_path.c_str(),
         .fd = -1,
     };
     static struct wakeup_info wi;
@@ -3183,13 +3196,13 @@
     const char *levelstr = level_name[level_idx];
 
     /* gid containing AID_SYSTEM required */
-    mpfd = open(MEMCG_SYSFS_PATH "memory.pressure_level", O_RDONLY | O_CLOEXEC);
+    mpfd = open(GetCgroupAttributePath("MemPressureLevel").c_str(), O_RDONLY | O_CLOEXEC);
     if (mpfd < 0) {
         ALOGI("No kernel memory.pressure_level support (errno=%d)", errno);
         goto err_open_mpfd;
     }
 
-    evctlfd = open(MEMCG_SYSFS_PATH "cgroup.event_control", O_WRONLY | O_CLOEXEC);
+    evctlfd = open(GetCgroupAttributePath("CgroupEventControl").c_str(), O_WRONLY | O_CLOEXEC);
     if (evctlfd < 0) {
         ALOGI("No kernel memory cgroup event control (errno=%d)", errno);
         goto err_open_evctlfd;