KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > runtime > internal > stats > StatsManager


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

11 package org.eclipse.core.runtime.internal.stats;
12
13 import java.io.*;
14 import java.net.URL JavaDoc;
15 import java.util.*;
16 import org.eclipse.osgi.baseadaptor.HookConfigurator;
17 import org.eclipse.osgi.baseadaptor.HookRegistry;
18 import org.eclipse.osgi.baseadaptor.bundlefile.BundleEntry;
19 import org.eclipse.osgi.baseadaptor.hooks.ClassLoadingStatsHook;
20 import org.eclipse.osgi.baseadaptor.loader.ClasspathEntry;
21 import org.eclipse.osgi.baseadaptor.loader.ClasspathManager;
22 import org.eclipse.osgi.framework.adaptor.BundleWatcher;
23 import org.eclipse.osgi.framework.adaptor.FrameworkAdaptor;
24 import org.eclipse.osgi.framework.debug.Debug;
25 import org.eclipse.osgi.framework.debug.FrameworkDebugOptions;
26 import org.eclipse.osgi.util.ManifestElement;
27 import org.osgi.framework.Bundle;
28
29 public class StatsManager implements BundleWatcher, HookConfigurator, ClassLoadingStatsHook {
30     // This connect bundles and their info, and so allows to access the info without running through
31
// the bundle registry. This map only contains activated bundles. The key is the bundle Id
32
private Hashtable bundles = new Hashtable(20);
33     private Map activationStacks = new HashMap(5);
34     private static boolean booting = true; // the state of the platform. This value is changed by the InternalPlatform itself.
35

36     private static StatsManager defaultInstance;
37
38     public static boolean MONITOR_ACTIVATION = false;
39     public static boolean MONITOR_CLASSES = false;
40     public static boolean MONITOR_RESOURCES = false;
41     public static String JavaDoc TRACE_FILENAME = "runtime.traces"; //$NON-NLS-1$
42
public static String JavaDoc TRACE_FILTERS = "trace.properties"; //$NON-NLS-1$
43
public static boolean TRACE_CLASSES = false;
44     public static boolean TRACE_BUNDLES = false;
45     public static final String JavaDoc FRAMEWORK_SYMBOLICNAME = "org.eclipse.osgi"; //$NON-NLS-1$
46

47     //Option names for spies
48
private static final String JavaDoc OPTION_MONITOR_ACTIVATION = FRAMEWORK_SYMBOLICNAME + "/monitor/activation"; //$NON-NLS-1$
49
private static final String JavaDoc OPTION_MONITOR_CLASSES = FRAMEWORK_SYMBOLICNAME + "/monitor/classes"; //$NON-NLS-1$
50
private static final String JavaDoc OPTION_MONITOR_RESOURCES = FRAMEWORK_SYMBOLICNAME + "/monitor/resources"; //$NON-NLS-1$
51
private static final String JavaDoc OPTION_TRACE_BUNDLES = FRAMEWORK_SYMBOLICNAME + "/trace/activation"; //$NON-NLS-1$
52
private static final String JavaDoc OPTION_TRACE_CLASSES = FRAMEWORK_SYMBOLICNAME + "/trace/classLoading"; //$NON-NLS-1$
53
private static final String JavaDoc OPTION_TRACE_FILENAME = FRAMEWORK_SYMBOLICNAME + "/trace/filename"; //$NON-NLS-1$
54
private static final String JavaDoc OPTION_TRACE_FILTERS = FRAMEWORK_SYMBOLICNAME + "/trace/filters"; //$NON-NLS-1$
55

56     static {
57         setDebugOptions();
58     }
59
60     public static StatsManager getDefault() {
61         if (defaultInstance == null) {
62             defaultInstance = new StatsManager();
63             defaultInstance.initialize();
64         }
65         return defaultInstance;
66     }
67
68     public static void setDebugOptions() {
69         FrameworkDebugOptions options = FrameworkDebugOptions.getDefault();
70         // may be null if debugging is not enabled
71
if (options == null)
72             return;
73         MONITOR_ACTIVATION = options.getBooleanOption(OPTION_MONITOR_ACTIVATION, false);
74         MONITOR_CLASSES = options.getBooleanOption(OPTION_MONITOR_CLASSES, false);
75         MONITOR_RESOURCES = options.getBooleanOption(OPTION_MONITOR_RESOURCES, false);
76         TRACE_CLASSES = options.getBooleanOption(OPTION_TRACE_CLASSES, false);
77         TRACE_BUNDLES = options.getBooleanOption(OPTION_TRACE_BUNDLES, false);
78         TRACE_FILENAME = options.getOption(OPTION_TRACE_FILENAME, TRACE_FILENAME);
79         TRACE_FILTERS = options.getOption(OPTION_TRACE_FILTERS, TRACE_FILTERS);
80     }
81
82     public static void doneBooting() {
83         booting = false;
84     }
85
86     public static boolean isBooting() {
87         return booting;
88     }
89
90     /**
91      * Returns the result of converting a list of comma-separated tokens into an array
92      *
93      * @return the array of string tokens
94      * @param prop the initial comma-separated string
95      */

96     public static String JavaDoc[] getArrayFromList(String JavaDoc prop) {
97         return ManifestElement.getArrayFromList(prop, ","); //$NON-NLS-1$
98
}
99
100     private void initialize() {
101         // add the system bundle
102
BundleStats bundle = findBundle(FrameworkAdaptor.FRAMEWORK_SYMBOLICNAME, 0);
103         bundle.setTimestamp(System.currentTimeMillis());
104         bundle.setActivationOrder(bundles.size());
105         bundle.setDuringStartup(booting);
106     }
107
108     public void watchBundle(Bundle bundle, int type) {
109         switch (type) {
110             case BundleWatcher.START_ACTIVATION :
111                 startActivation(bundle);
112                 break;
113             case BundleWatcher.END_ACTIVATION :
114                 endActivation(bundle);
115                 break;
116         }
117     }
118
119     public void startActivation(Bundle bundle) {
120         // should be called from a synchronized location to protect against concurrent updates
121
BundleStats info = findBundle(bundle.getSymbolicName(), bundle.getBundleId());
122         info.setTimestamp(System.currentTimeMillis());
123         info.setActivationOrder(bundles.size());
124         info.setDuringStartup(booting);
125
126         Stack activationStack = (Stack) activationStacks.get(Thread.currentThread());
127         if (activationStack==null) {
128             activationStack = new Stack();
129             activationStacks.put(Thread.currentThread(), activationStack);
130         }
131             
132         // set the parentage of activation
133
if (activationStack.size() != 0) {
134             BundleStats activatedBy = (BundleStats) activationStack.peek();
135             activatedBy.activated(info);
136             info.setActivatedBy(activatedBy);
137         }
138         activationStack.push(info);
139
140         if (TRACE_BUNDLES == true) {
141             traceActivate(bundle, info);
142         }
143     }
144
145     public void endActivation(Bundle symbolicName) {
146         Stack activationStack = (Stack) activationStacks.get(Thread.currentThread());
147         // should be called from a synchronized location to protect against concurrent updates
148
BundleStats info = (BundleStats) activationStack.pop();
149         info.endActivation();
150     }
151
152     private void traceActivate(Bundle bundle, BundleStats info) {
153         try {
154             PrintWriter output = new PrintWriter(new FileOutputStream(ClassloaderStats.traceFile.getAbsolutePath(), true));
155             try {
156                 long startPosition = ClassloaderStats.traceFile.length();
157                 output.println("Activating bundle: " + bundle.getSymbolicName()); //$NON-NLS-1$
158
output.println("Bundle activation stack:"); //$NON-NLS-1$
159
Stack activationStack = (Stack) activationStacks.get(Thread.currentThread());
160                 for (int i = activationStack.size() - 1; i >= 0; i--)
161                     output.println("\t" + ((BundleStats) activationStack.get(i)).getSymbolicName()); //$NON-NLS-1$
162
output.println("Class loading stack:"); //$NON-NLS-1$
163
Stack classStack = ClassloaderStats.getClassStack();
164                 for (int i = classStack.size() - 1; i >= 0; i--)
165                     output.println("\t" + ((ClassStats) classStack.get(i)).getClassName()); //$NON-NLS-1$
166
output.println("Stack trace:"); //$NON-NLS-1$
167
new Throwable JavaDoc().printStackTrace(output);
168                 info.setTraceStart(startPosition);
169             } finally {
170                 output.close();
171                 info.setTraceEnd(ClassloaderStats.traceFile.length());
172             }
173         } catch (IOException e) {
174             e.printStackTrace();
175         }
176     }
177
178     public BundleStats findBundle(String JavaDoc symbolicName, long id) {
179         BundleStats result = (BundleStats) bundles.get(new Long JavaDoc(id));
180         try {
181             if (result == null) {
182                 result = new BundleStats(symbolicName, id);
183                 bundles.put(new Long JavaDoc(id), result);
184             }
185         } catch (IllegalAccessError JavaDoc e) {
186             e.printStackTrace();
187         }
188         return result;
189     }
190
191     public BundleStats[] getBundles() {
192         return (BundleStats[]) bundles.values().toArray(new BundleStats[bundles.size()]);
193     }
194
195     public BundleStats getBundle(long id) {
196         return (BundleStats) bundles.get(new Long JavaDoc(id));
197     }
198
199     public void preFindLocalClass(String JavaDoc name, ClasspathManager manager) throws ClassNotFoundException JavaDoc {
200         if (StatsManager.MONITOR_CLASSES) //Support for performance analysis
201
ClassloaderStats.startLoadingClass(getClassloaderId(manager), name);
202     }
203
204     public void postFindLocalClass(String JavaDoc name, Class JavaDoc clazz, ClasspathManager manager) {
205         if (StatsManager.MONITOR_CLASSES)
206             ClassloaderStats.endLoadingClass(getClassloaderId(manager), name, clazz != null);
207     }
208
209     public void preFindLocalResource(String JavaDoc name, ClasspathManager manager) {
210         // do nothing
211
}
212
213     public void postFindLocalResource(String JavaDoc name, URL JavaDoc resource, ClasspathManager manager) {
214         if (StatsManager.MONITOR_RESOURCES)
215             if (resource != null && name.endsWith(".properties")) //$NON-NLS-1$
216
ClassloaderStats.loadedBundle(getClassloaderId(manager), new ResourceBundleStats(getClassloaderId(manager), name, resource));
217         return;
218     }
219
220     public void recordClassDefine(String JavaDoc name, Class JavaDoc clazz, byte[] classbytes, ClasspathEntry classpathEntry, BundleEntry entry, ClasspathManager manager) {
221         // do nothing
222
}
223
224     private String JavaDoc getClassloaderId(ClasspathManager loader) {
225         return loader.getBaseData().getSymbolicName();
226     }
227
228     public void addHooks(HookRegistry hookRegistry) {
229         if (Debug.MONITOR_ACTIVATION)
230             hookRegistry.addWatcher(StatsManager.getDefault());
231         if (StatsManager.MONITOR_CLASSES || StatsManager.MONITOR_RESOURCES)
232             hookRegistry.addClassLoadingStatsHook(StatsManager.getDefault());
233     }
234 }
235
Popular Tags