John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 19 | import android.app.AlarmManager; |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 20 | import android.app.AppOpsManager; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 21 | import android.content.Context; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 22 | import android.content.pm.PackageInfo; |
| 23 | import android.content.pm.PackageManager; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 24 | import android.os.Binder; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 25 | import android.os.Environment; |
| 26 | import android.os.Handler; |
| 27 | import android.os.HandlerThread; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 28 | import android.os.IBinder; |
| 29 | import android.os.MemoryFile; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 30 | import android.os.Message; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 31 | import android.os.ParcelFileDescriptor; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 32 | import android.os.Process; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 33 | import android.os.RemoteException; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 34 | import android.os.Trace; |
Tony Mak | 8256680 | 2017-03-20 18:35:43 +0000 | [diff] [blame] | 35 | import android.os.UserHandle; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 36 | import android.util.Log; |
| 37 | import android.view.IGraphicsStats; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 38 | import android.view.IGraphicsStatsCallback; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 39 | |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 40 | import com.android.internal.util.DumpUtils; |
| 41 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 42 | import java.io.File; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 43 | import java.io.FileDescriptor; |
| 44 | import java.io.IOException; |
| 45 | import java.io.PrintWriter; |
| 46 | import java.util.ArrayList; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 47 | import java.util.Arrays; |
| 48 | import java.util.Calendar; |
| 49 | import java.util.HashSet; |
| 50 | import java.util.TimeZone; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * This service's job is to collect aggregate rendering profile data. It |
| 54 | * does this by allowing rendering processes to request an ashmem buffer |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 55 | * to place their stats into. |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 56 | * |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 57 | * Buffers are rotated on a daily (in UTC) basis and only the 3 most-recent days |
| 58 | * are kept. |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 59 | * |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 60 | * The primary consumer of this is incident reports and automated metric checking. It is not |
| 61 | * intended for end-developer consumption, for that we have gfxinfo. |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 62 | * |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 63 | * Buffer rotation process: |
| 64 | * 1) Alarm fires |
| 65 | * 2) onRotateGraphicsStatsBuffer() is sent to all active processes |
| 66 | * 3) Upon receiving the callback, the process will stop using the previous ashmem buffer and |
| 67 | * request a new one. |
| 68 | * 4) When that request is received we now know that the ashmem region is no longer in use so |
| 69 | * it gets queued up for saving to disk and a new ashmem region is created and returned |
| 70 | * for the process to use. |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 71 | * |
| 72 | * @hide */ |
| 73 | public class GraphicsStatsService extends IGraphicsStats.Stub { |
| 74 | public static final String GRAPHICS_STATS_SERVICE = "graphicsstats"; |
| 75 | |
| 76 | private static final String TAG = "GraphicsStatsService"; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 77 | |
| 78 | private static final int SAVE_BUFFER = 1; |
| 79 | private static final int DELETE_OLD = 2; |
| 80 | |
| 81 | // This isn't static because we need this to happen after registerNativeMethods, however |
| 82 | // the class is loaded (and thus static ctor happens) before that occurs. |
| 83 | private final int ASHMEM_SIZE = nGetAshmemSize(); |
| 84 | private final byte[] ZERO_DATA = new byte[ASHMEM_SIZE]; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 85 | |
| 86 | private final Context mContext; |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 87 | private final AppOpsManager mAppOps; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 88 | private final AlarmManager mAlarmManager; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 89 | private final Object mLock = new Object(); |
| 90 | private ArrayList<ActiveBuffer> mActive = new ArrayList<>(); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 91 | private File mGraphicsStatsDir; |
| 92 | private final Object mFileAccessLock = new Object(); |
| 93 | private Handler mWriteOutHandler; |
| 94 | private boolean mRotateIsScheduled = false; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 95 | |
| 96 | public GraphicsStatsService(Context context) { |
| 97 | mContext = context; |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 98 | mAppOps = context.getSystemService(AppOpsManager.class); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 99 | mAlarmManager = context.getSystemService(AlarmManager.class); |
| 100 | File systemDataDir = new File(Environment.getDataDirectory(), "system"); |
| 101 | mGraphicsStatsDir = new File(systemDataDir, "graphicsstats"); |
| 102 | mGraphicsStatsDir.mkdirs(); |
| 103 | if (!mGraphicsStatsDir.exists()) { |
| 104 | throw new IllegalStateException("Graphics stats directory does not exist: " |
| 105 | + mGraphicsStatsDir.getAbsolutePath()); |
| 106 | } |
| 107 | HandlerThread bgthread = new HandlerThread("GraphicsStats-disk", Process.THREAD_PRIORITY_BACKGROUND); |
| 108 | bgthread.start(); |
| 109 | |
| 110 | mWriteOutHandler = new Handler(bgthread.getLooper(), new Handler.Callback() { |
| 111 | @Override |
| 112 | public boolean handleMessage(Message msg) { |
| 113 | switch (msg.what) { |
| 114 | case SAVE_BUFFER: |
| 115 | saveBuffer((HistoricalBuffer) msg.obj); |
| 116 | break; |
| 117 | case DELETE_OLD: |
| 118 | deleteOldBuffers(); |
| 119 | break; |
| 120 | } |
| 121 | return true; |
| 122 | } |
| 123 | }); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Current rotation policy is to rotate at midnight UTC. We don't specify RTC_WAKEUP because |
| 128 | * rotation can be delayed if there's otherwise no activity. However exact is used because |
| 129 | * we don't want the system to delay it by TOO much. |
| 130 | */ |
| 131 | private void scheduleRotateLocked() { |
| 132 | if (mRotateIsScheduled) { |
| 133 | return; |
| 134 | } |
| 135 | mRotateIsScheduled = true; |
| 136 | Calendar calendar = normalizeDate(System.currentTimeMillis()); |
| 137 | calendar.add(Calendar.DATE, 1); |
| 138 | mAlarmManager.setExact(AlarmManager.RTC, calendar.getTimeInMillis(), TAG, this::onAlarm, |
| 139 | mWriteOutHandler); |
| 140 | } |
| 141 | |
| 142 | private void onAlarm() { |
John Reck | 37dfb42 | 2017-02-22 17:01:30 -0800 | [diff] [blame] | 143 | // We need to make a copy since some of the callbacks won't be proxy and thus |
| 144 | // can result in a re-entrant acquisition of mLock that would result in a modification |
| 145 | // of mActive during iteration. |
| 146 | ActiveBuffer[] activeCopy; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 147 | synchronized (mLock) { |
| 148 | mRotateIsScheduled = false; |
| 149 | scheduleRotateLocked(); |
John Reck | 37dfb42 | 2017-02-22 17:01:30 -0800 | [diff] [blame] | 150 | activeCopy = mActive.toArray(new ActiveBuffer[0]); |
| 151 | } |
| 152 | for (ActiveBuffer active : activeCopy) { |
| 153 | try { |
| 154 | active.mCallback.onRotateGraphicsStatsBuffer(); |
| 155 | } catch (RemoteException e) { |
| 156 | Log.w(TAG, String.format("Failed to notify '%s' (pid=%d) to rotate buffers", |
| 157 | active.mInfo.packageName, active.mPid), e); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | // Give a few seconds for everyone to rotate before doing the cleanup |
| 161 | mWriteOutHandler.sendEmptyMessageDelayed(DELETE_OLD, 10000); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Override |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 165 | public ParcelFileDescriptor requestBufferForProcess(String packageName, IGraphicsStatsCallback token) |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 166 | throws RemoteException { |
| 167 | int uid = Binder.getCallingUid(); |
| 168 | int pid = Binder.getCallingPid(); |
| 169 | ParcelFileDescriptor pfd = null; |
| 170 | long callingIdentity = Binder.clearCallingIdentity(); |
| 171 | try { |
Jeff Sharkey | cd65448 | 2016-01-08 17:42:11 -0700 | [diff] [blame] | 172 | mAppOps.checkPackage(uid, packageName); |
Tony Mak | 8256680 | 2017-03-20 18:35:43 +0000 | [diff] [blame] | 173 | PackageInfo info = mContext.getPackageManager().getPackageInfoAsUser( |
| 174 | packageName, |
| 175 | 0, |
| 176 | UserHandle.getUserId(uid)); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 177 | synchronized (mLock) { |
Dianne Hackborn | 73453e4 | 2017-12-11 16:30:36 -0800 | [diff] [blame] | 178 | pfd = requestBufferForProcessLocked(token, uid, pid, packageName, |
| 179 | info.getLongVersionCode()); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 180 | } |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 181 | } catch (PackageManager.NameNotFoundException ex) { |
| 182 | throw new RemoteException("Unable to find package: '" + packageName + "'"); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 183 | } finally { |
| 184 | Binder.restoreCallingIdentity(callingIdentity); |
| 185 | } |
| 186 | return pfd; |
| 187 | } |
| 188 | |
| 189 | private ParcelFileDescriptor getPfd(MemoryFile file) { |
| 190 | try { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 191 | if (!file.getFileDescriptor().valid()) { |
| 192 | throw new IllegalStateException("Invalid file descriptor"); |
| 193 | } |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 194 | return new ParcelFileDescriptor(file.getFileDescriptor()); |
| 195 | } catch (IOException ex) { |
| 196 | throw new IllegalStateException("Failed to get PFD from memory file", ex); |
| 197 | } |
| 198 | } |
| 199 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 200 | private ParcelFileDescriptor requestBufferForProcessLocked(IGraphicsStatsCallback token, |
Dianne Hackborn | 73453e4 | 2017-12-11 16:30:36 -0800 | [diff] [blame] | 201 | int uid, int pid, String packageName, long versionCode) throws RemoteException { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 202 | ActiveBuffer buffer = fetchActiveBuffersLocked(token, uid, pid, packageName, versionCode); |
| 203 | scheduleRotateLocked(); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 204 | return getPfd(buffer.mProcessBuffer); |
| 205 | } |
| 206 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 207 | private Calendar normalizeDate(long timestamp) { |
| 208 | Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")); |
| 209 | calendar.setTimeInMillis(timestamp); |
| 210 | calendar.set(Calendar.HOUR_OF_DAY, 0); |
| 211 | calendar.set(Calendar.MINUTE, 0); |
| 212 | calendar.set(Calendar.SECOND, 0); |
| 213 | calendar.set(Calendar.MILLISECOND, 0); |
| 214 | return calendar; |
| 215 | } |
| 216 | |
| 217 | private File pathForApp(BufferInfo info) { |
| 218 | String subPath = String.format("%d/%s/%d/total", |
| 219 | normalizeDate(info.startTime).getTimeInMillis(), info.packageName, info.versionCode); |
| 220 | return new File(mGraphicsStatsDir, subPath); |
| 221 | } |
| 222 | |
| 223 | private void saveBuffer(HistoricalBuffer buffer) { |
| 224 | if (Trace.isTagEnabled(Trace.TRACE_TAG_SYSTEM_SERVER)) { |
| 225 | Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "saving graphicsstats for " + buffer.mInfo.packageName); |
| 226 | } |
| 227 | synchronized (mFileAccessLock) { |
| 228 | File path = pathForApp(buffer.mInfo); |
| 229 | File parent = path.getParentFile(); |
| 230 | parent.mkdirs(); |
| 231 | if (!parent.exists()) { |
| 232 | Log.w(TAG, "Unable to create path: '" + parent.getAbsolutePath() + "'"); |
| 233 | return; |
| 234 | } |
| 235 | nSaveBuffer(path.getAbsolutePath(), buffer.mInfo.packageName, buffer.mInfo.versionCode, |
| 236 | buffer.mInfo.startTime, buffer.mInfo.endTime, buffer.mData); |
| 237 | } |
| 238 | Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER); |
| 239 | } |
| 240 | |
| 241 | private void deleteRecursiveLocked(File file) { |
| 242 | if (file.isDirectory()) { |
| 243 | for (File child : file.listFiles()) { |
| 244 | deleteRecursiveLocked(child); |
| 245 | } |
| 246 | } |
| 247 | if (!file.delete()) { |
| 248 | Log.w(TAG, "Failed to delete '" + file.getAbsolutePath() + "'!"); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | private void deleteOldBuffers() { |
| 253 | Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "deleting old graphicsstats buffers"); |
| 254 | synchronized (mFileAccessLock) { |
| 255 | File[] files = mGraphicsStatsDir.listFiles(); |
| 256 | if (files == null || files.length <= 3) { |
| 257 | return; |
| 258 | } |
| 259 | long[] sortedDates = new long[files.length]; |
| 260 | for (int i = 0; i < files.length; i++) { |
| 261 | try { |
| 262 | sortedDates[i] = Long.parseLong(files[i].getName()); |
| 263 | } catch (NumberFormatException ex) { |
| 264 | // Skip unrecognized folders |
| 265 | } |
| 266 | } |
| 267 | if (sortedDates.length <= 3) { |
| 268 | return; |
| 269 | } |
| 270 | Arrays.sort(sortedDates); |
| 271 | for (int i = 0; i < sortedDates.length - 3; i++) { |
| 272 | deleteRecursiveLocked(new File(mGraphicsStatsDir, Long.toString(sortedDates[i]))); |
| 273 | } |
| 274 | } |
| 275 | Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER); |
| 276 | } |
| 277 | |
| 278 | private void addToSaveQueue(ActiveBuffer buffer) { |
| 279 | try { |
| 280 | HistoricalBuffer data = new HistoricalBuffer(buffer); |
| 281 | Message.obtain(mWriteOutHandler, SAVE_BUFFER, data).sendToTarget(); |
| 282 | } catch (IOException e) { |
| 283 | Log.w(TAG, "Failed to copy graphicsstats from " + buffer.mInfo.packageName, e); |
| 284 | } |
| 285 | buffer.closeAllBuffers(); |
| 286 | } |
| 287 | |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 288 | private void processDied(ActiveBuffer buffer) { |
| 289 | synchronized (mLock) { |
| 290 | mActive.remove(buffer); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 291 | } |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 292 | addToSaveQueue(buffer); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 293 | } |
| 294 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 295 | private ActiveBuffer fetchActiveBuffersLocked(IGraphicsStatsCallback token, int uid, int pid, |
Dianne Hackborn | 73453e4 | 2017-12-11 16:30:36 -0800 | [diff] [blame] | 296 | String packageName, long versionCode) throws RemoteException { |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 297 | int size = mActive.size(); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 298 | long today = normalizeDate(System.currentTimeMillis()).getTimeInMillis(); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 299 | for (int i = 0; i < size; i++) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 300 | ActiveBuffer buffer = mActive.get(i); |
| 301 | if (buffer.mPid == pid |
| 302 | && buffer.mUid == uid) { |
| 303 | // If the buffer is too old we remove it and return a new one |
| 304 | if (buffer.mInfo.startTime < today) { |
| 305 | buffer.binderDied(); |
| 306 | break; |
| 307 | } else { |
| 308 | return buffer; |
| 309 | } |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | // Didn't find one, need to create it |
| 313 | try { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 314 | ActiveBuffer buffers = new ActiveBuffer(token, uid, pid, packageName, versionCode); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 315 | mActive.add(buffers); |
| 316 | return buffers; |
| 317 | } catch (IOException ex) { |
| 318 | throw new RemoteException("Failed to allocate space"); |
| 319 | } |
| 320 | } |
| 321 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 322 | private HashSet<File> dumpActiveLocked(long dump, ArrayList<HistoricalBuffer> buffers) { |
| 323 | HashSet<File> skipFiles = new HashSet<>(buffers.size()); |
| 324 | for (int i = 0; i < buffers.size(); i++) { |
| 325 | HistoricalBuffer buffer = buffers.get(i); |
| 326 | File path = pathForApp(buffer.mInfo); |
| 327 | skipFiles.add(path); |
| 328 | nAddToDump(dump, path.getAbsolutePath(), buffer.mInfo.packageName, |
| 329 | buffer.mInfo.versionCode, buffer.mInfo.startTime, buffer.mInfo.endTime, |
| 330 | buffer.mData); |
| 331 | } |
| 332 | return skipFiles; |
| 333 | } |
| 334 | |
| 335 | private void dumpHistoricalLocked(long dump, HashSet<File> skipFiles) { |
| 336 | for (File date : mGraphicsStatsDir.listFiles()) { |
| 337 | for (File pkg : date.listFiles()) { |
| 338 | for (File version : pkg.listFiles()) { |
| 339 | File data = new File(version, "total"); |
| 340 | if (skipFiles.contains(data)) { |
| 341 | continue; |
| 342 | } |
| 343 | nAddToDump(dump, data.getAbsolutePath()); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 344 | } |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 345 | } |
| 346 | } |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | @Override |
| 350 | protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { |
Jeff Sharkey | 6df866a | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 351 | if (!DumpUtils.checkDumpAndUsageStatsPermission(mContext, TAG, fout)) return; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 352 | boolean dumpProto = false; |
| 353 | for (String str : args) { |
| 354 | if ("--proto".equals(str)) { |
| 355 | dumpProto = true; |
| 356 | break; |
| 357 | } |
| 358 | } |
| 359 | ArrayList<HistoricalBuffer> buffers; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 360 | synchronized (mLock) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 361 | buffers = new ArrayList<>(mActive.size()); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 362 | for (int i = 0; i < mActive.size(); i++) { |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 363 | try { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 364 | buffers.add(new HistoricalBuffer(mActive.get(i))); |
| 365 | } catch (IOException ex) { |
| 366 | // Ignore |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 367 | } |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 368 | } |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 369 | } |
| 370 | long dump = nCreateDump(fd.getInt$(), dumpProto); |
| 371 | try { |
| 372 | synchronized (mFileAccessLock) { |
| 373 | HashSet<File> skipList = dumpActiveLocked(dump, buffers); |
| 374 | buffers.clear(); |
| 375 | dumpHistoricalLocked(dump, skipList); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 376 | } |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 377 | } finally { |
| 378 | nFinishDump(dump); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | private static native int nGetAshmemSize(); |
| 383 | private static native long nCreateDump(int outFd, boolean isProto); |
| 384 | private static native void nAddToDump(long dump, String path, String packageName, |
Dianne Hackborn | 73453e4 | 2017-12-11 16:30:36 -0800 | [diff] [blame] | 385 | long versionCode, long startTime, long endTime, byte[] data); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 386 | private static native void nAddToDump(long dump, String path); |
| 387 | private static native void nFinishDump(long dump); |
Dianne Hackborn | 73453e4 | 2017-12-11 16:30:36 -0800 | [diff] [blame] | 388 | private static native void nSaveBuffer(String path, String packageName, long versionCode, |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 389 | long startTime, long endTime, byte[] data); |
| 390 | |
| 391 | private final class BufferInfo { |
| 392 | final String packageName; |
Dianne Hackborn | 73453e4 | 2017-12-11 16:30:36 -0800 | [diff] [blame] | 393 | final long versionCode; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 394 | long startTime; |
| 395 | long endTime; |
| 396 | |
Dianne Hackborn | 73453e4 | 2017-12-11 16:30:36 -0800 | [diff] [blame] | 397 | BufferInfo(String packageName, long versionCode, long startTime) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 398 | this.packageName = packageName; |
| 399 | this.versionCode = versionCode; |
| 400 | this.startTime = startTime; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
| 404 | private final class ActiveBuffer implements DeathRecipient { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 405 | final BufferInfo mInfo; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 406 | final int mUid; |
| 407 | final int mPid; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 408 | final IGraphicsStatsCallback mCallback; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 409 | final IBinder mToken; |
| 410 | MemoryFile mProcessBuffer; |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 411 | |
Dianne Hackborn | 73453e4 | 2017-12-11 16:30:36 -0800 | [diff] [blame] | 412 | ActiveBuffer(IGraphicsStatsCallback token, int uid, int pid, String packageName, |
| 413 | long versionCode) |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 414 | throws RemoteException, IOException { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 415 | mInfo = new BufferInfo(packageName, versionCode, System.currentTimeMillis()); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 416 | mUid = uid; |
| 417 | mPid = pid; |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 418 | mCallback = token; |
| 419 | mToken = mCallback.asBinder(); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 420 | mToken.linkToDeath(this, 0); |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 421 | mProcessBuffer = new MemoryFile("GFXStats-" + pid, ASHMEM_SIZE); |
| 422 | mProcessBuffer.writeBytes(ZERO_DATA, 0, 0, ASHMEM_SIZE); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public void binderDied() { |
| 427 | mToken.unlinkToDeath(this, 0); |
| 428 | processDied(this); |
| 429 | } |
| 430 | |
| 431 | void closeAllBuffers() { |
| 432 | if (mProcessBuffer != null) { |
| 433 | mProcessBuffer.close(); |
| 434 | mProcessBuffer = null; |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 439 | private final class HistoricalBuffer { |
| 440 | final BufferInfo mInfo; |
| 441 | final byte[] mData = new byte[ASHMEM_SIZE]; |
| 442 | HistoricalBuffer(ActiveBuffer active) throws IOException { |
| 443 | mInfo = active.mInfo; |
| 444 | mInfo.endTime = System.currentTimeMillis(); |
| 445 | active.mProcessBuffer.readBytes(mData, 0, 0, ASHMEM_SIZE); |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | } |