1 11 package org.eclipse.core.internal.events; 12 13 import java.io.PrintStream ; 14 import java.io.PrintWriter ; 15 import java.util.*; 16 import org.eclipse.core.internal.plugins.PluginClassLoader; 17 import org.eclipse.core.internal.utils.Policy; 18 import org.eclipse.core.resources.*; 19 import org.eclipse.core.runtime.*; 20 21 26 public class EventStats { 27 protected String id; 28 protected IProject project; 29 protected IPluginDescriptor plugin; 30 protected long notificationRunningTime = 0; 31 protected long buildRunningTime = 0; 32 protected int notificationCount = 0; 33 protected int buildCount = 0; 34 protected Vector exceptions = new Vector(); 35 36 40 public static boolean DEBUG = false; 41 42 45 private static long currentStart; 46 47 50 private static EventStats currentStats; 51 52 55 private static int snapshotCount = 0; 56 private static long snapshotTime = 0; 57 58 private static Map notificationStats = new HashMap(20); 59 private static QualifiedName STATS_PROPERTY = new QualifiedName(ResourcesPlugin.PI_RESOURCES, "buildstats"); 61 62 private EventStats(String id, IProject project) { 63 this.id = id; 64 this.project = project; 65 } 66 67 public static void buildException(Exception e) { 68 if (currentStats == null) { 69 if (DEBUG) 70 System.out.println(Policy.bind("utils.buildException")); } else 72 currentStats.addException(e); 73 } 74 75 public static void dumpStats() { 76 dumpStats(System.out); 77 } 78 79 public static void dumpStats(PrintStream out) { 80 PrintWriter writer = new PrintWriter (out); 81 dumpStats(writer); 82 writer.flush(); 83 writer.close(); 84 85 } 86 87 public static void dumpStats(PrintWriter out) { 88 89 long totalBuildTime = 0; 90 long totalNotifyTime = 0; 91 int totalExceptions = 0; 92 int totalBuilds = 0; 93 int totalNotifications = 0; 94 95 EventStats[] allStats = getAllStats(); 96 for (int i = 0; i < allStats.length; i++) { 97 EventStats stats = allStats[i]; 98 totalBuildTime += stats.getBuildRunningTime(); 99 totalNotifyTime += stats.getNotifyRunningTime(); 100 totalExceptions += stats.getExceptionCount(); 101 totalBuilds += stats.getBuildCount(); 102 totalNotifications += stats.getNotifyCount(); 103 } 104 out.println("---------------------------------------------------------------"); out.println(Policy.bind("utils.snapshots", Integer.toString(snapshotCount), Long.toString(snapshotTime))); for (int i = 0; i < allStats.length; i++) { 108 EventStats stats = allStats[i]; 109 out.println(Policy.bind("utils.stats", stats.getName())); 111 int notifyCount = stats.getNotifyCount(); 112 if (notifyCount > 0) 113 out.println(Policy.bind("utils.notifications", Integer.toString(notifyCount), Integer.toString((int) (notifyCount * 100.0 / totalNotifications)))); 115 long notifyTime = stats.getNotifyRunningTime(); 116 if (notifyTime > 0) 117 out.println(Policy.bind("utils.notifyTime", Long.toString(notifyTime), Integer.toString((int) (notifyTime * 100.0 / totalNotifyTime)))); 119 int buildCount = stats.getBuildCount(); 120 if (buildCount > 0) 121 out.println(Policy.bind("utils.builds", Integer.toString(buildCount), Integer.toString((int) (buildCount * 100.0 / totalBuilds)))); 123 long buildTime = stats.getBuildRunningTime(); 124 if (buildTime > 0) 125 out.println(Policy.bind("utils.buildTime", Long.toString(buildTime), Integer.toString((int) (buildTime * 100.0 / totalBuildTime)))); 127 int exceptions = stats.getExceptionCount(); 128 if (exceptions > 0) 129 out.println(Policy.bind("utils.exceptions", Integer.toString(exceptions), Integer.toString((int) (exceptions * 100.0 / totalExceptions)))); 131 out.println(""); } 133 } 134 135 public static void endBuild() { 136 long end = System.currentTimeMillis(); 137 if (currentStats == null || currentStart == -1) { 138 if (DEBUG) 139 System.err.println(Policy.bind("utils.endBuild")); return; 141 } 142 currentStats.addBuild(end - currentStart); 143 currentStats = null; 144 currentStart = -1; 145 } 146 147 public static void endNotify() { 148 long end = System.currentTimeMillis(); 149 if (currentStats == null || currentStart == -1) { 150 if (DEBUG) 151 System.err.println(Policy.bind("utils.endNotify")); return; 153 } 154 currentStats.addNotify(end - currentStart); 155 currentStart = -1; 156 } 157 158 public static void endSnapshot() { 159 snapshotTime += System.currentTimeMillis() - currentStart; 160 snapshotCount++; 161 currentStart = -1; 162 } 163 164 public static int getSnapCount() { 165 return snapshotCount; 166 } 167 168 private static IPluginDescriptor getPluginFor(Object target) { 169 ClassLoader loader = target.getClass().getClassLoader(); 170 if (loader instanceof PluginClassLoader) 171 return ((PluginClassLoader) loader).getPluginDescriptor(); 172 return null; 173 } 174 175 178 private static EventStats getStats(String id, IProject project, Object origin) { 179 EventStats result = null; 180 if (project == null) { 181 result = (EventStats) notificationStats.get(id); 182 if (result == null) { 183 result = new EventStats(id, project); 184 result.setPlugin(getPluginFor(origin)); 185 notificationStats.put(id, result); 186 } 187 } else { 188 try { 189 Map stats = (Map) project.getSessionProperty(STATS_PROPERTY); 190 if (stats == null) { 191 stats = new HashMap(5); 192 project.setSessionProperty(STATS_PROPERTY, stats); 193 } 194 result = (EventStats) stats.get(id); 195 if (result == null) { 196 result = new EventStats(id, project); 197 result.setPlugin(getPluginFor(origin)); 198 stats.put(id, result); 199 } 200 } catch (CoreException e) { 201 } 202 } 203 return result; 204 } 205 206 210 public static EventStats[] getAllStats() { 211 ArrayList result = new ArrayList(notificationStats.values()); 212 try { 213 IResource[] projects = ResourcesPlugin.getWorkspace().getRoot().members(); 214 for (int i = 0; i < projects.length; i++) { 215 IProject project = (IProject) projects[i]; 216 try { 217 Map stats = (Map) project.getSessionProperty(STATS_PROPERTY); 218 if (stats != null) 219 result.addAll(stats.values()); 220 } catch (CoreException e) { 221 } 222 } 223 } catch (CoreException e) { 224 } 225 return (EventStats[]) result.toArray(new EventStats[result.size()]); 226 } 227 228 231 public static void resetStats() { 232 for (Iterator iter = notificationStats.values().iterator(); iter.hasNext();) 233 ((EventStats) iter.next()).reset(); 234 try { 235 IResource[] projects = ResourcesPlugin.getWorkspace().getRoot().members(); 236 for (int i = 0; i < projects.length; i++) { 237 IProject project = (IProject) projects[i]; 238 try { 239 Map stats = (Map) project.getSessionProperty(STATS_PROPERTY); 240 if (stats != null) 241 for (Iterator iter = stats.values().iterator(); iter.hasNext();) 242 ((EventStats) iter.next()).reset(); 243 } catch (CoreException e) { 244 } 245 } 246 } catch (CoreException e) { 247 } 248 } 249 250 253 public static void listenerRemoved(IResourceChangeListener listener) { 254 if (listener != null) 255 notificationStats.remove(listener.toString()); 256 } 257 258 261 public static void listenerAdded(IResourceChangeListener listener) { 262 if (listener != null) 263 getStats(listener.toString(), null, listener); 264 } 265 266 public static void notifyException(Exception e) { 267 if (currentStats == null) { 268 if (DEBUG) 269 System.out.println(Policy.bind("utils.buildException")); return; 271 } 272 currentStats.addException(e); 273 } 274 275 public static void startBuild(IncrementalProjectBuilder builder) { 276 String key = ((InternalBuilder) builder).getLabel(); 277 currentStats = getStats(key, builder.getProject(), builder); 278 currentStart = System.currentTimeMillis(); 279 } 280 281 public static void startNotify(IResourceChangeListener listener) { 282 currentStats = getStats(listener.toString(), null, listener); 283 currentStart = System.currentTimeMillis(); 284 } 285 286 public static void startSnapshot() { 287 currentStart = System.currentTimeMillis(); 288 } 289 290 void addBuild(long elapsed) { 291 buildCount++; 292 buildRunningTime += elapsed; 293 } 294 295 void addException(Exception e) { 296 exceptions.addElement(e); 297 } 298 299 void addNotify(long elapsed) { 300 notificationCount++; 301 notificationRunningTime += elapsed; 302 } 303 304 public int getBuildCount() { 305 return buildCount; 306 } 307 308 public long getBuildRunningTime() { 309 return buildRunningTime; 310 } 311 312 public Enumeration getCoreExceptions() { 313 Vector runtime = new Vector(); 314 for (Enumeration e = exceptions.elements(); e.hasMoreElements();) { 315 Exception next = (Exception ) e.nextElement(); 316 if (next instanceof CoreException) { 317 runtime.addElement(next); 318 } 319 } 320 return runtime.elements(); 321 } 322 323 public int getExceptionCount() { 324 return exceptions.size(); 325 } 326 327 public String getName() { 328 return id; 329 } 330 331 public int getNotifyCount() { 332 return notificationCount; 333 } 334 335 public long getNotifyRunningTime() { 336 return notificationRunningTime; 337 } 338 339 public IPluginDescriptor getPlugin() { 340 return plugin; 341 } 342 343 public IProject getProject() { 344 return project; 345 } 346 347 public Enumeration getRuntimeExceptions() { 348 Vector runtime = new Vector(); 349 for (Enumeration e = exceptions.elements(); e.hasMoreElements();) { 350 Exception next = (Exception ) e.nextElement(); 351 if (next instanceof RuntimeException ) { 352 runtime.addElement(next); 353 } 354 } 355 return runtime.elements(); 356 } 357 358 public long getTotalRunningTime() { 359 return notificationRunningTime + buildRunningTime; 360 } 361 362 public void reset() { 363 notificationRunningTime = 0; 364 buildRunningTime = 0; 365 notificationCount = 0; 366 buildCount = 0; 367 exceptions = new Vector(); 368 } 369 370 public void setPlugin(IPluginDescriptor value) { 371 plugin = value; 372 } 373 } | Popular Tags |