2706 lines
83 KiB
Diff
2706 lines
83 KiB
Diff
From 00074ee13c6be9a0710fb8f46dd0731bf7444f81 Mon Sep 17 00:00:00 2001
|
|
From: Ziyang Zhou <ziyang.zhou@outlook.com>
|
|
Date: Sat, 4 Nov 2023 10:24:11 +0800
|
|
Subject: [PATCH] add PSI support to lmkd
|
|
|
|
---
|
|
lmkd/Android.bp | 10 +-
|
|
lmkd/event.logtags | 38 +
|
|
lmkd/include/lmkd.h | 193 ++++
|
|
lmkd/libpsi/Android.bp | 22 +
|
|
lmkd/libpsi/OWNERS | 1 +
|
|
lmkd/libpsi/include/psi/psi.h | 68 ++
|
|
lmkd/libpsi/psi.c | 99 ++
|
|
lmkd/lmkd.c | 1745 +++++++++++++++++++++++++++------
|
|
8 files changed, 1895 insertions(+), 281 deletions(-)
|
|
create mode 100644 lmkd/event.logtags
|
|
create mode 100644 lmkd/include/lmkd.h
|
|
create mode 100644 lmkd/libpsi/Android.bp
|
|
create mode 100644 lmkd/libpsi/OWNERS
|
|
create mode 100644 lmkd/libpsi/include/psi/psi.h
|
|
create mode 100644 lmkd/libpsi/psi.c
|
|
|
|
diff --git a/lmkd/Android.bp b/lmkd/Android.bp
|
|
index 3f8a5035f..0f4789774 100644
|
|
--- a/lmkd/Android.bp
|
|
+++ b/lmkd/Android.bp
|
|
@@ -3,11 +3,15 @@ cc_binary {
|
|
|
|
srcs: ["lmkd.c"],
|
|
shared_libs: [
|
|
+ "libcutils",
|
|
"liblog",
|
|
"libprocessgroup",
|
|
- "libcutils",
|
|
+ "libpsi",
|
|
],
|
|
- cflags: ["-Werror"],
|
|
-
|
|
+ local_include_dirs: ["include"],
|
|
+ cflags: ["-Werror", "-DLMKD_TRACE_KILLS"],
|
|
init_rc: ["lmkd.rc"],
|
|
+ logtags: ["event.logtags"],
|
|
}
|
|
+
|
|
+subdirs = ["*"]
|
|
diff --git a/lmkd/event.logtags b/lmkd/event.logtags
|
|
new file mode 100644
|
|
index 000000000..065c6db6d
|
|
--- /dev/null
|
|
+++ b/lmkd/event.logtags
|
|
@@ -0,0 +1,38 @@
|
|
+# The entries in this file map a sparse set of log tag numbers to tag names.
|
|
+# This is installed on the device, in /system/etc, and parsed by logcat.
|
|
+#
|
|
+# Tag numbers are decimal integers, from 0 to 2^31. (Let's leave the
|
|
+# negative values alone for now.)
|
|
+#
|
|
+# Tag names are one or more ASCII letters and numbers or underscores, i.e.
|
|
+# "[A-Z][a-z][0-9]_". Do not include spaces or punctuation (the former
|
|
+# impacts log readability, the latter makes regex searches more annoying).
|
|
+#
|
|
+# Tag numbers and names are separated by whitespace. Blank lines and lines
|
|
+# starting with '#' are ignored.
|
|
+#
|
|
+# Optionally, after the tag names can be put a description for the value(s)
|
|
+# of the tag. Description are in the format
|
|
+# (<name>|data type[|data unit])
|
|
+# Multiple values are separated by commas.
|
|
+#
|
|
+# The data type is a number from the following values:
|
|
+# 1: int
|
|
+# 2: long
|
|
+# 3: string
|
|
+# 4: list
|
|
+#
|
|
+# The data unit is a number taken from the following list:
|
|
+# 1: Number of objects
|
|
+# 2: Number of bytes
|
|
+# 3: Number of milliseconds
|
|
+# 4: Number of allocations
|
|
+# 5: Id
|
|
+# 6: Percent
|
|
+# s: Number of seconds (monotonic time)
|
|
+# Default value for data of type int/long is 2 (bytes).
|
|
+#
|
|
+# TODO: generate ".java" and ".h" files with integer constants from this file.
|
|
+
|
|
+# for meminfo logs
|
|
+10195355 meminfo (MemFree|1),(Cached|1),(SwapCached|1),(Buffers|1),(Shmem|1),(Unevictable|1),(SwapTotal|1),(SwapFree|1),(ActiveAnon|1),(InactiveAnon|1),(ActiveFile|1),(InactiveFile|1),(SReclaimable|1),(SUnreclaim|1),(KernelStack|1),(PageTables|1),(ION_heap|1),(ION_heap_pool|1),(CmaFree|1)
|
|
diff --git a/lmkd/include/lmkd.h b/lmkd/include/lmkd.h
|
|
new file mode 100644
|
|
index 000000000..59377dd1f
|
|
--- /dev/null
|
|
+++ b/lmkd/include/lmkd.h
|
|
@@ -0,0 +1,193 @@
|
|
+/*
|
|
+ * Copyright 2018 Google, Inc
|
|
+ *
|
|
+ * Licensed under the Apache License, Version 2.0 (the "License");
|
|
+ * you may not use this file except in compliance with the License.
|
|
+ * You may obtain a copy of the License at
|
|
+ *
|
|
+ * http://www.apache.org/licenses/LICENSE-2.0
|
|
+ *
|
|
+ * Unless required by applicable law or agreed to in writing, software
|
|
+ * distributed under the License is distributed on an "AS IS" BASIS,
|
|
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
+ * See the License for the specific language governing permissions and
|
|
+ * limitations under the License.
|
|
+ */
|
|
+
|
|
+#ifndef _LMKD_H_
|
|
+#define _LMKD_H_
|
|
+
|
|
+#include <arpa/inet.h>
|
|
+#include <sys/cdefs.h>
|
|
+#include <sys/types.h>
|
|
+
|
|
+__BEGIN_DECLS
|
|
+
|
|
+/*
|
|
+ * Supported LMKD commands
|
|
+ */
|
|
+enum lmk_cmd {
|
|
+ LMK_TARGET = 0, /* Associate minfree with oom_adj_score */
|
|
+ LMK_PROCPRIO, /* Register a process and set its oom_adj_score */
|
|
+ LMK_PROCREMOVE, /* Unregister a process */
|
|
+ LMK_PROCPURGE, /* Purge all registered processes */
|
|
+ LMK_GETKILLCNT, /* Get number of kills */
|
|
+};
|
|
+
|
|
+/*
|
|
+ * Max number of targets in LMK_TARGET command.
|
|
+ */
|
|
+#define MAX_TARGETS 6
|
|
+
|
|
+/*
|
|
+ * Max packet length in bytes.
|
|
+ * Longest packet is LMK_TARGET followed by MAX_TARGETS
|
|
+ * of minfree and oom_adj_score values
|
|
+ */
|
|
+#define CTRL_PACKET_MAX_SIZE (sizeof(int) * (MAX_TARGETS * 2 + 1))
|
|
+
|
|
+/* LMKD packet - first int is lmk_cmd followed by payload */
|
|
+typedef int LMKD_CTRL_PACKET[CTRL_PACKET_MAX_SIZE / sizeof(int)];
|
|
+
|
|
+/* Get LMKD packet command */
|
|
+static inline enum lmk_cmd lmkd_pack_get_cmd(LMKD_CTRL_PACKET pack) {
|
|
+ return (enum lmk_cmd)ntohl(pack[0]);
|
|
+}
|
|
+
|
|
+/* LMK_TARGET packet payload */
|
|
+struct lmk_target {
|
|
+ int minfree;
|
|
+ int oom_adj_score;
|
|
+};
|
|
+
|
|
+/*
|
|
+ * For LMK_TARGET packet get target_idx-th payload.
|
|
+ * Warning: no checks performed, caller should ensure valid parameters.
|
|
+ */
|
|
+static inline void lmkd_pack_get_target(LMKD_CTRL_PACKET packet, int target_idx,
|
|
+ struct lmk_target* target) {
|
|
+ target->minfree = ntohl(packet[target_idx * 2 + 1]);
|
|
+ target->oom_adj_score = ntohl(packet[target_idx * 2 + 2]);
|
|
+}
|
|
+
|
|
+/*
|
|
+ * Prepare LMK_TARGET packet and return packet size in bytes.
|
|
+ * Warning: no checks performed, caller should ensure valid parameters.
|
|
+ */
|
|
+static inline size_t lmkd_pack_set_target(LMKD_CTRL_PACKET packet, struct lmk_target* targets,
|
|
+ size_t target_cnt) {
|
|
+ int idx = 0;
|
|
+ packet[idx++] = htonl(LMK_TARGET);
|
|
+ while (target_cnt) {
|
|
+ packet[idx++] = htonl(targets->minfree);
|
|
+ packet[idx++] = htonl(targets->oom_adj_score);
|
|
+ targets++;
|
|
+ target_cnt--;
|
|
+ }
|
|
+ return idx * sizeof(int);
|
|
+}
|
|
+
|
|
+/* LMK_PROCPRIO packet payload */
|
|
+struct lmk_procprio {
|
|
+ pid_t pid;
|
|
+ uid_t uid;
|
|
+ int oomadj;
|
|
+};
|
|
+
|
|
+/*
|
|
+ * For LMK_PROCPRIO packet get its payload.
|
|
+ * Warning: no checks performed, caller should ensure valid parameters.
|
|
+ */
|
|
+static inline void lmkd_pack_get_procprio(LMKD_CTRL_PACKET packet, struct lmk_procprio* params) {
|
|
+ params->pid = (pid_t)ntohl(packet[1]);
|
|
+ params->uid = (uid_t)ntohl(packet[2]);
|
|
+ params->oomadj = ntohl(packet[3]);
|
|
+}
|
|
+
|
|
+/*
|
|
+ * Prepare LMK_PROCPRIO packet and return packet size in bytes.
|
|
+ * Warning: no checks performed, caller should ensure valid parameters.
|
|
+ */
|
|
+static inline size_t lmkd_pack_set_procprio(LMKD_CTRL_PACKET packet, struct lmk_procprio* params) {
|
|
+ packet[0] = htonl(LMK_PROCPRIO);
|
|
+ packet[1] = htonl(params->pid);
|
|
+ packet[2] = htonl(params->uid);
|
|
+ packet[3] = htonl(params->oomadj);
|
|
+ return 4 * sizeof(int);
|
|
+}
|
|
+
|
|
+/* LMK_PROCREMOVE packet payload */
|
|
+struct lmk_procremove {
|
|
+ pid_t pid;
|
|
+};
|
|
+
|
|
+/*
|
|
+ * For LMK_PROCREMOVE packet get its payload.
|
|
+ * Warning: no checks performed, caller should ensure valid parameters.
|
|
+ */
|
|
+static inline void lmkd_pack_get_procremove(LMKD_CTRL_PACKET packet,
|
|
+ struct lmk_procremove* params) {
|
|
+ params->pid = (pid_t)ntohl(packet[1]);
|
|
+}
|
|
+
|
|
+/*
|
|
+ * Prepare LMK_PROCREMOVE packet and return packet size in bytes.
|
|
+ * Warning: no checks performed, caller should ensure valid parameters.
|
|
+ */
|
|
+static inline size_t lmkd_pack_set_procremove(LMKD_CTRL_PACKET packet,
|
|
+ struct lmk_procprio* params) {
|
|
+ packet[0] = htonl(LMK_PROCREMOVE);
|
|
+ packet[1] = htonl(params->pid);
|
|
+ return 2 * sizeof(int);
|
|
+}
|
|
+
|
|
+/*
|
|
+ * Prepare LMK_PROCPURGE packet and return packet size in bytes.
|
|
+ * Warning: no checks performed, caller should ensure valid parameters.
|
|
+ */
|
|
+static inline size_t lmkd_pack_set_procpurge(LMKD_CTRL_PACKET packet) {
|
|
+ packet[0] = htonl(LMK_PROCPURGE);
|
|
+ return sizeof(int);
|
|
+}
|
|
+
|
|
+/* LMK_GETKILLCNT packet payload */
|
|
+struct lmk_getkillcnt {
|
|
+ int min_oomadj;
|
|
+ int max_oomadj;
|
|
+};
|
|
+
|
|
+/*
|
|
+ * For LMK_GETKILLCNT packet get its payload.
|
|
+ * Warning: no checks performed, caller should ensure valid parameters.
|
|
+ */
|
|
+static inline void lmkd_pack_get_getkillcnt(LMKD_CTRL_PACKET packet,
|
|
+ struct lmk_getkillcnt* params) {
|
|
+ params->min_oomadj = ntohl(packet[1]);
|
|
+ params->max_oomadj = ntohl(packet[2]);
|
|
+}
|
|
+
|
|
+/*
|
|
+ * Prepare LMK_GETKILLCNT packet and return packet size in bytes.
|
|
+ * Warning: no checks performed, caller should ensure valid parameters.
|
|
+ */
|
|
+static inline size_t lmkd_pack_set_getkillcnt(LMKD_CTRL_PACKET packet,
|
|
+ struct lmk_getkillcnt* params) {
|
|
+ packet[0] = htonl(LMK_GETKILLCNT);
|
|
+ packet[1] = htonl(params->min_oomadj);
|
|
+ packet[2] = htonl(params->max_oomadj);
|
|
+ return 3 * sizeof(int);
|
|
+}
|
|
+
|
|
+/*
|
|
+ * Prepare LMK_GETKILLCNT reply packet and return packet size in bytes.
|
|
+ * Warning: no checks performed, caller should ensure valid parameters.
|
|
+ */
|
|
+static inline size_t lmkd_pack_set_getkillcnt_repl(LMKD_CTRL_PACKET packet, int kill_cnt) {
|
|
+ packet[0] = htonl(LMK_GETKILLCNT);
|
|
+ packet[1] = htonl(kill_cnt);
|
|
+ return 2 * sizeof(int);
|
|
+}
|
|
+
|
|
+__END_DECLS
|
|
+
|
|
+#endif /* _LMKD_H_ */
|
|
diff --git a/lmkd/libpsi/Android.bp b/lmkd/libpsi/Android.bp
|
|
new file mode 100644
|
|
index 000000000..8a970942e
|
|
--- /dev/null
|
|
+++ b/lmkd/libpsi/Android.bp
|
|
@@ -0,0 +1,22 @@
|
|
+cc_library_headers {
|
|
+ name: "libpsi_headers",
|
|
+ export_include_dirs: ["include"],
|
|
+}
|
|
+
|
|
+cc_library {
|
|
+ name: "libpsi",
|
|
+ srcs: ["psi.c"],
|
|
+ shared_libs: [
|
|
+ "liblog"
|
|
+ ],
|
|
+ header_libs: [
|
|
+ "libpsi_headers",
|
|
+ ],
|
|
+ export_header_lib_headers: [
|
|
+ "libpsi_headers",
|
|
+ ],
|
|
+ cflags: [
|
|
+ "-Wall",
|
|
+ "-Werror",
|
|
+ ],
|
|
+}
|
|
diff --git a/lmkd/libpsi/OWNERS b/lmkd/libpsi/OWNERS
|
|
new file mode 100644
|
|
index 000000000..b15bb4870
|
|
--- /dev/null
|
|
+++ b/lmkd/libpsi/OWNERS
|
|
@@ -0,0 +1 @@
|
|
+surenb@google.com
|
|
diff --git a/lmkd/libpsi/include/psi/psi.h b/lmkd/libpsi/include/psi/psi.h
|
|
new file mode 100644
|
|
index 000000000..cd49e8b60
|
|
--- /dev/null
|
|
+++ b/lmkd/libpsi/include/psi/psi.h
|
|
@@ -0,0 +1,68 @@
|
|
+/*
|
|
+ * Copyright (C) 2018 The Android Open Source Project
|
|
+ *
|
|
+ * Licensed under the Apache License, Version 2.0 (the "License");
|
|
+ * you may not use this file except in compliance with the License.
|
|
+ * You may obtain a copy of the License at
|
|
+ *
|
|
+ * http://www.apache.org/licenses/LICENSE-2.0
|
|
+ *
|
|
+ * Unless required by applicable law or agreed to in writing, software
|
|
+ * distributed under the License is distributed on an "AS IS" BASIS,
|
|
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
+ * See the License for the specific language governing permissions and
|
|
+ * limitations under the License.
|
|
+ */
|
|
+
|
|
+#ifndef __ANDROID_PSI_H__
|
|
+#define __ANDROID_PSI_H__
|
|
+
|
|
+#include <sys/cdefs.h>
|
|
+#include <sys/types.h>
|
|
+
|
|
+__BEGIN_DECLS
|
|
+
|
|
+enum psi_stall_type {
|
|
+ PSI_SOME,
|
|
+ PSI_FULL,
|
|
+ PSI_TYPE_COUNT
|
|
+};
|
|
+
|
|
+/*
|
|
+ * Initializes psi monitor.
|
|
+ * stall_type, threshold_us and window_us are monitor parameters
|
|
+ * When successful, the function returns file descriptor that can
|
|
+ * be used with poll/epoll syscalls to wait for EPOLLPRI events.
|
|
+ * When unsuccessful, the function returns -1 and errno is set
|
|
+ * appropriately.
|
|
+ */
|
|
+int init_psi_monitor(enum psi_stall_type stall_type,
|
|
+ int threshold_us, int window_us);
|
|
+
|
|
+/*
|
|
+ * Registers psi monitor file descriptor fd on the epoll instance
|
|
+ * referred to by the file descriptor epollfd.
|
|
+ * data parameter will be associated with event's epoll_data.ptr
|
|
+ * member.
|
|
+ */
|
|
+int register_psi_monitor(int epollfd, int fd, void* data);
|
|
+
|
|
+/*
|
|
+ * Unregisters psi monitor file descriptor fd from the epoll instance
|
|
+ * referred to by the file descriptor epollfd.
|
|
+ */
|
|
+int unregister_psi_monitor(int epollfd, int fd);
|
|
+
|
|
+/*
|
|
+ * Destroys psi monitor.
|
|
+ * fd is the file descriptor returned by psi monitor initialization
|
|
+ * routine.
|
|
+ * Note that if user process exits without calling this routine
|
|
+ * kernel will destroy the monitor as its lifetime is linked to
|
|
+ * the file descriptor.
|
|
+ */
|
|
+void destroy_psi_monitor(int fd);
|
|
+
|
|
+__END_DECLS
|
|
+
|
|
+#endif // __ANDROID_PSI_H__
|
|
diff --git a/lmkd/libpsi/psi.c b/lmkd/libpsi/psi.c
|
|
new file mode 100644
|
|
index 000000000..f4d5d1878
|
|
--- /dev/null
|
|
+++ b/lmkd/libpsi/psi.c
|
|
@@ -0,0 +1,99 @@
|
|
+/*
|
|
+ * Copyright (C) 2018 The Android Open Source Project
|
|
+ *
|
|
+ * Licensed under the Apache License, Version 2.0 (the "License");
|
|
+ * you may not use this file except in compliance with the License.
|
|
+ * You may obtain a copy of the License at
|
|
+ *
|
|
+ * http://www.apache.org/licenses/LICENSE-2.0
|
|
+ *
|
|
+ * Unless required by applicable law or agreed to in writing, software
|
|
+ * distributed under the License is distributed on an "AS IS" BASIS,
|
|
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
+ * See the License for the specific language governing permissions and
|
|
+ * limitations under the License.
|
|
+ */
|
|
+
|
|
+#define LOG_TAG "libpsi"
|
|
+
|
|
+#include <errno.h>
|
|
+#include <string.h>
|
|
+#include <sys/epoll.h>
|
|
+
|
|
+#include <log/log.h>
|
|
+#include "psi/psi.h"
|
|
+
|
|
+#define PSI_MON_FILE_MEMORY "/proc/pressure/memory"
|
|
+
|
|
+static const char* stall_type_name[] = {
|
|
+ "some",
|
|
+ "full",
|
|
+};
|
|
+
|
|
+int init_psi_monitor(enum psi_stall_type stall_type,
|
|
+ int threshold_us, int window_us) {
|
|
+ int fd;
|
|
+ int res;
|
|
+ char buf[256];
|
|
+
|
|
+ fd = TEMP_FAILURE_RETRY(open(PSI_MON_FILE_MEMORY, O_WRONLY | O_CLOEXEC));
|
|
+ if (fd < 0) {
|
|
+ ALOGE("No kernel psi monitor support (errno=%d)", errno);
|
|
+ return -1;
|
|
+ }
|
|
+
|
|
+ switch (stall_type) {
|
|
+ case (PSI_SOME):
|
|
+ case (PSI_FULL):
|
|
+ res = snprintf(buf, sizeof(buf), "%s %d %d",
|
|
+ stall_type_name[stall_type], threshold_us, window_us);
|
|
+ break;
|
|
+ default:
|
|
+ ALOGE("Invalid psi stall type: %d", stall_type);
|
|
+ errno = EINVAL;
|
|
+ goto err;
|
|
+ }
|
|
+
|
|
+ if (res >= (ssize_t)sizeof(buf)) {
|
|
+ ALOGE("%s line overflow for psi stall type '%s'",
|
|
+ PSI_MON_FILE_MEMORY, stall_type_name[stall_type]);
|
|
+ errno = EINVAL;
|
|
+ goto err;
|
|
+ }
|
|
+
|
|
+ res = TEMP_FAILURE_RETRY(write(fd, buf, strlen(buf) + 1));
|
|
+ if (res < 0) {
|
|
+ ALOGE("%s write failed for psi stall type '%s'; errno=%d",
|
|
+ PSI_MON_FILE_MEMORY, stall_type_name[stall_type], errno);
|
|
+ goto err;
|
|
+ }
|
|
+
|
|
+ return fd;
|
|
+
|
|
+err:
|
|
+ close(fd);
|
|
+ return -1;
|
|
+}
|
|
+
|
|
+int register_psi_monitor(int epollfd, int fd, void* data) {
|
|
+ int res;
|
|
+ struct epoll_event epev;
|
|
+
|
|
+ epev.events = EPOLLPRI;
|
|
+ epev.data.ptr = data;
|
|
+ res = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &epev);
|
|
+ if (res < 0) {
|
|
+ ALOGE("epoll_ctl for psi monitor failed; errno=%d", errno);
|
|
+ }
|
|
+ return res;
|
|
+}
|
|
+
|
|
+int unregister_psi_monitor(int epollfd, int fd) {
|
|
+ return epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, NULL);
|
|
+}
|
|
+
|
|
+void destroy_psi_monitor(int fd) {
|
|
+ if (fd >= 0) {
|
|
+ close(fd);
|
|
+ }
|
|
+}
|
|
diff --git a/lmkd/lmkd.c b/lmkd/lmkd.c
|
|
index 5cfa2c887..3a42784bc 100644
|
|
--- a/lmkd/lmkd.c
|
|
+++ b/lmkd/lmkd.c
|
|
@@ -16,26 +16,59 @@
|
|
|
|
#define LOG_TAG "lowmemorykiller"
|
|
|
|
-#include <arpa/inet.h>
|
|
+#include <dirent.h>
|
|
#include <errno.h>
|
|
#include <inttypes.h>
|
|
+#include <pwd.h>
|
|
#include <sched.h>
|
|
#include <signal.h>
|
|
+#include <stdbool.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/cdefs.h>
|
|
#include <sys/epoll.h>
|
|
#include <sys/eventfd.h>
|
|
#include <sys/mman.h>
|
|
+#include <sys/resource.h>
|
|
#include <sys/socket.h>
|
|
+#include <sys/sysinfo.h>
|
|
+#include <sys/time.h>
|
|
#include <sys/types.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#include <cutils/properties.h>
|
|
+#include <cutils/sched_policy.h>
|
|
#include <cutils/sockets.h>
|
|
+#include <lmkd.h>
|
|
#include <log/log.h>
|
|
-#include <processgroup/processgroup.h>
|
|
+#include <log/log_event_list.h>
|
|
+#include <log/log_time.h>
|
|
+#include <psi/psi.h>
|
|
+#include <system/thread_defs.h>
|
|
+
|
|
+#ifdef LMKD_LOG_STATS
|
|
+#include "statslog.h"
|
|
+#endif
|
|
+
|
|
+/*
|
|
+ * Define LMKD_TRACE_KILLS to record lmkd kills in kernel traces
|
|
+ * to profile and correlate with OOM kills
|
|
+ */
|
|
+#ifdef LMKD_TRACE_KILLS
|
|
+
|
|
+#define ATRACE_TAG ATRACE_TAG_ALWAYS
|
|
+#include <cutils/trace.h>
|
|
+
|
|
+#define TRACE_KILL_START(pid) ATRACE_INT(__FUNCTION__, pid);
|
|
+#define TRACE_KILL_END() ATRACE_INT(__FUNCTION__, 0);
|
|
+
|
|
+#else /* LMKD_TRACE_KILLS */
|
|
+
|
|
+#define TRACE_KILL_START(pid) ((void)(pid))
|
|
+#define TRACE_KILL_END() ((void)0)
|
|
+
|
|
+#endif /* LMKD_TRACE_KILLS */
|
|
|
|
#ifndef __unused
|
|
#define __unused __attribute__((__unused__))
|
|
@@ -44,54 +77,120 @@
|
|
#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 MEMPRESSURE_WATCH_MEDIUM_LEVEL "medium"
|
|
-#define MEMPRESSURE_WATCH_CRITICAL_LEVEL "critical"
|
|
#define ZONEINFO_PATH "/proc/zoneinfo"
|
|
+#define MEMINFO_PATH "/proc/meminfo"
|
|
#define LINE_MAX 128
|
|
|
|
+/* Android Logger event logtags (see event.logtags) */
|
|
+#define MEMINFO_LOG_TAG 10195355
|
|
+
|
|
+/* gid containing AID_SYSTEM required */
|
|
#define INKERNEL_MINFREE_PATH "/sys/module/lowmemorykiller/parameters/minfree"
|
|
#define INKERNEL_ADJ_PATH "/sys/module/lowmemorykiller/parameters/adj"
|
|
|
|
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
|
|
#define EIGHT_MEGA (1 << 23)
|
|
|
|
-enum lmk_cmd {
|
|
- LMK_TARGET,
|
|
- LMK_PROCPRIO,
|
|
- LMK_PROCREMOVE,
|
|
-};
|
|
+#define TARGET_UPDATE_MIN_INTERVAL_MS 1000
|
|
+
|
|
+#define NS_PER_MS (NS_PER_SEC / MS_PER_SEC)
|
|
+#define US_PER_MS (US_PER_SEC / MS_PER_SEC)
|
|
+
|
|
+/* Defined as ProcessList.SYSTEM_ADJ in ProcessList.java */
|
|
+#define SYSTEM_ADJ (-900)
|
|
+
|
|
+#define STRINGIFY(x) STRINGIFY_INTERNAL(x)
|
|
+#define STRINGIFY_INTERNAL(x) #x
|
|
|
|
-#define MAX_TARGETS 6
|
|
/*
|
|
- * longest is LMK_TARGET followed by MAX_TARGETS each minfree and minkillprio
|
|
- * values
|
|
+ * PSI monitor tracking window size.
|
|
+ * PSI monitor generates events at most once per window,
|
|
+ * therefore we poll memory state for the duration of
|
|
+ * PSI_WINDOW_SIZE_MS after the event happens.
|
|
*/
|
|
-#define CTRL_PACKET_MAX (sizeof(int) * (MAX_TARGETS * 2 + 1))
|
|
+#define PSI_WINDOW_SIZE_MS 1000
|
|
+/* Polling period after initial PSI signal */
|
|
+#define PSI_POLL_PERIOD_MS 10
|
|
+/* Poll for the duration of one window after initial PSI signal */
|
|
+#define PSI_POLL_COUNT (PSI_WINDOW_SIZE_MS / PSI_POLL_PERIOD_MS)
|
|
+
|
|
+#define min(a, b) (((a) < (b)) ? (a) : (b))
|
|
+
|
|
+#define FAIL_REPORT_RLIMIT_MS 1000
|
|
|
|
/* default to old in-kernel interface if no memory pressure events */
|
|
-static int use_inkernel_interface = 1;
|
|
+static bool use_inkernel_interface = true;
|
|
static bool has_inkernel_module;
|
|
|
|
-/* memory pressure level medium event */
|
|
-static int mpevfd[2];
|
|
-#define CRITICAL_INDEX 1
|
|
-#define MEDIUM_INDEX 0
|
|
+/* memory pressure levels */
|
|
+enum vmpressure_level {
|
|
+ VMPRESS_LEVEL_LOW = 0,
|
|
+ VMPRESS_LEVEL_MEDIUM,
|
|
+ VMPRESS_LEVEL_CRITICAL,
|
|
+ VMPRESS_LEVEL_COUNT
|
|
+};
|
|
+
|
|
+static const char *level_name[] = {
|
|
+ "low",
|
|
+ "medium",
|
|
+ "critical"
|
|
+};
|
|
+
|
|
+struct {
|
|
+ int64_t min_nr_free_pages; /* recorded but not used yet */
|
|
+ int64_t max_nr_free_pages;
|
|
+} low_pressure_mem = { -1, -1 };
|
|
|
|
-static int medium_oomadj;
|
|
-static int critical_oomadj;
|
|
+struct psi_threshold {
|
|
+ enum psi_stall_type stall_type;
|
|
+ int threshold_ms;
|
|
+};
|
|
+
|
|
+static int level_oomadj[VMPRESS_LEVEL_COUNT];
|
|
+static int mpevfd[VMPRESS_LEVEL_COUNT] = { -1, -1, -1 };
|
|
static bool debug_process_killing;
|
|
static bool enable_pressure_upgrade;
|
|
static int64_t upgrade_pressure;
|
|
static int64_t downgrade_pressure;
|
|
-static bool is_go_device;
|
|
+static bool low_ram_device;
|
|
+static bool kill_heaviest_task;
|
|
+static unsigned long kill_timeout_ms;
|
|
+static bool use_minfree_levels;
|
|
+static bool per_app_memcg;
|
|
+static int swap_free_low_percentage;
|
|
+static bool use_psi_monitors = false;
|
|
+static struct psi_threshold psi_thresholds[VMPRESS_LEVEL_COUNT] = {
|
|
+ { PSI_SOME, 70 }, /* 70ms out of 1sec for partial stall */
|
|
+ { PSI_SOME, 100 }, /* 100ms out of 1sec for partial stall */
|
|
+ { PSI_FULL, 70 }, /* 70ms out of 1sec for complete stall */
|
|
+};
|
|
+
|
|
+static android_log_context ctx;
|
|
+
|
|
+/* data required to handle events */
|
|
+struct event_handler_info {
|
|
+ int data;
|
|
+ void (*handler)(int data, uint32_t events);
|
|
+};
|
|
+
|
|
+/* data required to handle socket events */
|
|
+struct sock_event_handler_info {
|
|
+ int sock;
|
|
+ struct event_handler_info handler_info;
|
|
+};
|
|
+
|
|
+/* max supported number of data connections */
|
|
+#define MAX_DATA_CONN 2
|
|
|
|
-/* control socket listen and data */
|
|
-static int ctrl_lfd;
|
|
-static int ctrl_dfd = -1;
|
|
-static int ctrl_dfd_reopened; /* did we reopen ctrl conn on this loop? */
|
|
+/* socket event handler data */
|
|
+static struct sock_event_handler_info ctrl_sock;
|
|
+static struct sock_event_handler_info data_sock[MAX_DATA_CONN];
|
|
|
|
-/* 2 memory pressure levels, 1 ctrl listen socket, 1 ctrl data socket */
|
|
-#define MAX_EPOLL_EVENTS 4
|
|
+/* vmpressure event handler data */
|
|
+static struct event_handler_info vmpressure_hinfo[VMPRESS_LEVEL_COUNT];
|
|
+
|
|
+/* 3 memory pressure levels, 1 ctrl listen socket, 2 ctrl data socket */
|
|
+#define MAX_EPOLL_EVENTS (1 + MAX_DATA_CONN + VMPRESS_LEVEL_COUNT)
|
|
static int epollfd;
|
|
static int maxevents;
|
|
|
|
@@ -103,11 +202,117 @@ static int lowmem_adj[MAX_TARGETS];
|
|
static int lowmem_minfree[MAX_TARGETS];
|
|
static int lowmem_targets_size;
|
|
|
|
-struct sysmeminfo {
|
|
- int nr_free_pages;
|
|
- int nr_file_pages;
|
|
- int nr_shmem;
|
|
- int totalreserve_pages;
|
|
+/* Fields to parse in /proc/zoneinfo */
|
|
+enum zoneinfo_field {
|
|
+ ZI_NR_FREE_PAGES = 0,
|
|
+ ZI_NR_FILE_PAGES,
|
|
+ ZI_NR_SHMEM,
|
|
+ ZI_NR_UNEVICTABLE,
|
|
+ ZI_WORKINGSET_REFAULT,
|
|
+ ZI_HIGH,
|
|
+ ZI_FIELD_COUNT
|
|
+};
|
|
+
|
|
+static const char* const zoneinfo_field_names[ZI_FIELD_COUNT] = {
|
|
+ "nr_free_pages",
|
|
+ "nr_file_pages",
|
|
+ "nr_shmem",
|
|
+ "nr_unevictable",
|
|
+ "workingset_refault",
|
|
+ "high",
|
|
+};
|
|
+
|
|
+union zoneinfo {
|
|
+ struct {
|
|
+ int64_t nr_free_pages;
|
|
+ int64_t nr_file_pages;
|
|
+ int64_t nr_shmem;
|
|
+ int64_t nr_unevictable;
|
|
+ int64_t workingset_refault;
|
|
+ int64_t high;
|
|
+ /* fields below are calculated rather than read from the file */
|
|
+ int64_t totalreserve_pages;
|
|
+ } field;
|
|
+ int64_t arr[ZI_FIELD_COUNT];
|
|
+};
|
|
+
|
|
+/* Fields to parse in /proc/meminfo */
|
|
+enum meminfo_field {
|
|
+ MI_NR_FREE_PAGES = 0,
|
|
+ MI_CACHED,
|
|
+ MI_SWAP_CACHED,
|
|
+ MI_BUFFERS,
|
|
+ MI_SHMEM,
|
|
+ MI_UNEVICTABLE,
|
|
+ MI_TOTAL_SWAP,
|
|
+ MI_FREE_SWAP,
|
|
+ MI_ACTIVE_ANON,
|
|
+ MI_INACTIVE_ANON,
|
|
+ MI_ACTIVE_FILE,
|
|
+ MI_INACTIVE_FILE,
|
|
+ MI_SRECLAIMABLE,
|
|
+ MI_SUNRECLAIM,
|
|
+ MI_KERNEL_STACK,
|
|
+ MI_PAGE_TABLES,
|
|
+ MI_ION_HELP,
|
|
+ MI_ION_HELP_POOL,
|
|
+ MI_CMA_FREE,
|
|
+ MI_FIELD_COUNT
|
|
+};
|
|
+
|
|
+static const char* const meminfo_field_names[MI_FIELD_COUNT] = {
|
|
+ "MemFree:",
|
|
+ "Cached:",
|
|
+ "SwapCached:",
|
|
+ "Buffers:",
|
|
+ "Shmem:",
|
|
+ "Unevictable:",
|
|
+ "SwapTotal:",
|
|
+ "SwapFree:",
|
|
+ "Active(anon):",
|
|
+ "Inactive(anon):",
|
|
+ "Active(file):",
|
|
+ "Inactive(file):",
|
|
+ "SReclaimable:",
|
|
+ "SUnreclaim:",
|
|
+ "KernelStack:",
|
|
+ "PageTables:",
|
|
+ "ION_heap:",
|
|
+ "ION_heap_pool:",
|
|
+ "CmaFree:",
|
|
+};
|
|
+
|
|
+union meminfo {
|
|
+ struct {
|
|
+ int64_t nr_free_pages;
|
|
+ int64_t cached;
|
|
+ int64_t swap_cached;
|
|
+ int64_t buffers;
|
|
+ int64_t shmem;
|
|
+ int64_t unevictable;
|
|
+ int64_t total_swap;
|
|
+ int64_t free_swap;
|
|
+ int64_t active_anon;
|
|
+ int64_t inactive_anon;
|
|
+ int64_t active_file;
|
|
+ int64_t inactive_file;
|
|
+ int64_t sreclaimable;
|
|
+ int64_t sunreclaimable;
|
|
+ int64_t kernel_stack;
|
|
+ int64_t page_tables;
|
|
+ int64_t ion_heap;
|
|
+ int64_t ion_heap_pool;
|
|
+ int64_t cma_free;
|
|
+ /* fields below are calculated rather than read from the file */
|
|
+ int64_t nr_file_pages;
|
|
+ } field;
|
|
+ int64_t arr[MI_FIELD_COUNT];
|
|
+};
|
|
+
|
|
+enum field_match_result {
|
|
+ NO_MATCH,
|
|
+ PARSE_FAIL,
|
|
+ PARSE_SUCCESS
|
|
};
|
|
|
|
struct adjslot_list {
|
|
@@ -123,22 +328,76 @@ struct proc {
|
|
struct proc *pidhash_next;
|
|
};
|
|
|
|
+struct reread_data {
|
|
+ const char* const filename;
|
|
+ int fd;
|
|
+};
|
|
+
|
|
+#ifdef LMKD_LOG_STATS
|
|
+static bool enable_stats_log;
|
|
+static android_log_context log_ctx;
|
|
+#endif
|
|
+
|
|
#define PIDHASH_SZ 1024
|
|
static struct proc *pidhash[PIDHASH_SZ];
|
|
#define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1))
|
|
|
|
#define ADJTOSLOT(adj) ((adj) + -OOM_SCORE_ADJ_MIN)
|
|
-static struct adjslot_list procadjslot_list[ADJTOSLOT(OOM_SCORE_ADJ_MAX) + 1];
|
|
+#define ADJTOSLOT_COUNT (ADJTOSLOT(OOM_SCORE_ADJ_MAX) + 1)
|
|
+static struct adjslot_list procadjslot_list[ADJTOSLOT_COUNT];
|
|
+
|
|
+#define MAX_DISTINCT_OOM_ADJ 32
|
|
+#define KILLCNT_INVALID_IDX 0xFF
|
|
+/*
|
|
+ * Because killcnt array is sparse a two-level indirection is used
|
|
+ * to keep the size small. killcnt_idx stores index of the element in
|
|
+ * killcnt array. Index KILLCNT_INVALID_IDX indicates an unused slot.
|
|
+ */
|
|
+static uint8_t killcnt_idx[ADJTOSLOT_COUNT];
|
|
+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 bool parse_int64(const char* str, int64_t* ret) {
|
|
+ char* endptr;
|
|
+ long long val = strtoll(str, &endptr, 10);
|
|
+ if (str == endptr || val > INT64_MAX) {
|
|
+ return false;
|
|
+ }
|
|
+ *ret = (int64_t)val;
|
|
+ return true;
|
|
+}
|
|
+
|
|
+static enum field_match_result match_field(const char* cp, const char* ap,
|
|
+ const char* const field_names[],
|
|
+ int field_count, int64_t* field,
|
|
+ int *field_idx) {
|
|
+ int64_t val;
|
|
+ int i;
|
|
+
|
|
+ for (i = 0; i < field_count; i++) {
|
|
+ if (!strcmp(cp, field_names[i])) {
|
|
+ *field_idx = i;
|
|
+ return parse_int64(ap, field) ? PARSE_SUCCESS : PARSE_FAIL;
|
|
+ }
|
|
+ }
|
|
+ return NO_MATCH;
|
|
+}
|
|
+
|
|
+/*
|
|
+ * Read file content from the beginning up to max_len bytes or EOF
|
|
+ * whichever happens first.
|
|
+ */
|
|
static ssize_t read_all(int fd, char *buf, size_t max_len)
|
|
{
|
|
ssize_t ret = 0;
|
|
+ off_t offset = 0;
|
|
|
|
while (max_len > 0) {
|
|
- ssize_t r = read(fd, buf, max_len);
|
|
+ ssize_t r = TEMP_FAILURE_RETRY(pread(fd, buf, max_len, offset));
|
|
if (r == 0) {
|
|
break;
|
|
}
|
|
@@ -147,12 +406,44 @@ static ssize_t read_all(int fd, char *buf, size_t max_len)
|
|
}
|
|
ret += r;
|
|
buf += r;
|
|
+ offset += r;
|
|
max_len -= r;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
+/*
|
|
+ * Read a new or already opened file from the beginning.
|
|
+ * If the file has not been opened yet data->fd should be set to -1.
|
|
+ * To be used with files which are read often and possibly during high
|
|
+ * memory pressure to minimize file opening which by itself requires kernel
|
|
+ * memory allocation and might result in a stall on memory stressed system.
|
|
+ */
|
|
+static int reread_file(struct reread_data *data, char *buf, size_t buf_size) {
|
|
+ ssize_t size;
|
|
+
|
|
+ if (data->fd == -1) {
|
|
+ data->fd = open(data->filename, O_RDONLY | O_CLOEXEC);
|
|
+ if (data->fd == -1) {
|
|
+ ALOGE("%s open: %s", data->filename, strerror(errno));
|
|
+ return -1;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ size = read_all(data->fd, buf, buf_size - 1);
|
|
+ if (size < 0) {
|
|
+ ALOGE("%s read: %s", data->filename, strerror(errno));
|
|
+ close(data->fd);
|
|
+ data->fd = -1;
|
|
+ return -1;
|
|
+ }
|
|
+ ALOG_ASSERT((size_t)size < buf_size - 1, "%s too large", data->filename);
|
|
+ buf[size] = 0;
|
|
+
|
|
+ return 0;
|
|
+}
|
|
+
|
|
static struct proc *pid_lookup(int pid) {
|
|
struct proc *procp;
|
|
|
|
@@ -226,77 +517,118 @@ static int pid_remove(int pid) {
|
|
return 0;
|
|
}
|
|
|
|
-static void writefilestring(char *path, char *s) {
|
|
+/*
|
|
+ * Write a string to a file.
|
|
+ * Returns false if the file does not exist.
|
|
+ */
|
|
+static bool writefilestring(const char *path, const char *s,
|
|
+ bool err_if_missing) {
|
|
int fd = open(path, O_WRONLY | O_CLOEXEC);
|
|
- int len = strlen(s);
|
|
- int ret;
|
|
+ ssize_t len = strlen(s);
|
|
+ ssize_t ret;
|
|
|
|
if (fd < 0) {
|
|
- ALOGE("Error opening %s; errno=%d", path, errno);
|
|
- return;
|
|
+ if (err_if_missing) {
|
|
+ ALOGE("Error opening %s; errno=%d", path, errno);
|
|
+ }
|
|
+ return false;
|
|
}
|
|
|
|
- ret = write(fd, s, len);
|
|
+ ret = TEMP_FAILURE_RETRY(write(fd, s, len));
|
|
if (ret < 0) {
|
|
ALOGE("Error writing %s; errno=%d", path, errno);
|
|
} else if (ret < len) {
|
|
- ALOGE("Short write on %s; length=%d", path, ret);
|
|
+ ALOGE("Short write on %s; length=%zd", path, ret);
|
|
}
|
|
|
|
close(fd);
|
|
+ return true;
|
|
}
|
|
|
|
-static void cmd_procprio(int pid, int uid, int oomadj) {
|
|
+static inline long get_time_diff_ms(struct timespec *from,
|
|
+ struct timespec *to) {
|
|
+ return (to->tv_sec - from->tv_sec) * (long)MS_PER_SEC +
|
|
+ (to->tv_nsec - from->tv_nsec) / (long)NS_PER_MS;
|
|
+}
|
|
+
|
|
+static void cmd_procprio(LMKD_CTRL_PACKET packet) {
|
|
struct proc *procp;
|
|
char path[80];
|
|
char val[20];
|
|
int soft_limit_mult;
|
|
+ struct lmk_procprio params;
|
|
+ bool is_system_server;
|
|
+ struct passwd *pwdrec;
|
|
|
|
- if (oomadj < OOM_SCORE_ADJ_MIN || oomadj > OOM_SCORE_ADJ_MAX) {
|
|
- ALOGE("Invalid PROCPRIO oomadj argument %d", oomadj);
|
|
+ lmkd_pack_get_procprio(packet, ¶ms);
|
|
+
|
|
+ if (params.oomadj < OOM_SCORE_ADJ_MIN ||
|
|
+ params.oomadj > OOM_SCORE_ADJ_MAX) {
|
|
+ ALOGE("Invalid PROCPRIO oomadj argument %d", params.oomadj);
|
|
return;
|
|
}
|
|
|
|
- snprintf(path, sizeof(path), "/proc/%d/oom_score_adj", pid);
|
|
- snprintf(val, sizeof(val), "%d", oomadj);
|
|
- writefilestring(path, val);
|
|
-
|
|
- if (use_inkernel_interface)
|
|
+ /* gid containing AID_READPROC required */
|
|
+ /* CAP_SYS_RESOURCE required */
|
|
+ /* CAP_DAC_OVERRIDE required */
|
|
+ snprintf(path, sizeof(path), "/proc/%d/oom_score_adj", params.pid);
|
|
+ snprintf(val, sizeof(val), "%d", params.oomadj);
|
|
+ if (!writefilestring(path, val, false)) {
|
|
+ ALOGW("Failed to open %s; errno=%d: process %d might have been killed",
|
|
+ path, errno, params.pid);
|
|
+ /* If this file does not exist the process is dead. */
|
|
return;
|
|
+ }
|
|
|
|
- if (oomadj >= 900) {
|
|
- soft_limit_mult = 0;
|
|
- } else if (oomadj >= 800) {
|
|
- soft_limit_mult = 0;
|
|
- } else if (oomadj >= 700) {
|
|
- soft_limit_mult = 0;
|
|
- } else if (oomadj >= 600) {
|
|
- // Launcher should be perceptible, don't kill it.
|
|
- oomadj = 200;
|
|
- soft_limit_mult = 1;
|
|
- } else if (oomadj >= 500) {
|
|
- soft_limit_mult = 0;
|
|
- } else if (oomadj >= 400) {
|
|
- soft_limit_mult = 0;
|
|
- } else if (oomadj >= 300) {
|
|
- soft_limit_mult = 1;
|
|
- } else if (oomadj >= 200) {
|
|
- soft_limit_mult = 2;
|
|
- } else if (oomadj >= 100) {
|
|
- soft_limit_mult = 10;
|
|
- } else if (oomadj >= 0) {
|
|
- soft_limit_mult = 20;
|
|
- } else {
|
|
- // Persistent processes will have a large
|
|
- // soft limit 512MB.
|
|
- soft_limit_mult = 64;
|
|
+ if (use_inkernel_interface) {
|
|
+ return;
|
|
}
|
|
|
|
- snprintf(path, sizeof(path), "/dev/memcg/apps/uid_%d/pid_%d/memory.soft_limit_in_bytes", uid, pid);
|
|
- snprintf(val, sizeof(val), "%d", soft_limit_mult * EIGHT_MEGA);
|
|
- writefilestring(path, val);
|
|
+ if (per_app_memcg) {
|
|
+ if (params.oomadj >= 900) {
|
|
+ soft_limit_mult = 0;
|
|
+ } else if (params.oomadj >= 800) {
|
|
+ soft_limit_mult = 0;
|
|
+ } else if (params.oomadj >= 700) {
|
|
+ soft_limit_mult = 0;
|
|
+ } else if (params.oomadj >= 600) {
|
|
+ // Launcher should be perceptible, don't kill it.
|
|
+ params.oomadj = 200;
|
|
+ soft_limit_mult = 1;
|
|
+ } else if (params.oomadj >= 500) {
|
|
+ soft_limit_mult = 0;
|
|
+ } else if (params.oomadj >= 400) {
|
|
+ soft_limit_mult = 0;
|
|
+ } else if (params.oomadj >= 300) {
|
|
+ soft_limit_mult = 1;
|
|
+ } else if (params.oomadj >= 200) {
|
|
+ soft_limit_mult = 8;
|
|
+ } else if (params.oomadj >= 100) {
|
|
+ soft_limit_mult = 10;
|
|
+ } else if (params.oomadj >= 0) {
|
|
+ soft_limit_mult = 20;
|
|
+ } else {
|
|
+ // Persistent processes will have a large
|
|
+ // soft limit 512MB.
|
|
+ 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);
|
|
+ snprintf(val, sizeof(val), "%d", soft_limit_mult * EIGHT_MEGA);
|
|
+
|
|
+ /*
|
|
+ * system_server process has no memcg under /dev/memcg/apps but should be
|
|
+ * registered with lmkd. This is the best way so far to identify it.
|
|
+ */
|
|
+ is_system_server = (params.oomadj == SYSTEM_ADJ &&
|
|
+ (pwdrec = getpwnam("system")) != NULL &&
|
|
+ params.uid == pwdrec->pw_uid);
|
|
+ writefilestring(path, val, !is_system_server);
|
|
+ }
|
|
|
|
- procp = pid_lookup(pid);
|
|
+ procp = pid_lookup(params.pid);
|
|
if (!procp) {
|
|
procp = malloc(sizeof(struct proc));
|
|
if (!procp) {
|
|
@@ -304,37 +636,167 @@ static void cmd_procprio(int pid, int uid, int oomadj) {
|
|
return;
|
|
}
|
|
|
|
- procp->pid = pid;
|
|
- procp->uid = uid;
|
|
- procp->oomadj = oomadj;
|
|
+ procp->pid = params.pid;
|
|
+ procp->uid = params.uid;
|
|
+ procp->oomadj = params.oomadj;
|
|
proc_insert(procp);
|
|
} else {
|
|
proc_unslot(procp);
|
|
- procp->oomadj = oomadj;
|
|
+ procp->oomadj = params.oomadj;
|
|
proc_slot(procp);
|
|
}
|
|
}
|
|
|
|
-static void cmd_procremove(int pid) {
|
|
- if (use_inkernel_interface)
|
|
+static void cmd_procremove(LMKD_CTRL_PACKET packet) {
|
|
+ struct lmk_procremove params;
|
|
+
|
|
+ if (use_inkernel_interface) {
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ lmkd_pack_get_procremove(packet, ¶ms);
|
|
+ /*
|
|
+ * WARNING: After pid_remove() procp is freed and can't be used!
|
|
+ * Therefore placed at the end of the function.
|
|
+ */
|
|
+ pid_remove(params.pid);
|
|
+}
|
|
+
|
|
+static void cmd_procpurge() {
|
|
+ int i;
|
|
+ struct proc *procp;
|
|
+ struct proc *next;
|
|
+
|
|
+ if (use_inkernel_interface) {
|
|
return;
|
|
+ }
|
|
|
|
- pid_remove(pid);
|
|
+ for (i = 0; i <= ADJTOSLOT(OOM_SCORE_ADJ_MAX); i++) {
|
|
+ procadjslot_list[i].next = &procadjslot_list[i];
|
|
+ procadjslot_list[i].prev = &procadjslot_list[i];
|
|
+ }
|
|
+
|
|
+ for (i = 0; i < PIDHASH_SZ; i++) {
|
|
+ procp = pidhash[i];
|
|
+ while (procp) {
|
|
+ next = procp->pidhash_next;
|
|
+ free(procp);
|
|
+ procp = next;
|
|
+ }
|
|
+ }
|
|
+ memset(&pidhash[0], 0, sizeof(pidhash));
|
|
+}
|
|
+
|
|
+static void inc_killcnt(int oomadj) {
|
|
+ int slot = ADJTOSLOT(oomadj);
|
|
+ uint8_t idx = killcnt_idx[slot];
|
|
+
|
|
+ if (idx == KILLCNT_INVALID_IDX) {
|
|
+ /* index is not assigned for this oomadj */
|
|
+ if (killcnt_free_idx < MAX_DISTINCT_OOM_ADJ) {
|
|
+ killcnt_idx[slot] = killcnt_free_idx;
|
|
+ killcnt[killcnt_free_idx] = 1;
|
|
+ killcnt_free_idx++;
|
|
+ } else {
|
|
+ ALOGW("Number of distinct oomadj levels exceeds %d",
|
|
+ MAX_DISTINCT_OOM_ADJ);
|
|
+ }
|
|
+ } else {
|
|
+ /*
|
|
+ * wraparound is highly unlikely and is detectable using total
|
|
+ * counter because it has to be equal to the sum of all counters
|
|
+ */
|
|
+ killcnt[idx]++;
|
|
+ }
|
|
+ /* increment total kill counter */
|
|
+ killcnt_total++;
|
|
+}
|
|
+
|
|
+static int get_killcnt(int min_oomadj, int max_oomadj) {
|
|
+ int slot;
|
|
+ int count = 0;
|
|
+
|
|
+ if (min_oomadj > max_oomadj)
|
|
+ return 0;
|
|
+
|
|
+ /* special case to get total kill count */
|
|
+ if (min_oomadj > OOM_SCORE_ADJ_MAX)
|
|
+ return killcnt_total;
|
|
+
|
|
+ while (min_oomadj <= max_oomadj &&
|
|
+ (slot = ADJTOSLOT(min_oomadj)) < ADJTOSLOT_COUNT) {
|
|
+ uint8_t idx = killcnt_idx[slot];
|
|
+ if (idx != KILLCNT_INVALID_IDX) {
|
|
+ count += killcnt[idx];
|
|
+ }
|
|
+ min_oomadj++;
|
|
+ }
|
|
+
|
|
+ return count;
|
|
+}
|
|
+
|
|
+static int cmd_getkillcnt(LMKD_CTRL_PACKET packet) {
|
|
+ struct lmk_getkillcnt params;
|
|
+
|
|
+ if (use_inkernel_interface) {
|
|
+ /* kernel driver does not expose this information */
|
|
+ return 0;
|
|
+ }
|
|
+
|
|
+ lmkd_pack_get_getkillcnt(packet, ¶ms);
|
|
+
|
|
+ return get_killcnt(params.min_oomadj, params.max_oomadj);
|
|
}
|
|
|
|
-static void cmd_target(int ntargets, int *params) {
|
|
+static void cmd_target(int ntargets, LMKD_CTRL_PACKET packet) {
|
|
int i;
|
|
+ struct lmk_target target;
|
|
+ char minfree_str[PROPERTY_VALUE_MAX];
|
|
+ char *pstr = minfree_str;
|
|
+ char *pend = minfree_str + sizeof(minfree_str);
|
|
+ static struct timespec last_req_tm;
|
|
+ struct timespec curr_tm;
|
|
+
|
|
+ if (ntargets < 1 || ntargets > (int)ARRAY_SIZE(lowmem_adj))
|
|
+ return;
|
|
|
|
- if (ntargets > (int)ARRAY_SIZE(lowmem_adj))
|
|
+ /*
|
|
+ * Ratelimit minfree updates to once per TARGET_UPDATE_MIN_INTERVAL_MS
|
|
+ * to prevent DoS attacks
|
|
+ */
|
|
+ if (clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm) != 0) {
|
|
+ ALOGE("Failed to get current time");
|
|
return;
|
|
+ }
|
|
+
|
|
+ if (get_time_diff_ms(&last_req_tm, &curr_tm) <
|
|
+ TARGET_UPDATE_MIN_INTERVAL_MS) {
|
|
+ ALOGE("Ignoring frequent updated to lmkd limits");
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ last_req_tm = curr_tm;
|
|
|
|
for (i = 0; i < ntargets; i++) {
|
|
- lowmem_minfree[i] = ntohl(*params++);
|
|
- lowmem_adj[i] = ntohl(*params++);
|
|
+ lmkd_pack_get_target(packet, i, &target);
|
|
+ lowmem_minfree[i] = target.minfree;
|
|
+ lowmem_adj[i] = target.oom_adj_score;
|
|
+
|
|
+ pstr += snprintf(pstr, pend - pstr, "%d:%d,", target.minfree,
|
|
+ target.oom_adj_score);
|
|
+ if (pstr >= pend) {
|
|
+ /* if no more space in the buffer then terminate the loop */
|
|
+ pstr = pend;
|
|
+ break;
|
|
+ }
|
|
}
|
|
|
|
lowmem_targets_size = ntargets;
|
|
|
|
+ /* Override the last extra comma */
|
|
+ pstr[-1] = '\0';
|
|
+ property_set("sys.lmk.minfree_levels", minfree_str);
|
|
+
|
|
if (has_inkernel_module) {
|
|
char minfreestr[128];
|
|
char killpriostr[128];
|
|
@@ -356,22 +818,29 @@ static void cmd_target(int ntargets, int *params) {
|
|
strlcat(killpriostr, val, sizeof(killpriostr));
|
|
}
|
|
|
|
- writefilestring(INKERNEL_MINFREE_PATH, minfreestr);
|
|
- writefilestring(INKERNEL_ADJ_PATH, killpriostr);
|
|
+ writefilestring(INKERNEL_MINFREE_PATH, minfreestr, true);
|
|
+ writefilestring(INKERNEL_ADJ_PATH, killpriostr, true);
|
|
}
|
|
}
|
|
|
|
-static void ctrl_data_close(void) {
|
|
- ALOGI("Closing Activity Manager data connection");
|
|
- close(ctrl_dfd);
|
|
- ctrl_dfd = -1;
|
|
+static void ctrl_data_close(int dsock_idx) {
|
|
+ struct epoll_event epev;
|
|
+
|
|
+ ALOGI("closing lmkd data connection");
|
|
+ if (epoll_ctl(epollfd, EPOLL_CTL_DEL, data_sock[dsock_idx].sock, &epev) == -1) {
|
|
+ // Log a warning and keep going
|
|
+ ALOGW("epoll_ctl for data connection socket failed; errno=%d", errno);
|
|
+ }
|
|
maxevents--;
|
|
+
|
|
+ close(data_sock[dsock_idx].sock);
|
|
+ data_sock[dsock_idx].sock = -1;
|
|
}
|
|
|
|
-static int ctrl_data_read(char *buf, size_t bufsz) {
|
|
+static int ctrl_data_read(int dsock_idx, char *buf, size_t bufsz) {
|
|
int ret = 0;
|
|
|
|
- ret = read(ctrl_dfd, buf, bufsz);
|
|
+ ret = TEMP_FAILURE_RETRY(read(data_sock[dsock_idx].sock, buf, bufsz));
|
|
|
|
if (ret == -1) {
|
|
ALOGE("control data socket read failed; errno=%d", errno);
|
|
@@ -383,39 +852,72 @@ static int ctrl_data_read(char *buf, size_t bufsz) {
|
|
return ret;
|
|
}
|
|
|
|
-static void ctrl_command_handler(void) {
|
|
- int ibuf[CTRL_PACKET_MAX / sizeof(int)];
|
|
+static int ctrl_data_write(int dsock_idx, char *buf, size_t bufsz) {
|
|
+ int ret = 0;
|
|
+
|
|
+ ret = TEMP_FAILURE_RETRY(write(data_sock[dsock_idx].sock, buf, bufsz));
|
|
+
|
|
+ if (ret == -1) {
|
|
+ ALOGE("control data socket write failed; errno=%d", errno);
|
|
+ } else if (ret == 0) {
|
|
+ ALOGE("Got EOF on control data socket");
|
|
+ ret = -1;
|
|
+ }
|
|
+
|
|
+ return ret;
|
|
+}
|
|
+
|
|
+static void ctrl_command_handler(int dsock_idx) {
|
|
+ LMKD_CTRL_PACKET packet;
|
|
int len;
|
|
- int cmd = -1;
|
|
+ enum lmk_cmd cmd;
|
|
int nargs;
|
|
int targets;
|
|
+ int kill_cnt;
|
|
|
|
- len = ctrl_data_read((char *)ibuf, CTRL_PACKET_MAX);
|
|
+ len = ctrl_data_read(dsock_idx, (char *)packet, CTRL_PACKET_MAX_SIZE);
|
|
if (len <= 0)
|
|
return;
|
|
|
|
+ if (len < (int)sizeof(int)) {
|
|
+ ALOGE("Wrong control socket read length len=%d", len);
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ cmd = lmkd_pack_get_cmd(packet);
|
|
nargs = len / sizeof(int) - 1;
|
|
if (nargs < 0)
|
|
goto wronglen;
|
|
|
|
- cmd = ntohl(ibuf[0]);
|
|
-
|
|
switch(cmd) {
|
|
case LMK_TARGET:
|
|
targets = nargs / 2;
|
|
if (nargs & 0x1 || targets > (int)ARRAY_SIZE(lowmem_adj))
|
|
goto wronglen;
|
|
- cmd_target(targets, &ibuf[1]);
|
|
+ cmd_target(targets, packet);
|
|
break;
|
|
case LMK_PROCPRIO:
|
|
if (nargs != 3)
|
|
goto wronglen;
|
|
- cmd_procprio(ntohl(ibuf[1]), ntohl(ibuf[2]), ntohl(ibuf[3]));
|
|
+ cmd_procprio(packet);
|
|
break;
|
|
case LMK_PROCREMOVE:
|
|
if (nargs != 1)
|
|
goto wronglen;
|
|
- cmd_procremove(ntohl(ibuf[1]));
|
|
+ cmd_procremove(packet);
|
|
+ break;
|
|
+ case LMK_PROCPURGE:
|
|
+ if (nargs != 0)
|
|
+ goto wronglen;
|
|
+ cmd_procpurge();
|
|
+ break;
|
|
+ case LMK_GETKILLCNT:
|
|
+ if (nargs != 2)
|
|
+ goto wronglen;
|
|
+ kill_cnt = cmd_getkillcnt(packet);
|
|
+ len = lmkd_pack_set_getkillcnt_repl(packet, kill_cnt);
|
|
+ if (ctrl_data_write(dsock_idx, (char *)packet, len) != len)
|
|
+ return;
|
|
break;
|
|
default:
|
|
ALOGE("Received unknown command code %d", cmd);
|
|
@@ -428,112 +930,290 @@ wronglen:
|
|
ALOGE("Wrong control socket read length cmd=%d len=%d", cmd, len);
|
|
}
|
|
|
|
-static void ctrl_data_handler(uint32_t events) {
|
|
- if (events & EPOLLHUP) {
|
|
- ALOGI("ActivityManager disconnected");
|
|
- if (!ctrl_dfd_reopened)
|
|
- ctrl_data_close();
|
|
- } else if (events & EPOLLIN) {
|
|
- ctrl_command_handler();
|
|
+static void ctrl_data_handler(int data, uint32_t events) {
|
|
+ if (events & EPOLLIN) {
|
|
+ ctrl_command_handler(data);
|
|
}
|
|
}
|
|
|
|
-static void ctrl_connect_handler(uint32_t events __unused) {
|
|
- struct epoll_event epev;
|
|
-
|
|
- if (ctrl_dfd >= 0) {
|
|
- ctrl_data_close();
|
|
- ctrl_dfd_reopened = 1;
|
|
+static int get_free_dsock() {
|
|
+ for (int i = 0; i < MAX_DATA_CONN; i++) {
|
|
+ if (data_sock[i].sock < 0) {
|
|
+ return i;
|
|
+ }
|
|
}
|
|
+ return -1;
|
|
+}
|
|
|
|
- ctrl_dfd = accept(ctrl_lfd, NULL, NULL);
|
|
+static void ctrl_connect_handler(int data __unused, uint32_t events __unused) {
|
|
+ struct epoll_event epev;
|
|
+ int free_dscock_idx = get_free_dsock();
|
|
+
|
|
+ if (free_dscock_idx < 0) {
|
|
+ /*
|
|
+ * Number of data connections exceeded max supported. This should not
|
|
+ * happen but if it does we drop all existing connections and accept
|
|
+ * the new one. This prevents inactive connections from monopolizing
|
|
+ * data socket and if we drop ActivityManager connection it will
|
|
+ * immediately reconnect.
|
|
+ */
|
|
+ for (int i = 0; i < MAX_DATA_CONN; i++) {
|
|
+ ctrl_data_close(i);
|
|
+ }
|
|
+ free_dscock_idx = 0;
|
|
+ }
|
|
|
|
- if (ctrl_dfd < 0) {
|
|
+ data_sock[free_dscock_idx].sock = accept(ctrl_sock.sock, NULL, NULL);
|
|
+ if (data_sock[free_dscock_idx].sock < 0) {
|
|
ALOGE("lmkd control socket accept failed; errno=%d", errno);
|
|
return;
|
|
}
|
|
|
|
- ALOGI("ActivityManager connected");
|
|
- maxevents++;
|
|
+ ALOGI("lmkd data connection established");
|
|
+ /* use data to store data connection idx */
|
|
+ data_sock[free_dscock_idx].handler_info.data = free_dscock_idx;
|
|
+ data_sock[free_dscock_idx].handler_info.handler = ctrl_data_handler;
|
|
epev.events = EPOLLIN;
|
|
- epev.data.ptr = (void *)ctrl_data_handler;
|
|
- if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_dfd, &epev) == -1) {
|
|
+ epev.data.ptr = (void *)&(data_sock[free_dscock_idx].handler_info);
|
|
+ if (epoll_ctl(epollfd, EPOLL_CTL_ADD, data_sock[free_dscock_idx].sock, &epev) == -1) {
|
|
ALOGE("epoll_ctl for data connection socket failed; errno=%d", errno);
|
|
- ctrl_data_close();
|
|
+ ctrl_data_close(free_dscock_idx);
|
|
+ return;
|
|
+ }
|
|
+ maxevents++;
|
|
+}
|
|
+
|
|
+#ifdef LMKD_LOG_STATS
|
|
+static void memory_stat_parse_line(char* line, struct memory_stat* mem_st) {
|
|
+ char key[LINE_MAX + 1];
|
|
+ int64_t value;
|
|
+
|
|
+ sscanf(line, "%" STRINGIFY(LINE_MAX) "s %" SCNd64 "", key, &value);
|
|
+
|
|
+ if (strcmp(key, "total_") < 0) {
|
|
return;
|
|
}
|
|
+
|
|
+ if (!strcmp(key, "total_pgfault"))
|
|
+ mem_st->pgfault = value;
|
|
+ else if (!strcmp(key, "total_pgmajfault"))
|
|
+ mem_st->pgmajfault = value;
|
|
+ else if (!strcmp(key, "total_rss"))
|
|
+ mem_st->rss_in_bytes = value;
|
|
+ else if (!strcmp(key, "total_cache"))
|
|
+ mem_st->cache_in_bytes = value;
|
|
+ else if (!strcmp(key, "total_swap"))
|
|
+ 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];
|
|
+
|
|
+ snprintf(buf, sizeof(buf), MEMCG_PROCESS_MEMORY_STAT_PATH, uid, pid);
|
|
+
|
|
+ fp = fopen(buf, "r");
|
|
+
|
|
+ if (fp == NULL) {
|
|
+ ALOGE("%s open failed: %s", buf, strerror(errno));
|
|
+ return -1;
|
|
+ }
|
|
+
|
|
+ while (fgets(buf, PAGE_SIZE, fp) != NULL) {
|
|
+ memory_stat_parse_line(buf, mem_st);
|
|
+ }
|
|
+ fclose(fp);
|
|
+
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+static int memory_stat_from_procfs(struct memory_stat* mem_st, int pid) {
|
|
+ char path[PATH_MAX];
|
|
+ char buffer[PROC_STAT_BUFFER_SIZE];
|
|
+ int fd, ret;
|
|
+
|
|
+ snprintf(path, sizeof(path), PROC_STAT_FILE_PATH, pid);
|
|
+ if ((fd = open(path, O_RDONLY | O_CLOEXEC)) < 0) {
|
|
+ ALOGE("%s open failed: %s", path, strerror(errno));
|
|
+ return -1;
|
|
+ }
|
|
+
|
|
+ ret = read(fd, buffer, sizeof(buffer));
|
|
+ if (ret < 0) {
|
|
+ ALOGE("%s read failed: %s", path, strerror(errno));
|
|
+ close(fd);
|
|
+ return -1;
|
|
+ }
|
|
+ close(fd);
|
|
+
|
|
+ // field 10 is pgfault
|
|
+ // field 12 is pgmajfault
|
|
+ // field 22 is starttime
|
|
+ // field 24 is rss_in_pages
|
|
+ int64_t pgfault = 0, pgmajfault = 0, starttime = 0, rss_in_pages = 0;
|
|
+ if (sscanf(buffer,
|
|
+ "%*u %*s %*s %*d %*d %*d %*d %*d %*d %" SCNd64 " %*d "
|
|
+ "%" SCNd64 " %*d %*u %*u %*d %*d %*d %*d %*d %*d "
|
|
+ "%" SCNd64 " %*d %" SCNd64 "",
|
|
+ &pgfault, &pgmajfault, &starttime, &rss_in_pages) != 4) {
|
|
+ return -1;
|
|
+ }
|
|
+ mem_st->pgfault = pgfault;
|
|
+ mem_st->pgmajfault = pgmajfault;
|
|
+ mem_st->rss_in_bytes = (rss_in_pages * PAGE_SIZE);
|
|
+ mem_st->process_start_time_ns = starttime * (NS_PER_SEC / sysconf(_SC_CLK_TCK));
|
|
+ return 0;
|
|
}
|
|
+#endif
|
|
|
|
-static int zoneinfo_parse_protection(char *cp) {
|
|
- int max = 0;
|
|
- int zoneval;
|
|
+/* /prop/zoneinfo parsing routines */
|
|
+static int64_t zoneinfo_parse_protection(char *cp) {
|
|
+ int64_t max = 0;
|
|
+ long long zoneval;
|
|
char *save_ptr;
|
|
|
|
- for (cp = strtok_r(cp, "(), ", &save_ptr); cp; cp = strtok_r(NULL, "), ", &save_ptr)) {
|
|
- zoneval = strtol(cp, &cp, 0);
|
|
- if (zoneval > max)
|
|
- max = zoneval;
|
|
+ for (cp = strtok_r(cp, "(), ", &save_ptr); cp;
|
|
+ cp = strtok_r(NULL, "), ", &save_ptr)) {
|
|
+ zoneval = strtoll(cp, &cp, 0);
|
|
+ if (zoneval > max) {
|
|
+ max = (zoneval > INT64_MAX) ? INT64_MAX : zoneval;
|
|
+ }
|
|
}
|
|
|
|
return max;
|
|
}
|
|
|
|
-static void zoneinfo_parse_line(char *line, struct sysmeminfo *mip) {
|
|
+static bool zoneinfo_parse_line(char *line, union zoneinfo *zi) {
|
|
char *cp = line;
|
|
char *ap;
|
|
char *save_ptr;
|
|
+ int64_t val;
|
|
+ int field_idx;
|
|
|
|
cp = strtok_r(line, " ", &save_ptr);
|
|
- if (!cp)
|
|
- return;
|
|
+ if (!cp) {
|
|
+ return true;
|
|
+ }
|
|
|
|
- ap = strtok_r(NULL, " ", &save_ptr);
|
|
- if (!ap)
|
|
- return;
|
|
+ if (!strcmp(cp, "protection:")) {
|
|
+ ap = strtok_r(NULL, ")", &save_ptr);
|
|
+ } else {
|
|
+ ap = strtok_r(NULL, " ", &save_ptr);
|
|
+ }
|
|
|
|
- if (!strcmp(cp, "nr_free_pages"))
|
|
- mip->nr_free_pages += strtol(ap, NULL, 0);
|
|
- else if (!strcmp(cp, "nr_file_pages"))
|
|
- mip->nr_file_pages += strtol(ap, NULL, 0);
|
|
- else if (!strcmp(cp, "nr_shmem"))
|
|
- mip->nr_shmem += strtol(ap, NULL, 0);
|
|
- else if (!strcmp(cp, "high"))
|
|
- mip->totalreserve_pages += strtol(ap, NULL, 0);
|
|
- else if (!strcmp(cp, "protection:"))
|
|
- mip->totalreserve_pages += zoneinfo_parse_protection(ap);
|
|
+ if (!ap) {
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ switch (match_field(cp, ap, zoneinfo_field_names,
|
|
+ ZI_FIELD_COUNT, &val, &field_idx)) {
|
|
+ case (PARSE_SUCCESS):
|
|
+ zi->arr[field_idx] += val;
|
|
+ break;
|
|
+ case (NO_MATCH):
|
|
+ if (!strcmp(cp, "protection:")) {
|
|
+ zi->field.totalreserve_pages +=
|
|
+ zoneinfo_parse_protection(ap);
|
|
+ }
|
|
+ break;
|
|
+ case (PARSE_FAIL):
|
|
+ default:
|
|
+ return false;
|
|
+ }
|
|
+ return true;
|
|
}
|
|
|
|
-static int zoneinfo_parse(struct sysmeminfo *mip) {
|
|
- int fd;
|
|
- ssize_t size;
|
|
+static int zoneinfo_parse(union zoneinfo *zi) {
|
|
+ static struct reread_data file_data = {
|
|
+ .filename = ZONEINFO_PATH,
|
|
+ .fd = -1,
|
|
+ };
|
|
char buf[PAGE_SIZE];
|
|
char *save_ptr;
|
|
char *line;
|
|
|
|
- memset(mip, 0, sizeof(struct sysmeminfo));
|
|
+ memset(zi, 0, sizeof(union zoneinfo));
|
|
|
|
- fd = open(ZONEINFO_PATH, O_RDONLY | O_CLOEXEC);
|
|
- if (fd == -1) {
|
|
- ALOGE("%s open: errno=%d", ZONEINFO_PATH, errno);
|
|
+ if (reread_file(&file_data, buf, sizeof(buf)) < 0) {
|
|
return -1;
|
|
}
|
|
|
|
- size = read_all(fd, buf, sizeof(buf) - 1);
|
|
- if (size < 0) {
|
|
- ALOGE("%s read: errno=%d", ZONEINFO_PATH, errno);
|
|
- close(fd);
|
|
+ for (line = strtok_r(buf, "\n", &save_ptr); line;
|
|
+ line = strtok_r(NULL, "\n", &save_ptr)) {
|
|
+ if (!zoneinfo_parse_line(line, zi)) {
|
|
+ ALOGE("%s parse error", file_data.filename);
|
|
+ return -1;
|
|
+ }
|
|
+ }
|
|
+ zi->field.totalreserve_pages += zi->field.high;
|
|
+
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+/* /prop/meminfo parsing routines */
|
|
+static bool meminfo_parse_line(char *line, union meminfo *mi) {
|
|
+ char *cp = line;
|
|
+ char *ap;
|
|
+ char *save_ptr;
|
|
+ int64_t val;
|
|
+ int field_idx;
|
|
+ enum field_match_result match_res;
|
|
+
|
|
+ cp = strtok_r(line, " ", &save_ptr);
|
|
+ if (!cp) {
|
|
+ return false;
|
|
+ }
|
|
+
|
|
+ ap = strtok_r(NULL, " ", &save_ptr);
|
|
+ if (!ap) {
|
|
+ return false;
|
|
+ }
|
|
+
|
|
+ match_res = match_field(cp, ap, meminfo_field_names, MI_FIELD_COUNT,
|
|
+ &val, &field_idx);
|
|
+ if (match_res == PARSE_SUCCESS) {
|
|
+ mi->arr[field_idx] = val / page_k;
|
|
+ }
|
|
+ return (match_res != PARSE_FAIL);
|
|
+}
|
|
+
|
|
+static int meminfo_parse(union meminfo *mi) {
|
|
+ static struct reread_data file_data = {
|
|
+ .filename = MEMINFO_PATH,
|
|
+ .fd = -1,
|
|
+ };
|
|
+ char buf[PAGE_SIZE];
|
|
+ char *save_ptr;
|
|
+ char *line;
|
|
+
|
|
+ memset(mi, 0, sizeof(union meminfo));
|
|
+
|
|
+ if (reread_file(&file_data, buf, sizeof(buf)) < 0) {
|
|
return -1;
|
|
}
|
|
- ALOG_ASSERT((size_t)size < sizeof(buf) - 1, "/proc/zoneinfo too large");
|
|
- buf[size] = 0;
|
|
|
|
- for (line = strtok_r(buf, "\n", &save_ptr); line; line = strtok_r(NULL, "\n", &save_ptr))
|
|
- zoneinfo_parse_line(line, mip);
|
|
+ for (line = strtok_r(buf, "\n", &save_ptr); line;
|
|
+ line = strtok_r(NULL, "\n", &save_ptr)) {
|
|
+ if (!meminfo_parse_line(line, mi)) {
|
|
+ ALOGE("%s parse error", file_data.filename);
|
|
+ return -1;
|
|
+ }
|
|
+ }
|
|
+ mi->field.nr_file_pages = mi->field.cached + mi->field.swap_cached +
|
|
+ mi->field.buffers;
|
|
|
|
- close(fd);
|
|
return 0;
|
|
}
|
|
|
|
+static void meminfo_log(union meminfo *mi) {
|
|
+ android_log_write_list_begin(ctx);
|
|
+ for (int field_idx = 0; field_idx < MI_FIELD_COUNT; field_idx++) {
|
|
+ android_log_write_int32(ctx, (int32_t)min(mi->arr[field_idx] * page_k, INT32_MAX));
|
|
+ }
|
|
+ android_log_write_list_end(ctx);
|
|
+ android_log_write_list(ctx, LOG_ID_EVENTS);
|
|
+}
|
|
+
|
|
static int proc_get_size(int pid) {
|
|
char path[PATH_MAX];
|
|
char line[LINE_MAX];
|
|
@@ -542,6 +1222,7 @@ static int proc_get_size(int pid) {
|
|
int total;
|
|
ssize_t ret;
|
|
|
|
+ /* gid containing AID_READPROC required */
|
|
snprintf(path, PATH_MAX, "/proc/%d/statm", pid);
|
|
fd = open(path, O_RDONLY | O_CLOEXEC);
|
|
if (fd == -1)
|
|
@@ -565,6 +1246,7 @@ static char *proc_get_name(int pid) {
|
|
char *cp;
|
|
ssize_t ret;
|
|
|
|
+ /* gid containing AID_READPROC required */
|
|
snprintf(path, PATH_MAX, "/proc/%d/cmdline", pid);
|
|
fd = open(path, O_RDONLY | O_CLOEXEC);
|
|
if (fd == -1)
|
|
@@ -586,87 +1268,207 @@ static struct proc *proc_adj_lru(int oomadj) {
|
|
return (struct proc *)adjslot_tail(&procadjslot_list[ADJTOSLOT(oomadj)]);
|
|
}
|
|
|
|
+static struct proc *proc_get_heaviest(int oomadj) {
|
|
+ struct adjslot_list *head = &procadjslot_list[ADJTOSLOT(oomadj)];
|
|
+ struct adjslot_list *curr = head->next;
|
|
+ struct proc *maxprocp = NULL;
|
|
+ int maxsize = 0;
|
|
+ while (curr != head) {
|
|
+ int pid = ((struct proc *)curr)->pid;
|
|
+ int tasksize = proc_get_size(pid);
|
|
+ if (tasksize <= 0) {
|
|
+ struct adjslot_list *next = curr->next;
|
|
+ pid_remove(pid);
|
|
+ curr = next;
|
|
+ } else {
|
|
+ if (tasksize > maxsize) {
|
|
+ maxsize = tasksize;
|
|
+ maxprocp = (struct proc *)curr;
|
|
+ }
|
|
+ curr = curr->next;
|
|
+ }
|
|
+ }
|
|
+ return maxprocp;
|
|
+}
|
|
+
|
|
+static void set_process_group_and_prio(int pid, SchedPolicy sp, int prio) {
|
|
+ DIR* d;
|
|
+ char proc_path[PATH_MAX];
|
|
+ struct dirent* de;
|
|
+
|
|
+ snprintf(proc_path, sizeof(proc_path), "/proc/%d/task", pid);
|
|
+ if (!(d = opendir(proc_path))) {
|
|
+ ALOGW("Failed to open %s; errno=%d: process pid(%d) might have died", proc_path, errno,
|
|
+ pid);
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ while ((de = readdir(d))) {
|
|
+ int t_pid;
|
|
+
|
|
+ if (de->d_name[0] == '.') continue;
|
|
+ t_pid = atoi(de->d_name);
|
|
+
|
|
+ if (!t_pid) {
|
|
+ ALOGW("Failed to get t_pid for '%s' of pid(%d)", de->d_name, pid);
|
|
+ continue;
|
|
+ }
|
|
+
|
|
+ if (setpriority(PRIO_PROCESS, t_pid, prio) && errno != ESRCH) {
|
|
+ ALOGW("Unable to raise priority of killing t_pid (%d): errno=%d", t_pid, errno);
|
|
+ }
|
|
+
|
|
+ if (set_cpuset_policy(t_pid, sp)) {
|
|
+ ALOGW("Failed to set_cpuset_policy on pid(%d) t_pid(%d) to %d", pid, t_pid, (int)sp);
|
|
+ continue;
|
|
+ }
|
|
+ }
|
|
+ closedir(d);
|
|
+}
|
|
+
|
|
+static int last_killed_pid = -1;
|
|
+
|
|
/* Kill one process specified by procp. Returns the size of the process killed */
|
|
-static int kill_one_process(struct proc* procp, int min_score_adj, bool is_critical) {
|
|
+static int kill_one_process(struct proc* procp, int min_oom_score) {
|
|
int pid = procp->pid;
|
|
uid_t uid = procp->uid;
|
|
char *taskname;
|
|
int tasksize;
|
|
int r;
|
|
+ int result = -1;
|
|
+
|
|
+#ifdef LMKD_LOG_STATS
|
|
+ struct memory_stat mem_st = {};
|
|
+ int memory_stat_parse_result = -1;
|
|
+#else
|
|
+ /* To prevent unused parameter warning */
|
|
+ (void)(min_oom_score);
|
|
+#endif
|
|
|
|
taskname = proc_get_name(pid);
|
|
if (!taskname) {
|
|
- pid_remove(pid);
|
|
- return -1;
|
|
+ goto out;
|
|
}
|
|
|
|
tasksize = proc_get_size(pid);
|
|
if (tasksize <= 0) {
|
|
- pid_remove(pid);
|
|
- return -1;
|
|
+ goto out;
|
|
+ }
|
|
+
|
|
+#ifdef LMKD_LOG_STATS
|
|
+ if (enable_stats_log) {
|
|
+ if (per_app_memcg) {
|
|
+ memory_stat_parse_result = memory_stat_from_cgroup(&mem_st, pid, uid);
|
|
+ } else {
|
|
+ memory_stat_parse_result = memory_stat_from_procfs(&mem_st, pid);
|
|
+ }
|
|
}
|
|
+#endif
|
|
+
|
|
+ TRACE_KILL_START(pid);
|
|
|
|
- ALOGI(
|
|
- "Killing '%s' (%d), uid %d, adj %d\n"
|
|
- " to free %ldkB because system is under %s memory pressure oom_adj %d\n",
|
|
- taskname, pid, uid, procp->oomadj, tasksize * page_k, is_critical ? "critical" : "medium",
|
|
- min_score_adj);
|
|
+ /* CAP_KILL required */
|
|
r = kill(pid, SIGKILL);
|
|
- pid_remove(pid);
|
|
+
|
|
+ set_process_group_and_prio(pid, SP_FOREGROUND, ANDROID_PRIORITY_HIGHEST);
|
|
+
|
|
+ inc_killcnt(procp->oomadj);
|
|
+ ALOGE("Kill '%s' (%d), uid %d, oom_adj %d to free %ldkB", taskname, pid, uid, procp->oomadj,
|
|
+ tasksize * page_k);
|
|
+
|
|
+ TRACE_KILL_END();
|
|
+
|
|
+ last_killed_pid = pid;
|
|
|
|
if (r) {
|
|
- ALOGE("kill(%d): errno=%d", procp->pid, errno);
|
|
- return -1;
|
|
+ ALOGE("kill(%d): errno=%d", pid, errno);
|
|
+ goto out;
|
|
} else {
|
|
- return tasksize;
|
|
+#ifdef LMKD_LOG_STATS
|
|
+ if (memory_stat_parse_result == 0) {
|
|
+ stats_write_lmk_kill_occurred(log_ctx, LMK_KILL_OCCURRED, uid, taskname,
|
|
+ procp->oomadj, mem_st.pgfault, mem_st.pgmajfault, mem_st.rss_in_bytes,
|
|
+ mem_st.cache_in_bytes, mem_st.swap_in_bytes, mem_st.process_start_time_ns,
|
|
+ min_oom_score);
|
|
+ } else if (enable_stats_log) {
|
|
+ stats_write_lmk_kill_occurred(log_ctx, LMK_KILL_OCCURRED, uid, taskname, procp->oomadj,
|
|
+ -1, -1, tasksize * BYTES_IN_KILOBYTE, -1, -1, -1,
|
|
+ min_oom_score);
|
|
+ }
|
|
+#endif
|
|
+ result = tasksize;
|
|
}
|
|
+
|
|
+out:
|
|
+ /*
|
|
+ * WARNING: After pid_remove() procp is freed and can't be used!
|
|
+ * Therefore placed at the end of the function.
|
|
+ */
|
|
+ pid_remove(pid);
|
|
+ return result;
|
|
}
|
|
|
|
/*
|
|
- * Find a process to kill based on the current (possibly estimated) free memory
|
|
- * and cached memory sizes. Returns the size of the killed processes.
|
|
+ * Find one process to kill at or above the given oom_adj level.
|
|
+ * Returns size of the killed process.
|
|
*/
|
|
-static int find_and_kill_process(bool is_critical) {
|
|
+static int find_and_kill_process(int min_score_adj) {
|
|
int i;
|
|
int killed_size = 0;
|
|
- int min_score_adj = is_critical ? critical_oomadj : medium_oomadj;
|
|
+
|
|
+#ifdef LMKD_LOG_STATS
|
|
+ bool lmk_state_change_start = false;
|
|
+#endif
|
|
|
|
for (i = OOM_SCORE_ADJ_MAX; i >= min_score_adj; i--) {
|
|
struct proc *procp;
|
|
|
|
-retry:
|
|
- procp = proc_adj_lru(i);
|
|
-
|
|
- if (procp) {
|
|
- killed_size = kill_one_process(procp, min_score_adj, is_critical);
|
|
- if (killed_size < 0) {
|
|
- goto retry;
|
|
- } else {
|
|
- return killed_size;
|
|
+ while (true) {
|
|
+ procp = kill_heaviest_task ?
|
|
+ proc_get_heaviest(i) : proc_adj_lru(i);
|
|
+
|
|
+ if (!procp)
|
|
+ break;
|
|
+
|
|
+ killed_size = kill_one_process(procp, min_score_adj);
|
|
+ if (killed_size >= 0) {
|
|
+#ifdef LMKD_LOG_STATS
|
|
+ if (enable_stats_log && !lmk_state_change_start) {
|
|
+ lmk_state_change_start = true;
|
|
+ stats_write_lmk_state_changed(log_ctx, LMK_STATE_CHANGED,
|
|
+ LMK_STATE_CHANGE_START);
|
|
+ }
|
|
+#endif
|
|
+ break;
|
|
}
|
|
}
|
|
+ if (killed_size) {
|
|
+ break;
|
|
+ }
|
|
}
|
|
|
|
- return 0;
|
|
+#ifdef LMKD_LOG_STATS
|
|
+ if (enable_stats_log && lmk_state_change_start) {
|
|
+ stats_write_lmk_state_changed(log_ctx, LMK_STATE_CHANGED, LMK_STATE_CHANGE_STOP);
|
|
+ }
|
|
+#endif
|
|
+
|
|
+ return killed_size;
|
|
}
|
|
|
|
-static int64_t get_memory_usage(const char* path) {
|
|
+static int64_t get_memory_usage(struct reread_data *file_data) {
|
|
int ret;
|
|
int64_t mem_usage;
|
|
char buf[32];
|
|
- int fd = open(path, O_RDONLY | O_CLOEXEC);
|
|
- if (fd == -1) {
|
|
- ALOGE("%s open: errno=%d", path, errno);
|
|
+
|
|
+ if (reread_file(file_data, buf, sizeof(buf)) < 0) {
|
|
return -1;
|
|
}
|
|
|
|
- ret = read_all(fd, buf, sizeof(buf) - 1);
|
|
- close(fd);
|
|
- if (ret < 0) {
|
|
- ALOGE("%s error: errno=%d", path, errno);
|
|
+ if (!parse_int64(buf, &mem_usage)) {
|
|
+ ALOGE("%s parse error", file_data->filename);
|
|
return -1;
|
|
}
|
|
- sscanf(buf, "%" SCNd64, &mem_usage);
|
|
if (mem_usage == 0) {
|
|
ALOGE("No memory!");
|
|
return -1;
|
|
@@ -674,76 +1476,337 @@ static int64_t get_memory_usage(const char* path) {
|
|
return mem_usage;
|
|
}
|
|
|
|
-static void mp_event_common(bool is_critical) {
|
|
+void record_low_pressure_levels(union meminfo *mi) {
|
|
+ if (low_pressure_mem.min_nr_free_pages == -1 ||
|
|
+ low_pressure_mem.min_nr_free_pages > mi->field.nr_free_pages) {
|
|
+ if (debug_process_killing) {
|
|
+ ALOGI("Low pressure min memory update from %" PRId64 " to %" PRId64,
|
|
+ low_pressure_mem.min_nr_free_pages, mi->field.nr_free_pages);
|
|
+ }
|
|
+ low_pressure_mem.min_nr_free_pages = mi->field.nr_free_pages;
|
|
+ }
|
|
+ /*
|
|
+ * Free memory at low vmpressure events occasionally gets spikes,
|
|
+ * possibly a stale low vmpressure event with memory already
|
|
+ * freed up (no memory pressure should have been reported).
|
|
+ * Ignore large jumps in max_nr_free_pages that would mess up our stats.
|
|
+ */
|
|
+ if (low_pressure_mem.max_nr_free_pages == -1 ||
|
|
+ (low_pressure_mem.max_nr_free_pages < mi->field.nr_free_pages &&
|
|
+ mi->field.nr_free_pages - low_pressure_mem.max_nr_free_pages <
|
|
+ low_pressure_mem.max_nr_free_pages * 0.1)) {
|
|
+ if (debug_process_killing) {
|
|
+ ALOGI("Low pressure max memory update from %" PRId64 " to %" PRId64,
|
|
+ low_pressure_mem.max_nr_free_pages, mi->field.nr_free_pages);
|
|
+ }
|
|
+ low_pressure_mem.max_nr_free_pages = mi->field.nr_free_pages;
|
|
+ }
|
|
+}
|
|
+
|
|
+enum vmpressure_level upgrade_level(enum vmpressure_level level) {
|
|
+ return (enum vmpressure_level)((level < VMPRESS_LEVEL_CRITICAL) ?
|
|
+ level + 1 : level);
|
|
+}
|
|
+
|
|
+enum vmpressure_level downgrade_level(enum vmpressure_level level) {
|
|
+ return (enum vmpressure_level)((level > VMPRESS_LEVEL_LOW) ?
|
|
+ level - 1 : level);
|
|
+}
|
|
+
|
|
+static bool is_kill_pending(void) {
|
|
+ char buf[24];
|
|
+
|
|
+ if (last_killed_pid < 0) {
|
|
+ return false;
|
|
+ }
|
|
+
|
|
+ snprintf(buf, sizeof(buf), "/proc/%d/", last_killed_pid);
|
|
+ if (access(buf, F_OK) == 0) {
|
|
+ return true;
|
|
+ }
|
|
+
|
|
+ // reset last killed PID because there's nothing pending
|
|
+ last_killed_pid = -1;
|
|
+ return false;
|
|
+}
|
|
+
|
|
+static void mp_event_common(int data, uint32_t events __unused) {
|
|
int ret;
|
|
unsigned long long evcount;
|
|
- int index = is_critical ? CRITICAL_INDEX : MEDIUM_INDEX;
|
|
int64_t mem_usage, memsw_usage;
|
|
int64_t mem_pressure;
|
|
+ enum vmpressure_level lvl;
|
|
+ union meminfo mi;
|
|
+ union zoneinfo zi;
|
|
+ struct timespec curr_tm;
|
|
+ static struct timespec last_kill_tm;
|
|
+ static unsigned long kill_skip_count = 0;
|
|
+ enum vmpressure_level level = (enum vmpressure_level)data;
|
|
+ long other_free = 0, other_file = 0;
|
|
+ int min_score_adj;
|
|
+ int minfree = 0;
|
|
+ static struct reread_data mem_usage_file_data = {
|
|
+ .filename = MEMCG_MEMORY_USAGE,
|
|
+ .fd = -1,
|
|
+ };
|
|
+ static struct reread_data memsw_usage_file_data = {
|
|
+ .filename = MEMCG_MEMORYSW_USAGE,
|
|
+ .fd = -1,
|
|
+ };
|
|
+
|
|
+ if (debug_process_killing) {
|
|
+ ALOGI("%s memory pressure event is triggered", level_name[level]);
|
|
+ }
|
|
+
|
|
+ if (!use_psi_monitors) {
|
|
+ /*
|
|
+ * Check all event counters from low to critical
|
|
+ * and upgrade to the highest priority one. By reading
|
|
+ * eventfd we also reset the event counters.
|
|
+ */
|
|
+ for (lvl = VMPRESS_LEVEL_LOW; lvl < VMPRESS_LEVEL_COUNT; lvl++) {
|
|
+ if (mpevfd[lvl] != -1 &&
|
|
+ TEMP_FAILURE_RETRY(read(mpevfd[lvl],
|
|
+ &evcount, sizeof(evcount))) > 0 &&
|
|
+ evcount > 0 && lvl > level) {
|
|
+ level = lvl;
|
|
+ }
|
|
+ }
|
|
+ }
|
|
+
|
|
+ if (clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm) != 0) {
|
|
+ ALOGE("Failed to get current time");
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ if (kill_timeout_ms) {
|
|
+ // If we're within the timeout, see if there's pending reclaim work
|
|
+ // from the last killed process. If there is (as evidenced by
|
|
+ // /proc/<pid> continuing to exist), skip killing for now.
|
|
+ if ((get_time_diff_ms(&last_kill_tm, &curr_tm) < (long) kill_timeout_ms) &&
|
|
+ (low_ram_device || is_kill_pending())) {
|
|
+ kill_skip_count++;
|
|
+ return;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ if (kill_skip_count > 0) {
|
|
+ ALOGI("%lu memory pressure events were skipped after a kill!",
|
|
+ kill_skip_count);
|
|
+ kill_skip_count = 0;
|
|
+ }
|
|
+
|
|
+ if (meminfo_parse(&mi) < 0 || zoneinfo_parse(&zi) < 0) {
|
|
+ ALOGE("Failed to get free memory!");
|
|
+ return;
|
|
+ }
|
|
+
|
|
+ if (use_minfree_levels) {
|
|
+ int i;
|
|
+
|
|
+ other_free = mi.field.nr_free_pages - zi.field.totalreserve_pages;
|
|
+ if (mi.field.nr_file_pages > (mi.field.shmem + mi.field.unevictable + mi.field.swap_cached)) {
|
|
+ other_file = (mi.field.nr_file_pages - mi.field.shmem -
|
|
+ mi.field.unevictable - mi.field.swap_cached);
|
|
+ } else {
|
|
+ other_file = 0;
|
|
+ }
|
|
+
|
|
+ min_score_adj = OOM_SCORE_ADJ_MAX + 1;
|
|
+ for (i = 0; i < lowmem_targets_size; i++) {
|
|
+ minfree = lowmem_minfree[i];
|
|
+ if (other_free < minfree && other_file < minfree) {
|
|
+ min_score_adj = lowmem_adj[i];
|
|
+ break;
|
|
+ }
|
|
+ }
|
|
+
|
|
+ if (min_score_adj == OOM_SCORE_ADJ_MAX + 1) {
|
|
+ if (debug_process_killing) {
|
|
+ ALOGI("Ignore %s memory pressure event "
|
|
+ "(free memory=%ldkB, cache=%ldkB, limit=%ldkB)",
|
|
+ level_name[level], other_free * page_k, other_file * page_k,
|
|
+ (long)lowmem_minfree[lowmem_targets_size - 1] * page_k);
|
|
+ }
|
|
+ return;
|
|
+ }
|
|
|
|
- ret = read(mpevfd[index], &evcount, sizeof(evcount));
|
|
- if (ret < 0)
|
|
- ALOGE("Error reading memory pressure event fd; errno=%d",
|
|
- errno);
|
|
+ goto do_kill;
|
|
+ }
|
|
+
|
|
+ if (level == VMPRESS_LEVEL_LOW) {
|
|
+ record_low_pressure_levels(&mi);
|
|
+ }
|
|
|
|
- mem_usage = get_memory_usage(MEMCG_MEMORY_USAGE);
|
|
- memsw_usage = get_memory_usage(MEMCG_MEMORYSW_USAGE);
|
|
- if (memsw_usage < 0 || mem_usage < 0) {
|
|
- find_and_kill_process(is_critical);
|
|
+ if (level_oomadj[level] > OOM_SCORE_ADJ_MAX) {
|
|
+ /* Do not monitor this pressure level */
|
|
return;
|
|
}
|
|
|
|
+ if ((mem_usage = get_memory_usage(&mem_usage_file_data)) < 0) {
|
|
+ goto do_kill;
|
|
+ }
|
|
+ if ((memsw_usage = get_memory_usage(&memsw_usage_file_data)) < 0) {
|
|
+ goto do_kill;
|
|
+ }
|
|
+
|
|
// Calculate percent for swappinness.
|
|
mem_pressure = (mem_usage * 100) / memsw_usage;
|
|
|
|
- if (enable_pressure_upgrade && !is_critical) {
|
|
+ if (enable_pressure_upgrade && level != VMPRESS_LEVEL_CRITICAL) {
|
|
// We are swapping too much.
|
|
if (mem_pressure < upgrade_pressure) {
|
|
- ALOGI("Event upgraded to critical.");
|
|
- is_critical = true;
|
|
+ level = upgrade_level(level);
|
|
+ if (debug_process_killing) {
|
|
+ ALOGI("Event upgraded to %s", level_name[level]);
|
|
+ }
|
|
}
|
|
}
|
|
|
|
- // If the pressure is larger than downgrade_pressure lmk will not
|
|
- // kill any process, since enough memory is available.
|
|
- if (mem_pressure > downgrade_pressure) {
|
|
- if (debug_process_killing) {
|
|
- ALOGI("Ignore %s memory pressure", is_critical ? "critical" : "medium");
|
|
- }
|
|
- return;
|
|
- } else if (is_critical && mem_pressure > upgrade_pressure) {
|
|
- if (debug_process_killing) {
|
|
- ALOGI("Downgrade critical memory pressure");
|
|
+ // If we still have enough swap space available, check if we want to
|
|
+ // ignore/downgrade pressure events.
|
|
+ if (mi.field.free_swap >=
|
|
+ mi.field.total_swap * swap_free_low_percentage / 100) {
|
|
+ // If the pressure is larger than downgrade_pressure lmk will not
|
|
+ // kill any process, since enough memory is available.
|
|
+ if (mem_pressure > downgrade_pressure) {
|
|
+ if (debug_process_killing) {
|
|
+ ALOGI("Ignore %s memory pressure", level_name[level]);
|
|
+ }
|
|
+ return;
|
|
+ } else if (level == VMPRESS_LEVEL_CRITICAL && mem_pressure > upgrade_pressure) {
|
|
+ if (debug_process_killing) {
|
|
+ ALOGI("Downgrade critical memory pressure");
|
|
+ }
|
|
+ // Downgrade event, since enough memory available.
|
|
+ level = downgrade_level(level);
|
|
}
|
|
- // Downgrade event to medium, since enough memory available.
|
|
- is_critical = false;
|
|
}
|
|
|
|
- if (find_and_kill_process(is_critical) == 0) {
|
|
- if (debug_process_killing) {
|
|
- ALOGI("Nothing to kill");
|
|
+do_kill:
|
|
+ if (low_ram_device) {
|
|
+ /* For Go devices kill only one task */
|
|
+ if (find_and_kill_process(level_oomadj[level]) == 0) {
|
|
+ if (debug_process_killing) {
|
|
+ ALOGI("Nothing to kill");
|
|
+ }
|
|
+ } else {
|
|
+ meminfo_log(&mi);
|
|
+ }
|
|
+ } else {
|
|
+ int pages_freed;
|
|
+ static struct timespec last_report_tm;
|
|
+ static unsigned long report_skip_count = 0;
|
|
+
|
|
+ if (!use_minfree_levels) {
|
|
+ /* Free up enough memory to downgrate the memory pressure to low level */
|
|
+ if (mi.field.nr_free_pages >= low_pressure_mem.max_nr_free_pages) {
|
|
+ if (debug_process_killing) {
|
|
+ ALOGI("Ignoring pressure since more memory is "
|
|
+ "available (%" PRId64 ") than watermark (%" PRId64 ")",
|
|
+ mi.field.nr_free_pages, low_pressure_mem.max_nr_free_pages);
|
|
+ }
|
|
+ return;
|
|
+ }
|
|
+ min_score_adj = level_oomadj[level];
|
|
+ }
|
|
+
|
|
+ pages_freed = find_and_kill_process(min_score_adj);
|
|
+
|
|
+ if (pages_freed == 0) {
|
|
+ /* Rate limit kill reports when nothing was reclaimed */
|
|
+ if (get_time_diff_ms(&last_report_tm, &curr_tm) < FAIL_REPORT_RLIMIT_MS) {
|
|
+ report_skip_count++;
|
|
+ return;
|
|
+ }
|
|
+ } else {
|
|
+ /* If we killed anything, update the last killed timestamp. */
|
|
+ last_kill_tm = curr_tm;
|
|
}
|
|
+
|
|
+ /* Log meminfo whenever we kill or when report rate limit allows */
|
|
+ meminfo_log(&mi);
|
|
+
|
|
+ if (use_minfree_levels) {
|
|
+ ALOGI("Reclaimed %ldkB, cache(%ldkB) and "
|
|
+ "free(%" PRId64 "kB)-reserved(%" PRId64 "kB) below min(%ldkB) for oom_adj %d",
|
|
+ pages_freed * page_k,
|
|
+ other_file * page_k, mi.field.nr_free_pages * page_k,
|
|
+ zi.field.totalreserve_pages * page_k,
|
|
+ minfree * page_k, min_score_adj);
|
|
+ } else {
|
|
+ ALOGI("Reclaimed %ldkB at oom_adj %d",
|
|
+ pages_freed * page_k, min_score_adj);
|
|
+ }
|
|
+
|
|
+ if (report_skip_count > 0) {
|
|
+ ALOGI("Suppressed %lu failed kill reports", report_skip_count);
|
|
+ report_skip_count = 0;
|
|
+ }
|
|
+
|
|
+ last_report_tm = curr_tm;
|
|
+ }
|
|
+}
|
|
+
|
|
+static bool init_mp_psi(enum vmpressure_level level) {
|
|
+ int fd = init_psi_monitor(psi_thresholds[level].stall_type,
|
|
+ psi_thresholds[level].threshold_ms * US_PER_MS,
|
|
+ PSI_WINDOW_SIZE_MS * US_PER_MS);
|
|
+
|
|
+ if (fd < 0) {
|
|
+ return false;
|
|
}
|
|
+
|
|
+ vmpressure_hinfo[level].handler = mp_event_common;
|
|
+ vmpressure_hinfo[level].data = level;
|
|
+ if (register_psi_monitor(epollfd, fd, &vmpressure_hinfo[level]) < 0) {
|
|
+ destroy_psi_monitor(fd);
|
|
+ return false;
|
|
+ }
|
|
+ maxevents++;
|
|
+ mpevfd[level] = fd;
|
|
+
|
|
+ return true;
|
|
}
|
|
|
|
-static void mp_event(uint32_t events __unused) {
|
|
- mp_event_common(false);
|
|
+static void destroy_mp_psi(enum vmpressure_level level) {
|
|
+ int fd = mpevfd[level];
|
|
+
|
|
+ if (unregister_psi_monitor(epollfd, fd) < 0) {
|
|
+ ALOGE("Failed to unregister psi monitor for %s memory pressure; errno=%d",
|
|
+ level_name[level], errno);
|
|
+ }
|
|
+ destroy_psi_monitor(fd);
|
|
+ mpevfd[level] = -1;
|
|
}
|
|
|
|
-static void mp_event_critical(uint32_t events __unused) {
|
|
- mp_event_common(true);
|
|
+static bool init_psi_monitors() {
|
|
+ if (!init_mp_psi(VMPRESS_LEVEL_LOW)) {
|
|
+ return false;
|
|
+ }
|
|
+ if (!init_mp_psi(VMPRESS_LEVEL_MEDIUM)) {
|
|
+ destroy_mp_psi(VMPRESS_LEVEL_LOW);
|
|
+ return false;
|
|
+ }
|
|
+ if (!init_mp_psi(VMPRESS_LEVEL_CRITICAL)) {
|
|
+ destroy_mp_psi(VMPRESS_LEVEL_MEDIUM);
|
|
+ destroy_mp_psi(VMPRESS_LEVEL_LOW);
|
|
+ return false;
|
|
+ }
|
|
+ return true;
|
|
}
|
|
|
|
-static int init_mp_common(char *levelstr, void *event_handler, bool is_critical)
|
|
-{
|
|
+static bool init_mp_common(enum vmpressure_level level) {
|
|
int mpfd;
|
|
int evfd;
|
|
int evctlfd;
|
|
char buf[256];
|
|
struct epoll_event epev;
|
|
int ret;
|
|
- int mpevfd_index = is_critical ? CRITICAL_INDEX : MEDIUM_INDEX;
|
|
+ int level_idx = (int)level;
|
|
+ const char *levelstr = level_name[level_idx];
|
|
|
|
+ /* gid containing AID_SYSTEM required */
|
|
mpfd = open(MEMCG_SYSFS_PATH "memory.pressure_level", O_RDONLY | O_CLOEXEC);
|
|
if (mpfd < 0) {
|
|
ALOGI("No kernel memory.pressure_level support (errno=%d)", errno);
|
|
@@ -768,7 +1831,7 @@ static int init_mp_common(char *levelstr, void *event_handler, bool is_critical)
|
|
goto err;
|
|
}
|
|
|
|
- ret = write(evctlfd, buf, strlen(buf) + 1);
|
|
+ ret = TEMP_FAILURE_RETRY(write(evctlfd, buf, strlen(buf) + 1));
|
|
if (ret == -1) {
|
|
ALOGE("cgroup.event_control write failed for level %s; errno=%d",
|
|
levelstr, errno);
|
|
@@ -776,15 +1839,19 @@ static int init_mp_common(char *levelstr, void *event_handler, bool is_critical)
|
|
}
|
|
|
|
epev.events = EPOLLIN;
|
|
- epev.data.ptr = event_handler;
|
|
+ /* use data to store event level */
|
|
+ vmpressure_hinfo[level_idx].data = level_idx;
|
|
+ vmpressure_hinfo[level_idx].handler = mp_event_common;
|
|
+ epev.data.ptr = (void *)&vmpressure_hinfo[level_idx];
|
|
ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, evfd, &epev);
|
|
if (ret == -1) {
|
|
ALOGE("epoll_ctl for level %s failed; errno=%d", levelstr, errno);
|
|
goto err;
|
|
}
|
|
maxevents++;
|
|
- mpevfd[mpevfd_index] = evfd;
|
|
- return 0;
|
|
+ mpevfd[level] = evfd;
|
|
+ close(evctlfd);
|
|
+ return true;
|
|
|
|
err:
|
|
close(evfd);
|
|
@@ -793,17 +1860,7 @@ err_eventfd:
|
|
err_open_evctlfd:
|
|
close(mpfd);
|
|
err_open_mpfd:
|
|
- return -1;
|
|
-}
|
|
-
|
|
-static int init_mp_medium()
|
|
-{
|
|
- return init_mp_common(MEMPRESSURE_WATCH_MEDIUM_LEVEL, (void *)&mp_event, false);
|
|
-}
|
|
-
|
|
-static int init_mp_critical()
|
|
-{
|
|
- return init_mp_common(MEMPRESSURE_WATCH_CRITICAL_LEVEL, (void *)&mp_event_critical, true);
|
|
+ return false;
|
|
}
|
|
|
|
static int init(void) {
|
|
@@ -822,36 +1879,54 @@ static int init(void) {
|
|
return -1;
|
|
}
|
|
|
|
- ctrl_lfd = android_get_control_socket("lmkd");
|
|
- if (ctrl_lfd < 0) {
|
|
+ // mark data connections as not connected
|
|
+ for (int i = 0; i < MAX_DATA_CONN; i++) {
|
|
+ data_sock[i].sock = -1;
|
|
+ }
|
|
+
|
|
+ ctrl_sock.sock = android_get_control_socket("lmkd");
|
|
+ if (ctrl_sock.sock < 0) {
|
|
ALOGE("get lmkd control socket failed");
|
|
return -1;
|
|
}
|
|
|
|
- ret = listen(ctrl_lfd, 1);
|
|
+ ret = listen(ctrl_sock.sock, MAX_DATA_CONN);
|
|
if (ret < 0) {
|
|
ALOGE("lmkd control socket listen failed (errno=%d)", errno);
|
|
return -1;
|
|
}
|
|
|
|
epev.events = EPOLLIN;
|
|
- epev.data.ptr = (void *)ctrl_connect_handler;
|
|
- if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_lfd, &epev) == -1) {
|
|
+ ctrl_sock.handler_info.handler = ctrl_connect_handler;
|
|
+ epev.data.ptr = (void *)&(ctrl_sock.handler_info);
|
|
+ if (epoll_ctl(epollfd, EPOLL_CTL_ADD, ctrl_sock.sock, &epev) == -1) {
|
|
ALOGE("epoll_ctl for lmkd control socket failed (errno=%d)", errno);
|
|
return -1;
|
|
}
|
|
maxevents++;
|
|
|
|
has_inkernel_module = !access(INKERNEL_MINFREE_PATH, W_OK);
|
|
- use_inkernel_interface = has_inkernel_module && !is_go_device;
|
|
+ use_inkernel_interface = has_inkernel_module;
|
|
|
|
if (use_inkernel_interface) {
|
|
ALOGI("Using in-kernel low memory killer interface");
|
|
} else {
|
|
- ret = init_mp_medium();
|
|
- ret |= init_mp_critical();
|
|
- if (ret)
|
|
+ /* Try to use psi monitor first if kernel has it */
|
|
+ use_psi_monitors = property_get_bool("ro.lmk.use_psi", true) &&
|
|
+ init_psi_monitors();
|
|
+ /* Fall back to vmpressure */
|
|
+ if (!use_psi_monitors &&
|
|
+ (!init_mp_common(VMPRESS_LEVEL_LOW) ||
|
|
+ !init_mp_common(VMPRESS_LEVEL_MEDIUM) ||
|
|
+ !init_mp_common(VMPRESS_LEVEL_CRITICAL))) {
|
|
ALOGE("Kernel does not support memory pressure events or in-kernel low memory killer");
|
|
+ return -1;
|
|
+ }
|
|
+ if (use_psi_monitors) {
|
|
+ ALOGI("Using psi monitors for memory pressure detection");
|
|
+ } else {
|
|
+ ALOGI("Using vmpressure for memory pressure detection");
|
|
+ }
|
|
}
|
|
|
|
for (i = 0; i <= ADJTOSLOT(OOM_SCORE_ADJ_MAX); i++) {
|
|
@@ -859,17 +1934,44 @@ static int init(void) {
|
|
procadjslot_list[i].prev = &procadjslot_list[i];
|
|
}
|
|
|
|
+ memset(killcnt_idx, KILLCNT_INVALID_IDX, sizeof(killcnt_idx));
|
|
+
|
|
return 0;
|
|
}
|
|
|
|
static void mainloop(void) {
|
|
+ struct event_handler_info* handler_info;
|
|
+ struct event_handler_info* poll_handler = NULL;
|
|
+ struct timespec last_report_tm, curr_tm;
|
|
+ struct epoll_event *evt;
|
|
+ long delay = -1;
|
|
+ int polling = 0;
|
|
+
|
|
while (1) {
|
|
struct epoll_event events[maxevents];
|
|
int nevents;
|
|
int i;
|
|
|
|
- ctrl_dfd_reopened = 0;
|
|
- nevents = epoll_wait(epollfd, events, maxevents, -1);
|
|
+ if (polling) {
|
|
+ /* Calculate next timeout */
|
|
+ clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm);
|
|
+ delay = get_time_diff_ms(&last_report_tm, &curr_tm);
|
|
+ delay = (delay < PSI_POLL_PERIOD_MS) ?
|
|
+ PSI_POLL_PERIOD_MS - delay : PSI_POLL_PERIOD_MS;
|
|
+
|
|
+ /* Wait for events until the next polling timeout */
|
|
+ nevents = epoll_wait(epollfd, events, maxevents, delay);
|
|
+
|
|
+ clock_gettime(CLOCK_MONOTONIC_COARSE, &curr_tm);
|
|
+ if (get_time_diff_ms(&last_report_tm, &curr_tm) >= PSI_POLL_PERIOD_MS) {
|
|
+ polling--;
|
|
+ poll_handler->handler(poll_handler->data, 0);
|
|
+ last_report_tm = curr_tm;
|
|
+ }
|
|
+ } else {
|
|
+ /* Wait for events with no timeout */
|
|
+ nevents = epoll_wait(epollfd, events, maxevents, -1);
|
|
+ }
|
|
|
|
if (nevents == -1) {
|
|
if (errno == EINTR)
|
|
@@ -878,11 +1980,44 @@ static void mainloop(void) {
|
|
continue;
|
|
}
|
|
|
|
- for (i = 0; i < nevents; ++i) {
|
|
- if (events[i].events & EPOLLERR)
|
|
+ /*
|
|
+ * First pass to see if any data socket connections were dropped.
|
|
+ * Dropped connection should be handled before any other events
|
|
+ * to deallocate data connection and correctly handle cases when
|
|
+ * connection gets dropped and reestablished in the same epoll cycle.
|
|
+ * In such cases it's essential to handle connection closures first.
|
|
+ */
|
|
+ for (i = 0, evt = &events[0]; i < nevents; ++i, evt++) {
|
|
+ if ((evt->events & EPOLLHUP) && evt->data.ptr) {
|
|
+ ALOGI("lmkd data connection dropped");
|
|
+ handler_info = (struct event_handler_info*)evt->data.ptr;
|
|
+ ctrl_data_close(handler_info->data);
|
|
+ }
|
|
+ }
|
|
+
|
|
+ /* Second pass to handle all other events */
|
|
+ for (i = 0, evt = &events[0]; i < nevents; ++i, evt++) {
|
|
+ if (evt->events & EPOLLERR)
|
|
ALOGD("EPOLLERR on event #%d", i);
|
|
- if (events[i].data.ptr)
|
|
- (*(void (*)(uint32_t))events[i].data.ptr)(events[i].events);
|
|
+ if (evt->events & EPOLLHUP) {
|
|
+ /* This case was handled in the first pass */
|
|
+ continue;
|
|
+ }
|
|
+ if (evt->data.ptr) {
|
|
+ handler_info = (struct event_handler_info*)evt->data.ptr;
|
|
+ handler_info->handler(handler_info->data, evt->events);
|
|
+
|
|
+ if (use_psi_monitors && handler_info->handler == mp_event_common) {
|
|
+ /*
|
|
+ * Poll for the duration of PSI_WINDOW_SIZE_MS after the
|
|
+ * initial PSI event because psi events are rate-limited
|
|
+ * at one per sec.
|
|
+ */
|
|
+ polling = PSI_POLL_COUNT;
|
|
+ poll_handler = handler_info;
|
|
+ clock_gettime(CLOCK_MONOTONIC_COARSE, &last_report_tm);
|
|
+ }
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
@@ -892,18 +2027,72 @@ int main(int argc __unused, char **argv __unused) {
|
|
.sched_priority = 1,
|
|
};
|
|
|
|
- medium_oomadj = property_get_int32("ro.lmk.medium", 800);
|
|
- critical_oomadj = property_get_int32("ro.lmk.critical", 0);
|
|
+ /* By default disable low level vmpressure events */
|
|
+ level_oomadj[VMPRESS_LEVEL_LOW] =
|
|
+ property_get_int32("ro.lmk.low", OOM_SCORE_ADJ_MAX + 1);
|
|
+ level_oomadj[VMPRESS_LEVEL_MEDIUM] =
|
|
+ property_get_int32("ro.lmk.medium", 800);
|
|
+ level_oomadj[VMPRESS_LEVEL_CRITICAL] =
|
|
+ property_get_int32("ro.lmk.critical", 0);
|
|
debug_process_killing = property_get_bool("ro.lmk.debug", false);
|
|
- enable_pressure_upgrade = property_get_bool("ro.lmk.critical_upgrade", false);
|
|
- upgrade_pressure = (int64_t)property_get_int32("ro.lmk.upgrade_pressure", 50);
|
|
- downgrade_pressure = (int64_t)property_get_int32("ro.lmk.downgrade_pressure", 60);
|
|
- is_go_device = property_get_bool("ro.config.low_ram", false);
|
|
-
|
|
- mlockall(MCL_FUTURE);
|
|
- sched_setscheduler(0, SCHED_FIFO, ¶m);
|
|
- if (!init())
|
|
+
|
|
+ /* By default disable upgrade/downgrade logic */
|
|
+ enable_pressure_upgrade =
|
|
+ property_get_bool("ro.lmk.critical_upgrade", false);
|
|
+ upgrade_pressure =
|
|
+ (int64_t)property_get_int32("ro.lmk.upgrade_pressure", 100);
|
|
+ downgrade_pressure =
|
|
+ (int64_t)property_get_int32("ro.lmk.downgrade_pressure", 100);
|
|
+ kill_heaviest_task =
|
|
+ property_get_bool("ro.lmk.kill_heaviest_task", false);
|
|
+ low_ram_device = property_get_bool("ro.config.low_ram", false);
|
|
+ kill_timeout_ms =
|
|
+ (unsigned long)property_get_int32("ro.lmk.kill_timeout_ms", 0);
|
|
+ use_minfree_levels =
|
|
+ property_get_bool("ro.lmk.use_minfree_levels", false);
|
|
+ per_app_memcg =
|
|
+ property_get_bool("ro.config.per_app_memcg", low_ram_device);
|
|
+ swap_free_low_percentage =
|
|
+ property_get_int32("ro.lmk.swap_free_low_percentage", 10);
|
|
+
|
|
+ ctx = create_android_logger(MEMINFO_LOG_TAG);
|
|
+
|
|
+#ifdef LMKD_LOG_STATS
|
|
+ statslog_init(&log_ctx, &enable_stats_log);
|
|
+#endif
|
|
+
|
|
+ if (!init()) {
|
|
+ if (!use_inkernel_interface) {
|
|
+ /*
|
|
+ * MCL_ONFAULT pins pages as they fault instead of loading
|
|
+ * everything immediately all at once. (Which would be bad,
|
|
+ * because as of this writing, we have a lot of mapped pages we
|
|
+ * never use.) Old kernels will see MCL_ONFAULT and fail with
|
|
+ * EINVAL; we ignore this failure.
|
|
+ *
|
|
+ * N.B. read the man page for mlockall. MCL_CURRENT | MCL_ONFAULT
|
|
+ * pins ⊆ MCL_CURRENT, converging to just MCL_CURRENT as we fault
|
|
+ * in pages.
|
|
+ */
|
|
+ /* CAP_IPC_LOCK required */
|
|
+ if (mlockall(MCL_CURRENT | MCL_FUTURE | MCL_ONFAULT) && (errno != EINVAL)) {
|
|
+ ALOGW("mlockall failed %s", strerror(errno));
|
|
+ }
|
|
+
|
|
+ /* CAP_NICE required */
|
|
+ if (sched_setscheduler(0, SCHED_FIFO, ¶m)) {
|
|
+ ALOGW("set SCHED_FIFO failed %s", strerror(errno));
|
|
+ }
|
|
+ }
|
|
+
|
|
mainloop();
|
|
+ }
|
|
+
|
|
+#ifdef LMKD_LOG_STATS
|
|
+ statslog_destroy(&log_ctx);
|
|
+#endif
|
|
+
|
|
+ android_log_destroy(&ctx);
|
|
|
|
ALOGI("exiting");
|
|
return 0;
|
|
--
|
|
2.45.1
|
|
|