From b91ecaa3e86c1ea0a745aa48784867cfcf21b25e Mon Sep 17 00:00:00 2001 From: Suren Baghdasaryan Date: Tue, 3 Aug 2021 15:40:23 -0700 Subject: [PATCH 1/2] lmkd: Add support for persist.device_config.lmkd_native.* properties Allow persist.device_config.lmkd_native.* to override ro.lmk.* properties to enable experiments with lmkd configuration properties. Experiments will be able to set appropriate persist.device_config.lmkd_native. property which will issue "lmkd --reinit" command to reinitialize lmkd with new parameters. Bug: 194316048 Test: adb shell device_config put lmkd_native thrashing_limit_critical 350 Signed-off-by: Suren Baghdasaryan Change-Id: Ia48fd51eab126d307a1604530b642e86cf250688 Merged-In: Ia48fd51eab126d307a1604530b642e86cf250688 --- lmkd.cpp | 50 ++++++++++++++++++++++++++++++-------------------- lmkd.rc | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 20 deletions(-) diff --git a/lmkd.cpp b/lmkd.cpp index 2f0df91..f01141b 100644 --- a/lmkd.cpp +++ b/lmkd.cpp @@ -114,6 +114,16 @@ #define STRINGIFY(x) STRINGIFY_INTERNAL(x) #define STRINGIFY_INTERNAL(x) #x +/* + * Read lmk property with persist.device_config.lmkd_native. overriding ro.lmk. + * persist.device_config.lmkd_native.* properties are being set by experiments. If a new property + * can be controlled by an experiment then use GET_LMK_PROPERTY instead of property_get_xxx and + * add "on property" triggers in lmkd.rc to react to the experiment flag changes. + */ +#define GET_LMK_PROPERTY(type, name, def) \ + property_get_##type("persist.device_config.lmkd_native." name, \ + property_get_##type("ro.lmk." name, def)) + /* * PSI monitor tracking window size. * PSI monitor generates events at most once per window, @@ -2984,7 +2994,7 @@ static bool init_psi_monitors() { * use new kill strategy based on zone watermarks, free swap and thrashing stats */ bool use_new_strategy = - property_get_bool("ro.lmk.use_new_strategy", low_ram_device || !use_minfree_levels); + GET_LMK_PROPERTY(bool, "use_new_strategy", low_ram_device || !use_minfree_levels); /* In default PSI mode override stall amounts using system properties */ if (use_new_strategy) { @@ -3100,7 +3110,7 @@ static void kernel_event_handler(int data __unused, uint32_t events __unused, static bool init_monitors() { /* Try to use psi monitor first if kernel has it */ - use_psi_monitors = property_get_bool("ro.lmk.use_psi", true) && + use_psi_monitors = GET_LMK_PROPERTY(bool, "use_psi", true) && init_psi_monitors(); /* Fall back to vmpressure */ if (!use_psi_monitors && @@ -3415,43 +3425,43 @@ int issue_reinit() { static void update_props() { /* By default disable low level vmpressure events */ level_oomadj[VMPRESS_LEVEL_LOW] = - property_get_int32("ro.lmk.low", OOM_SCORE_ADJ_MAX + 1); + GET_LMK_PROPERTY(int32, "low", OOM_SCORE_ADJ_MAX + 1); level_oomadj[VMPRESS_LEVEL_MEDIUM] = - property_get_int32("ro.lmk.medium", 800); + GET_LMK_PROPERTY(int32, "medium", 800); level_oomadj[VMPRESS_LEVEL_CRITICAL] = - property_get_int32("ro.lmk.critical", 0); - debug_process_killing = property_get_bool("ro.lmk.debug", false); + GET_LMK_PROPERTY(int32, "critical", 0); + debug_process_killing = GET_LMK_PROPERTY(bool, "debug", false); /* By default disable upgrade/downgrade logic */ enable_pressure_upgrade = - property_get_bool("ro.lmk.critical_upgrade", false); + GET_LMK_PROPERTY(bool, "critical_upgrade", false); upgrade_pressure = - (int64_t)property_get_int32("ro.lmk.upgrade_pressure", 100); + (int64_t)GET_LMK_PROPERTY(int32, "upgrade_pressure", 100); downgrade_pressure = - (int64_t)property_get_int32("ro.lmk.downgrade_pressure", 100); + (int64_t)GET_LMK_PROPERTY(int32, "downgrade_pressure", 100); kill_heaviest_task = - property_get_bool("ro.lmk.kill_heaviest_task", false); + GET_LMK_PROPERTY(bool, "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", 100); + (unsigned long)GET_LMK_PROPERTY(int32, "kill_timeout_ms", 100); use_minfree_levels = - property_get_bool("ro.lmk.use_minfree_levels", false); + GET_LMK_PROPERTY(bool, "use_minfree_levels", false); per_app_memcg = property_get_bool("ro.config.per_app_memcg", low_ram_device); - swap_free_low_percentage = clamp(0, 100, property_get_int32("ro.lmk.swap_free_low_percentage", + swap_free_low_percentage = clamp(0, 100, GET_LMK_PROPERTY(int32, "swap_free_low_percentage", DEF_LOW_SWAP)); - psi_partial_stall_ms = property_get_int32("ro.lmk.psi_partial_stall_ms", + psi_partial_stall_ms = GET_LMK_PROPERTY(int32, "psi_partial_stall_ms", low_ram_device ? DEF_PARTIAL_STALL_LOWRAM : DEF_PARTIAL_STALL); - psi_complete_stall_ms = property_get_int32("ro.lmk.psi_complete_stall_ms", + psi_complete_stall_ms = GET_LMK_PROPERTY(int32, "psi_complete_stall_ms", DEF_COMPLETE_STALL); - thrashing_limit_pct = max(0, property_get_int32("ro.lmk.thrashing_limit", + thrashing_limit_pct = max(0, GET_LMK_PROPERTY(int32, "thrashing_limit", low_ram_device ? DEF_THRASHING_LOWRAM : DEF_THRASHING)); - thrashing_limit_decay_pct = clamp(0, 100, property_get_int32("ro.lmk.thrashing_limit_decay", + thrashing_limit_decay_pct = clamp(0, 100, GET_LMK_PROPERTY(int32, "thrashing_limit_decay", low_ram_device ? DEF_THRASHING_DECAY_LOWRAM : DEF_THRASHING_DECAY)); - thrashing_critical_pct = max(0, property_get_int32("ro.lmk.thrashing_limit_critical", + thrashing_critical_pct = max(0, GET_LMK_PROPERTY(int32, "thrashing_limit_critical", thrashing_limit_pct * 2)); - swap_util_max = clamp(0, 100, property_get_int32("ro.lmk.swap_util_max", 100)); - filecache_min_kb = property_get_int64("ro.lmk.filecache_min_kb", 0); + swap_util_max = clamp(0, 100, GET_LMK_PROPERTY(int32, "swap_util_max", 100)); + filecache_min_kb = GET_LMK_PROPERTY(int64, "filecache_min_kb", 0); } int main(int argc, char **argv) { diff --git a/lmkd.rc b/lmkd.rc index 17c6560..c9eee68 100644 --- a/lmkd.rc +++ b/lmkd.rc @@ -9,3 +9,37 @@ service lmkd /system/bin/lmkd on property:lmkd.reinit=1 exec_background /system/bin/lmkd --reinit + +# properties most likely to be used in experiments +on property:persist.device_config.lmkd_native.debug=* + setprop lmkd.reinit 1 + +on property:persist.device_config.lmkd_native.kill_heaviest_task=* + setprop lmkd.reinit 1 + +on property:persist.device_config.lmkd_native.kill_timeout_ms=* + setprop lmkd.reinit 1 + +on property:persist.device_config.lmkd_native.swap_free_low_percentage=* + setprop lmkd.reinit 1 + +on property:persist.device_config.lmkd_native.psi_partial_stall_ms=* + setprop lmkd.reinit 1 + +on property:persist.device_config.lmkd_native.psi_complete_stall_ms=* + setprop lmkd.reinit 1 + +on property:persist.device_config.lmkd_native.thrashing_limit=* + setprop lmkd.reinit 1 + +on property:persist.device_config.lmkd_native.thrashing_limit_decay=* + setprop lmkd.reinit 1 + +on property:persist.device_config.lmkd_native.thrashing_limit_critical=* + setprop lmkd.reinit 1 + +on property:persist.device_config.lmkd_native.swap_util_max=* + setprop lmkd.reinit 1 + +on property:persist.device_config.lmkd_native.filecache_min_kb=* + setprop lmkd.reinit 1 From aced711991ac2ab8c6d0d104127358dcdcc9886b Mon Sep 17 00:00:00 2001 From: Suren Baghdasaryan Date: Wed, 1 Sep 2021 00:49:51 -0700 Subject: [PATCH 2/2] lmkd: Do not re-initialize lmkd when persistent properties are loaded When a device boots, lmkd starts before persistent properties are loaded, therefore if experiments set any flags, the corresponding persistent properties will trigger change notifications when they are first loaded during boot. In order to prevent lmkd from re-initializing on every property load, mark persistent property change by setting lmkd.reinit to 0 and delay lmkd re-initialization until sys.boot_completed=1 when all properties are set and only one re-initialization will capture them all. On devices with no experiment flags being set lmkd.reinit will be undefined at the boot completion time and re-initialization will not be triggered at all. Bug: 194316048 Test: adb shell device_config put lmkd_native thrashing_limit_critical 350 Test: adb shell device_config put lmkd_native thrashing_limit 100 Test: adb reboot; adb -b all logcat | grep lmkd Signed-off-by: Suren Baghdasaryan Change-Id: Iba34fc719a18d58b890549c7415bec869d471901 Merged-In: Iba34fc719a18d58b890549c7415bec869d471901 --- lmkd.cpp | 2 +- lmkd.rc | 30 ++++++++++++++++++------------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/lmkd.cpp b/lmkd.cpp index f01141b..68f9729 100644 --- a/lmkd.cpp +++ b/lmkd.cpp @@ -3466,7 +3466,7 @@ static void update_props() { int main(int argc, char **argv) { if ((argc > 1) && argv[1] && !strcmp(argv[1], "--reinit")) { - if (property_set(LMKD_REINIT_PROP, "0")) { + if (property_set(LMKD_REINIT_PROP, "")) { ALOGE("Failed to reset " LMKD_REINIT_PROP " property"); } return issue_reinit(); diff --git a/lmkd.rc b/lmkd.rc index c9eee68..6f90bcb 100644 --- a/lmkd.rc +++ b/lmkd.rc @@ -10,36 +10,42 @@ service lmkd /system/bin/lmkd on property:lmkd.reinit=1 exec_background /system/bin/lmkd --reinit -# properties most likely to be used in experiments -on property:persist.device_config.lmkd_native.debug=* +# reinitialize lmkd after device finished booting if experiments set any flags during boot +on property:sys.boot_completed=1 && property:lmkd.reinit=0 setprop lmkd.reinit 1 +# properties most likely to be used in experiments +# setting persist.device_config.* property either triggers immediate lmkd re-initialization +# if the device finished booting or sets lmkd.reinit=0 to re-initialize lmkd after boot completes +on property:persist.device_config.lmkd_native.debug=* + setprop lmkd.reinit ${sys.boot_completed:-0} + on property:persist.device_config.lmkd_native.kill_heaviest_task=* - setprop lmkd.reinit 1 + setprop lmkd.reinit ${sys.boot_completed:-0} on property:persist.device_config.lmkd_native.kill_timeout_ms=* - setprop lmkd.reinit 1 + setprop lmkd.reinit ${sys.boot_completed:-0} on property:persist.device_config.lmkd_native.swap_free_low_percentage=* - setprop lmkd.reinit 1 + setprop lmkd.reinit ${sys.boot_completed:-0} on property:persist.device_config.lmkd_native.psi_partial_stall_ms=* - setprop lmkd.reinit 1 + setprop lmkd.reinit ${sys.boot_completed:-0} on property:persist.device_config.lmkd_native.psi_complete_stall_ms=* - setprop lmkd.reinit 1 + setprop lmkd.reinit ${sys.boot_completed:-0} on property:persist.device_config.lmkd_native.thrashing_limit=* - setprop lmkd.reinit 1 + setprop lmkd.reinit ${sys.boot_completed:-0} on property:persist.device_config.lmkd_native.thrashing_limit_decay=* - setprop lmkd.reinit 1 + setprop lmkd.reinit ${sys.boot_completed:-0} on property:persist.device_config.lmkd_native.thrashing_limit_critical=* - setprop lmkd.reinit 1 + setprop lmkd.reinit ${sys.boot_completed:-0} on property:persist.device_config.lmkd_native.swap_util_max=* - setprop lmkd.reinit 1 + setprop lmkd.reinit ${sys.boot_completed:-0} on property:persist.device_config.lmkd_native.filecache_min_kb=* - setprop lmkd.reinit 1 + setprop lmkd.reinit ${sys.boot_completed:-0}