1 /* 2 * MM Events - eBPF programs 3 * 4 * Copyright 2024 The Android Open Source Project 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License 8 * You may obtain a copy of the License at 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 #include <string.h> 18 19 #include <linux/bpf_perf_event.h> 20 21 #include <memevents/bpf_helpers.h> 22 #include <memevents/bpf_types.h> 23 24 DEFINE_BPF_RINGBUF_EXT(ams_rb, struct mem_event_t, MEM_EVENTS_RINGBUF_SIZE, DEFAULT_BPF_MAP_UID, 25 AID_SYSTEM, 0660, DEFAULT_BPF_MAP_SELINUX_CONTEXT, 26 DEFAULT_BPF_MAP_PIN_SUBDIR, PRIVATE, BPFLOADER_MIN_VER, BPFLOADER_MAX_VER, 27 LOAD_ON_ENG, LOAD_ON_USER, LOAD_ON_USERDEBUG) 28 29 DEFINE_BPF_RINGBUF_EXT(lmkd_rb, struct mem_event_t, MEM_EVENTS_RINGBUF_SIZE, DEFAULT_BPF_MAP_UID, 30 AID_SYSTEM, 0660, DEFAULT_BPF_MAP_SELINUX_CONTEXT, 31 DEFAULT_BPF_MAP_PIN_SUBDIR, PRIVATE, BPFLOADER_MIN_VER, BPFLOADER_MAX_VER, 32 LOAD_ON_ENG, LOAD_ON_USER, LOAD_ON_USERDEBUG) 33 34 DEFINE_BPF_PROG("tracepoint/oom/mark_victim/ams", AID_ROOT, AID_SYSTEM, tp_ams) 35 (struct mark_victim_args* args) { 36 unsigned long long timestamp_ns = bpf_ktime_get_ns(); 37 struct mem_event_t* data = bpf_ams_rb_reserve(); 38 if (data == NULL) return 1; 39 40 data->type = MEM_EVENT_OOM_KILL; 41 data->event_data.oom_kill.pid = args->pid; 42 data->event_data.oom_kill.oom_score_adj = args->oom_score_adj; 43 data->event_data.oom_kill.uid = args->uid; 44 data->event_data.oom_kill.timestamp_ms = timestamp_ns / 1000000; // Convert to milliseconds 45 data->event_data.oom_kill.total_vm_kb = args->total_vm; 46 data->event_data.oom_kill.anon_rss_kb = args->anon_rss; 47 data->event_data.oom_kill.file_rss_kb = args->file_rss; 48 data->event_data.oom_kill.shmem_rss_kb = args->shmem_rss; 49 data->event_data.oom_kill.pgtables_kb = args->pgtables; 50 51 read_str((char*)args, args->__data_loc_comm, data->event_data.oom_kill.process_name, 52 MEM_EVENT_PROC_NAME_LEN); 53 54 bpf_ams_rb_submit(data); 55 56 return 0; 57 } 58 59 DEFINE_BPF_PROG("tracepoint/vmscan/mm_vmscan_direct_reclaim_begin/lmkd", AID_ROOT, AID_SYSTEM, 60 tp_lmkd_dr_start) 61 (struct direct_reclaim_begin_args* args) { 62 struct mem_event_t* data = bpf_lmkd_rb_reserve(); 63 if (data == NULL) return 1; 64 65 data->type = MEM_EVENT_DIRECT_RECLAIM_BEGIN; 66 67 bpf_lmkd_rb_submit(data); 68 69 return 0; 70 } 71 72 DEFINE_BPF_PROG("tracepoint/vmscan/mm_vmscan_direct_reclaim_end/lmkd", AID_ROOT, AID_SYSTEM, 73 tp_lmkd_dr_end) 74 (struct direct_reclaim_end_args* args) { 75 struct mem_event_t* data = bpf_lmkd_rb_reserve(); 76 if (data == NULL) return 1; 77 78 data->type = MEM_EVENT_DIRECT_RECLAIM_END; 79 80 bpf_lmkd_rb_submit(data); 81 82 return 0; 83 } 84 85 DEFINE_BPF_PROG("tracepoint/vmscan/mm_vmscan_kswapd_wake/lmkd", AID_ROOT, AID_SYSTEM, 86 tp_lmkd_kswapd_wake) 87 (struct kswapd_wake_args* args) { 88 struct mem_event_t* data = bpf_lmkd_rb_reserve(); 89 if (data == NULL) return 1; 90 91 data->type = MEM_EVENT_KSWAPD_WAKE; 92 data->event_data.kswapd_wake.node_id = args->nid; 93 data->event_data.kswapd_wake.zone_id = args->zid; 94 data->event_data.kswapd_wake.alloc_order = args->order; 95 96 bpf_lmkd_rb_submit(data); 97 98 return 0; 99 } 100 101 DEFINE_BPF_PROG("tracepoint/vmscan/mm_vmscan_kswapd_sleep/lmkd", AID_ROOT, AID_SYSTEM, 102 tp_lmkd_kswapd_sleep) 103 (struct kswapd_sleep_args* args) { 104 struct mem_event_t* data = bpf_lmkd_rb_reserve(); 105 if (data == NULL) return 1; 106 107 data->type = MEM_EVENT_KSWAPD_SLEEP; 108 data->event_data.kswapd_wake.node_id = args->nid; 109 110 bpf_lmkd_rb_submit(data); 111 112 return 0; 113 } 114 115 // bpf_probe_read_str is GPL only symbol 116 LICENSE("GPL"); 117