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 <surenb@google.com>
Change-Id: Icc1ed8ab2593530fa293ff9c82f6c8dc400485f5
Merged-In: Icc1ed8ab2593530fa293ff9c82f6c8dc400485f5
This commit is contained in:
Suren Baghdasaryan 2021-05-27 18:15:44 -07:00
parent 449e1017e1
commit a01f251ff2
3 changed files with 25 additions and 1 deletions

View File

@ -23,6 +23,9 @@ cc_binary {
"libstatslogc", "libstatslogc",
"liblmkd_utils", "liblmkd_utils",
], ],
header_libs: [
"bpf_syscall_wrappers",
],
local_include_dirs: ["include"], local_include_dirs: ["include"],
cflags: [ cflags: [
"-Wall", "-Wall",

View File

@ -35,4 +35,4 @@
# TODO: generate ".java" and ".h" files with integer constants from this file. # TODO: generate ".java" and ".h" files with integer constants from this file.
# for killinfo logs # 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)

View File

@ -53,6 +53,9 @@
#include "statslog.h" #include "statslog.h"
#define BPF_FD_JUST_USE_INT
#include "BpfSyscallWrappers.h"
/* /*
* Define LMKD_TRACE_KILLS to record lmkd kills in kernel traces * Define LMKD_TRACE_KILLS to record lmkd kills in kernel traces
* to profile and correlate with OOM kills * to profile and correlate with OOM kills
@ -431,6 +434,7 @@ union meminfo {
int64_t cma_free; int64_t cma_free;
/* fields below are calculated rather than read from the file */ /* fields below are calculated rather than read from the file */
int64_t nr_file_pages; int64_t nr_file_pages;
int64_t total_gpu_kb;
} field; } field;
int64_t arr[MI_FIELD_COUNT]; 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); 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 int meminfo_parse(union meminfo *mi) {
static struct reread_data file_data = { static struct reread_data file_data = {
.filename = MEMINFO_PATH, .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.nr_file_pages = mi->field.cached + mi->field.swap_cached +
mi->field.buffers; mi->field.buffers;
mi->field.total_gpu_kb = read_gpu_total_kb();
return 0; 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->wakeups_since_event);
android_log_write_int32(ctx, wi->skipped_wakeups); 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)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_write_list(ctx, LOG_ID_EVENTS);
android_log_reset(ctx); android_log_reset(ctx);