From 5d397582acb4a4ec2b7bb2aa08ec7c9dcb8d701d Mon Sep 17 00:00:00 2001 From: Kalesh Singh Date: Fri, 4 Aug 2023 16:59:12 -0700 Subject: [PATCH] lmkd: Remove uses of hardcoded 4k PAGE_SIZE macro Use getpagesize() to query the real page size instead. Bug: 294618124 Test: m Change-Id: If9046f36412a54ba08b94cf3b43cd7bf9c1f26b5 --- include/lmkd.h | 5 +++++ lmkd.cpp | 20 +++++++++----------- statslog.cpp | 7 ++++--- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/include/lmkd.h b/include/lmkd.h index d710d50..b610e77 100644 --- a/include/lmkd.h +++ b/include/lmkd.h @@ -50,6 +50,11 @@ enum lmk_cmd { */ #define CTRL_PACKET_MAX_SIZE (sizeof(int) * (MAX_TARGETS * 2 + 1)) +/* + * Max number of characters in line. + */ +#define LINE_MAX 128 + /* LMKD packet - first int is lmk_cmd followed by payload */ typedef int LMKD_CTRL_PACKET[CTRL_PACKET_MAX_SIZE / sizeof(int)]; diff --git a/lmkd.cpp b/lmkd.cpp index 02fb553..9a966a3 100644 --- a/lmkd.cpp +++ b/lmkd.cpp @@ -91,7 +91,6 @@ static inline void trace_kill_end() {} #define PROC_STATUS_TGID_FIELD "Tgid:" #define PROC_STATUS_RSS_FIELD "VmRSS:" #define PROC_STATUS_SWAP_FIELD "VmSwap:" -#define LINE_MAX 128 #define PERCEPTIBLE_APP_ADJ 200 @@ -545,8 +544,8 @@ static uint16_t killcnt[MAX_DISTINCT_OOM_ADJ]; static int killcnt_free_idx = 0; static uint32_t killcnt_total = 0; -/* PAGE_SIZE / 1024 */ -static long page_k; +static int pagesize; +static long page_k; /* page size in kB */ static bool update_props(); static bool init_monitors(); @@ -622,7 +621,7 @@ static ssize_t read_all(int fd, char *buf, size_t max_len) */ static char *reread_file(struct reread_data *data) { /* start with page-size buffer and increase if needed */ - static ssize_t buf_size = PAGE_SIZE; + static ssize_t buf_size = pagesize; static char *new_buf, *buf = NULL; ssize_t size; @@ -843,7 +842,7 @@ static void poll_kernel(int poll_fd) { if (fields_read == 10 && group_leader_pid == pid) { ctrl_data_write_lmk_kill_occurred((pid_t)pid, (uid_t)uid); mem_st.process_start_time_ns = starttime * (NS_PER_SEC / sysconf(_SC_CLK_TCK)); - mem_st.rss_in_bytes = rss_in_pages * PAGE_SIZE; + mem_st.rss_in_bytes = rss_in_pages * pagesize; struct kill_stat kill_st = { .uid = static_cast(uid), @@ -1104,7 +1103,7 @@ static void cmd_procprio(LMKD_CTRL_PACKET packet, int field_count, struct ucred bool is_system_server; struct passwd *pwdrec; int64_t tgid; - char buf[PAGE_SIZE]; + char buf[pagesize]; lmkd_pack_get_procprio(packet, field_count, ¶ms); @@ -2299,7 +2298,7 @@ static int kill_one_process(struct proc* procp, int min_oom_score, struct kill_i int64_t tgid; int64_t rss_kb; int64_t swap_kb; - char buf[PAGE_SIZE]; + char buf[pagesize]; char desc[LINE_MAX]; if (!procp->valid || !read_proc_status(pid, buf, sizeof(buf))) { @@ -3426,10 +3425,9 @@ static int init(void) { int i; int ret; - page_k = sysconf(_SC_PAGESIZE); - if (page_k == -1) - page_k = PAGE_SIZE; - page_k /= 1024; + // Initialize page size + pagesize = getpagesize(); + page_k = pagesize / 1024; epollfd = epoll_create(MAX_EPOLL_EVENTS); if (epollfd == -1) { diff --git a/statslog.cpp b/statslog.cpp index 6e6cb42..fbb8867 100644 --- a/statslog.cpp +++ b/statslog.cpp @@ -32,6 +32,7 @@ #include +#include #include #ifdef LMKD_LOG_STATS @@ -67,7 +68,7 @@ static struct proc* pid_lookup(int pid) { return procp; } -static void memory_stat_parse_line(char* line, struct memory_stat* mem_st) { +static void memory_stat_parse_line(const char* line, struct memory_stat* mem_st) { char key[MAX_TASKNAME_LEN + 1]; int64_t value; @@ -102,8 +103,8 @@ static int memory_stat_from_cgroup(struct memory_stat* mem_st, int pid, uid_t ui return -1; } - char buf[PAGE_SIZE]; - while (fgets(buf, PAGE_SIZE, fp) != NULL) { + char buf[LINE_MAX]; + while (fgets(buf, LINE_MAX, fp) != NULL) { memory_stat_parse_line(buf, mem_st); } fclose(fp);