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 package com.android.internal.os;
18 
19 import android.annotation.Nullable;
20 
21 /** JNI wrapper around libmeminfo for kernel memory allocation stats (dmabufs, gpu driver). */
22 public final class KernelAllocationStats {
KernelAllocationStats()23     private KernelAllocationStats() {}
24 
25     /** Process dma-buf stats. */
26     public static final class ProcessDmabuf {
27         public final int uid;
28         public final String processName;
29         public final int oomScore;
30 
31         /** Size of buffers retained by the process. */
32         public final int retainedSizeKb;
33         /** Number of buffers retained by the process. */
34         public final int retainedBuffersCount;
35         /** Size of buffers shared with Surface Flinger. */
36         public final int surfaceFlingerSizeKb;
37         /** Count of buffers shared with Surface Flinger. */
38         public final int surfaceFlingerCount;
39 
ProcessDmabuf(int uid, String processName, int oomScore, int retainedSizeKb, int retainedBuffersCount, int surfaceFlingerSizeKb, int surfaceFlingerCount)40         ProcessDmabuf(int uid, String processName, int oomScore, int retainedSizeKb,
41                 int retainedBuffersCount, int surfaceFlingerSizeKb,
42                 int surfaceFlingerCount) {
43             this.uid = uid;
44             this.processName = processName;
45             this.oomScore = oomScore;
46             this.retainedSizeKb = retainedSizeKb;
47             this.retainedBuffersCount = retainedBuffersCount;
48             this.surfaceFlingerSizeKb = surfaceFlingerSizeKb;
49             this.surfaceFlingerCount = surfaceFlingerCount;
50         }
51     }
52 
53     /**
54      * Return stats for DMA-BUFs retained by process pid or null if the DMA-BUF
55      * stats could not be read.
56      */
57     @Nullable
getDmabufAllocations()58     public static native ProcessDmabuf[] getDmabufAllocations();
59 
60     /** Pid to gpu memory size. */
61     public static final class ProcessGpuMem {
62         public final int pid;
63         public final int gpuMemoryKb;
64 
ProcessGpuMem(int pid, int gpuMemoryKb)65         ProcessGpuMem(int pid, int gpuMemoryKb) {
66             this.pid = pid;
67             this.gpuMemoryKb = gpuMemoryKb;
68         }
69     }
70 
71     /** Return list of pid to gpu memory size. */
getGpuAllocations()72     public static native ProcessGpuMem[] getGpuAllocations();
73 }
74