Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 android.os; |
| 18 | |
| 19 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 20 | import android.annotation.UnsupportedAppUsage; |
Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 21 | import java.util.ArrayList; |
| 22 | |
| 23 | /** |
| 24 | * Collects performance data between two function calls in Bundle objects and |
| 25 | * outputs the results using writer of type {@link PerformanceResultsWriter}. |
| 26 | * <p> |
| 27 | * {@link #beginSnapshot(String)} and {@link #endSnapshot()} functions collect |
| 28 | * memory usage information and measure runtime between calls to begin and end. |
| 29 | * These functions logically wrap around an entire test, and should be called |
| 30 | * with name of test as the label, e.g. EmailPerformanceTest. |
| 31 | * <p> |
| 32 | * {@link #startTiming(String)} and {@link #stopTiming(String)} functions |
| 33 | * measure runtime between calls to start and stop. These functions logically |
| 34 | * wrap around a single test case or a small block of code, and should be called |
| 35 | * with the name of test case as the label, e.g. testSimpleSendMailSequence. |
| 36 | * <p> |
| 37 | * {@link #addIteration(String)} inserts intermediate measurement point which |
| 38 | * can be labeled with a String, e.g. Launch email app, compose, send, etc. |
| 39 | * <p> |
| 40 | * Snapshot and timing functions do not interfere with each other, and thus can |
| 41 | * be called in any order. The intended structure is to wrap begin/endSnapshot |
| 42 | * around calls to start/stopTiming, for example: |
| 43 | * <p> |
| 44 | * <code>beginSnapshot("EmailPerformanceTest"); |
| 45 | * startTiming("testSimpleSendSequence"); |
| 46 | * addIteration("Launch email app"); |
| 47 | * addIteration("Compose"); |
| 48 | * stopTiming("Send"); |
| 49 | * startTiming("testComplexSendSequence"); |
| 50 | * stopTiming(""); |
| 51 | * startTiming("testAddLabel"); |
| 52 | * stopTiming(""); |
| 53 | * endSnapshot();</code> |
| 54 | * <p> |
| 55 | * Structure of results output is up to implementor of |
| 56 | * {@link PerformanceResultsWriter }. |
| 57 | * |
| 58 | * {@hide} Pending approval for public API. |
| 59 | */ |
| 60 | public class PerformanceCollector { |
| 61 | |
| 62 | /** |
| 63 | * Interface for reporting performance data. |
| 64 | */ |
| 65 | public interface PerformanceResultsWriter { |
| 66 | |
| 67 | /** |
| 68 | * Callback invoked as first action in |
| 69 | * PerformanceCollector#beginSnapshot(String) for reporting the start of |
| 70 | * a performance snapshot. |
| 71 | * |
| 72 | * @param label description of code block between beginSnapshot and |
| 73 | * PerformanceCollector#endSnapshot() |
| 74 | * @see PerformanceCollector#beginSnapshot(String) |
| 75 | */ |
| 76 | public void writeBeginSnapshot(String label); |
| 77 | |
| 78 | /** |
| 79 | * Callback invoked as last action in PerformanceCollector#endSnapshot() |
| 80 | * for reporting performance data collected in the snapshot. |
| 81 | * |
| 82 | * @param results memory and runtime metrics stored as key/value pairs, |
| 83 | * in the same structure as returned by |
| 84 | * PerformanceCollector#endSnapshot() |
| 85 | * @see PerformanceCollector#endSnapshot() |
| 86 | */ |
| 87 | public void writeEndSnapshot(Bundle results); |
| 88 | |
| 89 | /** |
| 90 | * Callback invoked as first action in |
| 91 | * PerformanceCollector#startTiming(String) for reporting the start of |
| 92 | * a timing measurement. |
| 93 | * |
| 94 | * @param label description of code block between startTiming and |
| 95 | * PerformanceCollector#stopTiming(String) |
| 96 | * @see PerformanceCollector#startTiming(String) |
| 97 | */ |
| 98 | public void writeStartTiming(String label); |
| 99 | |
| 100 | /** |
| 101 | * Callback invoked as last action in |
| 102 | * {@link PerformanceCollector#stopTiming(String)} for reporting the |
| 103 | * sequence of timings measured. |
| 104 | * |
| 105 | * @param results runtime metrics of code block between calls to |
| 106 | * startTiming and stopTiming, in the same structure as |
| 107 | * returned by PerformanceCollector#stopTiming(String) |
| 108 | * @see PerformanceCollector#stopTiming(String) |
| 109 | */ |
| 110 | public void writeStopTiming(Bundle results); |
Jack Wang | 075997f | 2009-10-27 22:01:09 -0700 | [diff] [blame] | 111 | |
| 112 | /** |
| 113 | * Callback invoked as last action in |
| 114 | * {@link PerformanceCollector#addMeasurement(String, long)} for |
| 115 | * reporting an integer type measurement. |
| 116 | * |
| 117 | * @param label short description of the metric that was measured |
| 118 | * @param value long value of the measurement |
| 119 | */ |
| 120 | public void writeMeasurement(String label, long value); |
| 121 | |
| 122 | /** |
| 123 | * Callback invoked as last action in |
| 124 | * {@link PerformanceCollector#addMeasurement(String, float)} for |
| 125 | * reporting a float type measurement. |
| 126 | * |
| 127 | * @param label short description of the metric that was measured |
| 128 | * @param value float value of the measurement |
| 129 | */ |
| 130 | public void writeMeasurement(String label, float value); |
| 131 | |
| 132 | /** |
| 133 | * Callback invoked as last action in |
| 134 | * {@link PerformanceCollector#addMeasurement(String, String)} for |
| 135 | * reporting a string field. |
| 136 | * |
| 137 | * @param label short description of the metric that was measured |
| 138 | * @param value string summary of the measurement |
| 139 | */ |
| 140 | public void writeMeasurement(String label, String value); |
Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
| 144 | * In a results Bundle, this key references a List of iteration Bundles. |
| 145 | */ |
| 146 | public static final String METRIC_KEY_ITERATIONS = "iterations"; |
| 147 | /** |
| 148 | * In an iteration Bundle, this key describes the iteration. |
| 149 | */ |
| 150 | public static final String METRIC_KEY_LABEL = "label"; |
| 151 | /** |
| 152 | * In a results Bundle, this key reports the cpu time of the code block |
| 153 | * under measurement. |
| 154 | */ |
| 155 | public static final String METRIC_KEY_CPU_TIME = "cpu_time"; |
| 156 | /** |
| 157 | * In a results Bundle, this key reports the execution time of the code |
| 158 | * block under measurement. |
| 159 | */ |
| 160 | public static final String METRIC_KEY_EXECUTION_TIME = "execution_time"; |
| 161 | /** |
| 162 | * In a snapshot Bundle, this key reports the number of received |
| 163 | * transactions from the binder driver before collection started. |
| 164 | */ |
| 165 | public static final String METRIC_KEY_PRE_RECEIVED_TRANSACTIONS = "pre_received_transactions"; |
| 166 | /** |
| 167 | * In a snapshot Bundle, this key reports the number of transactions sent by |
| 168 | * the running program before collection started. |
| 169 | */ |
| 170 | public static final String METRIC_KEY_PRE_SENT_TRANSACTIONS = "pre_sent_transactions"; |
| 171 | /** |
| 172 | * In a snapshot Bundle, this key reports the number of received |
| 173 | * transactions from the binder driver. |
| 174 | */ |
| 175 | public static final String METRIC_KEY_RECEIVED_TRANSACTIONS = "received_transactions"; |
| 176 | /** |
| 177 | * In a snapshot Bundle, this key reports the number of transactions sent by |
| 178 | * the running program. |
| 179 | */ |
| 180 | public static final String METRIC_KEY_SENT_TRANSACTIONS = "sent_transactions"; |
| 181 | /** |
| 182 | * In a snapshot Bundle, this key reports the number of garbage collection |
| 183 | * invocations. |
| 184 | */ |
| 185 | public static final String METRIC_KEY_GC_INVOCATION_COUNT = "gc_invocation_count"; |
| 186 | /** |
| 187 | * In a snapshot Bundle, this key reports the amount of allocated memory |
| 188 | * used by the running program. |
| 189 | */ |
| 190 | public static final String METRIC_KEY_JAVA_ALLOCATED = "java_allocated"; |
| 191 | /** |
| 192 | * In a snapshot Bundle, this key reports the amount of free memory |
| 193 | * available to the running program. |
| 194 | */ |
| 195 | public static final String METRIC_KEY_JAVA_FREE = "java_free"; |
| 196 | /** |
| 197 | * In a snapshot Bundle, this key reports the number of private dirty pages |
| 198 | * used by dalvik. |
| 199 | */ |
| 200 | public static final String METRIC_KEY_JAVA_PRIVATE_DIRTY = "java_private_dirty"; |
| 201 | /** |
| 202 | * In a snapshot Bundle, this key reports the proportional set size for |
| 203 | * dalvik. |
| 204 | */ |
| 205 | public static final String METRIC_KEY_JAVA_PSS = "java_pss"; |
| 206 | /** |
| 207 | * In a snapshot Bundle, this key reports the number of shared dirty pages |
| 208 | * used by dalvik. |
| 209 | */ |
| 210 | public static final String METRIC_KEY_JAVA_SHARED_DIRTY = "java_shared_dirty"; |
| 211 | /** |
| 212 | * In a snapshot Bundle, this key reports the total amount of memory |
| 213 | * available to the running program. |
| 214 | */ |
| 215 | public static final String METRIC_KEY_JAVA_SIZE = "java_size"; |
| 216 | /** |
| 217 | * In a snapshot Bundle, this key reports the amount of allocated memory in |
| 218 | * the native heap. |
| 219 | */ |
| 220 | public static final String METRIC_KEY_NATIVE_ALLOCATED = "native_allocated"; |
| 221 | /** |
| 222 | * In a snapshot Bundle, this key reports the amount of free memory in the |
| 223 | * native heap. |
| 224 | */ |
| 225 | public static final String METRIC_KEY_NATIVE_FREE = "native_free"; |
| 226 | /** |
| 227 | * In a snapshot Bundle, this key reports the number of private dirty pages |
| 228 | * used by the native heap. |
| 229 | */ |
| 230 | public static final String METRIC_KEY_NATIVE_PRIVATE_DIRTY = "native_private_dirty"; |
| 231 | /** |
| 232 | * In a snapshot Bundle, this key reports the proportional set size for the |
| 233 | * native heap. |
| 234 | */ |
| 235 | public static final String METRIC_KEY_NATIVE_PSS = "native_pss"; |
| 236 | /** |
| 237 | * In a snapshot Bundle, this key reports the number of shared dirty pages |
| 238 | * used by the native heap. |
| 239 | */ |
| 240 | public static final String METRIC_KEY_NATIVE_SHARED_DIRTY = "native_shared_dirty"; |
| 241 | /** |
| 242 | * In a snapshot Bundle, this key reports the size of the native heap. |
| 243 | */ |
| 244 | public static final String METRIC_KEY_NATIVE_SIZE = "native_size"; |
| 245 | /** |
| 246 | * In a snapshot Bundle, this key reports the number of objects allocated |
| 247 | * globally. |
| 248 | */ |
| 249 | public static final String METRIC_KEY_GLOBAL_ALLOC_COUNT = "global_alloc_count"; |
| 250 | /** |
| 251 | * In a snapshot Bundle, this key reports the size of all objects allocated |
| 252 | * globally. |
| 253 | */ |
| 254 | public static final String METRIC_KEY_GLOBAL_ALLOC_SIZE = "global_alloc_size"; |
| 255 | /** |
| 256 | * In a snapshot Bundle, this key reports the number of objects freed |
| 257 | * globally. |
| 258 | */ |
| 259 | public static final String METRIC_KEY_GLOBAL_FREED_COUNT = "global_freed_count"; |
| 260 | /** |
| 261 | * In a snapshot Bundle, this key reports the size of all objects freed |
| 262 | * globally. |
| 263 | */ |
| 264 | public static final String METRIC_KEY_GLOBAL_FREED_SIZE = "global_freed_size"; |
| 265 | /** |
| 266 | * In a snapshot Bundle, this key reports the number of private dirty pages |
| 267 | * used by everything else. |
| 268 | */ |
| 269 | public static final String METRIC_KEY_OTHER_PRIVATE_DIRTY = "other_private_dirty"; |
| 270 | /** |
| 271 | * In a snapshot Bundle, this key reports the proportional set size for |
| 272 | * everything else. |
| 273 | */ |
| 274 | public static final String METRIC_KEY_OTHER_PSS = "other_pss"; |
| 275 | /** |
| 276 | * In a snapshot Bundle, this key reports the number of shared dirty pages |
| 277 | * used by everything else. |
| 278 | */ |
| 279 | public static final String METRIC_KEY_OTHER_SHARED_DIRTY = "other_shared_dirty"; |
| 280 | |
| 281 | private PerformanceResultsWriter mPerfWriter; |
| 282 | private Bundle mPerfSnapshot; |
| 283 | private Bundle mPerfMeasurement; |
| 284 | private long mSnapshotCpuTime; |
| 285 | private long mSnapshotExecTime; |
| 286 | private long mCpuTime; |
| 287 | private long mExecTime; |
| 288 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 289 | @UnsupportedAppUsage |
Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 290 | public PerformanceCollector() { |
| 291 | } |
| 292 | |
| 293 | public PerformanceCollector(PerformanceResultsWriter writer) { |
| 294 | setPerformanceResultsWriter(writer); |
| 295 | } |
| 296 | |
| 297 | public void setPerformanceResultsWriter(PerformanceResultsWriter writer) { |
| 298 | mPerfWriter = writer; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Begin collection of memory usage information. |
| 303 | * |
| 304 | * @param label description of code block between beginSnapshot and |
| 305 | * endSnapshot, used to label output |
| 306 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 307 | @UnsupportedAppUsage |
Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 308 | public void beginSnapshot(String label) { |
| 309 | if (mPerfWriter != null) |
| 310 | mPerfWriter.writeBeginSnapshot(label); |
| 311 | startPerformanceSnapshot(); |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * End collection of memory usage information. Returns collected data in a |
| 316 | * Bundle object. |
| 317 | * |
| 318 | * @return Memory and runtime metrics stored as key/value pairs. Values are |
| 319 | * of type long, and keys include: |
| 320 | * <ul> |
| 321 | * <li>{@link #METRIC_KEY_CPU_TIME cpu_time} |
| 322 | * <li>{@link #METRIC_KEY_EXECUTION_TIME execution_time} |
| 323 | * <li>{@link #METRIC_KEY_PRE_RECEIVED_TRANSACTIONS |
| 324 | * pre_received_transactions} |
| 325 | * <li>{@link #METRIC_KEY_PRE_SENT_TRANSACTIONS |
| 326 | * pre_sent_transactions} |
| 327 | * <li>{@link #METRIC_KEY_RECEIVED_TRANSACTIONS |
| 328 | * received_transactions} |
| 329 | * <li>{@link #METRIC_KEY_SENT_TRANSACTIONS sent_transactions} |
| 330 | * <li>{@link #METRIC_KEY_GC_INVOCATION_COUNT gc_invocation_count} |
| 331 | * <li>{@link #METRIC_KEY_JAVA_ALLOCATED java_allocated} |
| 332 | * <li>{@link #METRIC_KEY_JAVA_FREE java_free} |
| 333 | * <li>{@link #METRIC_KEY_JAVA_PRIVATE_DIRTY java_private_dirty} |
| 334 | * <li>{@link #METRIC_KEY_JAVA_PSS java_pss} |
| 335 | * <li>{@link #METRIC_KEY_JAVA_SHARED_DIRTY java_shared_dirty} |
| 336 | * <li>{@link #METRIC_KEY_JAVA_SIZE java_size} |
| 337 | * <li>{@link #METRIC_KEY_NATIVE_ALLOCATED native_allocated} |
| 338 | * <li>{@link #METRIC_KEY_NATIVE_FREE native_free} |
| 339 | * <li>{@link #METRIC_KEY_NATIVE_PRIVATE_DIRTY native_private_dirty} |
| 340 | * <li>{@link #METRIC_KEY_NATIVE_PSS native_pss} |
| 341 | * <li>{@link #METRIC_KEY_NATIVE_SHARED_DIRTY native_shared_dirty} |
| 342 | * <li>{@link #METRIC_KEY_NATIVE_SIZE native_size} |
| 343 | * <li>{@link #METRIC_KEY_GLOBAL_ALLOC_COUNT global_alloc_count} |
| 344 | * <li>{@link #METRIC_KEY_GLOBAL_ALLOC_SIZE global_alloc_size} |
| 345 | * <li>{@link #METRIC_KEY_GLOBAL_FREED_COUNT global_freed_count} |
| 346 | * <li>{@link #METRIC_KEY_GLOBAL_FREED_SIZE global_freed_size} |
| 347 | * <li>{@link #METRIC_KEY_OTHER_PRIVATE_DIRTY other_private_dirty} |
| 348 | * <li>{@link #METRIC_KEY_OTHER_PSS other_pss} |
| 349 | * <li>{@link #METRIC_KEY_OTHER_SHARED_DIRTY other_shared_dirty} |
| 350 | * </ul> |
| 351 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 352 | @UnsupportedAppUsage |
Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 353 | public Bundle endSnapshot() { |
| 354 | endPerformanceSnapshot(); |
| 355 | if (mPerfWriter != null) |
| 356 | mPerfWriter.writeEndSnapshot(mPerfSnapshot); |
| 357 | return mPerfSnapshot; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Start measurement of user and cpu time. |
| 362 | * |
| 363 | * @param label description of code block between startTiming and |
| 364 | * stopTiming, used to label output |
| 365 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 366 | @UnsupportedAppUsage |
Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 367 | public void startTiming(String label) { |
| 368 | if (mPerfWriter != null) |
| 369 | mPerfWriter.writeStartTiming(label); |
| 370 | mPerfMeasurement = new Bundle(); |
| 371 | mPerfMeasurement.putParcelableArrayList( |
| 372 | METRIC_KEY_ITERATIONS, new ArrayList<Parcelable>()); |
| 373 | mExecTime = SystemClock.uptimeMillis(); |
| 374 | mCpuTime = Process.getElapsedCpuTime(); |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Add a measured segment, and start measuring the next segment. Returns |
| 379 | * collected data in a Bundle object. |
| 380 | * |
| 381 | * @param label description of code block between startTiming and |
| 382 | * addIteration, and between two calls to addIteration, used |
| 383 | * to label output |
| 384 | * @return Runtime metrics stored as key/value pairs. Values are of type |
| 385 | * long, and keys include: |
| 386 | * <ul> |
| 387 | * <li>{@link #METRIC_KEY_LABEL label} |
| 388 | * <li>{@link #METRIC_KEY_CPU_TIME cpu_time} |
| 389 | * <li>{@link #METRIC_KEY_EXECUTION_TIME execution_time} |
| 390 | * </ul> |
| 391 | */ |
| 392 | public Bundle addIteration(String label) { |
| 393 | mCpuTime = Process.getElapsedCpuTime() - mCpuTime; |
| 394 | mExecTime = SystemClock.uptimeMillis() - mExecTime; |
| 395 | |
| 396 | Bundle iteration = new Bundle(); |
| 397 | iteration.putString(METRIC_KEY_LABEL, label); |
| 398 | iteration.putLong(METRIC_KEY_EXECUTION_TIME, mExecTime); |
| 399 | iteration.putLong(METRIC_KEY_CPU_TIME, mCpuTime); |
| 400 | mPerfMeasurement.getParcelableArrayList(METRIC_KEY_ITERATIONS).add(iteration); |
| 401 | |
| 402 | mExecTime = SystemClock.uptimeMillis(); |
| 403 | mCpuTime = Process.getElapsedCpuTime(); |
| 404 | return iteration; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Stop measurement of user and cpu time. |
| 409 | * |
| 410 | * @param label description of code block between addIteration or |
| 411 | * startTiming and stopTiming, used to label output |
| 412 | * @return Runtime metrics stored in a bundle, including all iterations |
| 413 | * between calls to startTiming and stopTiming. List of iterations |
| 414 | * is keyed by {@link #METRIC_KEY_ITERATIONS iterations}. |
| 415 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame] | 416 | @UnsupportedAppUsage |
Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 417 | public Bundle stopTiming(String label) { |
| 418 | addIteration(label); |
| 419 | if (mPerfWriter != null) |
| 420 | mPerfWriter.writeStopTiming(mPerfMeasurement); |
| 421 | return mPerfMeasurement; |
| 422 | } |
| 423 | |
Jack Wang | 075997f | 2009-10-27 22:01:09 -0700 | [diff] [blame] | 424 | /** |
| 425 | * Add an integer type measurement to the collector. |
| 426 | * |
| 427 | * @param label short description of the metric that was measured |
| 428 | * @param value long value of the measurement |
| 429 | */ |
| 430 | public void addMeasurement(String label, long value) { |
| 431 | if (mPerfWriter != null) |
| 432 | mPerfWriter.writeMeasurement(label, value); |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Add a float type measurement to the collector. |
| 437 | * |
| 438 | * @param label short description of the metric that was measured |
| 439 | * @param value float value of the measurement |
| 440 | */ |
| 441 | public void addMeasurement(String label, float value) { |
| 442 | if (mPerfWriter != null) |
| 443 | mPerfWriter.writeMeasurement(label, value); |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Add a string field to the collector. |
| 448 | * |
| 449 | * @param label short description of the metric that was measured |
| 450 | * @param value string summary of the measurement |
| 451 | */ |
| 452 | public void addMeasurement(String label, String value) { |
| 453 | if (mPerfWriter != null) |
| 454 | mPerfWriter.writeMeasurement(label, value); |
| 455 | } |
| 456 | |
Jack Wang | ff1df69 | 2009-08-26 17:19:13 -0700 | [diff] [blame] | 457 | /* |
| 458 | * Starts tracking memory usage, binder transactions, and real & cpu timing. |
| 459 | */ |
| 460 | private void startPerformanceSnapshot() { |
| 461 | // Create new snapshot |
| 462 | mPerfSnapshot = new Bundle(); |
| 463 | |
| 464 | // Add initial binder counts |
| 465 | Bundle binderCounts = getBinderCounts(); |
| 466 | for (String key : binderCounts.keySet()) { |
| 467 | mPerfSnapshot.putLong("pre_" + key, binderCounts.getLong(key)); |
| 468 | } |
| 469 | |
| 470 | // Force a GC and zero out the performance counters. Do this |
| 471 | // before reading initial CPU/wall-clock times so we don't include |
| 472 | // the cost of this setup in our final metrics. |
| 473 | startAllocCounting(); |
| 474 | |
| 475 | // Record CPU time up to this point, and start timing. Note: this |
| 476 | // must happen at the end of this method, otherwise the timing will |
| 477 | // include noise. |
| 478 | mSnapshotExecTime = SystemClock.uptimeMillis(); |
| 479 | mSnapshotCpuTime = Process.getElapsedCpuTime(); |
| 480 | } |
| 481 | |
| 482 | /* |
| 483 | * Stops tracking memory usage, binder transactions, and real & cpu timing. |
| 484 | * Stores collected data as type long into Bundle object for reporting. |
| 485 | */ |
| 486 | private void endPerformanceSnapshot() { |
| 487 | // Stop the timing. This must be done first before any other counting is |
| 488 | // stopped. |
| 489 | mSnapshotCpuTime = Process.getElapsedCpuTime() - mSnapshotCpuTime; |
| 490 | mSnapshotExecTime = SystemClock.uptimeMillis() - mSnapshotExecTime; |
| 491 | |
| 492 | stopAllocCounting(); |
| 493 | |
| 494 | long nativeMax = Debug.getNativeHeapSize() / 1024; |
| 495 | long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024; |
| 496 | long nativeFree = Debug.getNativeHeapFreeSize() / 1024; |
| 497 | |
| 498 | Debug.MemoryInfo memInfo = new Debug.MemoryInfo(); |
| 499 | Debug.getMemoryInfo(memInfo); |
| 500 | |
| 501 | Runtime runtime = Runtime.getRuntime(); |
| 502 | |
| 503 | long dalvikMax = runtime.totalMemory() / 1024; |
| 504 | long dalvikFree = runtime.freeMemory() / 1024; |
| 505 | long dalvikAllocated = dalvikMax - dalvikFree; |
| 506 | |
| 507 | // Add final binder counts |
| 508 | Bundle binderCounts = getBinderCounts(); |
| 509 | for (String key : binderCounts.keySet()) { |
| 510 | mPerfSnapshot.putLong(key, binderCounts.getLong(key)); |
| 511 | } |
| 512 | |
| 513 | // Add alloc counts |
| 514 | Bundle allocCounts = getAllocCounts(); |
| 515 | for (String key : allocCounts.keySet()) { |
| 516 | mPerfSnapshot.putLong(key, allocCounts.getLong(key)); |
| 517 | } |
| 518 | |
| 519 | mPerfSnapshot.putLong(METRIC_KEY_EXECUTION_TIME, mSnapshotExecTime); |
| 520 | mPerfSnapshot.putLong(METRIC_KEY_CPU_TIME, mSnapshotCpuTime); |
| 521 | |
| 522 | mPerfSnapshot.putLong(METRIC_KEY_NATIVE_SIZE, nativeMax); |
| 523 | mPerfSnapshot.putLong(METRIC_KEY_NATIVE_ALLOCATED, nativeAllocated); |
| 524 | mPerfSnapshot.putLong(METRIC_KEY_NATIVE_FREE, nativeFree); |
| 525 | mPerfSnapshot.putLong(METRIC_KEY_NATIVE_PSS, memInfo.nativePss); |
| 526 | mPerfSnapshot.putLong(METRIC_KEY_NATIVE_PRIVATE_DIRTY, memInfo.nativePrivateDirty); |
| 527 | mPerfSnapshot.putLong(METRIC_KEY_NATIVE_SHARED_DIRTY, memInfo.nativeSharedDirty); |
| 528 | |
| 529 | mPerfSnapshot.putLong(METRIC_KEY_JAVA_SIZE, dalvikMax); |
| 530 | mPerfSnapshot.putLong(METRIC_KEY_JAVA_ALLOCATED, dalvikAllocated); |
| 531 | mPerfSnapshot.putLong(METRIC_KEY_JAVA_FREE, dalvikFree); |
| 532 | mPerfSnapshot.putLong(METRIC_KEY_JAVA_PSS, memInfo.dalvikPss); |
| 533 | mPerfSnapshot.putLong(METRIC_KEY_JAVA_PRIVATE_DIRTY, memInfo.dalvikPrivateDirty); |
| 534 | mPerfSnapshot.putLong(METRIC_KEY_JAVA_SHARED_DIRTY, memInfo.dalvikSharedDirty); |
| 535 | |
| 536 | mPerfSnapshot.putLong(METRIC_KEY_OTHER_PSS, memInfo.otherPss); |
| 537 | mPerfSnapshot.putLong(METRIC_KEY_OTHER_PRIVATE_DIRTY, memInfo.otherPrivateDirty); |
| 538 | mPerfSnapshot.putLong(METRIC_KEY_OTHER_SHARED_DIRTY, memInfo.otherSharedDirty); |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * Starts allocation counting. This triggers a gc and resets the counts. |
| 543 | */ |
| 544 | private static void startAllocCounting() { |
| 545 | // Before we start trigger a GC and reset the debug counts. Run the |
| 546 | // finalizers and another GC before starting and stopping the alloc |
| 547 | // counts. This will free up any objects that were just sitting around |
| 548 | // waiting for their finalizers to be run. |
| 549 | Runtime.getRuntime().gc(); |
| 550 | Runtime.getRuntime().runFinalization(); |
| 551 | Runtime.getRuntime().gc(); |
| 552 | |
| 553 | Debug.resetAllCounts(); |
| 554 | |
| 555 | // start the counts |
| 556 | Debug.startAllocCounting(); |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * Stops allocation counting. |
| 561 | */ |
| 562 | private static void stopAllocCounting() { |
| 563 | Runtime.getRuntime().gc(); |
| 564 | Runtime.getRuntime().runFinalization(); |
| 565 | Runtime.getRuntime().gc(); |
| 566 | Debug.stopAllocCounting(); |
| 567 | } |
| 568 | |
| 569 | /* |
| 570 | * Returns a bundle with the current results from the allocation counting. |
| 571 | */ |
| 572 | private static Bundle getAllocCounts() { |
| 573 | Bundle results = new Bundle(); |
| 574 | results.putLong(METRIC_KEY_GLOBAL_ALLOC_COUNT, Debug.getGlobalAllocCount()); |
| 575 | results.putLong(METRIC_KEY_GLOBAL_ALLOC_SIZE, Debug.getGlobalAllocSize()); |
| 576 | results.putLong(METRIC_KEY_GLOBAL_FREED_COUNT, Debug.getGlobalFreedCount()); |
| 577 | results.putLong(METRIC_KEY_GLOBAL_FREED_SIZE, Debug.getGlobalFreedSize()); |
| 578 | results.putLong(METRIC_KEY_GC_INVOCATION_COUNT, Debug.getGlobalGcInvocationCount()); |
| 579 | return results; |
| 580 | } |
| 581 | |
| 582 | /* |
| 583 | * Returns a bundle with the counts for various binder counts for this |
| 584 | * process. Currently the only two that are reported are the number of send |
| 585 | * and the number of received transactions. |
| 586 | */ |
| 587 | private static Bundle getBinderCounts() { |
| 588 | Bundle results = new Bundle(); |
| 589 | results.putLong(METRIC_KEY_SENT_TRANSACTIONS, Debug.getBinderSentTransactions()); |
| 590 | results.putLong(METRIC_KEY_RECEIVED_TRANSACTIONS, Debug.getBinderReceivedTransactions()); |
| 591 | return results; |
| 592 | } |
| 593 | } |