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.device.metric; 17 18 import com.android.tradefed.device.DeviceNotAvailableException; 19 import com.android.tradefed.device.ITestDevice; 20 import com.android.tradefed.log.LogUtil.CLog; 21 import com.android.tradefed.result.FileInputStreamSource; 22 import com.android.tradefed.result.InputStreamSource; 23 import com.android.tradefed.result.LogDataType; 24 import com.google.common.io.Files; 25 import java.io.File; 26 import java.io.IOException; 27 28 /** A {@link ScheduledDeviceMetricCollector} to collect kernel debug trace at regular intervals. */ 29 public class TraceMetricCollector extends ScheduledDeviceMetricCollector { 30 TraceMetricCollector() { 31 setTag("trace"); 32 } 33 34 @Override 35 void collect(ITestDevice device, DeviceMetricData runData) throws InterruptedException { 36 try { 37 CLog.i("Running trace collector..."); 38 String outputFileName = String.format("%s/trace-%s", createTempDir(), getFileSuffix()); 39 File outputFile = 40 saveProcessOutput( 41 device, "cat /sys/kernel/debug/tracing/trace", outputFileName); 42 try (InputStreamSource source = new FileInputStreamSource(outputFile, true)) { 43 getInvocationListener() 44 .testLog( 45 Files.getNameWithoutExtension(outputFile.getName()), 46 LogDataType.TEXT, 47 source); 48 } 49 } catch (DeviceNotAvailableException | IOException e) { 50 CLog.e(e); 51 } 52 } 53 } 54