1 /*
2 * Copyright (C) 2021 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 #include <dmabufinfo/dmabufinfo.h>
18 #include "core_jni_helpers.h"
19
20 namespace android {
21
DmabufInfoReader_getProcessStats(JNIEnv * env,jobject,jint pid)22 static jobject DmabufInfoReader_getProcessStats(JNIEnv *env, jobject, jint pid) {
23 std::vector<dmabufinfo::DmaBuffer> buffers;
24 if (!dmabufinfo::ReadDmaBufMapRefs(pid, &buffers)) {
25 return nullptr;
26 }
27 jint mappedSize = 0;
28 jint mappedCount = buffers.size();
29 for (const auto &buffer : buffers) {
30 mappedSize += buffer.size();
31 }
32 mappedSize /= 1024;
33
34 jint retainedSize = -1;
35 jint retainedCount = -1;
36 if (dmabufinfo::ReadDmaBufFdRefs(pid, &buffers)) {
37 retainedCount = buffers.size();
38 retainedSize = 0;
39 for (const auto &buffer : buffers) {
40 retainedSize += buffer.size();
41 }
42 retainedSize /= 1024;
43 }
44
45 jclass clazz = FindClassOrDie(env, "com/android/internal/os/DmabufInfoReader$ProcessDmabuf");
46 jmethodID constructID = GetMethodIDOrDie(env, clazz, "<init>", "(IIII)V");
47 return env->NewObject(clazz, constructID, retainedSize, retainedCount, mappedSize, mappedCount);
48 }
49
50 static const JNINativeMethod methods[] = {
51 {"getProcessStats", "(I)Lcom/android/internal/os/DmabufInfoReader$ProcessDmabuf;",
52 (void *)DmabufInfoReader_getProcessStats},
53 };
54
register_com_android_internal_os_DmabufInfoReader(JNIEnv * env)55 int register_com_android_internal_os_DmabufInfoReader(JNIEnv *env) {
56 return RegisterMethodsOrDie(env, "com/android/internal/os/DmabufInfoReader", methods,
57 NELEM(methods));
58 }
59
60 } // namespace android
61