1 11 package org.eclipse.core.runtime; 12 13 import java.io.PrintWriter ; 14 import java.util.*; 15 import org.eclipse.core.internal.runtime.InternalPlatform; 16 import org.eclipse.core.internal.runtime.PerformanceStatsProcessor; 17 18 44 public class PerformanceStats { 45 54 public static abstract class PerformanceListener { 55 58 protected PerformanceListener() { 59 super(); 60 } 61 62 71 public void eventFailed(PerformanceStats event, long duration) { 72 } 74 75 84 public void eventsOccurred(PerformanceStats[] event) { 85 } 87 } 88 89 92 private static final PerformanceStats EMPTY_STATS = new PerformanceStats("", ""); 94 97 public static final boolean ENABLED; 98 99 102 private static final long NOT_STARTED = -1; 103 104 107 private final static Map statMap = Collections.synchronizedMap(new HashMap()); 108 109 113 private final static Map thresholdMap = Collections.synchronizedMap(new HashMap()); 114 115 118 private static final boolean TRACE_SUCCESS; 119 120 125 private String blame; 126 127 131 private String blamePluginId; 132 133 138 private String context; 139 140 143 private long currentStart = NOT_STARTED; 144 145 149 private String event; 150 151 154 private boolean isFailure; 155 156 159 private int runCount = 0; 160 161 164 private long runningTime = 0; 165 166 static { 167 ENABLED = InternalPlatform.getDefault().getBooleanOption(Platform.PI_RUNTIME + "/perf", false); TRACE_SUCCESS = InternalPlatform.getDefault().getBooleanOption(Platform.PI_RUNTIME + "/perf/success", ENABLED); } 171 172 179 public static void addListener(PerformanceListener listener) { 180 if (ENABLED) 181 PerformanceStatsProcessor.addListener(listener); 182 } 183 184 187 public static void clear() { 188 statMap.clear(); 189 } 190 191 197 public static PerformanceStats[] getAllStats() { 198 return (PerformanceStats[]) statMap.values().toArray(new PerformanceStats[statMap.values().size()]); 199 } 200 201 214 public static PerformanceStats getStats(String eventName, Object blameObject) { 215 if (!ENABLED || eventName == null || blameObject == null) 216 return EMPTY_STATS; 217 PerformanceStats newStats = new PerformanceStats(eventName, blameObject); 218 if (!TRACE_SUCCESS) 219 return newStats; 220 PerformanceStats oldStats = (PerformanceStats) statMap.get(newStats); 222 if (oldStats != null) 223 return oldStats; 224 statMap.put(newStats, newStats); 225 return newStats; 226 } 227 228 241 public static boolean isEnabled(String eventName) { 242 if (!ENABLED) 243 return false; 244 String option = Platform.getDebugOption(eventName); 245 return option != null && !option.equalsIgnoreCase("false") && !option.equalsIgnoreCase("-1"); } 247 248 251 public static void printStats() { 252 if (!ENABLED) 253 return; 254 PrintWriter writer = new PrintWriter (System.out); 255 PerformanceStatsProcessor.printStats(writer); 256 writer.flush(); 257 writer.close(); 258 } 259 260 265 public static void printStats(PrintWriter out) { 266 if (!ENABLED) 267 return; 268 PerformanceStatsProcessor.printStats(out); 269 } 270 271 278 public static void removeListener(PerformanceListener listener) { 279 if (ENABLED) 280 PerformanceStatsProcessor.removeListener(listener); 281 } 282 283 289 public static void removeStats(String eventName, Object blameObject) { 290 synchronized (statMap) { 291 for (Iterator it = statMap.keySet().iterator(); it.hasNext();) { 292 PerformanceStats stats = (PerformanceStats) it.next(); 293 if (stats.getEvent().equals(eventName) && stats.getBlame().equals(blameObject)) 294 it.remove(); 295 } 296 } 297 } 298 299 302 private PerformanceStats(String event, Object blame) { 303 this(event, blame, null); 304 } 305 306 309 private PerformanceStats(String event, Object blameObject, String context) { 310 this.event = event; 311 this.blame = blameObject instanceof String ? (String ) blameObject : blameObject.getClass().getName(); 312 this.blamePluginId = InternalPlatform.getDefault().getBundleId(blameObject); 313 this.context = context; 314 } 315 316 326 public void addRun(long elapsed, String contextName) { 327 if (!ENABLED) 328 return; 329 runCount++; 330 runningTime += elapsed; 331 if (elapsed > getThreshold(event)) 332 PerformanceStatsProcessor.failed(createFailureStats(contextName, elapsed), blamePluginId, elapsed); 333 if (TRACE_SUCCESS) 334 PerformanceStatsProcessor.changed(this); 335 } 336 337 344 private PerformanceStats createFailureStats(String contextName, long elapsed) { 345 PerformanceStats failedStat = new PerformanceStats(event, blame, contextName); 346 PerformanceStats old = (PerformanceStats) statMap.get(failedStat); 347 if (old == null) 348 statMap.put(failedStat, failedStat); 349 else 350 failedStat = old; 351 failedStat.isFailure = true; 352 failedStat.runCount++; 353 failedStat.runningTime += elapsed; 354 return failedStat; 355 } 356 357 369 public void endRun() { 370 if (!ENABLED || currentStart == NOT_STARTED) 371 return; 372 addRun(System.currentTimeMillis() - currentStart, context); 373 currentStart = NOT_STARTED; 374 } 375 376 379 public boolean equals(Object obj) { 380 if (!(obj instanceof PerformanceStats)) 382 return false; 383 PerformanceStats that = (PerformanceStats) obj; 384 if (!this.event.equals(that.event)) 385 return false; 386 if (!this.getBlameString().equals(that.getBlameString())) 387 return false; 388 return this.context == null ? that.context == null : this.context.equals(that.context); 389 } 390 391 397 public Object getBlame() { 398 return blame; 399 } 400 401 406 public String getBlameString() { 407 return blame; 408 } 409 410 416 public String getContext() { 417 return context; 418 } 419 420 425 public String getEvent() { 426 return event; 427 } 428 429 434 public int getRunCount() { 435 return runCount; 436 } 437 438 444 public long getRunningTime() { 445 return runningTime; 446 } 447 448 451 private long getThreshold(String eventName) { 452 Long value = (Long ) thresholdMap.get(eventName); 453 if (value == null) { 454 String option = InternalPlatform.getDefault().getOption(eventName); 455 if (option != null) { 456 try { 457 value = new Long (option); 458 } catch (NumberFormatException e) { 459 } 461 } 462 if (value == null) 463 value = new Long (Long.MAX_VALUE); 464 thresholdMap.put(eventName, value); 465 } 466 return value.longValue(); 467 } 468 469 public int hashCode() { 470 int hash = event.hashCode() * 37 + getBlameString().hashCode(); 472 if (context != null) 473 hash = hash * 37 + context.hashCode(); 474 return hash; 475 } 476 477 483 public boolean isFailure() { 484 return isFailure; 485 } 486 487 490 public void reset() { 491 runningTime = 0; 492 runCount = 0; 493 } 494 495 499 public void startRun() { 500 if (ENABLED) 501 startRun(null); 502 } 503 504 513 public void startRun(String contextName) { 514 if (!ENABLED) 515 return; 516 this.context = contextName; 517 this.currentStart = System.currentTimeMillis(); 518 } 519 520 523 public String toString() { 524 StringBuffer result = new StringBuffer ("PerformanceStats("); result.append(event); 526 result.append(','); 527 result.append(blame); 528 if (context != null) { 529 result.append(','); 530 result.append(context); 531 } 532 result.append(')'); 533 return result.toString(); 534 } 535 } | Popular Tags |