/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.os; import java.util.ArrayList; /** * Collects performance data between two function calls in Bundle objects and * outputs the results using writer of type {@link PerformanceResultsWriter}. *
* {@link #beginSnapshot(String)} and {@link #endSnapshot()} functions collect * memory usage information and measure runtime between calls to begin and end. * These functions logically wrap around an entire test, and should be called * with name of test as the label, e.g. EmailPerformanceTest. *
* {@link #startTiming(String)} and {@link #stopTiming(String)} functions * measure runtime between calls to start and stop. These functions logically * wrap around a single test case or a small block of code, and should be called * with the name of test case as the label, e.g. testSimpleSendMailSequence. *
* {@link #addIteration(String)} inserts intermediate measurement point which * can be labeled with a String, e.g. Launch email app, compose, send, etc. *
* Snapshot and timing functions do not interfere with each other, and thus can * be called in any order. The intended structure is to wrap begin/endSnapshot * around calls to start/stopTiming, for example: *
* beginSnapshot("EmailPerformanceTest");
* startTiming("testSimpleSendSequence");
* addIteration("Launch email app");
* addIteration("Compose");
* stopTiming("Send");
* startTiming("testComplexSendSequence");
* stopTiming("");
* startTiming("testAddLabel");
* stopTiming("");
* endSnapshot();
*
* Structure of results output is up to implementor of * {@link PerformanceResultsWriter }. * * {@hide} Pending approval for public API. */ public class PerformanceCollector { /** * Interface for reporting performance data. */ public interface PerformanceResultsWriter { /** * Callback invoked as first action in * PerformanceCollector#beginSnapshot(String) for reporting the start of * a performance snapshot. * * @param label description of code block between beginSnapshot and * PerformanceCollector#endSnapshot() * @see PerformanceCollector#beginSnapshot(String) */ public void writeBeginSnapshot(String label); /** * Callback invoked as last action in PerformanceCollector#endSnapshot() * for reporting performance data collected in the snapshot. * * @param results memory and runtime metrics stored as key/value pairs, * in the same structure as returned by * PerformanceCollector#endSnapshot() * @see PerformanceCollector#endSnapshot() */ public void writeEndSnapshot(Bundle results); /** * Callback invoked as first action in * PerformanceCollector#startTiming(String) for reporting the start of * a timing measurement. * * @param label description of code block between startTiming and * PerformanceCollector#stopTiming(String) * @see PerformanceCollector#startTiming(String) */ public void writeStartTiming(String label); /** * Callback invoked as last action in * {@link PerformanceCollector#stopTiming(String)} for reporting the * sequence of timings measured. * * @param results runtime metrics of code block between calls to * startTiming and stopTiming, in the same structure as * returned by PerformanceCollector#stopTiming(String) * @see PerformanceCollector#stopTiming(String) */ public void writeStopTiming(Bundle results); /** * Callback invoked as last action in * {@link PerformanceCollector#addMeasurement(String, long)} for * reporting an integer type measurement. * * @param label short description of the metric that was measured * @param value long value of the measurement */ public void writeMeasurement(String label, long value); /** * Callback invoked as last action in * {@link PerformanceCollector#addMeasurement(String, float)} for * reporting a float type measurement. * * @param label short description of the metric that was measured * @param value float value of the measurement */ public void writeMeasurement(String label, float value); /** * Callback invoked as last action in * {@link PerformanceCollector#addMeasurement(String, String)} for * reporting a string field. * * @param label short description of the metric that was measured * @param value string summary of the measurement */ public void writeMeasurement(String label, String value); } /** * In a results Bundle, this key references a List of iteration Bundles. */ public static final String METRIC_KEY_ITERATIONS = "iterations"; /** * In an iteration Bundle, this key describes the iteration. */ public static final String METRIC_KEY_LABEL = "label"; /** * In a results Bundle, this key reports the cpu time of the code block * under measurement. */ public static final String METRIC_KEY_CPU_TIME = "cpu_time"; /** * In a results Bundle, this key reports the execution time of the code * block under measurement. */ public static final String METRIC_KEY_EXECUTION_TIME = "execution_time"; /** * In a snapshot Bundle, this key reports the number of received * transactions from the binder driver before collection started. */ public static final String METRIC_KEY_PRE_RECEIVED_TRANSACTIONS = "pre_received_transactions"; /** * In a snapshot Bundle, this key reports the number of transactions sent by * the running program before collection started. */ public static final String METRIC_KEY_PRE_SENT_TRANSACTIONS = "pre_sent_transactions"; /** * In a snapshot Bundle, this key reports the number of received * transactions from the binder driver. */ public static final String METRIC_KEY_RECEIVED_TRANSACTIONS = "received_transactions"; /** * In a snapshot Bundle, this key reports the number of transactions sent by * the running program. */ public static final String METRIC_KEY_SENT_TRANSACTIONS = "sent_transactions"; /** * In a snapshot Bundle, this key reports the number of garbage collection * invocations. */ public static final String METRIC_KEY_GC_INVOCATION_COUNT = "gc_invocation_count"; /** * In a snapshot Bundle, this key reports the amount of allocated memory * used by the running program. */ public static final String METRIC_KEY_JAVA_ALLOCATED = "java_allocated"; /** * In a snapshot Bundle, this key reports the amount of free memory * available to the running program. */ public static final String METRIC_KEY_JAVA_FREE = "java_free"; /** * In a snapshot Bundle, this key reports the number of private dirty pages * used by dalvik. */ public static final String METRIC_KEY_JAVA_PRIVATE_DIRTY = "java_private_dirty"; /** * In a snapshot Bundle, this key reports the proportional set size for * dalvik. */ public static final String METRIC_KEY_JAVA_PSS = "java_pss"; /** * In a snapshot Bundle, this key reports the number of shared dirty pages * used by dalvik. */ public static final String METRIC_KEY_JAVA_SHARED_DIRTY = "java_shared_dirty"; /** * In a snapshot Bundle, this key reports the total amount of memory * available to the running program. */ public static final String METRIC_KEY_JAVA_SIZE = "java_size"; /** * In a snapshot Bundle, this key reports the amount of allocated memory in * the native heap. */ public static final String METRIC_KEY_NATIVE_ALLOCATED = "native_allocated"; /** * In a snapshot Bundle, this key reports the amount of free memory in the * native heap. */ public static final String METRIC_KEY_NATIVE_FREE = "native_free"; /** * In a snapshot Bundle, this key reports the number of private dirty pages * used by the native heap. */ public static final String METRIC_KEY_NATIVE_PRIVATE_DIRTY = "native_private_dirty"; /** * In a snapshot Bundle, this key reports the proportional set size for the * native heap. */ public static final String METRIC_KEY_NATIVE_PSS = "native_pss"; /** * In a snapshot Bundle, this key reports the number of shared dirty pages * used by the native heap. */ public static final String METRIC_KEY_NATIVE_SHARED_DIRTY = "native_shared_dirty"; /** * In a snapshot Bundle, this key reports the size of the native heap. */ public static final String METRIC_KEY_NATIVE_SIZE = "native_size"; /** * In a snapshot Bundle, this key reports the number of objects allocated * globally. */ public static final String METRIC_KEY_GLOBAL_ALLOC_COUNT = "global_alloc_count"; /** * In a snapshot Bundle, this key reports the size of all objects allocated * globally. */ public static final String METRIC_KEY_GLOBAL_ALLOC_SIZE = "global_alloc_size"; /** * In a snapshot Bundle, this key reports the number of objects freed * globally. */ public static final String METRIC_KEY_GLOBAL_FREED_COUNT = "global_freed_count"; /** * In a snapshot Bundle, this key reports the size of all objects freed * globally. */ public static final String METRIC_KEY_GLOBAL_FREED_SIZE = "global_freed_size"; /** * In a snapshot Bundle, this key reports the number of private dirty pages * used by everything else. */ public static final String METRIC_KEY_OTHER_PRIVATE_DIRTY = "other_private_dirty"; /** * In a snapshot Bundle, this key reports the proportional set size for * everything else. */ public static final String METRIC_KEY_OTHER_PSS = "other_pss"; /** * In a snapshot Bundle, this key reports the number of shared dirty pages * used by everything else. */ public static final String METRIC_KEY_OTHER_SHARED_DIRTY = "other_shared_dirty"; private PerformanceResultsWriter mPerfWriter; private Bundle mPerfSnapshot; private Bundle mPerfMeasurement; private long mSnapshotCpuTime; private long mSnapshotExecTime; private long mCpuTime; private long mExecTime; public PerformanceCollector() { } public PerformanceCollector(PerformanceResultsWriter writer) { setPerformanceResultsWriter(writer); } public void setPerformanceResultsWriter(PerformanceResultsWriter writer) { mPerfWriter = writer; } /** * Begin collection of memory usage information. * * @param label description of code block between beginSnapshot and * endSnapshot, used to label output */ public void beginSnapshot(String label) { if (mPerfWriter != null) mPerfWriter.writeBeginSnapshot(label); startPerformanceSnapshot(); } /** * End collection of memory usage information. Returns collected data in a * Bundle object. * * @return Memory and runtime metrics stored as key/value pairs. Values are * of type long, and keys include: *