-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCall.java
228 lines (223 loc) · 8.37 KB
/
Call.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package com.mmt.travel.app.NFR.Latency;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.primitives.UnsignedInts;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
import java.util.concurrent.TimeUnit;
/**
* Created by mmt6054 on 09/10/18.
*/
public class Call {
private final long mMethodId;
/**
* Note: The thread entry and exit times are stored as unsigned integers in the trace data.
* In this model, they are stored as integers, but the getters for all of these time values
* convert them into longs.
*/
private final int mEntryGlobalTime;
private final int mExitGlobalTime;
private final int mEntryThreadTime;
private final int mExitThreadTime;
private final long mInclusiveThreadTimeInCallees;
private final long mInclusiveGlobalTimeInCallees;
private final int mDepth;
/**
* Indicates whether the current call is recursive. A call is recursive if the same method
* is present in its backstack.
*/
private final boolean mIsRecursive;
private final List<Call> mCallees;
private Call(@NonNull Builder builder, @NonNull Stack<Long> backStack) {
mMethodId = builder.mMethodId;
mEntryThreadTime = builder.mEntryThreadTime;
mEntryGlobalTime = builder.mEntryGlobalTime;
mExitThreadTime = builder.mExitThreadTime;
mExitGlobalTime = builder.mExitGlobalTime;
mDepth = backStack.size();
mIsRecursive = backStack.contains(mMethodId);
if (builder.mCallees == null) {
mCallees = Collections.emptyList();
} else {
backStack.push(mMethodId);
List<Call> callees = new ArrayList<Call>(builder.mCallees.size());
for (Builder b : builder.mCallees) {
callees.add(b.build(backStack));
}
backStack.pop();
mCallees = new ImmutableList.Builder<Call>().addAll(callees).build();
}
mInclusiveThreadTimeInCallees = sumInclusiveTimes(mCallees, ClockType.THREAD);
mInclusiveGlobalTimeInCallees = sumInclusiveTimes(mCallees, ClockType.GLOBAL);
}
private long sumInclusiveTimes(@NonNull List<Call> callees, ClockType clockType) {
long sum = 0;
for (Call c : callees) {
sum += c.getInclusiveTime(clockType, TimeUnit.MICROSECONDS);
}
return sum;
}
public long getMethodId() {
return mMethodId;
}
@NonNull
public List<Call> getCallees() {
return mCallees;
}
public int getDepth() {
return mDepth;
}
/**
* Returns true if the call is recursive (another call of the same method id is present
* in its backstack)
*/
public boolean isRecursive() {
return mIsRecursive;
}
public long getEntryTime(ClockType clockType, TimeUnit units) {
long entryTime = clockType == ClockType.THREAD ?
UnsignedInts.toLong(mEntryThreadTime) : UnsignedInts.toLong(mEntryGlobalTime);
return units.convert(entryTime, VmTraceData.getDefaultTimeUnits());
}
public long getExitTime(ClockType clockType, TimeUnit units) {
long exitTime = clockType == ClockType.THREAD ?
UnsignedInts.toLong(mExitThreadTime) : UnsignedInts.toLong(mExitGlobalTime);
return units.convert(exitTime, VmTraceData.getDefaultTimeUnits());
}
public long getInclusiveTime(ClockType clockType, TimeUnit units) {
long inclusiveTime = clockType == ClockType.THREAD ?
UnsignedInts.toLong(mExitThreadTime - mEntryThreadTime) :
UnsignedInts.toLong(mExitGlobalTime - mEntryGlobalTime);
return units.convert(inclusiveTime, VmTraceData.getDefaultTimeUnits());
}
public long getExclusiveTime(ClockType clockType, TimeUnit units) {
long inclusiveTimeInCallees = clockType == ClockType.THREAD ?
mInclusiveThreadTimeInCallees : mInclusiveGlobalTimeInCallees;
long exclusiveTime = getInclusiveTime(clockType, VmTraceData.getDefaultTimeUnits()) -
inclusiveTimeInCallees;
return units.convert(exclusiveTime, VmTraceData.getDefaultTimeUnits());
}
public static class Builder {
private final long mMethodId;
private int mEntryThreadTime;
private int mEntryGlobalTime;
private int mExitGlobalTime;
private int mExitThreadTime;
private List<Builder> mCallees = null;
public Builder(long methodId) {
mMethodId = methodId;
}
public long getMethodId() {
return mMethodId;
}
public void setMethodEntryTime(int threadTime, int globalTime) {
mEntryThreadTime = threadTime;
mEntryGlobalTime = globalTime;
}
public void setMethodExitTime(int threadTime, int globalTime) {
mExitThreadTime = threadTime;
mExitGlobalTime = globalTime;
}
public void addCallee(Builder c) {
if (mCallees == null) {
mCallees = new ArrayList<Builder>();
}
mCallees.add(c);
}
@Nullable
public List<Builder> getCallees() {
return mCallees;
}
public int getMethodEntryThreadTime() {
return mEntryThreadTime;
}
public int getMethodEntryGlobalTime() {
return mEntryGlobalTime;
}
public int getMethodExitThreadTime() {
return mExitThreadTime;
}
public int getMethodExitGlobalTime() {
return mExitGlobalTime;
}
@NonNull
public Call build(@NonNull Stack<Long> backStack) {
return new Call(this, backStack);
}
}
/**
* Formats this call and all its call hierarchy using the given {@link com.android.tools.perflib.vmtrace.Call.Formatter} to
* print the details for each method.
*/
public String format(Formatter f) {
StringBuilder sb = new StringBuilder(100);
printCallHierarchy(sb, f);
return sb.toString();
}
public interface Formatter {
String format(Call c);
}
private static final Formatter METHOD_ID_FORMATTER = new Formatter() {
@Override
public String format(Call c) {
return Long.toString(c.getMethodId());
}
};
@Override
public String toString() {
return format(METHOD_ID_FORMATTER);
}
private void printCallHierarchy(@NonNull StringBuilder sb, Formatter formatter) {
sb.append(" -> ");
sb.append(formatter.format(this));
List<Call> callees = getCallees();
int lineStart = sb.lastIndexOf("\n");
int depth = sb.length() - (lineStart + 1);
for (int i = 0; i < callees.size(); i++) {
if (i != 0) {
sb.append("\n");
sb.append(Strings.repeat(" ", depth));
}
Call callee = callees.get(i);
callee.printCallHierarchy(sb, formatter);
}
}
@NonNull
public Iterator<Call> getCallHierarchyIterator() {
return new CallHierarchyIterator(this);
}
/**
* An iterator for a call hierarchy. The iteration order matches the order in which the calls
* were invoked.
*/
private static class CallHierarchyIterator implements Iterator<Call> {
private final Stack<Call> mCallStack = new Stack<Call>();
public CallHierarchyIterator(@NonNull Call top) {
mCallStack.push(top);
}
@Override
public boolean hasNext() {
return !mCallStack.isEmpty();
}
@Override
public Call next() {
if (mCallStack.isEmpty()) {
return null;
}
Call top = mCallStack.pop();
for (int i = top.getCallees().size() - 1; i >= 0; i--) {
mCallStack.push(top.getCallees().get(i));
}
return top;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
}