1 11 package org.eclipse.core.runtime.internal.stats; 12 13 import java.io.*; 14 import java.util.*; 15 16 20 public class ClassloaderStats { 21 private String id; 22 private long loadingTime; private int failureCount = 0; 27 private Map classes = Collections.synchronizedMap(new HashMap(20)); 28 private ArrayList bundles = new ArrayList(2); 30 private boolean keepTraces = false; 32 private static ArrayList packageFilters = new ArrayList(4); private static Set pluginFilters = new HashSet(5); 36 private static Hashtable classStacks = new Hashtable(); 40 private static Map loaders = Collections.synchronizedMap(new HashMap(20)); 41 public static File traceFile; 42 43 static { 44 if (StatsManager.TRACE_CLASSES || StatsManager.TRACE_BUNDLES) 45 initializeTraceOptions(); 46 } 47 48 private static void initializeTraceOptions() { 49 String filename = StatsManager.TRACE_FILENAME; 51 traceFile = new File(filename); 52 traceFile.delete(); 53 54 if (!StatsManager.TRACE_CLASSES) 56 return; 57 filename = StatsManager.TRACE_FILTERS; 58 if (filename == null || filename.length() == 0) 59 return; 60 try { 61 File filterFile = new File(filename); 62 System.out.print("Runtime tracing elements defined in: " + filterFile.getAbsolutePath() + "..."); InputStream input = new FileInputStream(filterFile); 64 System.out.println(" Loaded."); Properties filters = new Properties() { 66 private static final long serialVersionUID = 3546359543853365296L; 67 68 public Object put(Object key, Object value) { 69 addFilters((String ) key, (String ) value); 70 return null; 71 } 72 }; 73 try { 74 filters.load(input); 75 } finally { 76 input.close(); 77 } 78 } catch (IOException e) { 79 System.out.println(" No trace filters loaded."); } 81 } 82 83 protected static void addFilters(String key, String value) { 84 String [] filters = StatsManager.getArrayFromList(value); 85 if ("plugins".equals(key)) pluginFilters.addAll(Arrays.asList(filters)); 87 if ("packages".equals(key)) packageFilters.addAll(Arrays.asList(filters)); 89 } 90 91 public static void startLoadingClass(String id, String className) { 92 findLoader(id).startLoadClass(className); 93 } 94 95 private static ClassloaderStats findLoader(String id) { 97 synchronized (loaders) { 98 ClassloaderStats result = (ClassloaderStats) loaders.get(id); 99 if (result == null) { 100 result = new ClassloaderStats(id); 101 loaders.put(id, result); 102 } 103 return result; 104 } 105 } 106 107 public static synchronized Stack getClassStack() { 108 Stack result = (Stack) classStacks.get(Thread.currentThread()); 109 if (result == null) { 110 result = new Stack(); 111 classStacks.put(Thread.currentThread(), result); 112 } 113 return result; 114 } 115 116 public static ClassloaderStats[] getLoaders() { 117 return (ClassloaderStats[]) loaders.values().toArray(new ClassloaderStats[0]); 120 } 121 122 public static void endLoadingClass(String id, String className, boolean success) { 123 findLoader(id).endLoadClass(className, success); 124 } 125 126 public static void loadedBundle(String id, ResourceBundleStats info) { 127 findLoader(id).loadedBundle(info); 128 } 129 130 public static ClassloaderStats getLoader(String id) { 131 return (ClassloaderStats) loaders.get(id); 132 } 133 134 public ClassloaderStats(String id) { 135 this.id = id; 136 keepTraces = pluginFilters.contains(id); 137 } 138 139 public void addBaseClasses(String [] baseClasses) { 140 for (int i = 0; i < baseClasses.length; i++) { 141 String name = baseClasses[i]; 142 if (classes.get(name) == null) { 143 ClassStats value = new ClassStats(name, this); 144 value.toBaseClass(); 145 classes.put(name, value); 146 } 147 } 148 } 149 150 private void loadedBundle(ResourceBundleStats bundle) { 151 bundles.add(bundle); 152 } 153 154 public ArrayList getBundles() { 155 return bundles; 156 } 157 158 private synchronized void startLoadClass(String name) { 159 getClassStack().push(findClass(name)); 160 } 161 162 private ClassStats findClass(String name) { 164 ClassStats result = (ClassStats) classes.get(name); 165 return result == null ? new ClassStats(name, this) : result; 166 } 167 168 private synchronized void endLoadClass(String name, boolean success) { 169 ClassStats current = (ClassStats) getClassStack().pop(); 170 if (!success) { 171 failureCount++; 172 return; 173 } 174 if (current.getLoadOrder() >= 0) 175 return; 176 177 classes.put(name, current); 178 current.setLoadOrder(classes.size()); 179 current.loadingDone(); 180 traceLoad(name, current); 181 182 Stack classStack = getClassStack(); 184 if (classStack.size() != 0) { 185 ClassStats previous = ((ClassStats) classStack.peek()); 187 previous.addTimeLoadingOthers(current.getTimeLoading()); 188 current.setLoadedBy(previous); 189 previous.loaded(current); 190 } else { 191 loadingTime = loadingTime + current.getTimeLoading(); 192 } 193 } 194 195 private void traceLoad(String name, ClassStats target) { 196 if (!keepTraces) { 198 boolean found = false; 199 for (int i = 0; !found && i < packageFilters.size(); i++) 200 if (name.startsWith((String ) packageFilters.get(i))) 201 found = true; 202 if (!found) 203 return; 204 } 205 206 try { 208 target.setTraceStart(traceFile.length()); 209 PrintWriter output = new PrintWriter(new FileOutputStream(traceFile.getAbsolutePath(), true)); 210 try { 211 output.println("Loading class: " + name); output.println("Class loading stack:"); output.println("\t" + name); Stack classStack = getClassStack(); 215 for (int i = classStack.size() - 1; i >= 0; i--) 216 output.println("\t" + ((ClassStats) classStack.get(i)).getClassName()); output.println("Stack trace:"); new Throwable ().printStackTrace(output); 219 } finally { 220 output.close(); 221 } 222 target.setTraceEnd(traceFile.length()); 223 } catch (FileNotFoundException e) { 224 e.printStackTrace(); 225 } 226 } 227 228 public int getClassLoadCount() { 229 return classes.size(); 230 } 231 232 public long getClassLoadTime() { 233 return loadingTime; 234 } 235 236 public ClassStats[] getClasses() { 237 return (ClassStats[]) classes.values().toArray(new ClassStats[0]); 240 } 241 242 public String getId() { 243 return id; 244 } 245 } 246 | Popular Tags |