blob: 044bb04dcd7fb98ece2440d823af4472250c3206 [file] [log] [blame]
John Reckedc524c2015-03-18 15:24:33 -07001/*
2 * Copyright (C) 2015 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
17package com.android.server;
18
Jeff Sharkeycd654482016-01-08 17:42:11 -070019import android.app.AppOpsManager;
John Reckedc524c2015-03-18 15:24:33 -070020import android.content.Context;
John Reckedc524c2015-03-18 15:24:33 -070021import android.os.Binder;
22import android.os.IBinder;
23import android.os.MemoryFile;
24import android.os.ParcelFileDescriptor;
25import android.os.RemoteException;
26import android.util.Log;
27import android.view.IGraphicsStats;
28import android.view.ThreadedRenderer;
29
30import java.io.FileDescriptor;
31import java.io.IOException;
32import java.io.PrintWriter;
33import java.util.ArrayList;
34
35/**
36 * This service's job is to collect aggregate rendering profile data. It
37 * does this by allowing rendering processes to request an ashmem buffer
38 * to place their stats into. This buffer will be pre-initialized with historical
39 * data for that process if it exists (if the userId & packageName match a buffer
40 * in the historical log)
41 *
42 * This service does not itself attempt to understand the data in the buffer,
43 * its primary job is merely to manage distributing these buffers. However,
44 * it is assumed that this buffer is for ThreadedRenderer and delegates
45 * directly to ThreadedRenderer for dumping buffers.
46 *
47 * MEMORY USAGE:
48 *
49 * This class consumes UP TO:
50 * 1) [active rendering processes] * (ASHMEM_SIZE * 2)
51 * 2) ASHMEM_SIZE (for scratch space used during dumping)
52 * 3) ASHMEM_SIZE * HISTORY_SIZE
53 *
John Reck5742b972015-05-11 14:29:55 -070054 * Currently ASHMEM_SIZE is 256 bytes and HISTORY_SIZE is 20. Assuming
John Reckedc524c2015-03-18 15:24:33 -070055 * the system then also has 10 active rendering processes in the worst case
John Reck5742b972015-05-11 14:29:55 -070056 * this would end up using under 14KiB (12KiB for the buffers, plus some overhead
John Reckedc524c2015-03-18 15:24:33 -070057 * for userId, pid, package name, and a couple other objects)
58 *
59 * @hide */
60public class GraphicsStatsService extends IGraphicsStats.Stub {
61 public static final String GRAPHICS_STATS_SERVICE = "graphicsstats";
62
63 private static final String TAG = "GraphicsStatsService";
64 private static final int ASHMEM_SIZE = 256;
John Reck5742b972015-05-11 14:29:55 -070065 private static final int HISTORY_SIZE = 20;
John Reckedc524c2015-03-18 15:24:33 -070066
67 private final Context mContext;
Jeff Sharkeycd654482016-01-08 17:42:11 -070068 private final AppOpsManager mAppOps;
John Reckedc524c2015-03-18 15:24:33 -070069 private final Object mLock = new Object();
70 private ArrayList<ActiveBuffer> mActive = new ArrayList<>();
71 private HistoricalData[] mHistoricalLog = new HistoricalData[HISTORY_SIZE];
72 private int mNextHistoricalSlot = 0;
73 private byte[] mTempBuffer = new byte[ASHMEM_SIZE];
74
75 public GraphicsStatsService(Context context) {
76 mContext = context;
Jeff Sharkeycd654482016-01-08 17:42:11 -070077 mAppOps = context.getSystemService(AppOpsManager.class);
John Reckedc524c2015-03-18 15:24:33 -070078 }
79
80 @Override
81 public ParcelFileDescriptor requestBufferForProcess(String packageName, IBinder token)
82 throws RemoteException {
83 int uid = Binder.getCallingUid();
84 int pid = Binder.getCallingPid();
85 ParcelFileDescriptor pfd = null;
86 long callingIdentity = Binder.clearCallingIdentity();
87 try {
Jeff Sharkeycd654482016-01-08 17:42:11 -070088 mAppOps.checkPackage(uid, packageName);
John Reckedc524c2015-03-18 15:24:33 -070089 synchronized (mLock) {
90 pfd = requestBufferForProcessLocked(token, uid, pid, packageName);
91 }
92 } finally {
93 Binder.restoreCallingIdentity(callingIdentity);
94 }
95 return pfd;
96 }
97
98 private ParcelFileDescriptor getPfd(MemoryFile file) {
99 try {
100 return new ParcelFileDescriptor(file.getFileDescriptor());
101 } catch (IOException ex) {
102 throw new IllegalStateException("Failed to get PFD from memory file", ex);
103 }
104 }
105
106 private ParcelFileDescriptor requestBufferForProcessLocked(IBinder token,
107 int uid, int pid, String packageName) throws RemoteException {
108 ActiveBuffer buffer = fetchActiveBuffersLocked(token, uid, pid, packageName);
109 return getPfd(buffer.mProcessBuffer);
110 }
111
112 private void processDied(ActiveBuffer buffer) {
113 synchronized (mLock) {
114 mActive.remove(buffer);
115 Log.d("GraphicsStats", "Buffer count: " + mActive.size());
116 }
117 HistoricalData data = buffer.mPreviousData;
118 buffer.mPreviousData = null;
119 if (data == null) {
120 data = mHistoricalLog[mNextHistoricalSlot];
121 if (data == null) {
122 data = new HistoricalData();
123 }
124 }
125 data.update(buffer.mPackageName, buffer.mUid, buffer.mProcessBuffer);
126 buffer.closeAllBuffers();
127
128 mHistoricalLog[mNextHistoricalSlot] = data;
129 mNextHistoricalSlot = (mNextHistoricalSlot + 1) % mHistoricalLog.length;
130 }
131
132 private ActiveBuffer fetchActiveBuffersLocked(IBinder token, int uid, int pid,
133 String packageName) throws RemoteException {
134 int size = mActive.size();
135 for (int i = 0; i < size; i++) {
136 ActiveBuffer buffers = mActive.get(i);
137 if (buffers.mPid == pid
138 && buffers.mUid == uid) {
139 return buffers;
140 }
141 }
142 // Didn't find one, need to create it
143 try {
144 ActiveBuffer buffers = new ActiveBuffer(token, uid, pid, packageName);
145 mActive.add(buffers);
146 return buffers;
147 } catch (IOException ex) {
148 throw new RemoteException("Failed to allocate space");
149 }
150 }
151
152 private HistoricalData removeHistoricalDataLocked(int uid, String packageName) {
153 for (int i = 0; i < mHistoricalLog.length; i++) {
154 final HistoricalData data = mHistoricalLog[i];
155 if (data != null && data.mUid == uid
156 && data.mPackageName.equals(packageName)) {
157 if (i == mNextHistoricalSlot) {
158 mHistoricalLog[i] = null;
159 } else {
160 mHistoricalLog[i] = mHistoricalLog[mNextHistoricalSlot];
161 mHistoricalLog[mNextHistoricalSlot] = null;
162 }
163 return data;
164 }
165 }
166 return null;
167 }
168
169 @Override
170 protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) {
171 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
172 synchronized (mLock) {
173 for (int i = 0; i < mActive.size(); i++) {
174 final ActiveBuffer buffer = mActive.get(i);
175 fout.print("Package: ");
176 fout.print(buffer.mPackageName);
177 fout.flush();
178 try {
179 buffer.mProcessBuffer.readBytes(mTempBuffer, 0, 0, ASHMEM_SIZE);
180 ThreadedRenderer.dumpProfileData(mTempBuffer, fd);
181 } catch (IOException e) {
182 fout.println("Failed to dump");
183 }
184 fout.println();
185 }
186 for (HistoricalData buffer : mHistoricalLog) {
187 if (buffer == null) continue;
188 fout.print("Package: ");
189 fout.print(buffer.mPackageName);
190 fout.flush();
191 ThreadedRenderer.dumpProfileData(buffer.mBuffer, fd);
192 fout.println();
193 }
194 }
195 }
196
197 private final class ActiveBuffer implements DeathRecipient {
198 final int mUid;
199 final int mPid;
200 final String mPackageName;
201 final IBinder mToken;
202 MemoryFile mProcessBuffer;
203 HistoricalData mPreviousData;
204
205 ActiveBuffer(IBinder token, int uid, int pid, String packageName)
206 throws RemoteException, IOException {
207 mUid = uid;
208 mPid = pid;
209 mPackageName = packageName;
210 mToken = token;
211 mToken.linkToDeath(this, 0);
212 mProcessBuffer = new MemoryFile("GFXStats-" + uid, ASHMEM_SIZE);
213 mPreviousData = removeHistoricalDataLocked(mUid, mPackageName);
214 if (mPreviousData != null) {
215 mProcessBuffer.writeBytes(mPreviousData.mBuffer, 0, 0, ASHMEM_SIZE);
216 }
217 }
218
219 @Override
220 public void binderDied() {
221 mToken.unlinkToDeath(this, 0);
222 processDied(this);
223 }
224
225 void closeAllBuffers() {
226 if (mProcessBuffer != null) {
227 mProcessBuffer.close();
228 mProcessBuffer = null;
229 }
230 }
231 }
232
233 private final static class HistoricalData {
234 final byte[] mBuffer = new byte[ASHMEM_SIZE];
235 int mUid;
236 String mPackageName;
237
238 void update(String packageName, int uid, MemoryFile file) {
239 mUid = uid;
240 mPackageName = packageName;
241 try {
242 file.readBytes(mBuffer, 0, 0, ASHMEM_SIZE);
243 } catch (IOException e) {}
244 }
245 }
246}