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