KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > tools > internal > NativeStats


1 /*******************************************************************************
2  * Copyright (c) 2004 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.swt.tools.internal;
12
13 import java.io.PrintStream JavaDoc;
14 import java.lang.reflect.Method JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.Hashtable JavaDoc;
18
19 /**
20  * Instructions on how to use the NativeStats tool with a standlaone SWT example:
21  *
22  * 1) Compile the SWT native libraries defining the NATIVE_STATS flag (i.e. uncomment line in makefile).
23  * 2) Add the following code around the sections of interest to dump the
24  * native calls done in that section.
25  *
26  * NativeStats stats = new NativeStats();
27  * ...
28  * <code section>
29  * ...
30  * stats.dumpDiff(System.out);
31  *
32  * 3) Or add the following code at a given point to dump a snapshot of
33  * the native calls done until that point.
34  *
35  * new NativeStats().dumpSnapshot(System.out);
36  */

37 public class NativeStats {
38     
39     Hashtable JavaDoc snapshot;
40     
41     final static String JavaDoc[] classes = new String JavaDoc[]{"OS", "ATK", "CDE", "GNOME", "GTK", "XPCOM", "COM"};
42
43     
44     public static class NativeFunction implements Comparable JavaDoc {
45         String JavaDoc name;
46         int callCount;
47         
48     public NativeFunction(String JavaDoc 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 JavaDoc getName() {
62         return name;
63     }
64     public int compareTo(Object JavaDoc func) {
65         return ((NativeFunction)func).callCount - callCount;
66     }
67     }
68     
69 public NativeStats() {
70     snapshot = snapshot();
71 }
72     
73 public Hashtable JavaDoc diff() {
74     Hashtable JavaDoc newSnapshot = snapshot();
75     Enumeration JavaDoc keys = newSnapshot.keys();
76     while (keys.hasMoreElements()) {
77         String JavaDoc className = (String JavaDoc)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 JavaDoc ps) {
90     dump(diff(), ps);
91 }
92
93 public void dumpSnapshot(PrintStream JavaDoc ps) {
94     dump(snapshot(), ps);
95 }
96
97 public void dumpSnapshot(String JavaDoc className, PrintStream JavaDoc ps) {
98     Hashtable JavaDoc snapshot = new Hashtable JavaDoc();
99     snapshot(className, snapshot);
100     dump(className, (NativeFunction[])snapshot.get(className), ps);
101 }
102
103 public void dump(Hashtable JavaDoc snapshot, PrintStream JavaDoc ps) {
104     Enumeration JavaDoc keys = snapshot.keys();
105     while (keys.hasMoreElements()) {
106         String JavaDoc className = (String JavaDoc)keys.nextElement();
107         dump(className, (NativeFunction[])snapshot.get(className), ps);
108     }
109 }
110     
111 void dump(String JavaDoc className, NativeFunction[] funcs, PrintStream JavaDoc 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 JavaDoc snapshot() {
140     Hashtable JavaDoc snapshot = new Hashtable JavaDoc();
141     for (int i = 0; i < classes.length; i++) {
142         String JavaDoc className = classes[i];
143         snapshot(className, snapshot);
144     }
145     return snapshot;
146 }
147
148 public Hashtable JavaDoc snapshot(String JavaDoc className, Hashtable JavaDoc snapshot) {
149     try {
150         Class JavaDoc clazz = getClass();
151         Method JavaDoc functionCount = clazz.getMethod(className + "_GetFunctionCount", new Class JavaDoc[0]);
152         Method JavaDoc functionCallCount = clazz.getMethod(className + "_GetFunctionCallCount", new Class JavaDoc[]{int.class});
153         Method JavaDoc functionName = clazz.getMethod(className + "_GetFunctionName", new Class JavaDoc[]{int.class});
154         int count = ((Integer JavaDoc)functionCount.invoke(clazz, new Object JavaDoc[0])).intValue();
155         NativeFunction[] funcs = new NativeFunction[count];
156         Object JavaDoc[] index = new Object JavaDoc[1];
157         for (int i = 0; i < count; i++) {
158             index[0] = new Integer JavaDoc(i);
159             int callCount = ((Integer JavaDoc)functionCallCount.invoke(clazz, index)).intValue();
160             String JavaDoc name = (String JavaDoc)functionName.invoke(clazz, index);
161             funcs[i] = new NativeFunction(name, callCount);
162         }
163         snapshot.put(className, funcs);
164     } catch (Throwable JavaDoc e) {
165 // e.printStackTrace(System.out);
166
}
167     return snapshot;
168 }
169     
170 public static final native int OS_GetFunctionCount();
171 public static final native String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc COM_GetFunctionName(int index);
196 public static final native int COM_GetFunctionCallCount(int index);
197
198 }
199
Popular Tags