From 940e7cf8bd9162310d6c621d3caa77ec407f80ef Mon Sep 17 00:00:00 2001 From: Suren Baghdasaryan Date: Thu, 27 May 2021 18:15:44 -0700 Subject: [PATCH] lmkd: Include total GPU memory usage in killinfo reports /sys/fs/bpf/map_gpu_mem_gpu_mem_total_map BPF map exposes total GPU allocations size. Include this value into killinfo reports to track GPU allocation size at the time of the kill. Bug: 189366037 Test: lmkd_unit_test Signed-off-by: Suren Baghdasaryan Change-Id: Icc1ed8ab2593530fa293ff9c82f6c8dc400485f5 --- Android.bp | 3 +++ event.logtags | 2 +- lmkd.cpp | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Android.bp b/Android.bp index fcf26ca..4f53ded 100644 --- a/Android.bp +++ b/Android.bp @@ -23,6 +23,9 @@ cc_binary { "libstatslogc", "liblmkd_utils", ], + header_libs: [ + "bpf_syscall_wrappers", + ], local_include_dirs: ["include"], cflags: [ "-Wall", diff --git a/event.logtags b/event.logtags index 0de32b7..5382b49 100644 --- a/event.logtags +++ b/event.logtags @@ -35,4 +35,4 @@ # TODO: generate ".java" and ".h" files with integer constants from this file. # for killinfo logs -10195355 killinfo (Pid|1|5),(Uid|1|5),(OomAdj|1),(MinOomAdj|1),(TaskSize|1),(enum kill_reasons|1|5),(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),(IonHeap|1),(IonHeapPool|1),(CmaFree|1),(MsSinceEvent|1),(MsSincePrevWakeup|1),(WakeupsSinceEvent|1),(SkippedWakeups|1),(TaskSwapSize|1) +10195355 killinfo (Pid|1|5),(Uid|1|5),(OomAdj|1),(MinOomAdj|1),(TaskSize|1),(enum kill_reasons|1|5),(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),(IonHeap|1),(IonHeapPool|1),(CmaFree|1),(MsSinceEvent|1),(MsSincePrevWakeup|1),(WakeupsSinceEvent|1),(SkippedWakeups|1),(TaskSwapSize|1),(GPU|1) diff --git a/lmkd.cpp b/lmkd.cpp index dc93ac4..0afcf2f 100644 --- a/lmkd.cpp +++ b/lmkd.cpp @@ -53,6 +53,9 @@ #include "statslog.h" +#define BPF_FD_JUST_USE_INT +#include "BpfSyscallWrappers.h" + /* * Define LMKD_TRACE_KILLS to record lmkd kills in kernel traces * to profile and correlate with OOM kills @@ -431,6 +434,7 @@ union meminfo { int64_t cma_free; /* fields below are calculated rather than read from the file */ int64_t nr_file_pages; + int64_t total_gpu_kb; } field; int64_t arr[MI_FIELD_COUNT]; }; @@ -1785,6 +1789,21 @@ static bool meminfo_parse_line(char *line, union meminfo *mi) { return (match_res != PARSE_FAIL); } +static int64_t read_gpu_total_kb() { + static int fd = android::bpf::bpfFdGet( + "/sys/fs/bpf/map_gpu_mem_gpu_mem_total_map", BPF_F_RDONLY); + static constexpr uint64_t kBpfKeyGpuTotalUsage = 0; + uint64_t value; + + if (fd < 0) { + return 0; + } + + return android::bpf::findMapEntry(fd, &kBpfKeyGpuTotalUsage, &value) + ? 0 + : (int32_t)(value / 1024); +} + static int meminfo_parse(union meminfo *mi) { static struct reread_data file_data = { .filename = MEMINFO_PATH, @@ -1809,6 +1828,7 @@ static int meminfo_parse(union meminfo *mi) { } mi->field.nr_file_pages = mi->field.cached + mi->field.swap_cached + mi->field.buffers; + mi->field.total_gpu_kb = read_gpu_total_kb(); return 0; } @@ -1923,6 +1943,7 @@ static void killinfo_log(struct proc* procp, int min_oom_score, int rss_kb, android_log_write_int32(ctx, wi->wakeups_since_event); android_log_write_int32(ctx, wi->skipped_wakeups); android_log_write_int32(ctx, (int32_t)min(swap_kb, INT32_MAX)); + android_log_write_int32(ctx, (int32_t)mi->field.total_gpu_kb); android_log_write_list(ctx, LOG_ID_EVENTS); android_log_reset(ctx);