1 11 package org.eclipse.core.runtime.internal.stats; 12 13 import java.io.*; 14 import java.net.URL ; 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 private Hashtable bundles = new Hashtable(20); 33 private Map activationStacks = new HashMap(5); 34 private static boolean booting = true; 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 TRACE_FILENAME = "runtime.traces"; public static String TRACE_FILTERS = "trace.properties"; public static boolean TRACE_CLASSES = false; 44 public static boolean TRACE_BUNDLES = false; 45 public static final String FRAMEWORK_SYMBOLICNAME = "org.eclipse.osgi"; 47 private static final String OPTION_MONITOR_ACTIVATION = FRAMEWORK_SYMBOLICNAME + "/monitor/activation"; private static final String OPTION_MONITOR_CLASSES = FRAMEWORK_SYMBOLICNAME + "/monitor/classes"; private static final String OPTION_MONITOR_RESOURCES = FRAMEWORK_SYMBOLICNAME + "/monitor/resources"; private static final String OPTION_TRACE_BUNDLES = FRAMEWORK_SYMBOLICNAME + "/trace/activation"; private static final String OPTION_TRACE_CLASSES = FRAMEWORK_SYMBOLICNAME + "/trace/classLoading"; private static final String OPTION_TRACE_FILENAME = FRAMEWORK_SYMBOLICNAME + "/trace/filename"; private static final String OPTION_TRACE_FILTERS = FRAMEWORK_SYMBOLICNAME + "/trace/filters"; 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 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 96 public static String [] getArrayFromList(String prop) { 97 return ManifestElement.getArrayFromList(prop, ","); } 99 100 private void initialize() { 101 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 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 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 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()); output.println("Bundle activation stack:"); 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()); output.println("Class loading stack:"); Stack classStack = ClassloaderStats.getClassStack(); 164 for (int i = classStack.size() - 1; i >= 0; i--) 165 output.println("\t" + ((ClassStats) classStack.get(i)).getClassName()); output.println("Stack trace:"); new Throwable ().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 symbolicName, long id) { 179 BundleStats result = (BundleStats) bundles.get(new Long (id)); 180 try { 181 if (result == null) { 182 result = new BundleStats(symbolicName, id); 183 bundles.put(new Long (id), result); 184 } 185 } catch (IllegalAccessError 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 (id)); 197 } 198 199 public void preFindLocalClass(String name, ClasspathManager manager) throws ClassNotFoundException { 200 if (StatsManager.MONITOR_CLASSES) ClassloaderStats.startLoadingClass(getClassloaderId(manager), name); 202 } 203 204 public void postFindLocalClass(String name, Class clazz, ClasspathManager manager) { 205 if (StatsManager.MONITOR_CLASSES) 206 ClassloaderStats.endLoadingClass(getClassloaderId(manager), name, clazz != null); 207 } 208 209 public void preFindLocalResource(String name, ClasspathManager manager) { 210 } 212 213 public void postFindLocalResource(String name, URL resource, ClasspathManager manager) { 214 if (StatsManager.MONITOR_RESOURCES) 215 if (resource != null && name.endsWith(".properties")) ClassloaderStats.loadedBundle(getClassloaderId(manager), new ResourceBundleStats(getClassloaderId(manager), name, resource)); 217 return; 218 } 219 220 public void recordClassDefine(String name, Class clazz, byte[] classbytes, ClasspathEntry classpathEntry, BundleEntry entry, ClasspathManager manager) { 221 } 223 224 private String 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 |