1 /* 2 * Copyright (C) 2018 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 package com.android.tradefed.targetprep; 17 18 import com.android.tradefed.build.IBuildInfo; 19 import com.android.tradefed.config.Option; 20 import com.android.tradefed.config.OptionClass; 21 import com.android.tradefed.device.DeviceNotAvailableException; 22 import com.android.tradefed.device.ITestDevice; 23 import com.android.tradefed.log.LogUtil.CLog; 24 25 import java.io.File; 26 27 /** 28 * Perfetto preparer pushes the config file in to the device at a standard location to which 29 * perfetto have access to. 30 */ 31 @OptionClass(alias = "perfetto-preparer") 32 public class PerfettoPreparer extends BaseTargetPreparer { 33 // Perfetto have access only to the config files under /data/misc/perfetto-traces/ 34 // in the device. 35 private static final String DEVICE_CONFIG_PATH = "/data/misc/perfetto-traces/trace_config.pb"; 36 37 @Option( 38 name = "abort-on-failure", 39 description = 40 "If false, continue if preparer fail. If true, abort the invocation on any failure.") 41 private boolean mAbortOnFailure = true; 42 43 @Option( 44 name = "binary-perfetto-config", 45 description = "Full path to the binary version of perfetto config file.") 46 private File mBinaryPerfettoConfigFile = null; 47 48 49 @Override setUp(ITestDevice device, IBuildInfo buildInfo)50 public void setUp(ITestDevice device, IBuildInfo buildInfo) 51 throws TargetSetupError, BuildError, DeviceNotAvailableException { 52 53 // If the encoded version is passed, push it to the device directly 54 // under /data/misc/perfetto-traces/ and use it. 55 if (mBinaryPerfettoConfigFile != null) { 56 if (!mBinaryPerfettoConfigFile.exists()) { 57 fail("Failed to find the local binary perfetto file", null, device); 58 } else if (!device.pushFile(mBinaryPerfettoConfigFile, DEVICE_CONFIG_PATH)) { 59 fail("Failed to push the binary perfetto file", null, device); 60 } 61 } 62 } 63 64 /** 65 * Helper method to only throw if mAbortOnFailure is enabled. Callers should behave as if this 66 * method may return. 67 */ fail(String message, Throwable cause, ITestDevice device)68 private void fail(String message, Throwable cause, ITestDevice device) throws TargetSetupError { 69 if (mAbortOnFailure) { 70 if (cause != null) { 71 throw new TargetSetupError(message, cause, device.getDeviceDescriptor()); 72 } 73 else { 74 throw new TargetSetupError(message, device.getDeviceDescriptor()); 75 } 76 } else { 77 // Log the error and return 78 CLog.w(message); 79 } 80 } 81 82 @Override tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e)83 public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e) 84 throws DeviceNotAvailableException { 85 device.executeShellCommand("rm " + DEVICE_CONFIG_PATH); 86 } 87 } 88