lmkd: Look up cgroup attribute paths instead of hardcoding these am: b4d26bb22b am: 08f4720373 am: def6a3f12f

Original change: https://android-review.googlesource.com/c/platform/system/memory/lmkd/+/1990496

Change-Id: Ica6100474a8ed7dce01045ad3a5362ce4e7721ab
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Bart Van Assche 2022-03-25 22:32:33 +00:00 committed by Automerger Merge Worker
commit cbe6b65f72
1 changed files with 24 additions and 11 deletions

View File

@ -46,6 +46,7 @@
#include <log/log_event_list.h> #include <log/log_event_list.h>
#include <log/log_time.h> #include <log/log_time.h>
#include <private/android_filesystem_config.h> #include <private/android_filesystem_config.h>
#include <processgroup/processgroup.h>
#include <psi/psi.h> #include <psi/psi.h>
#include "reaper.h" #include "reaper.h"
@ -85,9 +86,6 @@ static inline void trace_kill_end() {}
#define __unused __attribute__((__unused__)) #define __unused __attribute__((__unused__))
#endif #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 ZONEINFO_PATH "/proc/zoneinfo"
#define MEMINFO_PATH "/proc/meminfo" #define MEMINFO_PATH "/proc/meminfo"
#define VMSTAT_PATH "/proc/vmstat" #define VMSTAT_PATH "/proc/vmstat"
@ -1182,9 +1180,12 @@ static void cmd_procprio(LMKD_CTRL_PACKET packet, int field_count, struct ucred
soft_limit_mult = 64; soft_limit_mult = 64;
} }
snprintf(path, sizeof(path), MEMCG_SYSFS_PATH std::string path;
"apps/uid_%d/pid_%d/memory.soft_limit_in_bytes", if (!CgroupGetAttributePathForTask("MemSoftLimit", params.pid, &path)) {
params.uid, params.pid); ALOGE("Querying MemSoftLimit path failed");
return;
}
snprintf(val, sizeof(val), "%d", soft_limit_mult * EIGHT_MEGA); snprintf(val, sizeof(val), "%d", soft_limit_mult * EIGHT_MEGA);
/* /*
@ -1194,7 +1195,7 @@ static void cmd_procprio(LMKD_CTRL_PACKET packet, int field_count, struct ucred
is_system_server = (params.oomadj == SYSTEM_ADJ && is_system_server = (params.oomadj == SYSTEM_ADJ &&
(pwdrec = getpwnam("system")) != NULL && (pwdrec = getpwnam("system")) != NULL &&
params.uid == pwdrec->pw_uid); params.uid == pwdrec->pw_uid);
writefilestring(path, val, !is_system_server); writefilestring(path.c_str(), val, !is_system_server);
} }
procp = pid_lookup(params.pid); procp = pid_lookup(params.pid);
@ -2861,6 +2862,16 @@ no_kill:
} }
} }
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) { static void mp_event_common(int data, uint32_t events, struct polling_params *poll_params) {
unsigned long long evcount; unsigned long long evcount;
int64_t mem_usage, memsw_usage; int64_t mem_usage, memsw_usage;
@ -2873,12 +2884,14 @@ static void mp_event_common(int data, uint32_t events, struct polling_params *po
long other_free = 0, other_file = 0; long other_free = 0, other_file = 0;
int min_score_adj; int min_score_adj;
int minfree = 0; int minfree = 0;
static const std::string mem_usage_path = GetCgroupAttributePath("MemUsage");
static struct reread_data mem_usage_file_data = { static struct reread_data mem_usage_file_data = {
.filename = MEMCG_MEMORY_USAGE, .filename = mem_usage_path.c_str(),
.fd = -1, .fd = -1,
}; };
static const std::string memsw_usage_path = GetCgroupAttributePath("MemAndSwapUsage");
static struct reread_data memsw_usage_file_data = { static struct reread_data memsw_usage_file_data = {
.filename = MEMCG_MEMORYSW_USAGE, .filename = memsw_usage_path.c_str(),
.fd = -1, .fd = -1,
}; };
static struct wakeup_info wi; static struct wakeup_info wi;
@ -3183,13 +3196,13 @@ static bool init_mp_common(enum vmpressure_level level) {
const char *levelstr = level_name[level_idx]; const char *levelstr = level_name[level_idx];
/* gid containing AID_SYSTEM required */ /* 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) { if (mpfd < 0) {
ALOGI("No kernel memory.pressure_level support (errno=%d)", errno); ALOGI("No kernel memory.pressure_level support (errno=%d)", errno);
goto err_open_mpfd; 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) { if (evctlfd < 0) {
ALOGI("No kernel memory cgroup event control (errno=%d)", errno); ALOGI("No kernel memory cgroup event control (errno=%d)", errno);
goto err_open_evctlfd; goto err_open_evctlfd;