| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.gameperformance; |
| 17 |
|
| 18 |
import java.io.BufferedReader; |
| 19 |
import java.io.FileReader; |
| 20 |
import java.io.IOException; |
| 21 |
import java.util.ArrayList; |
| 22 |
import java.util.Arrays; |
| 23 |
import java.util.Collections; |
| 24 |
import java.util.Comparator; |
| 25 |
import java.util.HashMap; |
| 26 |
import java.util.List; |
| 27 |
import java.util.Map; |
| 28 |
import java.util.Set; |
| 29 |
import java.util.StringTokenizer; |
| 30 |
import java.util.TreeMap; |
| 31 |
import java.util.TreeSet; |
| 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 |
public class GraphicBufferMetrics { |
| 64 |
private final static String TAG = "GraphicBufferMetrics"; |
| 65 |
|
| 66 |
private final static String KEY_POST_TIME = "post_time"; |
| 67 |
private final static String KEY_READY_TIME = "ready_time"; |
| 68 |
private final static String KEY_LATENCY = "latency"; |
| 69 |
private final static String SUFFIX_MIN = "min"; |
| 70 |
private final static String SUFFIX_MAX = "max"; |
| 71 |
private final static String SUFFIX_MEDIAN = "median"; |
| 72 |
private final static String KEY_MISSED_FRAME_RATE = "missed_frame_percents"; |
| 73 |
|
| 74 |
private final static int EVENT_POST_BUFFER = 0; |
| 75 |
private final static int EVENT_QUEUE_BUFFER = 1; |
| 76 |
private final static int EVENT_ACQUIRE_BUFFER = 2; |
| 77 |
private final static int EVENT_RELEASE_BUFFER = 3; |
| 78 |
|
| 79 |
|
| 80 |
private final static String ATRACE_HEADER = |
| 81 |
"# TASK-PID TGID CPU# |||| TIMESTAMP FUNCTION"; |
| 82 |
|
| 83 |
private final static String[] KNOWN_PHRASES = new String[] { |
| 84 |
"capturing trace... done", "TRACE:"}; |
| 85 |
private final static List<String> KNWON_PHRASES_LIST = Arrays.asList(KNOWN_PHRASES); |
| 86 |
|
| 87 |
|
| 88 |
private final static String FUNCTION_TRACING_MARK_WRITE = "tracing_mark_write"; |
| 89 |
|
| 90 |
|
| 91 |
private final static String TRACE_EVENT_CLOCK_SYNC = "trace_event_clock_sync:"; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
private final static int MINIMAL_SAMPLE_CNT_TO_PASS = 50; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
private static class RawEvent { |
| 101 |
|
| 102 |
public final RawEvent mParent; |
| 103 |
|
| 104 |
public final long mTime; |
| 105 |
|
| 106 |
public long mDuration; |
| 107 |
|
| 108 |
public final String mName; |
| 109 |
|
| 110 |
public final List<RawEvent> mChildren; |
| 111 |
|
| 112 |
public RawEvent(RawEvent parent, long time, String name) { |
| 113 |
mParent = parent; |
| 114 |
mTime = time; |
| 115 |
mName = name; |
| 116 |
mDuration = -1; |
| 117 |
mChildren = new ArrayList<>(); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public void findEvents(String path, List<RawEvent> collector) { |
| 129 |
final int separator = path.indexOf('/'); |
| 130 |
final String current = separator > 0 ? path.substring(0, separator) : path; |
| 131 |
final String nextPath = separator > 0 ? path.substring(separator + 1) : null; |
| 132 |
for (RawEvent child : mChildren) { |
| 133 |
if (child.mName.equals(current)) { |
| 134 |
if (nextPath != null) { |
| 135 |
child.findEvents(nextPath, collector); |
| 136 |
} else { |
| 137 |
collector.add(child); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
public void dump(String prefix) { |
| 144 |
System.err.print(prefix); |
| 145 |
System.err.println(mTime + "[" + mDuration + "] " + mName); |
| 146 |
for (RawEvent e : mChildren) { |
| 147 |
e.dump(prefix + " "); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
private static class BufferEvent { |
| 156 |
public final int mType; |
| 157 |
public final long mTime; |
| 158 |
public final long mDuration; |
| 159 |
public final String mBufferId; |
| 160 |
|
| 161 |
public BufferEvent(int type, long time, long duration, String bufferId) { |
| 162 |
mType = type; |
| 163 |
mTime = time; |
| 164 |
mDuration = duration; |
| 165 |
mBufferId = bufferId; |
| 166 |
} |
| 167 |
|
| 168 |
@Override |
| 169 |
public String toString() { |
| 170 |
return "Type: " + mType + ". Time: " + mTime + |
| 171 |
"[" + mDuration + "]. Buffer: " + mBufferId + "."; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
private static boolean isDigitChar(char c) { |
| 179 |
return (c >= '0') && (c <= '9'); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
private static boolean isDoubleDigitChar(char c) { |
| 186 |
return (c == '.') || isDigitChar(c); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
private static long getTimeStamp(String timeStampStr) { |
| 194 |
return (long)(1000000.0 * Double.parseDouble(timeStampStr)); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
private static Map<Integer, RawEvent> buildEventModel(String fileName) throws IOException { |
| 203 |
Map<Integer, RawEvent> result = new HashMap<>(); |
| 204 |
|
| 205 |
BufferedReader bufferedReader = null; |
| 206 |
String line = ""; |
| 207 |
boolean headerDetected = false; |
| 208 |
try { |
| 209 |
bufferedReader = new BufferedReader(new FileReader(fileName)); |
| 210 |
while ((line = bufferedReader.readLine()) != null) { |
| 211 |
|
| 212 |
headerDetected |= line.equals(ATRACE_HEADER); |
| 213 |
|
| 214 |
if (line.startsWith("#")) { |
| 215 |
continue; |
| 216 |
} |
| 217 |
|
| 218 |
if (KNWON_PHRASES_LIST.contains(line)) { |
| 219 |
continue; |
| 220 |
} |
| 221 |
|
| 222 |
if (!headerDetected) { |
| 223 |
|
| 224 |
throw new IllegalStateException("Header was not detected"); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
if (!isDigitChar(line.charAt(17)) || line.charAt(16) != '-') { |
| 230 |
throw new IllegalStateException("Failed to parse thread id: " + line); |
| 231 |
} |
| 232 |
int rightIndex = line.indexOf(' ', 17); |
| 233 |
final String threadIdStr = line.substring(17, rightIndex); |
| 234 |
final int threadId = Integer.parseInt(threadIdStr); |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
int leftIndex = 45; |
| 239 |
while (isDoubleDigitChar(line.charAt(leftIndex))) { |
| 240 |
--leftIndex; |
| 241 |
} |
| 242 |
rightIndex = line.indexOf(':', 45); |
| 243 |
|
| 244 |
final String timeStampString = line.substring(leftIndex + 1, rightIndex); |
| 245 |
final long timeStampMcs = getTimeStamp(timeStampString); |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
leftIndex = rightIndex + 1; |
| 250 |
while (Character.isWhitespace(line.charAt(leftIndex))) { |
| 251 |
++leftIndex; |
| 252 |
} |
| 253 |
rightIndex = line.indexOf(':', leftIndex); |
| 254 |
final String function = line.substring(leftIndex, rightIndex); |
| 255 |
|
| 256 |
if (!function.equals(FUNCTION_TRACING_MARK_WRITE)) { |
| 257 |
continue; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
leftIndex = rightIndex + 1; |
| 262 |
while (Character.isWhitespace(line.charAt(leftIndex))) { |
| 263 |
++leftIndex; |
| 264 |
} |
| 265 |
|
| 266 |
final String event = line.substring(leftIndex); |
| 267 |
if (event.startsWith(TRACE_EVENT_CLOCK_SYNC)) { |
| 268 |
continue; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
StringTokenizer eventTokenizer = new StringTokenizer(event, "|"); |
| 276 |
final String eventType = eventTokenizer.nextToken(); |
| 277 |
|
| 278 |
|
| 279 |
if (!result.containsKey(threadId)) { |
| 280 |
result.put(threadId, new RawEvent(null , |
| 281 |
timeStampMcs, |
| 282 |
"#ROOT_" + threadId)); |
| 283 |
} |
| 284 |
|
| 285 |
switch (eventType) { |
| 286 |
case "B": { |
| 287 |
|
| 288 |
eventTokenizer.nextToken(); |
| 289 |
String eventText = eventTokenizer.nextToken(); |
| 290 |
while (eventTokenizer.hasMoreTokens()) { |
| 291 |
eventText += " "; |
| 292 |
eventText += eventTokenizer.nextToken(); |
| 293 |
} |
| 294 |
RawEvent parent = result.get(threadId); |
| 295 |
RawEvent current = new RawEvent(parent, timeStampMcs, eventText); |
| 296 |
parent.mChildren.add(current); |
| 297 |
result.put(threadId, current); |
| 298 |
} |
| 299 |
break; |
| 300 |
case "E": { |
| 301 |
|
| 302 |
RawEvent current = result.get(threadId); |
| 303 |
current.mDuration = timeStampMcs - current.mTime; |
| 304 |
if (current.mParent == null) { |
| 305 |
|
| 306 |
|
| 307 |
if (!current.mChildren.isEmpty()) { |
| 308 |
current.mChildren.remove(current.mChildren.size() -1); |
| 309 |
} |
| 310 |
} else { |
| 311 |
result.put(threadId, current.mParent); |
| 312 |
} |
| 313 |
} |
| 314 |
break; |
| 315 |
case "C": |
| 316 |
|
| 317 |
break; |
| 318 |
default: |
| 319 |
throw new IllegalStateException( |
| 320 |
"Unrecognized trace: " + line + " # " + eventType + " # " + event); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
Set<Integer> threadIds = new TreeSet<>(); |
| 326 |
threadIds.addAll(result.keySet()); |
| 327 |
for (int threadId : threadIds) { |
| 328 |
RawEvent root = result.get(threadId); |
| 329 |
if (root.mParent == null) { |
| 330 |
|
| 331 |
continue; |
| 332 |
} |
| 333 |
|
| 334 |
while (root.mParent != null) { |
| 335 |
root = root.mParent; |
| 336 |
} |
| 337 |
|
| 338 |
root.mChildren.remove(root.mChildren.size() - 1); |
| 339 |
result.put(threadId, root); |
| 340 |
} |
| 341 |
} catch (Exception e) { |
| 342 |
throw new IOException("Failed to process " + line, e); |
| 343 |
} finally { |
| 344 |
Utils.closeQuietly(bufferedReader); |
| 345 |
} |
| 346 |
|
| 347 |
return result; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public static Map<String, Double> processGraphicBufferResult( |
| 356 |
String fileName, String testTag) throws IOException { |
| 357 |
final Map<Integer, RawEvent> model = buildEventModel(fileName); |
| 358 |
|
| 359 |
List<RawEvent> collectorPostBuffer = new ArrayList<>(); |
| 360 |
List<RawEvent> collectorQueueBuffer = new ArrayList<>(); |
| 361 |
List<RawEvent> collectorReleaseBuffer = new ArrayList<>(); |
| 362 |
List<RawEvent> collectorAcquireBuffer = new ArrayList<>(); |
| 363 |
|
| 364 |
|
| 365 |
for (RawEvent root : model.values()) { |
| 366 |
|
| 367 |
root.findEvents("localPostBuffer", collectorPostBuffer); |
| 368 |
|
| 369 |
root.findEvents("eglSwapBuffersWithDamageKHR", collectorPostBuffer); |
| 370 |
|
| 371 |
root.findEvents("queueBuffer", collectorQueueBuffer); |
| 372 |
root.findEvents("onMessageReceived/handleMessageInvalidate/latchBuffer/" + |
| 373 |
"updateTexImage/acquireBuffer", |
| 374 |
collectorAcquireBuffer); |
| 375 |
|
| 376 |
root.findEvents( |
| 377 |
"onMessageReceived/handleMessageRefresh/postComposition/releaseBuffer", |
| 378 |
collectorReleaseBuffer); |
| 379 |
|
| 380 |
root.findEvents( |
| 381 |
"onMessageReceived/handleMessageRefresh/releaseBuffer", |
| 382 |
collectorReleaseBuffer); |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
List<BufferEvent> bufferEvents = new ArrayList<>(); |
| 387 |
for (RawEvent event : collectorPostBuffer) { |
| 388 |
bufferEvents.add( |
| 389 |
new BufferEvent(EVENT_POST_BUFFER, event.mTime, event.mDuration, null)); |
| 390 |
} |
| 391 |
toBufferEvents(EVENT_QUEUE_BUFFER, collectorQueueBuffer, bufferEvents); |
| 392 |
toBufferEvents(EVENT_ACQUIRE_BUFFER, collectorAcquireBuffer, bufferEvents); |
| 393 |
toBufferEvents(EVENT_RELEASE_BUFFER, collectorReleaseBuffer, bufferEvents); |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
Collections.sort(bufferEvents, new Comparator<BufferEvent>() { |
| 398 |
@Override |
| 399 |
public int compare(BufferEvent o1, BufferEvent o2) { |
| 400 |
if (o1.mTime < o2.mTime) { |
| 401 |
return -1; |
| 402 |
} if (o1.mTime > o2.mTime) { |
| 403 |
return +1; |
| 404 |
} else { |
| 405 |
return 0; |
| 406 |
} |
| 407 |
} |
| 408 |
}); |
| 409 |
|
| 410 |
|
| 411 |
List<Long> postTimes = new ArrayList<>(); |
| 412 |
List<Long> readyTimes = new ArrayList<>(); |
| 413 |
List<Long> latencyTimes = new ArrayList<>(); |
| 414 |
long missedCnt = 0; |
| 415 |
|
| 416 |
for (int i = 0; i < bufferEvents.size(); ++i) { |
| 417 |
if (bufferEvents.get(i).mType != EVENT_POST_BUFFER) { |
| 418 |
continue; |
| 419 |
} |
| 420 |
final int queueIndex = findNextOfType(bufferEvents, i + 1, EVENT_QUEUE_BUFFER); |
| 421 |
if (queueIndex < 0) { |
| 422 |
break; |
| 423 |
} |
| 424 |
final int acquireIndex = findNextOfBufferId(bufferEvents, queueIndex + 1, |
| 425 |
bufferEvents.get(queueIndex).mBufferId); |
| 426 |
if (acquireIndex < 0) { |
| 427 |
break; |
| 428 |
} |
| 429 |
if (bufferEvents.get(acquireIndex).mType != EVENT_ACQUIRE_BUFFER) { |
| 430 |
|
| 431 |
++missedCnt; |
| 432 |
continue; |
| 433 |
} |
| 434 |
final int releaseIndex = findNextOfBufferId(bufferEvents, acquireIndex + 1, |
| 435 |
bufferEvents.get(queueIndex).mBufferId); |
| 436 |
if (releaseIndex < 0) { |
| 437 |
break; |
| 438 |
} |
| 439 |
if (bufferEvents.get(releaseIndex).mType != EVENT_RELEASE_BUFFER) { |
| 440 |
|
| 441 |
++missedCnt; |
| 442 |
continue; |
| 443 |
} |
| 444 |
|
| 445 |
postTimes.add(bufferEvents.get(i).mDuration); |
| 446 |
readyTimes.add( |
| 447 |
bufferEvents.get(acquireIndex).mTime - bufferEvents.get(i).mTime); |
| 448 |
latencyTimes.add( |
| 449 |
bufferEvents.get(releaseIndex).mTime - bufferEvents.get(i).mTime); |
| 450 |
} |
| 451 |
|
| 452 |
if (postTimes.size() < MINIMAL_SAMPLE_CNT_TO_PASS) { |
| 453 |
throw new IllegalStateException("Too few sample cnt: " + postTimes.size() +". " + |
| 454 |
MINIMAL_SAMPLE_CNT_TO_PASS + " is required."); |
| 455 |
} |
| 456 |
|
| 457 |
Map<String, Double> status = new TreeMap<>(); |
| 458 |
addResults(status, testTag, KEY_POST_TIME, postTimes); |
| 459 |
addResults(status, testTag, KEY_READY_TIME, readyTimes); |
| 460 |
addResults(status, testTag, KEY_LATENCY, latencyTimes); |
| 461 |
status.put(testTag + "_" + KEY_MISSED_FRAME_RATE, |
| 462 |
100.0 * missedCnt / (missedCnt + postTimes.size())); |
| 463 |
return status; |
| 464 |
} |
| 465 |
|
| 466 |
private static void addResults( |
| 467 |
Map<String, Double> status, String tag, String key, List<Long> times) { |
| 468 |
Collections.sort(times); |
| 469 |
long min = times.get(0); |
| 470 |
long max = times.get(0); |
| 471 |
for (long time : times) { |
| 472 |
min = Math.min(min, time); |
| 473 |
max = Math.max(max, time); |
| 474 |
} |
| 475 |
status.put(tag + "_" + key + "_" + SUFFIX_MIN, (double)min); |
| 476 |
status.put(tag + "_" + key + "_" + SUFFIX_MAX, (double)max); |
| 477 |
status.put(tag + "_" + key + "_" + SUFFIX_MEDIAN, (double)times.get(times.size() / 2)); |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
private static void toBufferEvents( |
| 482 |
int type, List<RawEvent> rawEvents, List<BufferEvent> bufferEvents) { |
| 483 |
for (RawEvent event : rawEvents) { |
| 484 |
if (event.mChildren.isEmpty()) { |
| 485 |
throw new IllegalStateException("Buffer name is expected"); |
| 486 |
} |
| 487 |
final String bufferName = event.mChildren.get(0).mName; |
| 488 |
if (bufferName.startsWith("SurfaceView - android.gameperformance")) { |
| 489 |
bufferEvents.add( |
| 490 |
new BufferEvent(type, event.mTime, event.mDuration, bufferName)); |
| 491 |
} |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
private static int findNextOfType(List<BufferEvent> events, int startIndex, int type) { |
| 496 |
for (int i = startIndex; i < events.size(); ++i) { |
| 497 |
if (events.get(i).mType == type) { |
| 498 |
return i; |
| 499 |
} |
| 500 |
} |
| 501 |
return -1; |
| 502 |
} |
| 503 |
|
| 504 |
private static int findNextOfBufferId( |
| 505 |
List<BufferEvent> events, int startIndex, String bufferId) { |
| 506 |
for (int i = startIndex; i < events.size(); ++i) { |
| 507 |
if (bufferId.equals(events.get(i).mBufferId)) { |
| 508 |
return i; |
| 509 |
} |
| 510 |
} |
| 511 |
return -1; |
| 512 |
} |
| 513 |
|
| 514 |
public static void main(String[] args) { |
| 515 |
if (args.length != 1) { |
| 516 |
System.err.println("Usage: " + TAG + " atrace.log"); |
| 517 |
return; |
| 518 |
} |
| 519 |
|
| 520 |
try { |
| 521 |
System.out.println("Results:"); |
| 522 |
for (Map.Entry<?, ?> entry : |
| 523 |
processGraphicBufferResult(args[0], "default").entrySet()) { |
| 524 |
System.out.println(" " + entry.getKey() + " = " + entry.getValue()); |
| 525 |
} |
| 526 |
} catch (IOException e) { |
| 527 |
e.printStackTrace(); |
| 528 |
} |
| 529 |
} |
| 530 |
} |