1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.base;
7 import android.os.Debug;
8 import android.os.Debug.MemoryInfo;
9 import android.util.Log;
11 import org.json.JSONArray;
12 import org.json.JSONException;
13 import org.json.JSONObject;
16 import java.io.FileNotFoundException;
17 import java.io.FileOutputStream;
18 import java.io.PrintStream;
19 import java.util.LinkedList;
20 import java.util.List;
23 * PerfTraceEvent can be used like TraceEvent, but is intended for
24 * performance measurement. By limiting the types of tracing we hope
25 * to minimize impact on measurement.
27 * All PerfTraceEvent events funnel into TraceEvent. When not doing
28 * performance measurements, they act the same. However,
29 * PerfTraceEvents can be enabled even when TraceEvent is not.
31 * Unlike TraceEvent, PerfTraceEvent data is sent to the system log,
32 * not to a trace file.
34 * Performance events need to have very specific names so we find
35 * the right ones. For example, we specify the name exactly in
36 * the @TracePerf annotation. Thus, unlike TraceEvent, we do not
37 * support an implicit trace name based on the callstack.
39 public class PerfTraceEvent {
40 private static final int MAX_NAME_LENGTH = 40;
41 private static final String MEMORY_TRACE_NAME_SUFFIX = "_BZR_PSS";
42 private static File sOutputFile = null;
44 /** The event types understood by the perf trace scripts. */
45 private enum EventType {
50 // The string understood by the trace scripts.
51 private final String mTypeStr;
53 EventType(String typeStr) {
58 public String toString() {
63 private static boolean sEnabled = false;
64 private static boolean sTrackTiming = true;
65 private static boolean sTrackMemory = false;
67 // A list of performance trace event strings.
68 // Events are stored as a JSON dict much like TraceEvent.
69 // E.g. timestamp is in microseconds.
70 private static JSONArray sPerfTraceStrings;
72 // A filter for performance tracing. Only events that match a
73 // string in the list are saved. Presence of a filter does not
74 // necessarily mean perf tracing is enabled.
75 private static List<String> sFilter;
77 // Nanosecond start time of performance tracing.
78 private static long sBeginNanoTime;
81 * Specifies what event names will be tracked.
83 * @param strings Event names we will record.
86 public static synchronized void setFilter(List<String> strings) {
87 sFilter = new LinkedList<String>(strings);
91 * Enable or disable perf tracing.
92 * Disabling of perf tracing will dump trace data to the system log.
95 public static synchronized void setEnabled(boolean enabled) {
96 if (sEnabled == enabled) {
100 sBeginNanoTime = System.nanoTime();
101 sPerfTraceStrings = new JSONArray();
104 sPerfTraceStrings = null;
111 * Enables memory tracking for all timing perf events tracked.
114 * Only works when called in combination with {@link #setEnabled(boolean)}.
117 * By enabling this feature, an additional perf event containing the memory usage will be
118 * logged whenever {@link #instant(String)}, {@link #begin(String)}, or {@link #end(String)}
121 * @param enabled Whether to enable memory tracking for all perf events.
124 public static synchronized void setMemoryTrackingEnabled(boolean enabled) {
125 sTrackMemory = enabled;
129 * Enables timing tracking for all perf events tracked.
132 * Only works when called in combination with {@link #setEnabled(boolean)}.
135 * If this feature is enabled, whenever {@link #instant(String)}, {@link #begin(String)},
136 * or {@link #end(String)} is called the time since start of tracking will be logged.
138 * @param enabled Whether to enable timing tracking for all perf events.
141 public static synchronized void setTimingTrackingEnabled(boolean enabled) {
142 sTrackTiming = enabled;
146 * @return True if tracing is enabled, false otherwise.
147 * It is safe to call trace methods without checking if PerfTraceEvent
151 public static synchronized boolean enabled() {
156 * Record an "instant" perf trace event. E.g. "screen update happened".
158 public static synchronized void instant(String name) {
159 // Instant doesn't really need/take an event id, but this should be okay.
160 final long eventId = name.hashCode();
161 TraceEvent.instant(name);
162 if (sEnabled && matchesFilter(name)) {
163 savePerfString(name, eventId, EventType.INSTANT, false);
169 * Record an "begin" perf trace event.
170 * Begin trace events should have a matching end event.
173 public static synchronized void begin(String name) {
174 final long eventId = name.hashCode();
175 TraceEvent.startAsync(name, eventId);
176 if (sEnabled && matchesFilter(name)) {
177 // Done before calculating the starting perf data to ensure calculating the memory usage
178 // does not influence the timing data.
180 savePerfString(makeMemoryTraceNameFromTimingName(name), eventId, EventType.START,
184 savePerfString(name, eventId, EventType.START, false);
190 * Record an "end" perf trace event, to match a begin event. The
191 * time delta between begin and end is usually interesting to
195 public static synchronized void end(String name) {
196 final long eventId = name.hashCode();
197 TraceEvent.finishAsync(name, eventId);
198 if (sEnabled && matchesFilter(name)) {
200 savePerfString(name, eventId, EventType.FINISH, false);
202 // Done after calculating the ending perf data to ensure calculating the memory usage
203 // does not influence the timing data.
205 savePerfString(makeMemoryTraceNameFromTimingName(name), eventId, EventType.FINISH,
212 * Record an "begin" memory trace event.
213 * Begin trace events should have a matching end event.
216 public static synchronized void begin(String name, MemoryInfo memoryInfo) {
217 final long eventId = name.hashCode();
218 TraceEvent.startAsync(name, eventId);
219 if (sEnabled && matchesFilter(name)) {
220 // Done before calculating the starting perf data to ensure calculating the memory usage
221 // does not influence the timing data.
222 long timestampUs = (System.nanoTime() - sBeginNanoTime) / 1000;
223 savePerfString(makeMemoryTraceNameFromTimingName(name), eventId, EventType.START,
224 timestampUs, memoryInfo);
226 savePerfString(name, eventId, EventType.START, false);
232 * Record an "end" memory trace event, to match a begin event. The
233 * memory usage delta between begin and end is usually interesting to
237 public static synchronized void end(String name, MemoryInfo memoryInfo) {
238 final long eventId = name.hashCode();
239 TraceEvent.finishAsync(name, eventId);
240 if (sEnabled && matchesFilter(name)) {
242 savePerfString(name, eventId, EventType.FINISH, false);
244 // Done after calculating the instant perf data to ensure calculating the memory usage
245 // does not influence the timing data.
246 long timestampUs = (System.nanoTime() - sBeginNanoTime) / 1000;
247 savePerfString(makeMemoryTraceNameFromTimingName(name), eventId, EventType.FINISH,
248 timestampUs, memoryInfo);
253 * Determine if we are interested in this trace event.
254 * @return True if the name matches the allowed filter; else false.
256 private static boolean matchesFilter(String name) {
257 return sFilter != null ? sFilter.contains(name) : false;
261 * Save a perf trace event as a JSON dict. The format mirrors a TraceEvent dict.
263 * @param name The trace data
264 * @param id The id of the event
265 * @param type the type of trace event (I, S, F)
266 * @param includeMemory Whether to include current browser process memory usage in the trace.
268 private static void savePerfString(String name, long id, EventType type,
269 boolean includeMemory) {
270 long timestampUs = (System.nanoTime() - sBeginNanoTime) / 1000;
271 MemoryInfo memInfo = null;
273 memInfo = new MemoryInfo();
274 Debug.getMemoryInfo(memInfo);
276 savePerfString(name, id, type, timestampUs, memInfo);
280 * Save a perf trace event as a JSON dict. The format mirrors a TraceEvent dict.
282 * @param name The trace data
283 * @param id The id of the event
284 * @param type the type of trace event (I, S, F)
285 * @param timestampUs The time stamp at which this event was recorded
286 * @param memoryInfo Memory details to be included in this perf string, null if
287 * no memory details are to be included.
289 private static void savePerfString(String name, long id, EventType type, long timestampUs,
290 MemoryInfo memoryInfo) {
292 JSONObject traceObj = new JSONObject();
293 traceObj.put("cat", "Java");
294 traceObj.put("ts", timestampUs);
295 traceObj.put("ph", type);
296 traceObj.put("name", name);
297 traceObj.put("id", id);
298 if (memoryInfo != null) {
299 int pss = memoryInfo.nativePss + memoryInfo.dalvikPss + memoryInfo.otherPss;
300 traceObj.put("mem", pss);
302 sPerfTraceStrings.put(traceObj);
303 } catch (JSONException e) {
304 throw new RuntimeException(e);
309 * Generating a trace name for tracking memory based on the timing name passed in.
311 * @param name The timing name to use as a base for the memory perf name.
312 * @return The memory perf name to use.
314 public static String makeMemoryTraceNameFromTimingName(String name) {
315 return makeSafeTraceName(name, MEMORY_TRACE_NAME_SUFFIX);
319 * Builds a name to be used in the perf trace framework. The framework has length requirements
320 * for names, so this ensures the generated name does not exceed the maximum (trimming the
321 * base name if necessary).
323 * @param baseName The base name to use when generating the name.
324 * @param suffix The required suffix to be appended to the name.
325 * @return A name that is safe for the perf trace framework.
327 public static String makeSafeTraceName(String baseName, String suffix) {
328 int suffixLength = suffix.length();
330 if (baseName.length() + suffixLength > MAX_NAME_LENGTH) {
331 baseName = baseName.substring(0, MAX_NAME_LENGTH - suffixLength);
333 return baseName + suffix;
337 * Sets a file to dump the results to. If {@code file} is {@code null}, it will be dumped
338 * to STDOUT, otherwise the JSON performance data will be appended to {@code file}. This should
339 * be called before the performance run starts. When {@link #setEnabled(boolean)} is called
340 * with {@code false}, the perf data will be dumped.
342 * @param file Which file to append the performance data to. If {@code null}, the performance
343 * data will be sent to STDOUT.
346 public static synchronized void setOutputFile(File file) {
351 * Dump all performance data we have saved up to the log.
352 * Output as JSON for parsing convenience.
354 private static void dumpPerf() {
355 String json = sPerfTraceStrings.toString();
357 if (sOutputFile == null) {
358 System.out.println(json);
361 PrintStream stream = new PrintStream(new FileOutputStream(sOutputFile, true));
367 } catch (Exception ex) {
368 Log.e("PerfTraceEvent", "Unable to close perf trace output file.");
371 } catch (FileNotFoundException ex) {
372 Log.e("PerfTraceEvent", "Unable to dump perf trace data to output file.");