This source file includes following definitions.
- START
- FINISH
- INSTANT
- toString
- setFilter
- setEnabled
- setMemoryTrackingEnabled
- setTimingTrackingEnabled
- enabled
- instant
- begin
- end
- begin
- end
- matchesFilter
- savePerfString
- savePerfString
- makeMemoryTraceNameFromTimingName
- makeSafeTraceName
- setOutputFile
- dumpPerf
package org.chromium.base;
import android.os.Debug;
import android.os.Debug.MemoryInfo;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.LinkedList;
import java.util.List;
public class PerfTraceEvent {
private static final int MAX_NAME_LENGTH = 40;
private static final String MEMORY_TRACE_NAME_SUFFIX = "_BZR_PSS";
private static File sOutputFile = null;
private enum EventType {
START("S"),
FINISH("F"),
INSTANT("I");
private final String mTypeStr;
EventType(String typeStr) {
mTypeStr = typeStr;
}
@Override
public String toString() {
return mTypeStr;
}
}
private static boolean sEnabled = false;
private static boolean sTrackTiming = true;
private static boolean sTrackMemory = false;
private static JSONArray sPerfTraceStrings;
private static List<String> sFilter;
private static long sBeginNanoTime;
public static synchronized void setFilter(List<String> strings) {
sFilter = new LinkedList<String>(strings);
}
public static synchronized void setEnabled(boolean enabled) {
if (sEnabled == enabled) {
return;
}
if (enabled) {
sBeginNanoTime = System.nanoTime();
sPerfTraceStrings = new JSONArray();
} else {
dumpPerf();
sPerfTraceStrings = null;
sFilter = null;
}
sEnabled = enabled;
}
public static synchronized void setMemoryTrackingEnabled(boolean enabled) {
sTrackMemory = enabled;
}
public static synchronized void setTimingTrackingEnabled(boolean enabled) {
sTrackTiming = enabled;
}
public static synchronized boolean enabled() {
return sEnabled;
}
public static synchronized void instant(String name) {
final long eventId = name.hashCode();
TraceEvent.instant(name);
if (sEnabled && matchesFilter(name)) {
savePerfString(name, eventId, EventType.INSTANT, false);
}
}
public static synchronized void begin(String name) {
final long eventId = name.hashCode();
TraceEvent.startAsync(name, eventId);
if (sEnabled && matchesFilter(name)) {
if (sTrackMemory) {
savePerfString(makeMemoryTraceNameFromTimingName(name), eventId, EventType.START,
true);
}
if (sTrackTiming) {
savePerfString(name, eventId, EventType.START, false);
}
}
}
public static synchronized void end(String name) {
final long eventId = name.hashCode();
TraceEvent.finishAsync(name, eventId);
if (sEnabled && matchesFilter(name)) {
if (sTrackTiming) {
savePerfString(name, eventId, EventType.FINISH, false);
}
if (sTrackMemory) {
savePerfString(makeMemoryTraceNameFromTimingName(name), eventId, EventType.FINISH,
true);
}
}
}
public static synchronized void begin(String name, MemoryInfo memoryInfo) {
final long eventId = name.hashCode();
TraceEvent.startAsync(name, eventId);
if (sEnabled && matchesFilter(name)) {
long timestampUs = (System.nanoTime() - sBeginNanoTime) / 1000;
savePerfString(makeMemoryTraceNameFromTimingName(name), eventId, EventType.START,
timestampUs, memoryInfo);
if (sTrackTiming) {
savePerfString(name, eventId, EventType.START, false);
}
}
}
public static synchronized void end(String name, MemoryInfo memoryInfo) {
final long eventId = name.hashCode();
TraceEvent.finishAsync(name, eventId);
if (sEnabled && matchesFilter(name)) {
if (sTrackTiming) {
savePerfString(name, eventId, EventType.FINISH, false);
}
long timestampUs = (System.nanoTime() - sBeginNanoTime) / 1000;
savePerfString(makeMemoryTraceNameFromTimingName(name), eventId, EventType.FINISH,
timestampUs, memoryInfo);
}
}
private static boolean matchesFilter(String name) {
return sFilter != null ? sFilter.contains(name) : false;
}
private static void savePerfString(String name, long id, EventType type,
boolean includeMemory) {
long timestampUs = (System.nanoTime() - sBeginNanoTime) / 1000;
MemoryInfo memInfo = null;
if (includeMemory) {
memInfo = new MemoryInfo();
Debug.getMemoryInfo(memInfo);
}
savePerfString(name, id, type, timestampUs, memInfo);
}
private static void savePerfString(String name, long id, EventType type, long timestampUs,
MemoryInfo memoryInfo) {
try {
JSONObject traceObj = new JSONObject();
traceObj.put("cat", "Java");
traceObj.put("ts", timestampUs);
traceObj.put("ph", type);
traceObj.put("name", name);
traceObj.put("id", id);
if (memoryInfo != null) {
int pss = memoryInfo.nativePss + memoryInfo.dalvikPss + memoryInfo.otherPss;
traceObj.put("mem", pss);
}
sPerfTraceStrings.put(traceObj);
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
public static String makeMemoryTraceNameFromTimingName(String name) {
return makeSafeTraceName(name, MEMORY_TRACE_NAME_SUFFIX);
}
public static String makeSafeTraceName(String baseName, String suffix) {
int suffixLength = suffix.length();
if (baseName.length() + suffixLength > MAX_NAME_LENGTH) {
baseName = baseName.substring(0, MAX_NAME_LENGTH - suffixLength);
}
return baseName + suffix;
}
public static synchronized void setOutputFile(File file) {
sOutputFile = file;
}
private static void dumpPerf() {
String json = sPerfTraceStrings.toString();
if (sOutputFile == null) {
System.out.println(json);
} else {
try {
PrintStream stream = new PrintStream(new FileOutputStream(sOutputFile, true));
try {
stream.print(json);
} finally {
try {
stream.close();
} catch (Exception ex) {
Log.e("PerfTraceEvent", "Unable to close perf trace output file.");
}
}
} catch (FileNotFoundException ex) {
Log.e("PerfTraceEvent", "Unable to dump perf trace data to output file.");
}
}
}
}