From 63dd69984d21971e0b707af24754572a40340b3f Mon Sep 17 00:00:00 2001 From: Suren Baghdasaryan Date: Thu, 8 Jun 2023 20:51:20 +0000 Subject: [PATCH] lmkd: introduce swap_compression_ratio tunable Free swap is calculated using the min of free swap that kernel would consider using and easily available memory which can be used by ZRAM for swapping purposes. However calculation does not consider the average data compression ratio of ZRAM. Introduce a tunable to set the average swap compression ratio used when evaluating the amount of data which can be swapped. Default is set to 1 (no compression) to keep current behavior. Setting it to 0 will ignore available memory and assume that configured swap size can be always utilized fully. Bug: 285854307 Bug: 327561101 Change-Id: I6b0f93ce24179ebf7365a3dbcd52c6e4a52ac200 Signed-off-by: Suren Baghdasaryan --- README.md | 5 +++++ lmkd.cpp | 11 ++++++++++- lmkd.rc | 3 +++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c378bda..14743d5 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,11 @@ properties: - `ro.lmk.direct_reclaim_threshold_ms`: direct reclaim duration threshold in milliseconds to consider the system as stuck in direct reclaim. Default = 0 (disabled) + - `ro.lmk.swap_compression_ratio`: swap average compression ratio to be used when + estimating how much data can be swapped. Setting it + to 0 will ignore available memory and assume that + configured swap size can be always utilized fully. + Default = 1 (no compression). lmkd will set the following Android properties according to current system configurations: diff --git a/lmkd.cpp b/lmkd.cpp index e88aedc..f162f6c 100644 --- a/lmkd.cpp +++ b/lmkd.cpp @@ -162,6 +162,8 @@ static inline void trace_kill_end() {} #define DEF_COMPLETE_STALL 700 /* ro.lmk.direct_reclaim_threshold_ms property defaults */ #define DEF_DIRECT_RECL_THRESH_MS 0 +/* ro.lmk.swap_compression_ratio property defaults */ +#define DEF_SWAP_COMP_RATIO 1 #define LMKD_REINIT_PROP "lmkd.reinit" @@ -232,6 +234,7 @@ static bool use_psi_monitors = false; static int kpoll_fd; static bool delay_monitors_until_boot; static int direct_reclaim_threshold_ms; +static int swap_compression_ratio; 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 */ @@ -1946,8 +1949,12 @@ static int meminfo_parse(union meminfo *mi) { // from the free memory or reclaimed. Use the lowest of free_swap and easily available memory to // measure free swap because they represent how much swap space the system will consider to use // and how much it can actually use. +// Swap compression ratio in the calculation can be adjusted using swap_compression_ratio tunable. +// By setting swap_compression_ratio to 0, available memory can be ignored. static inline int64_t get_free_swap(union meminfo *mi) { - return std::min(mi->field.free_swap, mi->field.easy_available); + if (swap_compression_ratio) + return std::min(mi->field.free_swap, mi->field.easy_available * swap_compression_ratio); + return mi->field.free_swap; } /* /proc/vmstat parsing routines */ @@ -3998,6 +4005,8 @@ static bool update_props() { delay_monitors_until_boot = GET_LMK_PROPERTY(bool, "delay_monitors_until_boot", false); direct_reclaim_threshold_ms = GET_LMK_PROPERTY(int64, "direct_reclaim_threshold_ms", DEF_DIRECT_RECL_THRESH_MS); + swap_compression_ratio = + GET_LMK_PROPERTY(int64, "swap_compression_ratio", DEF_SWAP_COMP_RATIO); reaper.enable_debug(debug_process_killing); diff --git a/lmkd.rc b/lmkd.rc index 73ed79c..05e9db7 100644 --- a/lmkd.rc +++ b/lmkd.rc @@ -55,3 +55,6 @@ on property:persist.device_config.lmkd_native.filecache_min_kb=* on property:persist.device_config.lmkd_native.direct_reclaim_threshold_ms=* setprop lmkd.reinit ${sys.boot_completed:-0} + +on property:persist.device_config.lmkd_native.swap_compression_ratio=* + setprop lmkd.reinit ${sys.boot_completed:-0}