Home | History | Annotate | Download | only in metric
      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 /**
     29  * A {@link ScheduledDeviceMetricCollector} to collect audio and system memory heaps at regular
     30  * intervals.
     31  */
     32 public class IonHeapInfoMetricCollector extends ScheduledDeviceMetricCollector {
     33     public IonHeapInfoMetricCollector() {
     34         setTag("ion");
     35     }
     36 
     37     @Override
     38     void collect(ITestDevice device, DeviceMetricData runData) throws InterruptedException {
     39         collectIonAudio(device);
     40         collectIonSystem(device);
     41     }
     42 
     43     private void collectIonAudio(ITestDevice device) {
     44         try {
     45             CLog.i("Running ionheap audio collector...");
     46             String outputFileName =
     47                     String.format("%s/ion-audio-%s", createTempDir(), getFileSuffix());
     48             File outputFile = saveProcessOutput(device, "cat /d/ion/heaps/audio", outputFileName);
     49             try (InputStreamSource source = new FileInputStreamSource(outputFile, true)) {
     50                 getInvocationListener()
     51                         .testLog(
     52                                 Files.getNameWithoutExtension(outputFile.getName()),
     53                                 LogDataType.TEXT,
     54                                 source);
     55             }
     56         } catch (DeviceNotAvailableException | IOException e) {
     57             CLog.e(e);
     58         }
     59     }
     60 
     61     private void collectIonSystem(ITestDevice device) {
     62         try {
     63             CLog.i("Running ionheap system collector...");
     64             String outputFileName =
     65                     String.format("%s/ion-system-%s", createTempDir(), getFileSuffix());
     66             File outputFile = saveProcessOutput(device, "cat /d/ion/heaps/system", outputFileName);
     67             try (InputStreamSource source = new FileInputStreamSource(outputFile, true)) {
     68                 getInvocationListener()
     69                         .testLog(
     70                                 Files.getNameWithoutExtension(outputFile.getName()),
     71                                 LogDataType.TEXT,
     72                                 source);
     73             }
     74         } catch (DeviceNotAvailableException | IOException e) {
     75             CLog.e(e);
     76         }
     77     }
     78 }
     79