1 11 package org.eclipse.swt.tools.internal; 12 13 import java.io.PrintStream ; 14 import java.lang.reflect.Method ; 15 import java.util.Arrays ; 16 import java.util.Enumeration ; 17 import java.util.Hashtable ; 18 19 37 public class NativeStats { 38 39 Hashtable snapshot; 40 41 final static String [] classes = new String []{"OS", "ATK", "CDE", "GNOME", "GTK", "XPCOM", "COM"}; 42 43 44 public static class NativeFunction implements Comparable { 45 String name; 46 int callCount; 47 48 public NativeFunction(String name, int callCount) { 49 this.name = name; 50 this.callCount = callCount; 51 } 52 53 void subtract(NativeFunction func) { 54 this.callCount -= func.callCount; 55 } 56 57 public int getCallCount() { 58 return callCount; 59 } 60 61 public String getName() { 62 return name; 63 } 64 public int compareTo(Object func) { 65 return ((NativeFunction)func).callCount - callCount; 66 } 67 } 68 69 public NativeStats() { 70 snapshot = snapshot(); 71 } 72 73 public Hashtable diff() { 74 Hashtable newSnapshot = snapshot(); 75 Enumeration keys = newSnapshot.keys(); 76 while (keys.hasMoreElements()) { 77 String className = (String )keys.nextElement(); 78 NativeFunction[] newFuncs = (NativeFunction[])newSnapshot.get(className); 79 NativeFunction[] funcs = (NativeFunction[])snapshot.get(className); 80 if (funcs != null) { 81 for (int i = 0; i < newFuncs.length; i++) { 82 newFuncs[i].subtract(funcs[i]); 83 } 84 } 85 } 86 return newSnapshot; 87 } 88 89 public void dumpDiff(PrintStream ps) { 90 dump(diff(), ps); 91 } 92 93 public void dumpSnapshot(PrintStream ps) { 94 dump(snapshot(), ps); 95 } 96 97 public void dumpSnapshot(String className, PrintStream ps) { 98 Hashtable snapshot = new Hashtable (); 99 snapshot(className, snapshot); 100 dump(className, (NativeFunction[])snapshot.get(className), ps); 101 } 102 103 public void dump(Hashtable snapshot, PrintStream ps) { 104 Enumeration keys = snapshot.keys(); 105 while (keys.hasMoreElements()) { 106 String className = (String )keys.nextElement(); 107 dump(className, (NativeFunction[])snapshot.get(className), ps); 108 } 109 } 110 111 void dump(String className, NativeFunction[] funcs, PrintStream ps) { 112 if (funcs == null) return; 113 Arrays.sort(funcs); 114 int total = 0; 115 for (int i = 0; i < funcs.length; i++) { 116 NativeFunction func = funcs[i]; 117 total += func.getCallCount(); 118 } 119 ps.print(className); 120 ps.print("="); 121 ps.print(total); 122 ps.println(); 123 for (int i = 0; i < funcs.length; i++) { 124 NativeFunction func = funcs[i]; 125 if (func.getCallCount() > 0) { 126 ps.print("\t"); 127 ps.print(func.getName()); 128 ps.print("="); 129 ps.print(func.getCallCount()); 130 ps.println(); 131 } 132 } 133 } 134 135 public void reset() { 136 snapshot = snapshot(); 137 } 138 139 public Hashtable snapshot() { 140 Hashtable snapshot = new Hashtable (); 141 for (int i = 0; i < classes.length; i++) { 142 String className = classes[i]; 143 snapshot(className, snapshot); 144 } 145 return snapshot; 146 } 147 148 public Hashtable snapshot(String className, Hashtable snapshot) { 149 try { 150 Class clazz = getClass(); 151 Method functionCount = clazz.getMethod(className + "_GetFunctionCount", new Class [0]); 152 Method functionCallCount = clazz.getMethod(className + "_GetFunctionCallCount", new Class []{int.class}); 153 Method functionName = clazz.getMethod(className + "_GetFunctionName", new Class []{int.class}); 154 int count = ((Integer )functionCount.invoke(clazz, new Object [0])).intValue(); 155 NativeFunction[] funcs = new NativeFunction[count]; 156 Object [] index = new Object [1]; 157 for (int i = 0; i < count; i++) { 158 index[0] = new Integer (i); 159 int callCount = ((Integer )functionCallCount.invoke(clazz, index)).intValue(); 160 String name = (String )functionName.invoke(clazz, index); 161 funcs[i] = new NativeFunction(name, callCount); 162 } 163 snapshot.put(className, funcs); 164 } catch (Throwable e) { 165 } 167 return snapshot; 168 } 169 170 public static final native int OS_GetFunctionCount(); 171 public static final native String OS_GetFunctionName(int index); 172 public static final native int OS_GetFunctionCallCount(int index); 173 174 public static final native int ATK_GetFunctionCount(); 175 public static final native String ATK_GetFunctionName(int index); 176 public static final native int ATK_GetFunctionCallCount(int index); 177 178 public static final native int CDE_GetFunctionCount(); 179 public static final native String CDE_GetFunctionName(int index); 180 public static final native int CDE_GetFunctionCallCount(int index); 181 182 public static final native int GNOME_GetFunctionCount(); 183 public static final native String GNOME_GetFunctionName(int index); 184 public static final native int GNOME_GetFunctionCallCount(int index); 185 186 public static final native int GTK_GetFunctionCount(); 187 public static final native String GTK_GetFunctionName(int index); 188 public static final native int GTK_GetFunctionCallCount(int index); 189 190 public static final native int XPCOM_GetFunctionCount(); 191 public static final native String XPCOM_GetFunctionName(int index); 192 public static final native int XPCOM_GetFunctionCallCount(int index); 193 194 public static final native int COM_GetFunctionCount(); 195 public static final native String COM_GetFunctionName(int index); 196 public static final native int COM_GetFunctionCallCount(int index); 197 198 } 199 | Popular Tags |