KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > events > EventStats


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.events;
12
13 import java.io.PrintStream JavaDoc;
14 import java.io.PrintWriter JavaDoc;
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 /**
22  * A placeholder for statistics about the runtime behaviour
23  * of a particular plugin. PluginStats objects are used internally
24  * by the CoreStats mechanism.
25  */

26 public class EventStats {
27     protected String JavaDoc 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     /**
37      * If this flag is true, print debug info to the console. If false, it
38      * fails silently.
39      */

40     public static boolean DEBUG = false;
41
42     /**
43      * The start time of the current build or notification
44      */

45     private static long currentStart;
46
47     /**
48      * The stat object tracking the current event
49      */

50     private static EventStats currentStats;
51
52     /**
53      * Plugin statistics
54      */

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"); //$NON-NLS-1$
60

61     /** private constructor to prevent instantiation */
62     private EventStats(String JavaDoc id, IProject project) {
63         this.id = id;
64         this.project = project;
65     }
66
67     public static void buildException(Exception JavaDoc e) {
68         if (currentStats == null) {
69             if (DEBUG)
70                 System.out.println(Policy.bind("utils.buildException")); //$NON-NLS-1$
71
} else
72             currentStats.addException(e);
73     }
74
75     public static void dumpStats() {
76         dumpStats(System.out);
77     }
78
79     public static void dumpStats(PrintStream JavaDoc out) {
80         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(out);
81         dumpStats(writer);
82         writer.flush();
83         writer.close();
84
85     }
86
87     public static void dumpStats(PrintWriter JavaDoc out) {
88         /* gather totals */
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         //dump stats
105
out.println("---------------------------------------------------------------"); //$NON-NLS-1$
106
out.println(Policy.bind("utils.snapshots", Integer.toString(snapshotCount), Long.toString(snapshotTime))); //$NON-NLS-1$
107
for (int i = 0; i < allStats.length; i++) {
108             EventStats stats = allStats[i];
109             out.println(Policy.bind("utils.stats", stats.getName())); //$NON-NLS-1$
110

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)))); //$NON-NLS-1$
114

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)))); //$NON-NLS-1$
118

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)))); //$NON-NLS-1$
122

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)))); //$NON-NLS-1$
126

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)))); //$NON-NLS-1$
130

131             out.println(""); //$NON-NLS-1$
132
}
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")); //$NON-NLS-1$
140
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")); //$NON-NLS-1$
152
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 JavaDoc target) {
169         ClassLoader JavaDoc loader = target.getClass().getClassLoader();
170         if (loader instanceof PluginClassLoader)
171             return ((PluginClassLoader) loader).getPluginDescriptor();
172         return null;
173     }
174
175     /**
176      * Returns the stats object for the given ID.
177      */

178     private static EventStats getStats(String JavaDoc id, IProject project, Object JavaDoc 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     /**
207      * Returns stats objects for all plugins that are registered
208      * for statistics (either notification listeners or builders).
209      */

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     /**
229      * Resets all known statistics.
230      */

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     /**
251      * Notifies the stats tool that a resource change listener has been removed.
252      */

253     public static void listenerRemoved(IResourceChangeListener listener) {
254         if (listener != null)
255             notificationStats.remove(listener.toString());
256     }
257
258     /**
259      * Notifies the stats tool that a resource change listener has been added.
260      */

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 JavaDoc e) {
267         if (currentStats == null) {
268             if (DEBUG)
269                 System.out.println(Policy.bind("utils.buildException")); //$NON-NLS-1$
270
return;
271         }
272         currentStats.addException(e);
273     }
274
275     public static void startBuild(IncrementalProjectBuilder builder) {
276         String JavaDoc 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 JavaDoc 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 JavaDoc next = (Exception JavaDoc) 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 JavaDoc 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 JavaDoc next = (Exception JavaDoc) e.nextElement();
351             if (next instanceof RuntimeException JavaDoc) {
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