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.traceur; 18 19 import android.os.Build; 20 import android.util.Log; 21 22 import java.io.File; 23 import java.io.IOException; 24 import java.nio.file.Files; 25 import java.nio.file.Path; 26 import java.nio.file.Paths; 27 import java.text.SimpleDateFormat; 28 import java.util.Arrays; 29 import java.util.Date; 30 import java.util.Locale; 31 import java.util.Collection; 32 import java.util.TreeMap; 33 34 /** 35 * Utility functions for tracing. 36 * Will call atrace or perfetto depending on the setting. 37 */ 38 public class TraceUtils { 39 40 static final String TAG = "Traceur"; 41 42 public static final String TRACE_DIRECTORY = "/data/local/traces/"; 43 44 // To change Traceur to use atrace to collect traces, 45 // change mTraceEngine to point to AtraceUtils(). 46 private static TraceEngine mTraceEngine = new PerfettoUtils(); 47 48 private static final Runtime RUNTIME = Runtime.getRuntime(); 49 50 public interface TraceEngine { getName()51 public String getName(); getOutputExtension()52 public String getOutputExtension(); traceStart(Collection<String> tags, int bufferSizeKb, boolean apps, boolean longTrace, int maxLongTraceSizeMb, int maxLongTraceDurationMinutes)53 public boolean traceStart(Collection<String> tags, int bufferSizeKb, boolean apps, 54 boolean longTrace, int maxLongTraceSizeMb, int maxLongTraceDurationMinutes); traceStop()55 public void traceStop(); traceDump(File outFile)56 public boolean traceDump(File outFile); isTracingOn()57 public boolean isTracingOn(); 58 } 59 currentTraceEngine()60 public static String currentTraceEngine() { 61 return mTraceEngine.getName(); 62 } 63 traceStart(Collection<String> tags, int bufferSizeKb, boolean apps, boolean longTrace, int maxLongTraceSizeMb, int maxLongTraceDurationMinutes)64 public static boolean traceStart(Collection<String> tags, int bufferSizeKb, boolean apps, 65 boolean longTrace, int maxLongTraceSizeMb, int maxLongTraceDurationMinutes) { 66 return mTraceEngine.traceStart(tags, bufferSizeKb, apps, 67 longTrace, maxLongTraceSizeMb, maxLongTraceDurationMinutes); 68 } 69 traceStop()70 public static void traceStop() { 71 mTraceEngine.traceStop(); 72 } 73 traceDump(File outFile)74 public static boolean traceDump(File outFile) { 75 return mTraceEngine.traceDump(outFile); 76 } 77 isTracingOn()78 public static boolean isTracingOn() { 79 return mTraceEngine.isTracingOn(); 80 } 81 listCategories()82 public static TreeMap<String, String> listCategories() { 83 return AtraceUtils.atraceListCategories(); 84 } 85 clearSavedTraces()86 public static void clearSavedTraces() { 87 String cmd = "rm -f " + TRACE_DIRECTORY + "trace-*.*trace"; 88 89 Log.v(TAG, "Clearing trace directory: " + cmd); 90 try { 91 Process rm = exec(cmd); 92 93 if (rm.waitFor() != 0) { 94 Log.e(TAG, "clearSavedTraces failed with: " + rm.exitValue()); 95 } 96 } catch (Exception e) { 97 throw new RuntimeException(e); 98 } 99 } 100 exec(String cmd)101 public static Process exec(String cmd) throws IOException { 102 return exec(cmd, null); 103 } 104 exec(String cmd, String tmpdir)105 public static Process exec(String cmd, String tmpdir) throws IOException { 106 String[] cmdarray = {"sh", "-c", cmd}; 107 String[] envp = {"TMPDIR=" + tmpdir}; 108 envp = tmpdir == null ? null : envp; 109 110 Log.v(TAG, "exec: " + Arrays.toString(envp) + " " + Arrays.toString(cmdarray)); 111 112 return RUNTIME.exec(cmdarray, envp); 113 } 114 getOutputFilename()115 public static String getOutputFilename() { 116 String format = "yyyy-MM-dd-HH-mm-ss"; 117 String now = new SimpleDateFormat(format, Locale.US).format(new Date()); 118 return String.format("trace-%s-%s-%s.%s", Build.BOARD, Build.ID, now, 119 mTraceEngine.getOutputExtension()); 120 } 121 getOutputFile(String filename)122 public static File getOutputFile(String filename) { 123 return new File(TraceUtils.TRACE_DIRECTORY, filename); 124 } 125 126 } 127