1 9 package javolution.realtime; 10 11 import java.io.InputStream; 12 import java.io.OutputStream; 13 import java.io.PrintStream; 14 15 import javolution.JavolutionError; 16 import javolution.io.Utf8StreamReader; 17 import javolution.io.Utf8StreamWriter; 18 import javolution.lang.Reflection; 19 import javolution.lang.TextBuilder; 20 import javolution.lang.TypeFormat; 21 22 import j2me.io.File; 23 import j2me.io.FileInputStream; 24 import j2me.io.FileNotFoundException; 25 import j2me.io.FileOutputStream; 26 import j2me.lang.IllegalStateException; 27 28 67 public final class AllocationProfile { 68 69 72 private static final Utf8StreamReader READER = new Utf8StreamReader(); 73 74 77 private static final Utf8StreamWriter WRITER = new Utf8StreamWriter(); 78 79 82 private static final TextBuilder ClassName = new TextBuilder(); 83 84 87 private static final TextBuilder Counter = new TextBuilder(); 88 89 92 volatile static Runnable OverflowHandler; 93 94 97 private AllocationProfile() { 98 } 99 100 106 public static void setEnabled(boolean isEnabled) { 107 ObjectFactory.IsAllocationProfileEnabled = isEnabled; 108 } 109 110 116 public static boolean isEnabled() { 117 return ObjectFactory.IsAllocationProfileEnabled; 118 } 119 120 127 public static void load() { 128 File file = new File("profile.txt"); 129 if (file.exists()) { 130 try { 131 load(new FileInputStream(file)); 132 } catch (FileNotFoundException e) { 133 throw new JavolutionError(e); 134 } 135 } else { 136 ObjectFactory.IsAllocationProfileEnabled = true; 137 System.err.println("No initial allocation profile."); 138 } 139 } 140 141 150 public static synchronized void load(InputStream in) { 151 try { 152 READER.setInputStream(in); 153 for (int c = READER.read(); c >= 0; c = READER.read()) { 154 if (c <= ' ') 155 continue; 157 ClassName.append((char) c); 159 for (c = READER.read(); c > ' '; c = READER.read()) { 160 ClassName.append((char) c); 161 } 162 163 while ((c >= 0) && (c != '\n') && (c <= ' ')) { 165 c = READER.read(); 166 } 167 if (c > ' ') { Counter.append((char) c); 169 for (c = READER.read(); c > ' '; c = READER.read()) { 170 Counter.append((char) c); 171 } 172 } 173 174 String factoryName = ClassName.toString(); 176 Class factoryClass = Reflection.getClass(factoryName); 177 ClassName.reset(); 178 179 int sep = factoryName.lastIndexOf('$'); 181 Reflection.getClass(factoryName.substring(0, sep)); 182 183 ObjectFactory factory = null; 185 for (int j = 0; j < ObjectFactory.Count; j++) { 186 if (ObjectFactory.INSTANCES[j].getClass() == factoryClass) { 187 factory = ObjectFactory.INSTANCES[j]; 188 break; 189 } 190 } 191 192 if (factory != null) { 194 if (Counter.length() > 0) { 195 factory._preallocatedCount = TypeFormat 196 .parseInt(Counter); 197 Counter.reset(); 198 } 199 } else { 200 throw new Error("Factory class: " + factoryClass 201 + " not found"); 202 } 203 } 204 } catch (Throwable e) { 205 throw new JavolutionError(e); 206 } finally { 207 READER.reset(); 208 ClassName.reset(); 209 Counter.reset(); 210 ObjectFactory.IsAllocationProfileEnabled = true; 211 } 212 } 213 214 221 public static void save() { 222 File file = new File("profile.txt"); 223 try { 224 save(new FileOutputStream(file)); 225 } catch (FileNotFoundException e) { 226 throw new JavolutionError(e); 227 } 228 } 229 230 237 public static synchronized void save(OutputStream out) { 238 if (!ObjectFactory.IsAllocationProfileEnabled) { 239 throw new IllegalStateException("Allocation profiling disabled"); 240 } 241 try { 242 WRITER.setOutputStream(out); 243 for (int i = 0; i < ObjectFactory.Count; i++) { 244 ObjectFactory factory = ObjectFactory.INSTANCES[i]; 245 WRITER.write(factory.getClass().getName()); 246 WRITER.write(' '); 247 int maxCount = factory._allocatedCount > factory._preallocatedCount ? factory._allocatedCount 248 : factory._preallocatedCount; 249 Counter.append(maxCount); 250 WRITER.write(Counter); 251 Counter.reset(); 252 WRITER.write('\n'); 253 } 254 WRITER.close(); 255 } catch (Throwable e) { 256 throw new JavolutionError(e); 257 } finally { 258 WRITER.reset(); 259 Counter.reset(); 260 } 261 } 262 263 270 public static synchronized void preallocate() { 271 if (ObjectFactory.IsAllocationProfileEnabled) { 272 for (int i = 0; i < ObjectFactory.Count;) { 273 ObjectFactory.INSTANCES[i++].preallocate(); 274 } 275 } else { 276 throw new IllegalStateException("Allocation profiling disabled"); 277 } 278 } 279 280 287 public static void reset() { 288 if (ObjectFactory.IsAllocationProfileEnabled) { 289 for (int i = 0; i < ObjectFactory.Count;) { 290 ObjectFactory.INSTANCES[i++].reset(); 291 } 292 } else { 293 throw new IllegalStateException("Allocation profiling disabled"); 294 } 295 } 296 297 301 public static void setOverflowHandler(Runnable handler) { 302 AllocationProfile.OverflowHandler = handler; 303 } 304 305 310 public static void print(PrintStream out) { 311 synchronized (out) { 312 if (ObjectFactory.IsAllocationProfileEnabled) { 313 out.println("Current Allocation Profile:"); 314 for (int i = 0; i < ObjectFactory.Count;) { 315 ObjectFactory factory = ObjectFactory.INSTANCES[i++]; 316 if (factory._allocatedCount == 0) 317 continue; 318 out.println(factory.getClass().getName() 319 + " has allocated " + factory._allocatedCount + " " 320 + factory._productClass.getName() + " out of " 321 + factory._preallocatedCount); 322 323 } 324 } else { 325 out.print("Allocation profiling disabled"); 326 } 327 out.println(); 328 } 329 } 330 331 } | Popular Tags |