Commit Graph

167 Commits

Author SHA1 Message Date
Suren Baghdasaryan df6f9e30ed lmkd: Introduce support for legacy kill algorithm that uses minfree levels
am: ffdc4ddb25

Change-Id: Ic18a67fe5cbdf541f1b6c439e2b7f0488d4d1dcf
2018-04-16 20:45:49 -07:00
Suren Baghdasaryan 81999c34b4 lmkd: Switch to using /proc/meminfo to have access to file cache size
am: 9926e57ee8

Change-Id: Ie3f2773b2dc970ddea5b67632921eedfe87afd4d
2018-04-16 20:44:46 -07:00
Suren Baghdasaryan a15a9c423d lmkd: Add zoneinfo and meminfo parsing routines
am: 8b9deaf618

Change-Id: I1132199473d335197cbc9677431d3e2937e123c6
2018-04-16 20:44:28 -07:00
Suren Baghdasaryan ffdc4ddb25 lmkd: Introduce support for legacy kill algorithm that uses minfree levels
Add ability to switch to the algorithm used by lowmemorykiller driver
for determining when to kill. It uses minfree levels to decide at which
levels of free memory and file cache to kill a process. oom_adj_score
is also determined by comparing current memory resources against minfree
levels.
ro.lmk.use_minfree_levels property is introduces for switching into this
mode. By default it is disabled.

Bug: 77299493
Bug: 75322373
Change-Id: I6b51972951026854a079fcda33d6786b7ed035e4
Merged-In: I6b51972951026854a079fcda33d6786b7ed035e4
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
(cherry picked from commit d273b6630d)
2018-04-17 01:42:06 +00:00
Suren Baghdasaryan 9926e57ee8 lmkd: Switch to using /proc/meminfo to have access to file cache size
Current mechanism of getting system memory state by using sysinfo()
is not enough because it does not provide information about file cache
size which is needed to correctly assess memory state. Switch to using
data from /proc/meminfo that includes information provided by sysinfo()
and file cache size in addition to that.

Bug: 77299493
Bug: 75322373
Change-Id: I16106fc4f9254f17f776803e60502b7b6474e1b7
Merged-In: I16106fc4f9254f17f776803e60502b7b6474e1b7
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
(cherry picked from commit 9ac54eb2f8)
2018-04-17 01:41:59 +00:00
Suren Baghdasaryan 8b9deaf618 lmkd: Add zoneinfo and meminfo parsing routines
/proc/zoneinfo and /proc/meminfo contain information necessary for lmkd
to assess system memory state. Add routines to parse these files.

Bug: 77299493
Bug: 75322373
Change-Id: Ie7d80bbb81fd0d2fc0629f6f678e6afc97fed1f6
Merged-In: Ie7d80bbb81fd0d2fc0629f6f678e6afc97fed1f6
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
(cherry picked from commit da0bc05b22)
2018-04-17 01:41:38 +00:00
Mark Salyzyn 64d97d8761 lmkd: limit capability set to minimum
Set F() capability set and 'drop' lmkd from AID_ROOT to AID_LMKD uid
and from AID_ROOT to AID_LMKD and AID_SYSTEM gid.

/dev/memcg/memory.pressure defaults to root.root mode 0000, set it up
as root.system mode 0040 to allow lmkd read access.

Instrument failure to set SCHED_FIFO.

Annotate access points that require elevated capabilities.

Test: check /proc/`pidof lmkd`/status for capability set
Test: lmkd_unit_test
Bug: 77650566
Change-Id: I986081a0434cf6e842b63a55726380205b30a3ea
2018-04-16 14:51:56 -07:00
Suren Baghdasaryan b2d59ee367 lmkd: Optimize frequent file reads by keeping file descriptors open
To check system memory state lmkd is using same files every time vmpressure
event is received. Instead of opening and closing these files every time
we store the file descriptor and use pread() to reread the file from
the beginning.

Bug: 77299493
Bug: 75322373
Change-Id: I8e27f8b9526e82e3cc313a02fce215c2e4dd3d29
Merged-In: I8e27f8b9526e82e3cc313a02fce215c2e4dd3d29
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
(cherry picked from commit d716fe3610)
2018-04-16 09:33:44 -07:00
Suren Baghdasaryan 9ff66ff0b3 lmkd: Fix usage of ro.lmk.kill_heaviest_task property
lmkd should use ro.lmk.kill_heaviest_task property to select between
algorithms for victim selection. Set ro.lmk.kill_heaviest_task default
value to false in order to keep it compatible with previous versions
of lmkd (killing the heaviest task is a new mechanism).

Bug: 77299493
Bug: 75322373
Change-Id: I78d2dc79d9c54e636c26665605518d9c87b535b3
Merged-In: I78d2dc79d9c54e636c26665605518d9c87b535b3
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
(cherry picked from commit 818b59b229)
2018-04-16 09:30:29 -07:00
Suren Baghdasaryan 39a22e751b lmkd: Rename is_go_device variable to better reflect its function
Rename is_go_device variable to low_ram_device to better reflect
its meaning and relation to ro.config.low_ram variable.

Bug: 77299493
Bug: 75322373
Change-Id: I6e2eaebe79cf2e6edf861f7c602e52a5b573ad0a
Merged-In: I6e2eaebe79cf2e6edf861f7c602e52a5b573ad0a
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
(cherry picked from commit fe2be6cc44073b7ef09ca849004e5072bdf18857)
2018-04-16 09:27:32 -07:00
Suren Baghdasaryan 9dbd937b4c lmkd: Add lmkd README file
Add README file providing lmkd overview and description for its
properties.

Bug: 77299493
Bug: 75322373
Change-Id: I30236dd4ccaa2b48c31dfb80b63f0b82b49b5744
Merged-In: I30236dd4ccaa2b48c31dfb80b63f0b82b49b5744
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
(cherry picked from commit 7e5dea82a5)
2018-04-16 09:26:40 -07:00
Florian Mayer 7725a9d9aa Merge commit '6499e5ec' into pi-dev-plus-aosp 2018-04-16 14:55:03 +01:00
Suren Baghdasaryan c09b53db7b lmkd: Introduce support for legacy kill algorithm that uses minfree levels
Add ability to switch to the algorithm used by lowmemorykiller driver
for determining when to kill. It uses minfree levels to decide at which
levels of free memory and file cache to kill a process. oom_adj_score
is also determined by comparing current memory resources against minfree
levels.
ro.lmk.use_minfree_levels property is introduces for switching into this
mode. By default it is disabled.

Bug: 77299493
Bug: 75322373
Merged-In: I6b51972951026854a079fcda33d6786b7ed035e4
Change-Id: I6b51972951026854a079fcda33d6786b7ed035e4
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-04-15 16:02:36 +00:00
Suren Baghdasaryan 45c9e0b30c lmkd: Switch to using /proc/meminfo to have access to file cache size
Current mechanism of getting system memory state by using sysinfo()
is not enough because it does not provide information about file cache
size which is needed to correctly assess memory state. Switch to using
data from /proc/meminfo that includes information provided by sysinfo()
and file cache size in addition to that.


Bug: 77299493
Bug: 75322373
Merged-In: I16106fc4f9254f17f776803e60502b7b6474e1b7
Change-Id: I16106fc4f9254f17f776803e60502b7b6474e1b7
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-04-15 10:03:05 +00:00
Suren Baghdasaryan ceffb41162 lmkd: Add zoneinfo and meminfo parsing routines
/proc/zoneinfo and /proc/meminfo contain information necessary for lmkd
to assess system memory state. Add routines to parse these files.


Bug: 77299493
Bug: 75322373
Merged-In: Ie7d80bbb81fd0d2fc0629f6f678e6afc97fed1f6
Change-Id: Ie7d80bbb81fd0d2fc0629f6f678e6afc97fed1f6
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-04-15 07:55:27 +00:00
Suren Baghdasaryan 6499e5ec4a lmkd: Optimize frequent file reads by keeping file descriptors open
To check system memory state lmkd is using same files every time vmpressure
event is received. Instead of opening and closing these files every time
we store the file descriptor and use pread() to reread the file from
the beginning.


Bug: 77299493
Bug: 75322373
Merged-In: I8e27f8b9526e82e3cc313a02fce215c2e4dd3d29
Change-Id: I8e27f8b9526e82e3cc313a02fce215c2e4dd3d29
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-04-15 07:54:03 +00:00
Suren Baghdasaryan 6607770968 Merge changes I78d2dc79,I6e2eaebe am: bb266150fe
am: 4d41a1f882

Change-Id: If65529061b6bd49c06b0eae37dd9e196fa805ab7
2018-04-14 16:25:15 -07:00
Suren Baghdasaryan 94b5686aca Merge "lmkd: Add lmkd README file" am: b518418fbb
am: 29aca8b0be

Change-Id: I0153fae612008cc2fe75a1ae8f29c8c4fe90d4b8
2018-04-14 16:03:45 -07:00
Suren Baghdasaryan 818b59b229 lmkd: Fix usage of ro.lmk.kill_heaviest_task property
lmkd should use ro.lmk.kill_heaviest_task property to select between
algorithms for victim selection. Set ro.lmk.kill_heaviest_task default
value to false in order to keep it compatible with previous versions
of lmkd (killing the heaviest task is a new mechanism).

Change-Id: I78d2dc79d9c54e636c26665605518d9c87b535b3
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-04-13 23:24:24 +00:00
Suren Baghdasaryan ff61afb002 lmkd: Rename is_go_device variable to better reflect its function
Rename is_go_device variable to low_ram_device to better reflect
its meaning and relation to ro.config.low_ram variable.

Change-Id: I6e2eaebe79cf2e6edf861f7c602e52a5b573ad0a
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-04-13 23:24:09 +00:00
Suren Baghdasaryan 7e5dea82a5 lmkd: Add lmkd README file
Add README file providing lmkd overview and description for its
properties.

Change-Id: I30236dd4ccaa2b48c31dfb80b63f0b82b49b5744
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-04-13 21:36:44 +00:00
Mark Salyzyn be5235619f resolve merge conflicts of f062a2cd9e to pi-dev-plus-aosp
Test: I solemnly swear I tested this conflict resolution.
Change-Id: I5eb1bc16bf25b27172b6d2ea355768df2f6d8ff7
2018-04-06 07:56:54 -07:00
Mark Salyzyn aa0b84afca Merge "lmkd: test: access /sys/module/lowmemorykiller/ as u:r:shell:s0" am: b69cb7071d
am: 4f7d8851a5

Change-Id: I1ef7aab68bfb5a11891ef4777f6dc46774120a21
2018-04-05 19:45:16 -07:00
Mark Salyzyn 721d7c7fa3 lmkd: only mlockall() if enabled
Move process initialization, mlockall() and sched_setscheduler() to
only occur if initialization succeeds and is enabled.  This
conserves mlock'd memory and a FIFO processing slot if (deprecated)
kernel lowmemorykiller is active.

Cleanup: Android coding standard compliance.

Test: lmkd_unit_test
Bug: 33808187
Bug: 72838192
Change-Id: I51af0235c8eca4ef958ef2d83a8071ff58b49d78
2018-04-03 14:41:26 +00:00
Mark Salyzyn ce32539cf3 lmkd: test: access /sys/module/lowmemorykiller/ as u:r:shell:s0
lmkd_unit_test gets an selinux violation when run unprivileged:

lmkd_unit_test: type=1400 audit(0.0:???): avc: denied { search } for
    name="lowmemorykiller" dev="sysfs" ino=??? scontext=u:r:shell:s0
    tcontext=u:object_r:sysfs_lowmemorykiller:s0 tclass=dir
    permissive=0

resulting in log noise.  Check for INKERNEL_MINFREE_PATH existence
after known to be escalated as root user to suppress noise.

Test: lmkd_unit_test as unprivileged user, there should be no audits.
Change-Id: Ia5c7824e7070c98ee6eea20ca53097e4e56d61b6
2018-04-03 14:41:11 +00:00
Greg Kaiser d6d847142b lmkd: Protect against buffer overflow
We're passing a 'line' whose backing buffer is PAGE_MAX in size
into memory_stat_parse_line().  We protect overflowing the smaller
LINE_MAX 'key' buffer via some C preprocessing macros to assure
we limit the size.

Test: Local build with LMKD_LOG_STATS set for this file.
Bug: 76220622
Merged-In: I9e50d4270f7099e37a9bfc7fb9b9b95cc7adb086
Change-Id: I9e50d4270f7099e37a9bfc7fb9b9b95cc7adb086
2018-03-27 21:59:31 +00:00
Rajeev Kumar f09c593dd5 Merge "Use usel_mkd_stats_log build variable while compiling lmkd with stats logging feature." into pi-dev
am: 42c17c8ee5

Change-Id: I4d5b2851bb465dfaf0c8c7b863611685aedaf03d
2018-03-26 23:34:24 +00:00
TreeHugger Robot 42c17c8ee5 Merge "Use usel_mkd_stats_log build variable while compiling lmkd with stats logging feature." into pi-dev 2018-03-26 23:14:23 +00:00
Rajeev Kumar 716fe89b88 Fix compilation issue when LMKD_LOG_STATS is defined.
am: 1c669f764d

Change-Id: I750b6a13b1a897b3dc77e1823a997afe0c40e4a3
2018-03-26 21:46:22 +00:00
Rajeev Kumar 1c669f764d Fix compilation issue when LMKD_LOG_STATS is defined.
Note: The breakage was caused by http://ag/3621623

Test: mmma system/core/lmkd/
Change-Id: I17033aeedb3183d4777dacb89ec84430ff061b3c
Bug: 74443701
(cherry picked from commit fb25ddd9c9)
2018-03-26 18:15:25 +00:00
Rajeev Kumar b7273785a2 Use usel_mkd_stats_log build variable while compiling lmkd with stats logging feature.
Bug: 74443701
Test: Tested manually
Merged-In: Ifa13cc7d1c3ff3ac71a16cf55be5a48ce808ef59
Change-Id: Ifa13cc7d1c3ff3ac71a16cf55be5a48ce808ef59
(cherry picked from commit 083be30783)
2018-03-23 20:33:57 +00:00
Suren Baghdasaryan 0bff4732d4 Merge "lmkd: Fix the message printed during a kill to avoid confusion" am: 5ea2c4baf1
am: ccb9744562

Change-Id: I45960a335ad426fdd69a2a36268ddfc425f92de0
2018-03-21 19:35:53 +00:00
Suren Baghdasaryan b939f0d45e Merge "lmkd: Suppress error when accessing soft_limit_in_bytes for system_server" am: d4463fd9d7
am: ec2bc88aa1

Change-Id: I10f524189c88dded9cce608cd2bec18cf03497f4
2018-03-21 19:35:22 +00:00
Suren Baghdasaryan d33812455d Merge "lmkd: Change error to warning when accessing oom_score_adj file" am: a77411f425
am: 3d3e875767

Change-Id: I59e03c59b9ee8a6a8e4dd8bf7ea4a26eb9bc4ecc
2018-03-21 19:34:51 +00:00
Suren Baghdasaryan d07a94f973 lmkd: Fix the message printed during a kill to avoid confusion
Current kills message prints min_score_adj marked as "oom_adj" which
is confusing. This change marks it as "min_oom_adj" to be more clear.

Change-Id: If52f0fb25fe3208c928c5d6adce77c04b9f15a0e
2018-03-21 17:12:42 +00:00
Suren Baghdasaryan 4311d1ed36 lmkd: Suppress error when accessing soft_limit_in_bytes for system_server
system_server needs to register with lmkd, however it has no memory
cgroup under /dev/memcg/apps. This change detects if the process being
registered is system_server and suppresses the error message when
/dev/memcg/apps/uid_%d/pid_%d/memory.soft_limit_in_bytes file can't
be accessed.

Bug: 73483785
Test: verified logcat output
Change-Id: I03df7831f41f512ac8d3ebc46330546d08a3cbc6
2018-03-21 10:11:23 -07:00
Suren Baghdasaryan 1ffa246c86 lmkd: Change error to warning when accessing oom_score_adj file
Occasionally processes are killed or crash while lmkd is trying to access
their oom_score_adj file. This is not necessarily an erroneous condition
and therefore should not be reported as an error. Demoting error to a
warning with a detailed message.

Bug: 72039129
Test: verified logcat output
Change-Id: I97444ba5198c02cb7f7ba03c3af12e4aad9d233e
2018-03-20 17:14:38 -07:00
Suren Baghdasaryan b453066e20 lmkd: Fix lmkd-test to work with relative paths
This change allows lmkd-test to work correctly when it's started
using absolute as well as relative path.

Change-Id: Ida58d13d66a224119a363f59cc59289f0167d3c9
2018-03-13 12:34:18 -07:00
Suren Baghdasaryan e708b6fc68 lmkd: Implement lmkd-test
(cherry pick from commit 5f5b30e58b)

lmkd-test executes alloc-stress native application and monitors kernel
logs for OOM kills.

Bug: 63631020

Change-Id: Ia9441cc5f8e283131f0bc839cb808a911bcdb168
Merged-In: Ia9441cc5f8e283131f0bc839cb808a911bcdb168
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-03-09 11:19:24 -08:00
Suren Baghdasaryan a92de71a51 lmkd: Introduce liblmkd_utils for communicating with lmkd process
(cherry pick from commit 0f10051805)

A number of tools and tests require communication with lmkd.
In order to avoid code duplication liblmkd_utils implements
functions commonly used when interacting with lmkd process.
Isolate communication protocol details into lmkd.h

Bug: 63631020

Change-Id: Id840983d55b7db60013d52dee0c3187943811822
Merged-In: Id840983d55b7db60013d52dee0c3187943811822
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-03-09 11:18:53 -08:00
Suren Baghdasaryan 06f75707e1 lmkd: Add support for multiple lmkd client connections
(cherry pick from commit 3cfb2c8b10)

lmkd testing requires communication with lmkd daemon to register new
native processes. New implementation allows more than one communication
channel to lmkd. Current max number of communication channels is set to
two - one for ActivityManager and another one for a test process.

Bug: 63631020

Change-Id: I736115938a3c5ad9253bce29a17cd5349af190eb
Merged-In: I736115938a3c5ad9253bce29a17cd5349af190eb
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-03-09 11:18:44 -08:00
Suren Baghdasaryan 2088f6ad03 lmkd: Select in-kernel vs userspace lmk based on kernel driver presence
(cherry pick from commit 979591b627)

Currently selection criteria for in-kernel vs userspace lmk is kernel
driver presence and device not being a Go device. This change removes
Go device check leaving kernel driver presence to be the only selection
criteria.

Bug: 71502948

Change-Id: I394a7920433a8d090e207ea86296356413a63fe7
Merged-In: I394a7920433a8d090e207ea86296356413a63fe7
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-03-02 01:16:05 +00:00
Suren Baghdasaryan 63dadcf79e lmkd: Implement kill timeout
(cherry pick from commit caa2dc56fd)

New ro.lmk.kill_timeout_ms property defines timeout in ms after a
successful kill cycle for more kills to be considered. This is
necessary because memory pressure after a kill does not go down
instantly and system needs time to reflect new memory state. This
timeout prevents extra kills in the period immediately after a
kill cycle. By default it is set to 0 which disables this feature.

Bug: 63631020
Test: alloc-stress

Change-Id: Ia847118c8c4a659a7fc38cd5cd0042acb514ae28
Merged-In: Ia847118c8c4a659a7fc38cd5cd0042acb514ae28
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-03-02 01:15:01 +00:00
Suren Baghdasaryan aa73bafea7 lmkd: Allow killing multiple processes to downgrade memory pressure
(cherry pick from commit 65f54a2665)

Record free memory at low vmpressure levels and whenever pressure
increases beyond low free up enough memory to downgrade memory pressure
to low. This is done by freeing enough memory to get to the max free
memory levels seen during low vmpressure.
The kill logic for Go devices is not changed as these devices are designed
to operate under high memory pressure.

Bug: 63631020
Test: alloc-stress

Change-Id: Ic8396eee08013b1c709072a13525601d5c8bf1f1
Merged-In: Ic8396eee08013b1c709072a13525601d5c8bf1f1
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-03-02 01:14:13 +00:00
Suren Baghdasaryan 1a2589e52b lmkd: Detect the highest level of vmpressure when event is detected
(cherry pick from commit e82e15c242)

lmkd checks for vmpressure events using epoll_wait() with eventfds of
all registered events. It's possible that multiple events of different
priorities happen before epoll_wait() returns. For these cases we
use conservative approach by assuming that the system is under the
highest registered vmpressure levels. This speeds up lmkd response time
to high memory pressure by not responding to possibly stale low pressure
levels when vmpressure rises quickly.

Bug: 63631020
Test: alloc-stress

Change-Id: I79a85c3342e7e1b3a3be82945266b2cc60b437cf
Merged-In: I79a85c3342e7e1b3a3be82945266b2cc60b437cf
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-03-02 01:13:16 +00:00
Suren Baghdasaryan ab05d67d86 lmkd: Close cgroup.event_control file when done writing
(cherry pick from commit 1bd2fc4fb6)

After events are specified by writing into cgroup.event_control file
the file should be closed.

Change-Id: Id015e6a7bac2b74bbc8d8793c85f529ee00bdf55
Merged-In: Id015e6a7bac2b74bbc8d8793c85f529ee00bdf55
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-03-02 01:12:29 +00:00
Suren Baghdasaryan e57c3d313d lmkd: Remove stale dependency on libprocessgroup
(cherry pick from commit b333f83481)

Remove stale dependencies and header file inclusions

Change-Id: Ic0e7adb5bd2a0832937a831b6918e52ace4ad46a
Merged-In: Ic0e7adb5bd2a0832937a831b6918e52ace4ad46a
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-03-02 01:11:34 +00:00
Suren Baghdasaryan e1217c025d lmkd: Add ability to trace lmkd kills
(cherry pick from commit c71355991d)

For tracing lmkd kills inside kernel it is useful to have traces
indicating when and which process lmkd is killing. By default the
tracing is disabled.

Bug: 63631020
Test: alloc-stress

Change-Id: I3ceb2bde0c292eec55855cb4535927f3b4c5d08b
Merged-In: I3ceb2bde0c292eec55855cb4535927f3b4c5d08b
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-03-02 01:10:42 +00:00
Suren Baghdasaryan b93764db9c lmkd: add logic to kill the heaviest of the eligible processes
(cherry pick from commit 662492ab1d)

Killing the most memory-demanding process from the set of eligible
processes yields better results on high-performance devices than
killing the first one we could find. This is in line with how in-kernel
lowmemorykiller driver chooses its victims.

Bug: 63631020
Test: alloc-stress

Change-Id: Ie1ef7f33f3e79698a9b4120c14490386d6129f9b
Merged-In: Ie1ef7f33f3e79698a9b4120c14490386d6129f9b
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-03-02 01:09:12 +00:00
Suren Baghdasaryan e5c9643119 lmkd: change defaults to disable event upgrade/downgrade logic
(cherry pick from commit ad2fd9150b)

vmpressure upgrade/downgrade logic based on swap utilization works well
for low memory devices because of a small swap size, however for high
performance devices this measure is not a good indication of the memory
pressure because of large swap resources. This change sets the default
levels to disable upgrade/downgrade logic by default and each device
can set these properties appropriately.

Bug: 63631020
Test: alloc-stress

Change-Id: Ifd4fbd4d6bb3e82f0f87b029df94934f1e7b1c9c
Merged-In: Ifd4fbd4d6bb3e82f0f87b029df94934f1e7b1c9c
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-03-02 01:07:42 +00:00
Suren Baghdasaryan cd7ad2f081 lmkd: add ability to monitor all vmpressure events
(cherry pick from commit 96bf3a600c)

Ability to monitor all available vmpressure event levels is needed
to accommodate systems with different memory resources. Low memory
systems can rely on medium and critical level events because working
under memory pressure is usual mode of operation. High performance
systems with more memory need to react earlier using also low
vmpressure level events to free memory early and prevent low memory
condition affecting its performance.

Bug: 63631020
Test: alloc-stress

Change-Id: I0cef1bd4c97d32c005045ae47f0ce3464ed98899
Merged-In: I0cef1bd4c97d32c005045ae47f0ce3464ed98899
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-03-01 21:40:13 +00:00
Mark Salyzyn 61421baf96 lmkd: move sundry pieces to statslog.h
Pragma once is not part of the standard, and is actually a gnu
C++ addition. Android coding standard requires the #ifndef header
wrappers. Moved things that belong in statslog.h from the lmkd files.

SideEffects: None
Test: lmkd_unit_tests
Bug: 33808187
Bug: 72838192
Change-Id: I9686b1a0791ee2b723d05b91905eda0bb64a1156
2018-02-28 19:36:12 +00:00
Rajeev Kumar 92b659ba9a Fix variable name in log statement and initialize memory_stat before using it.
Bug: 72177881
Test: Tested manually
Change-Id: Icbf948dd0e40eaa6c660d55f5cd72182436da086
2018-02-22 10:57:18 -08:00
Daniel Colascione 347f6b4462 Enable lmkd kill tracing everywhere
Test: asdf
Exempt-From-Owner-Approval: vacation
Change-Id: If6af7df74d303466aac4a69cdfe558e9748f72b9
2018-02-12 12:09:04 -08:00
Rajeev Kumar 7045003d0d Implement stats logging in LMKD.
It implements logging of following atoms:
  -- LMK_STATE_CHANGED
  -- LMK_KILL_OCCURRED

We would like to gather memory metrics of the process killed by LMKD
because by gathering this info we would be able to analyze and improve
system health by potentially reducing memory footprint of the process.

This feature would be available on production builds.

To know more about this see: http://go/android-p-memory-metrics

Bug: 65738734
Test: Tested manually
Change-Id: I064e0cdcb47c3b4c95d8b8d5654050c9812008d8
2018-02-09 13:07:40 -08:00
Mark Salyzyn aa41172e81 Merge "lmkd: use after free" am: 7c3c8c27e1 am: c95fb64f0f
am: 9e11bb0cdb

Change-Id: I2ee21f9d995b588a2a08eeaf9366dcfce4354e63
2018-02-07 03:51:59 +00:00
Mark Salyzyn 919f538e6d lmkd: use after free
Remove a use-after free reference of procp->pid, using the already
captured pid variable.

Test: lmkd_unit_tests
Bug: 33808187
Change-Id: I3f5f8dd9acab2e28c81465d6195b73ae47e0a3c4
2018-02-05 11:00:59 -08:00
Rajeev Kumar 2af695c4fa Cleanup zoneinfo parsing function.
Usage of this function was removed in http://ag/2402070

Bug: 62626918
Test: Manual
Change-Id: Ib6af945d1005062d4219deae078a415bb7369261
2018-01-31 18:13:46 -08:00
Suren Baghdasaryan 5f5b30e58b lmkd: Implement lmkd-test
lmkd-test executes alloc-stress native application and monitors kernel
logs for OOM kills.

Bug: 63631020

Change-Id: Ia9441cc5f8e283131f0bc839cb808a911bcdb168
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-01-31 18:11:38 +00:00
Suren Baghdasaryan 0f10051805 lmkd: Introduce liblmkd_utils for communicating with lmkd process
A number of tools and tests require communication with lmkd.
In order to avoid code duplication liblmkd_utils implements
functions commonly used when interacting with lmkd process.
Isolate communication protocol details into lmkd.h

Bug: 63631020

Change-Id: Id840983d55b7db60013d52dee0c3187943811822
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-01-31 18:10:49 +00:00
Suren Baghdasaryan 3cfb2c8b10 lmkd: Add support for multiple lmkd client connections
lmkd testing requires communication with lmkd daemon to register new
native processes. New implementation allows more than one communication
channel to lmkd. Current max number of communication channels is set to
two - one for ActivityManager and another one for a test process.

Bug: 63631020

Change-Id: I736115938a3c5ad9253bce29a17cd5349af190eb
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-01-31 04:45:01 +00:00
Rajeev Kumar f0216a8ac5 Functions to log stats log from LMKD.
Bug: 72177881
Test: Manual
Change-Id: I0f618013e242fabae785b62eb39f9bfbcaac16da
2018-01-29 15:18:04 -08:00
Daniel Colascione 6a6629ffe9 Merge "Add MCL_ONFAULT to mlockall" am: ccb9960beb am: 179c81f733
am: 1592d9b458

Change-Id: I55420b49d0902dc00b5741294c05a1320bf13325
2018-01-24 08:31:28 +00:00
Daniel Colascione d39adf2a4a Add MCL_ONFAULT to mlockall
This way, we don't fault in the entirety of our DSOs immediately;
instead, used pages are "sticky" in memory. Works only on kernel 4.4
and up: downlevel, we ignore the mlockall failure.

Once we get statically-linked lmkd in better shape, we'll just switch
to that.

Change-Id: I07a75ee3bc1264a1db41635c2acf611fede99b91
2018-01-24 01:53:52 +00:00
Suren Baghdasaryan 979591b627 lmkd: Select in-kernel vs userspace lmk based on kernel driver presence
Currently selection criteria for in-kernel vs userspace lmk is kernel
driver presence and device not being a Go device. This change removes
Go device check leaving kernel driver presence to be the only selection
criteria.

Bug: 71502948

Change-Id: I394a7920433a8d090e207ea86296356413a63fe7
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-01-20 03:50:41 +00:00
Suren Baghdasaryan caa2dc56fd lmkd: Implement kill timeout
New ro.lmk.kill_timeout_ms property defines timeout in ms after a
successful kill cycle for more kills to be considered. This is
necessary because memory pressure after a kill does not go down
instantly and system needs time to reflect new memory state. This
timeout prevents extra kills in the period immediately after a
kill cycle. By default it is set to 0 which disables this feature.

Bug: 63631020
Test: alloc-stress

Change-Id: Ia847118c8c4a659a7fc38cd5cd0042acb514ae28
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-01-20 03:49:35 +00:00
Suren Baghdasaryan 65f54a2665 lmkd: Allow killing multiple processes to downgrade memory pressure
Record free memory at low vmpressure levels and whenever pressure
increases beyond low free up enough memory to downgrade memory pressure
to low. This is done by freeing enough memory to get to the max free
memory levels seen during low vmpressure.
The kill logic for Go devices is not changed as these devices are designed
to operate under high memory pressure.

Bug: 63631020
Test: alloc-stress

Change-Id: Ic8396eee08013b1c709072a13525601d5c8bf1f1
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-01-17 17:23:41 -08:00
Suren Baghdasaryan e82e15c242 lmkd: Detect the highest level of vmpressure when event is detected
lmkd checks for vmpressure events using epoll_wait() with eventfds of
all registered events. It's possible that multiple events of different
priorities happen before epoll_wait() returns. For these cases we
use conservative approach by assuming that the system is under the
highest registered vmpressure levels. This speeds up lmkd response time
to high memory pressure by not responding to possibly stale low pressure
levels when vmpressure rises quickly.

Bug: 63631020
Test: alloc-stress

Change-Id: I79a85c3342e7e1b3a3be82945266b2cc60b437cf
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-01-17 17:05:42 -08:00
Suren Baghdasaryan 1bd2fc4fb6 lmkd: Close cgroup.event_control file when done writing
After events are specified by writing into cgroup.event_control file
the file should be closed.

Change-Id: Id015e6a7bac2b74bbc8d8793c85f529ee00bdf55
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-01-17 17:03:09 -08:00
Suren Baghdasaryan b333f83481 lmkd: Remove stale dependency on libprocessgroup
Remove stale dependencies and header file inclusions

Change-Id: Ic0e7adb5bd2a0832937a831b6918e52ace4ad46a
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-01-17 17:02:27 -08:00
Suren Baghdasaryan c71355991d lmkd: Add ability to trace lmkd kills
For tracing lmkd kills inside kernel it is useful to have traces
indicating when and which process lmkd is killing. By default the
tracing is disabled.

Bug: 63631020
Test: alloc-stress

Change-Id: I3ceb2bde0c292eec55855cb4535927f3b4c5d08b
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-01-17 16:52:23 -08:00
Suren Baghdasaryan 662492ab1d lmkd: add logic to kill the heaviest of the eligible processes
Killing the most memory-demanding process from the set of eligible
processes yields better results on high-performance devices than
killing the first one we could find. This is in line with how in-kernel
lowmemorykiller driver chooses its victims.

Bug: 63631020
Test: alloc-stress

Change-Id: Ie1ef7f33f3e79698a9b4120c14490386d6129f9b
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-01-16 15:42:33 -08:00
Suren Baghdasaryan ad2fd9150b lmkd: change defaults to disable event upgrade/downgrade logic
vmpressure upgrade/downgrade logic based on swap utilization works well
for low memory devices because of a small swap size, however for high
performance devices this measure is not a good indication of the memory
pressure because of large swap resources. This change sets the default
levels to disable upgrade/downgrade logic by default and each device
can set these properties appropriately.

Bug: 63631020
Test: alloc-stress

Change-Id: Ifd4fbd4d6bb3e82f0f87b029df94934f1e7b1c9c
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-01-16 15:42:25 -08:00
TreeHugger Robot 1b5f483604 Merge "lmkd: add ability to monitor all vmpressure events" 2018-01-05 00:10:40 +00:00
Suren Baghdasaryan 96bf3a600c lmkd: add ability to monitor all vmpressure events
Ability to monitor all available vmpressure event levels is needed
to accommodate systems with different memory resources. Low memory
systems can rely on medium and critical level events because working
under memory pressure is usual mode of operation. High performance
systems with more memory need to react earlier using also low
vmpressure level events to free memory early and prevent low memory
condition affecting its performance.

Bug: 63631020
Test: alloc-stress

Change-Id: I0cef1bd4c97d32c005045ae47f0ce3464ed98899
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
2018-01-04 10:28:13 -08:00
Daniel Colascione 4dd5d00ecc Pin lmkd for real
We pin lmkd in memory so that we don't take page faults (and thus
requisition memory) while we're in the process of responding to a
low-memory condition. mlockall(2) is the right primitive for this
pinning. Previously, we used the MCL_FUTURE flag to mlockall: used
this way, mlockall doesn't actually pin all pages in memory, since
MCL_FUTURE affects only the default flags for future mappings and
doesn't affect mapping already in existence at the time of the
mlockall call --- like the lmkd executable itself.

This patch adds the MCL_CURRENT flag, which also pins all pages
already mapped.

Test: code inspection
Change-Id: I4563959367a2f0a9cadc3ea41731b7f311326685
2018-01-03 12:32:14 -08:00
Elliott Hughes d65c6ba955 Merge "Add lmkd/OWNERS." 2017-12-09 18:47:57 +00:00
Elliott Hughes ad4e3e2f54 Add lmkd/OWNERS.
Bug: N/A
Test: N/A
Change-Id: Ic4970a95d799c16ffe06bc94a85adabc18d6cc0b
2017-12-08 16:12:58 -08:00
Robert Benea 19e26708c2 Simplify lmkd functionality
Get min adj score when needed, instead to propagate from caller funct.

Bug:65642829
Test: tested on gobo_512
Change-Id: I87f15c5c6206d471002828a24d0462b0abb0d196
2017-09-17 18:31:35 -07:00
Robert Benea 6e8e710675 Downgrade memory pressure events
If the system has enough memory the pressure events are ignored or
downgraded from critical to medium.

Bug: 65642829
Test: tested on gobo
Change-Id: I44e66d2e35508aceee5c1719313db217b80d582e
2017-09-14 13:47:40 -07:00
Robert Benea 164baebc01 Allow ANDROID_LOW_MEMORY_KILLER to coexist with lmkd
For Go devices fallback to memcg/memory pressure events for LMK.
Go devices will use memcg pressure events while inkernel module
is disabled.

Bug: 64852905
Test: tested on gobo
Change-Id: I267ab00be85e324331f6c91551ba013184de817e
2017-09-11 16:57:26 -07:00
Robert Benea c47f2992b5 Detect critical preassure
When close to oom the system tend to become very thrashy
and keeps paging. This change looks at the current working sent and
checks it against swap.

Test: tested on gobo
Bug: 64721547

Change-Id: I93d42def93cbc03a01a54988fd5286ec9f124e36
2017-08-25 19:01:54 -07:00
Robert Benea caeaa655c1 Improvements to lmkd
- Don't show misleading messages, add the correct description for
  the reason of process killing
- Don't kill Launcher3 upgrade the score to perceptible (200)
- Limit the verbose logging

Test: tested on gobo
Bug: 64316273
Change-Id: Ic7c13853d939afe4f20cdaa5b488b6e1810f5ddf
2017-08-11 16:13:57 -07:00
Robert Benea 58891d59b7 Make lmkd memory pressure levels configurable.
By doing so different platforms can customize them accordingly.
For instance a low mem, 512MB device can do
ro.lmk.medium=400, while a device with more memory can keep
the default(800).

Test: tested on gobo.
Bug: 64316084
Change-Id: Ifc4f3853bc06f30488adb25883ccd9aaf683ba9b
2017-08-02 23:42:48 +00:00
Robert Benea 673e27677a Make lmkd use medium/critical mem pressure, and update soft limit based on adj score.
Test: GO device (512MB/1GB), both show improvements under heavy load.

Bug: 62626918

Change-Id: I98afc8b1171db5b57056bc05d1f1ae9c5eed8506
2017-06-14 13:28:44 -07:00
Steven Moreland 1457c9290d lmkd: Android.mk -> Android.bp
Test: links
Change-Id: I19aa62270488af02d64830ca90faf80b35423009
2017-04-28 15:20:18 -07:00
Mark Salyzyn 30f991f251 liblog: use log/log.h when utilizing ALOG macros
Test: compile
Bug: 30465923
Change-Id: Id6d76510819ebd88c3f5003d00d73a0dbe85e943
2017-01-11 09:31:15 -08:00
Mark Salyzyn cfd5b080af system/core: preparation to pull back interfaces from android/log.h
Point to log/log.h where necessary, define LOG_TAG where necessary.
Accept that private/android_logger.h is suitable replacement for
log/logger.h and android/log.h.

Correct liblog/README

Effectively a cleanup and controlled select revert of
'system/core: drop or replace log/logger.h' and
'system/core: Replace log/log.h with android/log.h'.

Test: compile
Bug: 30465923
Change-Id: Ic2ad157bad6f5efe2c6af293a73bb753300b17a2
2016-10-20 08:11:39 -07:00
Mark Salyzyn ff2dcd9af9 system/core Replace log/log.h with android/log.h
Should use android/log.h instead of log/log.h as a good example
to all others.  Adjust header order to comply with Android Coding
standards.

Test: Compile
Bug: 26552300
Bug: 31289077
Change-Id: I33a8fb4e754d2dc4754d335660c450e0a67190fc
2016-09-30 12:47:05 -07:00
Colin Cross e8ffa449fd Move android_get_control_socket out of line
android_get_control_socket has a warning from the implicit cast from
long to int.  The warning was being hidden because cutils/sockets.h was
included with -isystem.  Move android_get_control_socket to sockets.cpp,
since we don't want header only dependencies anyways, and fix the
warning with a range check and a static_cast.

Bug: 31492149
Test: m -j <module that uses sockets.h and -Wall>
Change-Id: I1f394ab26d4ec8a7dd0e7907c10416d7f8647624
2016-09-23 11:26:08 -07:00
Elliott Hughes 8615d79d85 Merge "Most accept/accept4 calls in system/core don't actually want the remote address."
am: 299d64144b

Change-Id: I3fb9678dfed08f0b1d6e90e547ecae6cdcabc9a1
2016-08-23 23:03:20 +00:00
Elliott Hughes 3dcfa3fb1c Most accept/accept4 calls in system/core don't actually want the remote address.
So don't write the extra code for it or waste the kernel's time
copying it around.

Change-Id: I93de64064c2d4fe58ba5b5322cfa69bf31a76dad
2016-08-23 12:50:00 -07:00
Chih-Hung Hsieh daa13eaa92 resolve merge conflicts of 3252d11 to nyc-dev-plus-aosp
Change-Id: I2973439a194a3048d009159d75572a5366021f21
2016-05-19 16:02:22 -07:00
Chih-Hung Hsieh 2b82e918d8 Fix misc-macro-parentheses warnings in lmkd.
Bug: 28705665
Change-Id: I43388c0f9fdc8c2a0c0f4495f96bbc27e3de8a1d
2016-05-18 15:37:20 -07:00
Martijn Coenen 9010a2398e lmkd: subscribe to low mempressure events.
This will make sure we get invoked in time,
and combined with a kernel change to increase
the window size (and thus rate-limit the
notifications) based on the amount of memory
on the device, this should provide a nice
balance between receiving too many notifications
vs getting notified too late.

Change-Id: I2f5e92390b94e582aaa06d88763a232e39673496
2016-02-09 11:25:18 +01:00
Nick Kralevich 261952e570 Merge "more O_CLOEXEC" am: 920d637d25
am: 890cd99709

* commit '890cd997098795b7a58c1d56c49c23895da7b4f9':
  more O_CLOEXEC
2015-12-18 23:05:34 -08:00
Nick Kralevich c68c8862f9 more O_CLOEXEC
Change-Id: I330aef8d6f1fc35d6649995ef312954b84dff555
2015-12-18 20:57:25 -08:00
Erik Kline 79a3210eda Merge "Switch from using sockaddr to sockaddr_storage." am: 46b0b1c694 am: 4c1b3840f0
am: cbc6f68e32

* commit 'cbc6f68e320bd30d0cd7fbe73847deacbd54e073':
  Switch from using sockaddr to sockaddr_storage.
2015-12-08 04:54:05 +00:00
Erik Kline 7e16cc15b5 Switch from using sockaddr to sockaddr_storage.
This is to ensure sufficient space is always available.

Change-Id: Ifa87b93ecdc90dcacbfb24446c872344da6703d3
2015-12-07 16:07:46 +09:00
Nick Kralevich bdf20142b9 Merge "Enable hidepid=2 on /proc" am: 2d8f1d4c47 am: ea8452cc72
am: 7dafe5b818

* commit '7dafe5b818f7771cb00c4a0ea3ab1dbf74593185':
  Enable hidepid=2 on /proc
2015-11-09 20:41:19 +00:00