| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.annotations.VisibleForTesting; |
| 20 |
import com.google.common.base.Joiner; |
| 21 |
import com.google.common.base.Preconditions; |
| 22 |
|
| 23 |
import java.util.ArrayList; |
| 24 |
import java.util.HashMap; |
| 25 |
import java.util.HashSet; |
| 26 |
import java.util.LinkedList; |
| 27 |
import java.util.List; |
| 28 |
import java.util.Map; |
| 29 |
import java.util.concurrent.ConcurrentHashMap; |
| 30 |
import java.util.concurrent.ConcurrentMap; |
| 31 |
import java.util.concurrent.CopyOnWriteArrayList; |
| 32 |
import java.util.logging.Level; |
| 33 |
import java.util.logging.Logger; |
| 34 |
|
| 35 |
import javax.annotation.Nullable; |
| 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 |
final class Tracer { |
| 162 |
|
| 163 |
static final Logger logger = |
| 164 |
Logger.getLogger(Tracer.class.getName()); |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
private static volatile boolean defaultPrettyPrint; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
private static List<TracingStatistic> extraTracingStatistics = |
| 177 |
new CopyOnWriteArrayList<TracingStatistic>(); |
| 178 |
|
| 179 |
|
| 180 |
private long[] extraTracingValues; |
| 181 |
|
| 182 |
|
| 183 |
private final @Nullable String type; |
| 184 |
|
| 185 |
|
| 186 |
private final String comment; |
| 187 |
|
| 188 |
|
| 189 |
private final long startTimeMs; |
| 190 |
|
| 191 |
|
| 192 |
private long stopTimeMs; |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
final Thread startThread; |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
static final int MAX_TRACE_SIZE = 1000; |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
static interface InternalClock { |
| 214 |
long currentTimeMillis(); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
static InternalClock clock = new InternalClock() { |
| 222 |
@Override |
| 223 |
public long currentTimeMillis() { |
| 224 |
return System.currentTimeMillis(); |
| 225 |
} |
| 226 |
}; |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
Tracer(@Nullable String type, @Nullable String comment) { |
| 236 |
this.type = type; |
| 237 |
this.comment = comment == null ? "" : comment; |
| 238 |
startTimeMs = clock.currentTimeMillis(); |
| 239 |
startThread = Thread.currentThread(); |
| 240 |
if (!extraTracingStatistics.isEmpty()) { |
| 241 |
int size = extraTracingStatistics.size(); |
| 242 |
extraTracingValues = new long[size]; |
| 243 |
int i = 0; |
| 244 |
for (TracingStatistic tracingStatistic : extraTracingStatistics) { |
| 245 |
extraTracingValues[i] = tracingStatistic.start(startThread); |
| 246 |
i++; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
ThreadTrace trace = getThreadTrace(); |
| 251 |
|
| 252 |
|
| 253 |
if (!trace.isInitialized()) { |
| 254 |
return; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
if (trace.events.size() >= MAX_TRACE_SIZE) { |
| 259 |
logger.log(Level.WARNING, |
| 260 |
"Giant thread trace. Too many Tracers created. " |
| 261 |
+ "Clearing to avoid memory leak.", |
| 262 |
new Throwable(trace.toString())); |
| 263 |
trace.truncateEvents(); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
if (trace.outstandingEvents.size() >= MAX_TRACE_SIZE) { |
| 268 |
logger.log(Level.WARNING, |
| 269 |
"Too many outstanding Tracers. Tracer.stop() is missing " |
| 270 |
+ "or Tracer.stop() is not wrapped in a " |
| 271 |
+ "try/finally block. " |
| 272 |
+ "Clearing to avoid memory leak.", |
| 273 |
new Throwable(trace.toString())); |
| 274 |
trace.truncateOutstandingEvents(); |
| 275 |
} |
| 276 |
|
| 277 |
trace.startEvent(this); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
Tracer(String comment) { |
| 286 |
this(null, comment); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
static Tracer shortName(Object object, String comment) { |
| 296 |
if (object == null) { |
| 297 |
return new Tracer(comment); |
| 298 |
} |
| 299 |
return new Tracer(object.getClass().getSimpleName(), comment); |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
private static String longToPaddedString(long v, int digits_column_width) { |
| 309 |
int digit_width = numDigits(v); |
| 310 |
StringBuilder sb = new StringBuilder(); |
| 311 |
appendSpaces(sb, digits_column_width - digit_width); |
| 312 |
sb.append(v); |
| 313 |
return sb.toString(); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
private static int numDigits(long n) { |
| 323 |
int i = 0; |
| 324 |
do { |
| 325 |
i++; |
| 326 |
n = n / 10; |
| 327 |
} while (n > 0); |
| 328 |
return i; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
@VisibleForTesting |
| 337 |
static void appendSpaces(StringBuilder sb, int numSpaces) { |
| 338 |
if (numSpaces > 16) { |
| 339 |
logger.warning("Tracer.appendSpaces called with large numSpaces"); |
| 340 |
|
| 341 |
numSpaces = 16; |
| 342 |
} |
| 343 |
while (numSpaces >= 5) { |
| 344 |
sb.append(" "); |
| 345 |
numSpaces -= 5; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
switch (numSpaces) { |
| 350 |
case 1: |
| 351 |
sb.append(" "); |
| 352 |
break; |
| 353 |
case 2: |
| 354 |
sb.append(" "); |
| 355 |
break; |
| 356 |
case 3: |
| 357 |
sb.append(" "); |
| 358 |
break; |
| 359 |
case 4: |
| 360 |
sb.append(" "); |
| 361 |
break; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
static int addTracingStatistic(TracingStatistic tracingStatistic) { |
| 373 |
|
| 374 |
|
| 375 |
if (tracingStatistic.enable()) { |
| 376 |
|
| 377 |
extraTracingStatistics.add(tracingStatistic); |
| 378 |
|
| 379 |
|
| 380 |
return extraTracingStatistics.lastIndexOf(tracingStatistic); |
| 381 |
} else { |
| 382 |
return -1; |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
@VisibleForTesting |
| 394 |
static void clearTracingStatisticsTestingOnly() { |
| 395 |
extraTracingStatistics.clear(); |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
long stop(int silence_threshold) { |
| 408 |
Preconditions.checkState(Thread.currentThread() == startThread); |
| 409 |
|
| 410 |
ThreadTrace trace = getThreadTrace(); |
| 411 |
|
| 412 |
if (!trace.isInitialized()) { |
| 413 |
return 0; |
| 414 |
} |
| 415 |
|
| 416 |
stopTimeMs = clock.currentTimeMillis(); |
| 417 |
if (extraTracingValues != null) { |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
for (int i = 0; i < extraTracingValues.length; i++) { |
| 422 |
long value = extraTracingStatistics.get(i).stop(startThread); |
| 423 |
extraTracingValues[i] = value - extraTracingValues[i]; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
if (!trace.isInitialized()) { |
| 429 |
return 0; |
| 430 |
} |
| 431 |
|
| 432 |
trace.endEvent(this, silence_threshold); |
| 433 |
return stopTimeMs - startTimeMs; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
long stop() { |
| 441 |
return stop(-1); |
| 442 |
} |
| 443 |
|
| 444 |
@Override public String toString() { |
| 445 |
if (type == null) { |
| 446 |
return comment; |
| 447 |
} else { |
| 448 |
return "[" + type + "] " + comment; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
static void setDefaultSilenceThreshold(int threshold) { |
| 453 |
getThreadTrace().defaultSilenceThreshold = threshold; |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
static void initCurrentThreadTrace() { |
| 462 |
ThreadTrace events = getThreadTrace(); |
| 463 |
if (!events.isEmpty()) { |
| 464 |
logger.log(Level.WARNING, |
| 465 |
"Non-empty timer log:\n" + events, |
| 466 |
new Throwable()); |
| 467 |
clearThreadTrace(); |
| 468 |
|
| 469 |
|
| 470 |
events = getThreadTrace(); |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
events.init(); |
| 475 |
} |
| 476 |
|
| 477 |
static void initCurrentThreadTrace(int default_silence_threshold) { |
| 478 |
initCurrentThreadTrace(); |
| 479 |
setDefaultSilenceThreshold(default_silence_threshold); |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
static String getCurrentThreadTraceReport() { |
| 488 |
return getThreadTrace().toString(); |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
static void logCurrentThreadTrace() { |
| 495 |
ThreadTrace trace = getThreadTrace(); |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
if (!trace.isInitialized()) { |
| 503 |
logger.log(Level.WARNING, |
| 504 |
"Tracer log requested for this thread but was not " |
| 505 |
+ "initialized using Tracer.initCurrentThreadTrace().", |
| 506 |
new Throwable()); |
| 507 |
return; |
| 508 |
} |
| 509 |
|
| 510 |
if (!trace.isEmpty()) { |
| 511 |
logger.log(Level.WARNING, "timers:\n{0}", getCurrentThreadTraceReport()); |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
static void clearCurrentThreadTrace() { |
| 519 |
clearThreadTrace(); |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
static void logAndClearCurrentThreadTrace() { |
| 526 |
logCurrentThreadTrace(); |
| 527 |
clearThreadTrace(); |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
static void setPrettyPrint(boolean enabled) { |
| 536 |
defaultPrettyPrint = enabled; |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
static final class Stat { |
| 541 |
private int count; |
| 542 |
private int silent; |
| 543 |
private int clockTime; |
| 544 |
private int[] extraInfo; |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
int getCount() { return count; } |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
int getSilentCount() { return silent; } |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
int getTotalTime() { return clockTime; } |
| 563 |
|
| 564 |
|
| 565 |
@VisibleForTesting |
| 566 |
int getExtraInfo(int index) { |
| 567 |
return index >= extraInfo.length ? 0 : extraInfo[index]; |
| 568 |
} |
| 569 |
|
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
private static @Nullable AtomicTracerStatMap typeToCountMap; |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
private static @Nullable AtomicTracerStatMap typeToSilentMap; |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
private static @Nullable AtomicTracerStatMap typeToTimeMap; |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
static synchronized void enableTypeMaps() { |
| 593 |
if (typeToCountMap == null) { |
| 594 |
typeToCountMap = new AtomicTracerStatMap(); |
| 595 |
typeToSilentMap = new AtomicTracerStatMap(); |
| 596 |
typeToTimeMap = new AtomicTracerStatMap(); |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
static @Nullable Map<String, Long> getTypeToCountMap() { |
| 606 |
return typeToCountMap != null ? typeToCountMap.getMap() : null; |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
static @Nullable Map<String, Long> getTypeToSilentMap() { |
| 615 |
return typeToSilentMap != null ? typeToSilentMap.getMap() : null; |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
static @Nullable Map<String, Long> getTypeToTimeMap() { |
| 624 |
return typeToTimeMap != null ? typeToTimeMap.getMap() : null; |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
static Stat getStatsForType(String type) { |
| 629 |
Stat stat = getThreadTrace().stats.get(type); |
| 630 |
return stat != null ? stat : ZERO_STAT; |
| 631 |
} |
| 632 |
|
| 633 |
private static final Stat ZERO_STAT = new Stat(); |
| 634 |
|
| 635 |
|
| 636 |
private static String formatTime(long time) { |
| 637 |
int sec = (int) ((time / 1000) % 60); |
| 638 |
int ms = (int) (time % 1000); |
| 639 |
return String.format("%02d.%03d", sec, ms); |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
private static final class Event { |
| 644 |
boolean isStart; |
| 645 |
Tracer tracer; |
| 646 |
|
| 647 |
Event(boolean start, Tracer t) { |
| 648 |
isStart = start; |
| 649 |
tracer = t; |
| 650 |
} |
| 651 |
|
| 652 |
long eventTime() { |
| 653 |
return isStart ? tracer.startTimeMs : tracer.stopTimeMs; |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
String toString(long prevEventTime, String indent, int digitsColWidth) { |
| 666 |
StringBuilder sb = new StringBuilder(120); |
| 667 |
|
| 668 |
if (prevEventTime == -1) { |
| 669 |
appendSpaces(sb, digitsColWidth); |
| 670 |
} else { |
| 671 |
sb.append(longToPaddedString( |
| 672 |
eventTime() - prevEventTime, digitsColWidth)); |
| 673 |
} |
| 674 |
|
| 675 |
sb.append(' '); |
| 676 |
sb.append(formatTime(eventTime())); |
| 677 |
if (isStart) { |
| 678 |
sb.append(" Start "); |
| 679 |
appendSpaces(sb, digitsColWidth); |
| 680 |
sb.append(" "); |
| 681 |
} else { |
| 682 |
sb.append(" Done "); |
| 683 |
long delta = tracer.stopTimeMs - tracer.startTimeMs; |
| 684 |
sb.append(longToPaddedString(delta, digitsColWidth)); |
| 685 |
sb.append(" ms "); |
| 686 |
if (tracer.extraTracingValues != null) { |
| 687 |
for (int i = 0; i < tracer.extraTracingValues.length; i++) { |
| 688 |
delta = tracer.extraTracingValues[i]; |
| 689 |
sb.append(String.format("%4d", delta)); |
| 690 |
sb.append(extraTracingStatistics.get(i).getUnits()); |
| 691 |
sb.append("; "); |
| 692 |
} |
| 693 |
} |
| 694 |
} |
| 695 |
sb.append(indent); |
| 696 |
sb.append(tracer.toString()); |
| 697 |
return sb.toString(); |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
|
| 702 |
static final class ThreadTrace { |
| 703 |
|
| 704 |
|
| 705 |
int defaultSilenceThreshold; |
| 706 |
|
| 707 |
|
| 708 |
final ArrayList<Event> events = new ArrayList<Event>(); |
| 709 |
|
| 710 |
|
| 711 |
final HashSet<Tracer> outstandingEvents = new HashSet<Tracer>(); |
| 712 |
|
| 713 |
|
| 714 |
final Map<String, Stat> stats = new HashMap<String, Stat>(); |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
boolean isOutstandingEventsTruncated = false; |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
boolean isEventsTruncated = false; |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
boolean isInitialized = false; |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
boolean prettyPrint = false; |
| 738 |
|
| 739 |
|
| 740 |
void init() { |
| 741 |
isInitialized = true; |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
boolean isInitialized() { |
| 746 |
return isInitialized; |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
void startEvent(Tracer t) { |
| 754 |
events.add(new Event(true, t)); |
| 755 |
boolean notAlreadyOutstanding = outstandingEvents.add(t); |
| 756 |
Preconditions.checkState(notAlreadyOutstanding); |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
void endEvent(Tracer t, int silenceThreshold) { |
| 763 |
boolean wasOutstanding = outstandingEvents.remove(t); |
| 764 |
if (!wasOutstanding) { |
| 765 |
if (isOutstandingEventsTruncated) { |
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
logger.log(Level.WARNING, |
| 770 |
"event not found, probably because the event stack " |
| 771 |
+ "overflowed and was truncated", |
| 772 |
new Throwable()); |
| 773 |
} else { |
| 774 |
|
| 775 |
|
| 776 |
throw new IllegalStateException(); |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
long elapsed = t.stopTimeMs - t.startTimeMs; |
| 781 |
|
| 782 |
if (silenceThreshold == -1) { |
| 783 |
silenceThreshold = defaultSilenceThreshold; |
| 784 |
} |
| 785 |
|
| 786 |
if (elapsed < silenceThreshold) { |
| 787 |
|
| 788 |
boolean removed = false; |
| 789 |
for (int i = 0; i < events.size(); i++) { |
| 790 |
Event e = events.get(i); |
| 791 |
if (e.tracer == t) { |
| 792 |
Preconditions.checkState(e.isStart); |
| 793 |
events.remove(i); |
| 794 |
removed = true; |
| 795 |
break; |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
Preconditions.checkState(removed || isEventsTruncated); |
| 802 |
} else { |
| 803 |
events.add(new Event(false, t)); |
| 804 |
} |
| 805 |
|
| 806 |
if (t.type != null) { |
| 807 |
Stat stat = stats.get(t.type); |
| 808 |
if (stat == null) { |
| 809 |
stat = new Stat(); |
| 810 |
if (!extraTracingStatistics.isEmpty()) { |
| 811 |
stat.extraInfo = new int[extraTracingStatistics.size()]; |
| 812 |
} |
| 813 |
stats.put(t.type, stat); |
| 814 |
} |
| 815 |
|
| 816 |
stat.count++; |
| 817 |
if (typeToCountMap != null) { |
| 818 |
typeToCountMap.incrementBy(t.type, 1); |
| 819 |
} |
| 820 |
|
| 821 |
stat.clockTime += elapsed; |
| 822 |
if (typeToTimeMap != null) { |
| 823 |
typeToTimeMap.incrementBy(t.type, elapsed); |
| 824 |
} |
| 825 |
|
| 826 |
if (stat.extraInfo != null && t.extraTracingValues != null) { |
| 827 |
int overlapLength = |
| 828 |
Math.min(stat.extraInfo.length, t.extraTracingValues.length); |
| 829 |
for (int i = 0; i < overlapLength; i++) { |
| 830 |
stat.extraInfo[i] += t.extraTracingValues[i]; |
| 831 |
AtomicTracerStatMap map = |
| 832 |
extraTracingStatistics.get(i).getTracingStat(); |
| 833 |
if (map != null) { |
| 834 |
map.incrementBy(t.type, t.extraTracingValues[i]); |
| 835 |
} |
| 836 |
} |
| 837 |
} |
| 838 |
|
| 839 |
if (elapsed < silenceThreshold) { |
| 840 |
stat.silent++; |
| 841 |
if (typeToSilentMap != null) { |
| 842 |
typeToSilentMap.incrementBy(t.type, 1); |
| 843 |
} |
| 844 |
} |
| 845 |
} |
| 846 |
} |
| 847 |
|
| 848 |
boolean isEmpty() { |
| 849 |
return events.size() == 0 && outstandingEvents.size() == 0; |
| 850 |
} |
| 851 |
|
| 852 |
void truncateOutstandingEvents() { |
| 853 |
isOutstandingEventsTruncated = true; |
| 854 |
outstandingEvents.clear(); |
| 855 |
} |
| 856 |
|
| 857 |
void truncateEvents() { |
| 858 |
isEventsTruncated = true; |
| 859 |
events.clear(); |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
@SuppressWarnings("nullness") |
| 865 |
@Override public String toString() { |
| 866 |
|
| 867 |
int numDigits = getMaxDigits(); |
| 868 |
StringBuilder sb = new StringBuilder(); |
| 869 |
long etime = -1; |
| 870 |
LinkedList<String> indent = prettyPrint ? new LinkedList<String>() : null; |
| 871 |
for (Event e : events) { |
| 872 |
if (prettyPrint && !e.isStart && !indent.isEmpty()) { |
| 873 |
indent.pop(); |
| 874 |
} |
| 875 |
sb.append(" "); |
| 876 |
if (prettyPrint) { |
| 877 |
sb.append(e.toString(etime, Joiner.on("").join(indent), numDigits)); |
| 878 |
} else { |
| 879 |
sb.append(e.toString(etime, "", 4)); |
| 880 |
} |
| 881 |
etime = e.eventTime(); |
| 882 |
sb.append('\n'); |
| 883 |
if (prettyPrint && e.isStart) { |
| 884 |
indent.push("| "); |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
if (outstandingEvents.size() != 0) { |
| 889 |
long now = clock.currentTimeMillis(); |
| 890 |
|
| 891 |
sb.append(" Unstopped timers:\n"); |
| 892 |
for (Tracer t : outstandingEvents) { |
| 893 |
sb.append(" "). |
| 894 |
append(t). |
| 895 |
append(" ("). |
| 896 |
append(now - t.startTimeMs). |
| 897 |
append(" ms, started at "). |
| 898 |
append(formatTime(t.startTimeMs)). |
| 899 |
append(")\n"); |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
for (String key : stats.keySet()) { |
| 904 |
Stat stat = stats.get(key); |
| 905 |
if (stat.count > 1) { |
| 906 |
sb.append(" TOTAL "). |
| 907 |
append(key). |
| 908 |
append(" "). |
| 909 |
append(stat.count). |
| 910 |
append(" ("). |
| 911 |
append(stat.clockTime). |
| 912 |
append(" ms"); |
| 913 |
if (stat.extraInfo != null) { |
| 914 |
for (int i = 0; i < stat.extraInfo.length; i++) { |
| 915 |
sb.append("; "); |
| 916 |
sb.append(stat.extraInfo[i]). |
| 917 |
append(' '). |
| 918 |
append(extraTracingStatistics.get(i).getUnits()); |
| 919 |
} |
| 920 |
} |
| 921 |
sb.append(")\n"); |
| 922 |
} |
| 923 |
} |
| 924 |
return sb.toString(); |
| 925 |
} |
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
private int getMaxDigits() { |
| 933 |
long etime = -1; |
| 934 |
long max_time = 0; |
| 935 |
for (Event e : events) { |
| 936 |
if (etime != -1) { |
| 937 |
long time = e.eventTime() - etime; |
| 938 |
max_time = Math.max(max_time, time); |
| 939 |
} |
| 940 |
if (!e.isStart) { |
| 941 |
long time = e.tracer.stopTimeMs - e.tracer.startTimeMs; |
| 942 |
max_time = Math.max(max_time, time); |
| 943 |
} |
| 944 |
etime = e.eventTime(); |
| 945 |
} |
| 946 |
|
| 947 |
return Math.max(3, numDigits(max_time)); |
| 948 |
} |
| 949 |
} |
| 950 |
|
| 951 |
|
| 952 |
private static ThreadLocal<ThreadTrace> traces = |
| 953 |
new ThreadLocal<ThreadTrace>(); |
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
static ThreadTrace getThreadTrace() { |
| 959 |
ThreadTrace t = traces.get(); |
| 960 |
if (t == null) { |
| 961 |
t = new ThreadTrace(); |
| 962 |
t.prettyPrint = defaultPrettyPrint; |
| 963 |
traces.set(t); |
| 964 |
} |
| 965 |
return t; |
| 966 |
} |
| 967 |
|
| 968 |
|
| 969 |
static void clearThreadTrace() { |
| 970 |
traces.remove(); |
| 971 |
} |
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
static interface TracingStatistic { |
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
long start(Thread thread); |
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
long stop(Thread thread); |
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
boolean enable(); |
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
AtomicTracerStatMap getTracingStat(); |
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
String getUnits(); |
| 1025 |
} |
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
static final class AtomicTracerStatMap { |
| 1033 |
private ConcurrentMap<String, Long> map = |
| 1034 |
new ConcurrentHashMap<String, Long>(); |
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
@SuppressWarnings("nullness") |
| 1045 |
void incrementBy(String key, long delta) { |
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
Long oldValue = map.get(key); |
| 1051 |
if (oldValue == null) { |
| 1052 |
|
| 1053 |
oldValue = map.putIfAbsent(key, delta); |
| 1054 |
if (oldValue == null) { |
| 1055 |
|
| 1056 |
return; |
| 1057 |
} else { |
| 1058 |
|
| 1059 |
|
| 1060 |
} |
| 1061 |
} |
| 1062 |
while (true) { |
| 1063 |
if (map.replace(key, oldValue, oldValue + delta)) { |
| 1064 |
break; |
| 1065 |
} |
| 1066 |
|
| 1067 |
oldValue = map.get(key); |
| 1068 |
} |
| 1069 |
} |
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
Map<String, Long> getMap() { |
| 1075 |
return map; |
| 1076 |
} |
| 1077 |
} |
| 1078 |
} |