1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "OomConnection"
18 
19 #include <core_jni_helpers.h>
20 #include <jni.h>
21 #include <memevents/memevents.h>
22 
23 namespace android {
24 
25 using namespace ::android::bpf::memevents;
26 
27 // Used to cache the results of the JNI name lookup
28 static struct {
29     jclass clazz;
30     jmethodID ctor;
31 } sOomKillRecordInfo;
32 
33 static MemEventListener memevent_listener(MemEventClient::AMS);
34 
35 /**
36  * Initialize listening and waiting for new out-of-memory (OOM) events to occur.
37  * Once a OOM event is detected, we then fetch the list of OOM kills, and return
38  * a corresponding java array with the information gathered.
39  *
40  * In the case that we encounter an error, we make sure to close the epfd, and
41  * the OOM file descriptor, by calling `deregisterAllEvents()`.
42  *
43  * @return list of `android.os.OomKillRecord`
44  * @throws java.lang.RuntimeException
45  */
android_server_am_OomConnection_waitOom(JNIEnv * env,jobject)46 static jobjectArray android_server_am_OomConnection_waitOom(JNIEnv* env, jobject) {
47     if (!memevent_listener.ok()) {
48         memevent_listener.deregisterAllEvents();
49         jniThrowRuntimeException(env, "Failed to initialize memevents listener");
50         return nullptr;
51     }
52 
53     if (!memevent_listener.registerEvent(MEM_EVENT_OOM_KILL)) {
54         memevent_listener.deregisterAllEvents();
55         jniThrowRuntimeException(env, "listener failed to register to OOM events");
56         return nullptr;
57     }
58 
59     if (!memevent_listener.listen()) {
60         memevent_listener.deregisterAllEvents();
61         jniThrowRuntimeException(env, "listener failed waiting for OOM event");
62         return nullptr;
63     }
64 
65     std::vector<mem_event_t> oom_events;
66     if (!memevent_listener.getMemEvents(oom_events)) {
67         memevent_listener.deregisterAllEvents();
68         jniThrowRuntimeException(env, "Failed to get OOM events");
69         return nullptr;
70     }
71 
72     jobjectArray java_oom_array =
73             env->NewObjectArray(oom_events.size(), sOomKillRecordInfo.clazz, nullptr);
74     if (java_oom_array == NULL) {
75         memevent_listener.deregisterAllEvents();
76         jniThrowRuntimeException(env, "Failed to create OomKillRecord array");
77         return nullptr;
78     }
79 
80     for (int i = 0; i < oom_events.size(); i++) {
81         const mem_event_t mem_event = oom_events[i];
82         if (mem_event.type != MEM_EVENT_OOM_KILL) {
83             memevent_listener.deregisterAllEvents();
84             jniThrowRuntimeException(env, "Received invalid memory event");
85             return java_oom_array;
86         }
87 
88         const auto oom_kill = mem_event.event_data.oom_kill;
89 
90         jstring process_name = env->NewStringUTF(oom_kill.process_name);
91         if (process_name == NULL) {
92             memevent_listener.deregisterAllEvents();
93             jniThrowRuntimeException(env, "Failed creating java string for process name");
94         }
95         jobject java_oom_kill =
96                 env->NewObject(sOomKillRecordInfo.clazz, sOomKillRecordInfo.ctor,
97                                oom_kill.timestamp_ms, oom_kill.pid, oom_kill.uid, process_name,
98                                oom_kill.oom_score_adj, oom_kill.total_vm_kb, oom_kill.anon_rss_kb,
99                                oom_kill.file_rss_kb, oom_kill.shmem_rss_kb, oom_kill.pgtables_kb);
100         if (java_oom_kill == NULL) {
101             memevent_listener.deregisterAllEvents();
102             jniThrowRuntimeException(env, "Failed to create OomKillRecord object");
103             return java_oom_array;
104         }
105         env->SetObjectArrayElement(java_oom_array, i, java_oom_kill);
106     }
107     return java_oom_array;
108 }
109 
110 static const JNINativeMethod sOomConnectionMethods[] = {
111         /* name, signature, funcPtr */
112         {"waitOom", "()[Landroid/os/OomKillRecord;",
113          (void*)android_server_am_OomConnection_waitOom},
114 };
115 
register_android_server_am_OomConnection(JNIEnv * env)116 int register_android_server_am_OomConnection(JNIEnv* env) {
117     sOomKillRecordInfo.clazz = FindClassOrDie(env, "android/os/OomKillRecord");
118     sOomKillRecordInfo.clazz = MakeGlobalRefOrDie(env, sOomKillRecordInfo.clazz);
119 
120     sOomKillRecordInfo.ctor = GetMethodIDOrDie(env, sOomKillRecordInfo.clazz, "<init>",
121                                                "(JIILjava/lang/String;SJJJJJ)V");
122 
123     return RegisterMethodsOrDie(env, "com/android/server/am/OomConnection", sOomConnectionMethods,
124                                 NELEM(sOomConnectionMethods));
125 }
126 } // namespace android