Commit Graph

95 Commits

Author SHA1 Message Date
Carlos Galo 8b5a67d35a Applying clang-format suggestions in PROCPRIO cmd
Updating the cmd_procprio function with the clang-format suggestions. No logic changes.

Test: m
Bug: 325525024
Change-Id: Id6c1feb717259406d953e5e2a174398bccf65d23
Signed-off-by: Carlos Galo <carlosgalo@google.com>
2024-05-03 17:02:10 +00:00
Carlos Galo 7da353fb00 Move register proc logic to prepare for new PROCS_PRIO cmd
Moving logic for registering proc, after oom adjustment, to its
own function. This work is for the introduction of the new
PROCS_PRIO cmd. This logic will be shared between the current PROCPRIO
and PROCS_PRIO cmd.

Test: m
Bug: 325525024
Change-Id: I0683f63faa3dfa2e4534cdfb8935b4d2f83a6af9
Signed-off-by: Carlos Galo <carlosgalo@google.com>
2024-05-03 17:01:09 +00:00
Treehugger Robot e1eb315723 Merge "Replace PATH_MAX usages with smaller size" into main 2024-05-03 00:29:11 +00:00
Carlos Galo eccbfe3256 Replace PATH_MAX usages with smaller size
All the PATH_MAX usages are used to store proc/<pid>/filename
information in lmkd. PATH_MAX is 4096, which is an overkill
of buffer sizes for their usage. Replace PATH_MAX with a smaller size.

Test: m
Bug: 325525024
Change-Id: If6d500102fca532a8afc331d0c847675d6e9e96f
Signed-off-by: Carlos Galo <carlosgalo@google.com>
2024-05-03 00:28:02 +00:00
Suren Baghdasaryan fcb1b8a339 Merge "lmkd: introduce lowmem_min_oom_score tunable" into main 2024-04-30 20:08:35 +00:00
Suren Baghdasaryan 3dc50798a3 lmkd: introduce lowmem_min_oom_score tunable
Current lmkd behavior to kill cached apps when free memory hits low
watermark threshold does not work well on certain devices where more
or less aggressive behavior would yield better results. Introduce a
tunable to control the min oom_score_adj level at which lmkd considers
to kill processes when the system gets into this state. The default
value is set to 701 which preserves the current behavior of killing
cached apps except for the last active one. Setting it to lower values
will make more processes eligible to be killed, setting it to higher
values will limit the kills to a smaller set of processes. Setting it
to 1001 will prevent any process from being killed for this reason.

Bug: 334867461
Bug: 337063274
Change-Id: I1447436e0a0cd1e696b34d2c06b92ff73a5100a9
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2024-04-30 12:05:14 -07:00
Lakshman Annadorai d872253483 Update init_psi_monitor to include MEMORY/IO/CPU resources.
Update the init_psi_monitor API to monitor MEMORY, IO, and CPU
resources.

Test: m libpsi
Bug: 335872571
Change-Id: Ieae8c98be0e6353a1d0ca0728c84bcf1897b259c
2024-04-29 13:25:50 -07:00
Suren Baghdasaryan 20de82dde2 lmkd: update watermarks before the first kill
Change-Id: Ie8080fd8995c54fc8c1f5b94e77dbf32e33783fe
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2024-04-05 17:58:41 -07:00
Suren Baghdasaryan 63dd69984d 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 <surenb@google.com>
2024-04-04 23:28:04 +00:00
Carlos Galo 6593e2cee6 Adding kswapd state monitoring with memevent_listener
Replacing mechanism of reading vmstats to detect kswapd with
memevents instead. Maintain vmstats mechanism if bpf is not supported by
current kernel.

Test: Verified lmkd receives kswapd state changes
Test: m
Bug: 330606003
Change-Id: I9b980a8b94e015d1b8e0986fff9113890420d102
Signed-off-by: Carlos Galo <carlosgalo@google.com>
2024-04-03 15:38:07 -07:00
Carlos Galo f9dfb20456 Renaming direct reclaim monitoring to memevent monitoring
This monitoring will no longer only track direct reclaim state changes.
Adjusting naming to reflect the broader utilization.

Test: m
Bug: 330606003
Change-Id: Ib77b8b58cd6e8ce1296ffa14481018c29e979754
Signed-off-by: Carlos Galo <carlosgalo@google.com>
2024-04-03 00:19:26 -07:00
Carlos Galo 65c1b5dcc5 Add boot completed cmd to LMKD
Add new command to allow for post-boot actions to occur. This
will allow for the MemEventListener to start after the BPF files are
loaded, removing the need to stall during boot-up until they are loaded
in the device.

Test: Verified memevent listener initialized post-boot
Test: Verified LMKD no longer stalls until BPF progs are loaded
Bug: 331008250
Bug: 244232958
Change-Id: I55f97b41349ea7693cff81b1170d33712b820292
Signed-off-by: Carlos Galo <carlosgalo@google.com>
2024-04-02 22:25:36 +00:00
Carlos Galo 81a7c21087 lmkd: Introduce kill strategy based on direct reclaim length
Add kill reason for when the device is stuck in direct reclaim for
longer than the configurable threshold. Only allow configurable
threshold, and direct reclaim stuck detection, if memevents direct
reclaim monitoring is enabled.

Test: Verified direct reclaim stuck kill log with memory pressure test
Test: m
Bug: 244232958
Change-Id: I1156899874d2eb7e0f4b61597741087c110b3414
Signed-off-by: Carlos Galo <carlosgalo@google.com>
2024-03-22 16:21:30 -07:00
Carlos Galo 9e136285a6 Adding direct reclaim state monitoring
Replacing mechanism of reading vmstats to detect direct reclaim with
memevents instead. Maintain vmstats mechanism if bpf ring buffer is not
supported by current kernel.

Test: Verified lmkd receives direct reclaim state changes
Test: m
Bug: 244232958
Change-Id: I59ee7657da1240355d611dfa129c4d50bed2c330
Signed-off-by: Carlos Galo <carlosgalo@google.com>
2024-03-20 06:46:04 +00:00
Matt Stephenson 18578a55d0 add min score option to PRESSURE_AFTER_KILL
Adjust lmk cycle after kill to honor an optional min score.

Test: cycle after kill under mem pressure honors specified score
Bug: 309380316
Change-Id: I9ab8e29b58846cc291acb2834638ddf7a7759eca
Signed-off-by: Matt Stephenson <stephensonmatt@google.com>
2023-12-11 22:35:50 +00:00
T.J. Mercier e2288909ab Merge "Use correct language for cgroups" into main 2023-11-06 21:44:18 +00:00
Suren Baghdasaryan 9e4d3ad1a2 lmkd: Add new low memory kill reason
Add kill reason for a cached app kill when free memory is under low
watermark.

Bug: 306755741
Change-Id: Idf92da326f6e0990e6d9fd9acdd21b19f6bdd241
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2023-10-31 13:02:55 -07:00
T.J. Mercier c2b250bb7e Use correct language for cgroups
Refer to cgroups and the cgroup hierarchy by their correct names.

Bug: 301871933
Change-Id: Ibe65900812e1de3b60f5b7104ae2c9a02a3f8a8c
2023-10-27 18:12:06 +00:00
Suren Baghdasaryan 0ac96fcb1a lmkd: Kill cached apps when thrashing or below low watermark
With Android U removing AMS kills, lmkd has additional duty to kill
cached apps which previously were killed by AMS. The former logic is
not proactive enough and leads to too many cached apps contributing
to memory pressure.
Implement additional logic to kill cached apps (excluding previous
foreground apps) when low watermark is breached or when device is
thrashing.

Bug: 300660611
Change-Id: I356eac1fe6d44dad292a7ea2fadee69a5be61479
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2023-10-10 15:52:24 -07:00
Suren Baghdasaryan ab906fb0ce lmkd: Change critical thrashing limit to 3x of normal one
As a result of experiments, the default relation between critical and
normal thrashing limits has been shown to be insufficient. Increase the
relation from 2x to 3x.

Bug: 194316048
Change-Id: I19877e0df56be07f3f503688f408f5f91f4b1e67
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2023-10-05 15:07:04 +00:00
Kalesh Singh f6f744fcc9 Merge "lmkd: Remove uses of hardcoded 4k PAGE_SIZE macro" into main 2023-08-09 00:18:34 +00:00
Kalesh Singh 5d397582ac lmkd: Remove uses of hardcoded 4k PAGE_SIZE macro
Use getpagesize() to query the real page size instead.

Bug: 294618124
Test: m
Change-Id: If9046f36412a54ba08b94cf3b43cd7bf9c1f26b5
2023-08-08 15:58:16 -07:00
Suren Baghdasaryan 4d8791b1f1 lmkd: check pgrefill vmstat when deciding active reclaim
In rare cases it's possible that pgscan is not changing because inactive
LRU is empty and can't be refilled from the active LRU due to all
pages being hot. In such conditions pgscan_kswapd/pgscan_direct will
not change while pgrefill will be increasing due to active LRU being
scanned. Lmkd would incorrectly treat this situation as if no reclaim
activity happened.
Change lmkd to check pgrefill as well to detect such conditions.

Bug: 288383787
Change-Id: I6b49607429e2f673bba2645ccddff1a141afbcd1
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2023-07-28 20:48:15 +00:00
Lee George Thomas 1847e9d7ab Add a configuration to delay monitor initialization
To save CPU cycles during boot for low resource device a new
configuration is added to delay initialization of monitoring until boot
is complete.

Bug: 288566858
Test: Build, boot and verified boot logs to confirm the behavior.
Merged-In: I17cfbf4c7f83bc80dd92a99dfb0254a7e20289be

Change-Id: I17cfbf4c7f83bc80dd92a99dfb0254a7e20289be
2023-07-19 19:46:12 +00:00
Suren Baghdasaryan 5860e852f8 lmkd: remove unused LMK_STAT_STATE_CHANGED notification
The LmkStateChanged atom was historically used to mark lmk activity
and trigger additional stats polling. For more than a year this has
not been used at all (as statsd supported event-based triggering).
Remove unnecessary functionality.

Bug: 278174420
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Change-Id: I9f7f56711fabb751cf7a57ea7279759bcc4a3dff
2023-05-19 14:08:10 -07:00
Kameron Lutes 556740ef04 lmkd: Send Actual OOM Score to lmkd_free_memory_before_kill_hook
Previously the min_oom score of the candidate search was sent to
lmkd_free_memory_before_kill_hook. This is incorrect as the hook expects
the actual oom score of the process.

Bug: b/273670531
Test: cq
Change-Id: Id72c8b39f9c745a8f20fde15266857cb2d2222bf
2023-03-22 00:33:30 +00:00
Suren Baghdasaryan 495db5c643 lmkd: measure free swap as available and easily reclaimable memory
In the case of ZRAM, SwapFree does not represent the actual free swap
amount because swap space is taken from the free memory or reclaimed.
Therefore use free memory and easily reclaimable memory as an
approximation of how much free swap system can use. Use SwapFree as
a measure of how much swap space the system will consider using. Min
of those two measurements is used to decide how much usable swap the
system still has.

Bug: 238495258
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Change-Id: Ia7b0cc4a744d14c0d6e52603795917cf5824ea15
2022-10-04 12:53:23 -07:00
Suren Baghdasaryan ba9ea6e3d6 lmkd: Fix UAF caused by calling pid_remove() from the watchdog thread
pid_remove() is not a thread-safe function and can be called only from
the main thread. Calling it from the watchdog_callback() executed in the
context of the watchdog thread can cause a use-after-free failure if the
same record is being used by the main thread. Fix this issue by marking
the process record as invalid instead of destroying it. Later on invalid
records will be cleared in kill_one_process() called from the context of
the main thread.

Fixes: f8727745f9 ("lmkd: Remove process record after it is killed by lmkd watchdog")
Bug: 248448498
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Change-Id: I0c7776aea1518c17f0a29904a44b7fe8f33980ca
2022-09-27 14:30:34 -07:00
Suren Baghdasaryan c555ec6eeb lmkd: Remove process record after it is killed by lmkd watchdog
After lmkd watchdog kills a process, its record should be removed.

Bug: 243567425
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Change-Id: I70bb2a432df8088ebc9865fbc36b065738248d19
2022-08-23 15:28:53 -07:00
Ioannis Ilkos b9d0592bba Remove kill_one_process tracepoint from lmkd
We already emit a richer slice (including pid and oom score) so there's
no reason for the additional print event

Bug: 195085238
Change-Id: I1140f0287934e5f0abdeeb64554a249c4c940def
2022-08-04 14:45:24 +01:00
Ken Chen 310fa3ab1b Merge "Rename gpu_mem.o to gpuMem.o" 2022-07-21 13:43:50 +00:00
Ken Chen 4c18e91f22 Rename gpu_mem.o to gpuMem.o
Underscore character may cause bpf prog/map naming collision. For
example, x.o with map y_z and x_y.o with map z both result in x_y_z
prog/map name, which should be prevented during compile-time.

aosp/2147825 will prohibit underscore character in bpf source name
(source name derives the obj name). Existing bpf modules with underscore
characters in source name need to be updated accordingly.

Bug: 236706995
Test: adb root; adb shell ls -l /sys/fs/bpf/ | grep gpuMem
Change-Id: I213624e59ce1bca6ee7c22028504f2d51e9c50df
2022-07-08 19:09:50 +08:00
Yuming Han ed8fc168e6 lmkd : Fixed running wrong for Go devices when use_minfree_levels is TRUE
The lmkd run wrong for Go devices, because min_score_adj is unused
when use_minfree_levels is set TURE.

Bug: 237947900
Signed-off-by: Yuming Han <yuming.han@unisoc.com>
Change-Id: I717561cd9e5f4d1a2ca60d9fc84adcd6e129420a
2022-07-07 18:24:22 +00:00
Yuming Han 79f58c012d lmkd: Fixed data overflow on ARM
Both pgscan_kwsapd and pgscan_direct are defined as unsigned long,
the overflow issues occur on ARM kernel space. Just check whether
their values changed.

Signed-off-by: Yuming Han <yuming.han@unisoc.com>
Change-Id: I73b27855ede9ca729208775e982660bae967ab92
2022-06-29 16:06:18 +08:00
Suren Baghdasaryan caebcddf9f lmkd: Fix the size of vmstat_field_names array
The size of the vmstat_field_names array should correspond to the number
of elements in vmstat_field enum (VS_FIELD_COUNT).

Bug: 227769256
Reported-by: Yuming Han <yuming.han@unisoc.com>
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Change-Id: Icac2810c4efca2a07cefba6e220165ef4f194867
2022-06-22 23:10:47 +00:00
Kameron Lutes e9769f7cf3 lmkd: Fix potential null dereference in hook call
If hooks are enabled in LMKD and kill_info is not supplied to
kill_one_process, there will be a null dereference on kill_info. This
changes validates ki before dereferencing.

Bug: b/210075795
Test: cq
Change-Id: Ie81ca9bdb73a71f16dc5682c8721a557b8b094fb
Merged-In: Ie81ca9bdb73a71f16dc5682c8721a557b8b094fb
2022-06-22 03:40:33 +00:00
Kameron Lutes 2cce3066da lmkd: Add hooks to LMKD
Adds several hooks to LMKD that can be overridden by the vendor. This
allows for device specific control of LMKD when necessary.

Bug: b/210075795
Test: cq

Change-Id: Ib231743183134b05148d45d681765860da6274ae
(cherry picked from commit 2c1248381a52fc520c6cd1acfaee80818eaa9ee1)
Merged-In: Ib231743183134b05148d45d681765860da6274ae
2022-06-22 03:35:23 +00:00
Yongqin Liu bf819b5593 lmkd: fix the cgroup attribute name to MemCgroupEventControl
which was CgroupEventControl before, but it's not the one
that definied in system/core/libprocessgroup/profiles/task_profiles.json.

And it causes lmkd crash for some setups like 4.19q + AOSP Master

Bug: 230642311

Test: boot the 4.19q + AOSP master setup with hikey960 board

Signed-off-by: Yongqin Liu <yongqin.liu@linaro.org>
Change-Id: I87b1ea2040f21c52d549db58692fc8a2b114f8e6
2022-04-29 18:50:20 +08:00
전윤재 f70a8a260f lmkd: Fix a comparison operation with uninitialized variable.
Prevent comparing uninitialized poll_start_tm with curr_tm in call_handler().
The bug caused by this has been fixed by the commit: d816ab.
But the main bug is not fixed yet and it may cause problem
in later if we add another operations in this if block.

Change-Id: Id13318297a2cbf2f9784134a2ccd648cc221e8c4
Signed-off-by: Yoonjae Jeon <yj213.jeon@samsung.com>
2022-04-25 00:51:47 +00:00
Bart Van Assche 759943643f lmkd: Add support for cgroups v2 memcg hierarchy
Use the /sys/fs/cgroup/uid_%u/pid_%u path instead of
/dev/memcg/apps/uid_%u/pid_%u" if the memcg controller is mounted in the
v2 cgroup hierarchy. Skip the code that refers to memcg attributes that
only exist in the cgroup v1 hierarchy when using the v2 memcg. Complain
if it is attempted to use the old kill strategy in combination with
memcg v2 since only the new strategy is compatible with the v2 cgroup
hiearchy.

Bug: 213617178
Test: Tested lmkd inside the Cuttlefish emulator. Triggered an
Test: out-of-memory condition as follows:
Test: i=0; while [ $i -lt 16 ]; do dd if=/dev/zero of=/dev/null bs=1G count=1 & ((i++)) done
Test: That caused the following output to appear in logcat:
Test: 02-03 18:13:02.772   241   241 I lowmemorykiller: Kill 'com.android.packageinstaller' (3031), uid 10022, oom_score_adj 975 to free 29348kB rss, 21232kB swap; reason: min watermark is breached and swap is low (135016kB < 150384kB)
Test: 02-03 18:13:02.772   241   241 I killinfo: [3031,10022,975,0,29348,3,73644,98460,8,1000,33448,40220,1503848,135016,81720,1387040,27268,29520,50348,103164,20176,57664,0,0,0,519,103,5,0,21232,30016,0,2]
Test: From the kernel log:
Test: [  302.834958] Out of memory: Killed process 3017 (ADB-JDWP Connec) total-vm:13522856kB, anon-rss:0kB, file-rss:0kB, shmem-rss:560kB, UID:10052 pgtables:1008kB oom_score_adj:975
Test: [  303.223702] Out of memory: Killed process 2859 (HeapTaskDaemon) total-vm:13534452kB, anon-rss:0kB, file-rss:0kB, shmem-rss:560kB, UID:10051 pgtables:1060kB oom_score_adj:965
Test: [  303.478833] Out of memory: Killed process 2816 (Signal Catcher) total-vm:13524108kB, anon-rss:0kB, file-rss:0kB, shmem-rss:564kB, UID:10073 pgtables:1016kB oom_score_adj:965
Test: [  304.823796] Out of memory: Killed process 2438 (ReferenceQueueD) total-vm:13529180kB, anon-rss:0kB, file-rss:0kB, shmem-rss:568kB, UID:10015 pgtables:1056kB oom_score_adj:955
Test: [  305.226728] Out of memory: Killed process 3126 (DefaultDispatch) total-vm:13532596kB, anon-rss:0kB, file-rss:0kB, shmem-rss:528kB, UID:10019 pgtables:1064kB oom_score_adj:945
Test: [  305.935615] Out of memory: Killed process 2637 (Jit thread pool) total-vm:13523084kB, anon-rss:0kB, file-rss:0kB, shmem-rss:0kB, UID:10024 pgtables:1036kB oom_score_adj:935
Test: [  307.055895] Out of memory: Killed process 2063 (HeapTaskDaemon) total-vm:13755876kB, anon-rss:0kB, file-rss:0kB, shmem-rss:11600kB, UID:10074 pgtables:1552kB oom_score_adj:0
Test: [  307.398512] Out of memory: Killed process 2298 (rs.media.module) total-vm:13560404kB, anon-rss:12924kB, file-rss:0kB, shmem-rss:720kB, UID:10076 pgtables:1156kB oom_score_adj:-700
Test: [  309.888679] Out of memory: Killed process 1745 (droid.bluetooth) total-vm:13720424kB, anon-rss:11084kB, file-rss:0kB, shmem-rss:208kB, UID:1002 pgtables:1220kB oom_score_adj:-700
Test: [  311.050133] Out of memory: Killed process 1759 (ndroid.systemui) total-vm:13852268kB, anon-rss:26468kB, file-rss:0kB, shmem-rss:6988kB, UID:10075 pgtables:1628kB oom_score_adj:-800
Test: [  313.520057] Out of memory: Killed process 2000 (m.android.phone) total-vm:13593404kB, anon-rss:14600kB, file-rss:236kB, shmem-rss:340kB, UID:1001 pgtables:1312kB oom_score_adj:-800
Test: [  314.337744] Out of memory: Killed process 1911 (rkstack.process) total-vm:13540420kB, anon-rss:8232kB, file-rss:0kB, shmem-rss:180kB, UID:1073 pgtables:1128kB oom_score_adj:-800
Test: [  314.745542] Out of memory: Killed process 1984 (com.android.se) total-vm:13525236kB, anon-rss:6944kB, file-rss:0kB, shmem-rss:188kB, UID:1068 pgtables:1012kB oom_score_adj:-800
Test: [  315.007212] Out of memory: Killed process 677 (system_server) total-vm:15049836kB, anon-rss:1856kB, file-rss:0kB, shmem-rss:240kB, UID:1000 pgtables:2084kB oom_score_adj:-900
Change-Id: I78821bcd332af7b3f642f037faa2df15a937dc26
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-03-29 13:38:04 -07:00
Bart Van Assche b4d26bb22b lmkd: Look up cgroup attribute paths instead of hardcoding these
Retrieve cgroup attribute paths from task_profiles.json instead of
hardcoding these paths.

Bug: 213617178
Test: Tested lmkd in Cuttlefish.
Change-Id: I03f40ac8ccd4635f21432214e1acf997c505d1e9
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-03-24 15:58:02 -07:00
Bart Van Assche 5ebc4e8f51 lmkd: Fix a potential buffer overflow
Prevent that the statement that writes '\0' past the read data can write
past the end of the buffer.

Bug: 213617178
Test: Compile-tested only.
Change-Id: I6922c343a6bcb52dce0b5cf54f09b2850e9dfde2
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-02-23 17:05:11 +00:00
Bart Van Assche 545067957c lmkd: Use std::array<> and remove the ARRAY_SIZE() definition
Using ARRAY_SIZE() on a pointer yields 1 while applying .size() to a
pointer triggers a compiler error. Hence use .size() instead of
ARRAY_SIZE().

Bug: 213617178
Test: Compile-tested only.
Change-Id: Ie0f9740f59470c943f8d62b9475f7f987ed8707b
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-02-23 17:05:11 +00:00
Bart Van Assche 80a3dba57a lmkd: Use std::min() and std::max() instead of defining min() and max() macros
std::min() and std::max() check whether the argument types match but
min() and max() not. Hence switch to std::min() and std::max().

Bug: 213617178
Test: Compile-tested only.
Change-Id: Iaf1f63360c9360938db56d485dbd1e504129c52d
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-02-23 17:05:10 +00:00
Suren Baghdasaryan 014dd7156e lmkd: Log psi averages in killinfo reports
Add psi memory, io and cpu 10sec averages into killinfo reports.

Bug: 205182133
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Change-Id: Ifbcf47cab291fb20dbf0b5d32f1965f4e6462b49
2022-02-21 20:30:50 -08:00
Suren Baghdasaryan 5ae47a9563 lmkd: Allow killing perceptible apps when recorded stall is too high
When system is under heavy memory pressure the system might be able
to keep free memory above the min watermark avoiding perceptible app
kills. In such situation system might end up using all its cpu
capacity on memory reclaim and not doing productive work. To detect
this condition, check memory full stall and compare it with the new
ro.lmk.stall_limit_critical tunable representing the stall threshold.
When the recorded level is over ro.lmk.stall_limit_critical, lmkd will
be allowed to kill perceptible apps. ro.lmk.stall_limit_critical
represents the max memory full stall in % that is allowed before
perceptible apps will get killed. By default it is set to 100%, which
effectively disables the feature.
Currently system stall is measured based on psi memory stall 10s average
value, however this definition might change in the future if better
metrics are developed. Setting ro.lmk.stall_limit_critical to 5 means
the system should be fully stalled (no productive work is done) for 5%
of the 10sec period, resulting in 0.5 sec loss due to the stall.

Bug: 205182133
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Change-Id: I9713e30d82641d86d1b7edb5e1ba2971b935c898
2022-02-21 20:27:45 -08:00
Suren Baghdasaryan 2bdf7f0c74 lmkd: Set task profiles to the entire process
set_process_group_and_prio sets task profiles for each thread in the
process separately. This can be avoided by setting task profiles to
the entire process using its pid.

Bug: 215557553
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Change-Id: I9c1917172019a42809385f6c9c084b8cb343b520
2022-01-24 13:43:42 -08:00
Suren Baghdasaryan af1b0e0627 lmkd: Implement watchdog thread
To detect lmkd being stuck on a syscall for prolonged period of time,
introduce a watchdog thread which gets set when lmkd starts handling of
events and is reset after handling is done. If it takes more than the
timeout period (2 sec) to handle an event, watchdog wakes up and kills
the least important process to prevent mounting memory pressure caused
by lmkd lockup. After a kill, watchdog will wait for the reset for
another timeout period and kill again. This repeats until lmkd unlocks
and resets the watchdog.

Bug: 201671997
Test: induce random sleep in lmkd main handler and observe watchdog kills
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Change-Id: I56a55834582e11c06cc6cf9da3bc7380e634b301
2022-01-06 18:14:25 +00:00
Suren Baghdasaryan 7c3addb2a1 lmkd: Use process_mrelease to reap the target process from a thread
process_mrelease syscall can be used to expedite memory release of
a process after it was killed. This allows memory to be released
without the target process being scheduled, therefore does not depend
on target's priority or the CPU it's running on.
However process_mrelease syscall can take considerable time. Blocking
lmkd main thread during that time can cause memory pressure events
being missed while lmkd is busy reaping previous target's memory.
For this reason reaping should be done in a separate thread. This way
lmkd main thread can keep monitoring memory pressure while memory is
being released.
Introduce Reaper class which maintains a pool of threads to perform
process killing and reaping. The main thread submits a request to the
Reaper to kill and reap the process without blocking. If all the threads
in the pool are busy at the time the next kill is needed, the kill is
performed by the main thread without reaping.

Bug: 130172058
Bug: 189803002
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Change-Id: If7b10fdd1838bdfeea3fed3031565feffe0b52be
2022-01-06 18:14:14 +00:00
liuhailong cf8af501dc lmkd: Fix lowmem_minfree out of bounds
lmkd daemon launches before system_server. If lowmem_targets_size
does not initialize by system_server, the value will be zero.
Before system_server starts lmkd receives a psi event
and debug_process_killing on, the lmkd crashes here.

Bug: 209090314
Signed-off-by: liuhailong <liuhailong@oppo.com>
Change-Id: I0736a882ed1ff5eee2b07676ae590a2cb2a7721c
2021-12-04 17:59:24 +08:00