1 /* 2 * Copyright (C) 2019 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.simpleperf; 18 19 import android.system.Os; 20 21 import android.support.annotation.NonNull; 22 import android.support.annotation.Nullable; 23 import android.support.annotation.RequiresApi; 24 25 import java.time.LocalDateTime; 26 import java.time.format.DateTimeFormatter; 27 import java.util.ArrayList; 28 import java.util.List; 29 30 /** 31 * <p> 32 * This class sets record options used by ProfileSession. The options are 33 * converted to a string list in toRecordArgs(), which is then passed to 34 * `simpleperf record` cmd. Run `simpleperf record -h` or 35 * `run_simpleperf_on_device.py record -h` for help messages. 36 * </p> 37 * 38 * <p> 39 * Example: 40 * RecordOptions options = new RecordOptions(); 41 * options.setDuration(3).recordDwarfCallGraph().setOutputFilename("perf.data"); 42 * ProfileSession session = new ProfileSession(); 43 * session.startRecording(options); 44 * </p> 45 */ 46 @RequiresApi(28) 47 public class RecordOptions { 48 49 /** 50 * Set output filename. Default is perf-<month>-<day>-<hour>-<minute>-<second>.data. 51 * The file will be generated under simpleperf_data/. 52 */ 53 @NonNull setOutputFilename(@onNull String filename)54 public RecordOptions setOutputFilename(@NonNull String filename) { 55 mOutputFilename = filename; 56 return this; 57 } 58 59 /** 60 * Set event to record. Default is cpu-cycles. See `simpleperf list` for all available events. 61 */ 62 @NonNull setEvent(@onNull String event)63 public RecordOptions setEvent(@NonNull String event) { 64 mEvent = event; 65 return this; 66 } 67 68 /** 69 * Set how many samples to generate each second running. Default is 4000. 70 */ 71 @NonNull setSampleFrequency(int freq)72 public RecordOptions setSampleFrequency(int freq) { 73 mFreq = freq; 74 return this; 75 } 76 77 /** 78 * Set record duration. The record stops after `durationInSecond` seconds. By default, 79 * record stops only when stopRecording() is called. 80 */ 81 @NonNull setDuration(double durationInSecond)82 public RecordOptions setDuration(double durationInSecond) { 83 mDurationInSeconds = durationInSecond; 84 return this; 85 } 86 87 /** 88 * Record some threads in the app process. By default, record all threads in the process. 89 */ 90 @NonNull setSampleThreads(@onNull List<Integer> threads)91 public RecordOptions setSampleThreads(@NonNull List<Integer> threads) { 92 mThreads.addAll(threads); 93 return this; 94 } 95 96 /** 97 * Record dwarf based call graph. It is needed to get Java callstacks. 98 */ 99 @NonNull recordDwarfCallGraph()100 public RecordOptions recordDwarfCallGraph() { 101 mDwarfCallGraph = true; 102 mFpCallGraph = false; 103 return this; 104 } 105 106 /** 107 * Record frame pointer based call graph. It is suitable to get C++ callstacks on 64bit devices. 108 */ 109 @NonNull recordFramePointerCallGraph()110 public RecordOptions recordFramePointerCallGraph() { 111 mFpCallGraph = true; 112 mDwarfCallGraph = false; 113 return this; 114 } 115 116 /** 117 * Trace context switch info to show where threads spend time off cpu. 118 */ 119 @NonNull traceOffCpu()120 public RecordOptions traceOffCpu() { 121 mTraceOffCpu = true; 122 return this; 123 } 124 125 /** 126 * Translate record options into arguments for `simpleperf record` cmd. 127 */ 128 @NonNull toRecordArgs()129 public List<String> toRecordArgs() { 130 ArrayList<String> args = new ArrayList<>(); 131 132 String filename = mOutputFilename; 133 if (filename == null) { 134 filename = getDefaultOutputFilename(); 135 } 136 args.add("-o"); 137 args.add(filename); 138 args.add("-e"); 139 args.add(mEvent); 140 args.add("-f"); 141 args.add(String.valueOf(mFreq)); 142 if (mDurationInSeconds != 0.0) { 143 args.add("--duration"); 144 args.add(String.valueOf(mDurationInSeconds)); 145 } 146 if (mThreads.isEmpty()) { 147 args.add("-p"); 148 args.add(String.valueOf(Os.getpid())); 149 } else { 150 String s = ""; 151 for (int i = 0; i < mThreads.size(); i++) { 152 if (i > 0) { 153 s += ","; 154 } 155 s += mThreads.get(i).toString(); 156 } 157 args.add("-t"); 158 args.add(s); 159 } 160 if (mDwarfCallGraph) { 161 args.add("-g"); 162 } else if (mFpCallGraph) { 163 args.add("--call-graph"); 164 args.add("fp"); 165 } 166 if (mTraceOffCpu) { 167 args.add("--trace-offcpu"); 168 } 169 return args; 170 } 171 getDefaultOutputFilename()172 private String getDefaultOutputFilename() { 173 LocalDateTime time = LocalDateTime.now(); 174 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("'perf'-MM-dd-HH-mm-ss'.data'"); 175 return time.format(formatter); 176 } 177 178 @Nullable 179 private String mOutputFilename; 180 181 @NonNull 182 private String mEvent = "cpu-cycles"; 183 184 private int mFreq = 4000; 185 186 private double mDurationInSeconds = 0.0; 187 188 @NonNull 189 private ArrayList<Integer> mThreads = new ArrayList<>(); 190 191 private boolean mDwarfCallGraph = false; 192 193 private boolean mFpCallGraph = false; 194 195 private boolean mTraceOffCpu = false; 196 } 197