• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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.ide.eclipse.ddms.systrace;
18 
19 import com.android.ddmlib.IDevice;
20 import com.android.ddmlib.IShellOutputReceiver;
21 import com.google.common.primitives.Bytes;
22 
23 public class SystraceTask implements Runnable {
24     private final IDevice mDevice;
25     private final String mOptions;
26 
27     private volatile boolean mCancel;
28 
29     private final Object mLock = new Object();
30     private String errorMessage;
31     private boolean mTraceComplete;
32     private byte[] mBuffer = new byte[1024];
33     private int mDataLength = 0;
34 
SystraceTask(IDevice device, String options)35     public SystraceTask(IDevice device, String options) {
36         mDevice = device;
37         mOptions = options;
38     }
39 
40     @Override
run()41     public void run() {
42         try {
43             mDevice.executeShellCommand("atrace " + mOptions, new Receiver(), 0);
44         } catch (Exception e) {
45             synchronized (mLock) {
46                 errorMessage = "Unexpected error while running atrace on device: " + e;
47             }
48         }
49     }
50 
cancel()51     public void cancel() {
52         mCancel = true;
53     }
54 
getError()55     public String getError() {
56         synchronized (mLock) {
57             return errorMessage;
58         }
59     }
60 
getAtraceOutput()61     public byte[] getAtraceOutput() {
62         synchronized (mLock) {
63             return mTraceComplete ? mBuffer : null;
64         }
65     }
66 
67     private class Receiver implements IShellOutputReceiver {
68         @Override
addOutput(byte[] data, int offset, int length)69         public void addOutput(byte[] data, int offset, int length) {
70             synchronized (mLock) {
71                 if (mDataLength + length > mBuffer.length) {
72                     mBuffer = Bytes.ensureCapacity(mBuffer, mDataLength + length + 1, 1024);
73                 }
74 
75                 for (int i = 0; i < length; i++) {
76                     mBuffer[mDataLength + i] = data[offset + i];
77                 }
78                 mDataLength += length;
79             }
80         }
81 
82         @Override
flush()83         public void flush() {
84             synchronized (mLock) {
85                 // trim mBuffer to its final size
86                 byte[] copy = new byte[mDataLength];
87                 for (int i = 0; i < mDataLength; i++) {
88                     copy[i] = mBuffer[i];
89                 }
90                 mBuffer = copy;
91 
92                 mTraceComplete = true;
93             }
94         }
95 
96         @Override
isCancelled()97         public boolean isCancelled() {
98             return mCancel;
99         }
100     }
101 }
102