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 android.gameperformance;
17 
18 import java.io.BufferedReader;
19 import java.io.FileWriter;
20 import java.io.IOException;
21 import java.io.InputStreamReader;
22 
23 import android.app.Instrumentation;
24 import android.os.AsyncTask;
25 import android.os.ParcelFileDescriptor;
26 import android.util.Log;
27 
28 /**
29  * Helper that runs atrace command for required duration and category. Results are read from
30  * the output of atrace and serialized to the provided file. We cannot use direct atrace to
31  * file because atrace is executed in UI automator context and analysis is done in test context.
32  * In last case output file is not accessible from the both contexts.
33  */
34 public class ATraceRunner extends AsyncTask<Void, Integer, Boolean>{
35     private final static String TAG = "ATraceRunner";
36 
37     // Report that atrace is done.
38     public interface Delegate {
onProcessed(boolean success)39         public void onProcessed(boolean success);
40     }
41 
42     private final Instrumentation mInstrumentation;
43     private final String mOutput;
44     private final int mTimeInSeconds;
45     private final String mCategory;
46     private final Delegate mDelegate;
47 
ATraceRunner(Instrumentation instrumentation, String output, int timeInSeconds, String category, Delegate delegate)48     public ATraceRunner(Instrumentation instrumentation,
49                         String output,
50                         int timeInSeconds,
51                         String category,
52                         Delegate delegate) {
53         mInstrumentation = instrumentation;
54         mOutput = output;
55         mTimeInSeconds = timeInSeconds;
56         mCategory = category;
57         mDelegate = delegate;
58     }
59 
60     @Override
doInBackground(Void... params)61     protected Boolean doInBackground(Void... params) {
62         BufferedReader bufferedReader = null;
63         FileWriter writer = null;
64         try {
65             // Run the command.
66             final String cmd = "atrace -t " + mTimeInSeconds + " " + mCategory;
67             Log.i(TAG, "Running atrace... " + cmd);
68             writer = new FileWriter(mOutput);
69             final ParcelFileDescriptor fd =
70                     mInstrumentation.getUiAutomation().executeShellCommand(cmd);
71             bufferedReader = new BufferedReader(
72                     new InputStreamReader(new ParcelFileDescriptor.AutoCloseInputStream(fd)));
73             String line;
74             while ((line = bufferedReader.readLine()) != null) {
75                 writer.write(line);
76                 writer.write("\n");
77             }
78             Log.i(TAG, "Running atrace... DONE");
79             return true;
80         } catch (IOException e) {
81             Log.i(TAG, "atrace failed", e);
82             return false;
83         } finally {
84             Utils.closeQuietly(bufferedReader);
85             Utils.closeQuietly(writer);
86         }
87     }
88 
89     @Override
onPostExecute(Boolean result)90     protected void onPostExecute(Boolean result) {
91         mDelegate.onProcessed(result);
92     }
93 
94 }
95