KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > jni > Jni


1 package com.quadcap.jni;
2
3 /* Copyright 2000 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.util.ArrayList JavaDoc;
42 import java.util.Collections JavaDoc;
43 import java.util.Comparator JavaDoc;
44 import java.util.HashMap JavaDoc;
45 import java.util.Iterator JavaDoc;
46
47 /**
48  * Interface to native hi-res timers.
49  *
50  * @author Stan Bailes
51  */

52 public class Jni {
53 //#set defs(ENABLE_JNIxx) 1
54
//#autogen begin
55
//#autogen end
56

57     //#ifdef ENABLE_JNI
58
//- static {
59
//- System.loadLibrary("Jni");
60
//- System.out.println("Jni loaded");
61
//- }
62
//- public static native long getHiResTimer();
63
//- public static native long getHiResFrequency();
64
//- static boolean silent = false;
65
//#else
66
public static long getHiResFrequency() {
67         return 1000;
68     }
69     public static long getHiResTimer() {
70         return System.currentTimeMillis();
71     }
72     static boolean silent = true;
73     //#endif
74

75     long start = getHiResTimer();
76
77     static int deferCount = 0;
78     
79     static double F = (double)getHiResFrequency();
80     public static Jni jni = new Jni(null);
81     static String JavaDoc lastStat;
82     
83     public Jni() {
84     }
85
86     String JavaDoc name;
87     
88     public Jni(String JavaDoc name) {
89         this.name = name;
90     }
91
92     public final double next() {
93         //#ifdef ENABLE_JNI
94
//- long mark = getHiResTimer();
95
//- double d = 1000000000.0 * ((double)(mark - start)) / F;
96
//- d = ((double)((long)d)) / 1000.0;
97
//- start = getHiResTimer();
98
//- return d;
99
//#else
100
return 0;
101         //#endif
102
}
103
104     public final static double D() {
105         return jni.next();
106     }
107
108     public final void reset() {
109         //#ifdef ENABLE_JNI
110
//- start = getHiResTimer();
111
//#endif
112
}
113
114     public String JavaDoc toString() {
115         return name + ": " + next();
116     }
117
118     int count = 0;
119
120     final public void dump(String JavaDoc s) {
121         //#ifdef ENABLE_JNI
122
//- long mark = getHiResTimer();
123
//- if (!silent && deferCount-- <= 0) {
124
//- double d = 1000000000.0 * ((double)(mark - start)) / F;
125
//- System.out.println(Thread.currentThread().getName() + ": " +
126
//- name + ": " + formatInterval(d, -1) + " " +
127
//- s + " " + (count++));
128
//- start = getHiResTimer();
129
//- }
130
//#endif
131
}
132
133     static HashMap JavaDoc stats = new HashMap JavaDoc();
134     
135     final public void stat(String JavaDoc s) {
136         //#ifdef ENABLE_JNI
137
//- long mark = getHiResTimer();
138
//- double d = 1000000000.0 * ((double)(mark - start)) / F;
139
//- if (name != null) s = name + ": " + s;
140
//- Stat stat = (Stat)stats.get(s);
141
//- if (stat == null) {
142
//- stat = new Stat(s);
143
//- stats.put(s, stat);
144
//- }
145
//- if (lastStat != null) {
146
//- stat.addPrev(lastStat);
147
//- }
148
//- lastStat = s;
149
//- stat.add(d);
150
//- start = getHiResTimer();
151
//#endif
152
}
153
154     public static String JavaDoc formatInterval(double d, int pad) {
155         String JavaDoc unit = "ns";
156         if (false) {
157             d = ((double)((long)d)) / 1000000.0;
158             unit = "ms";
159         } else {
160             d = ((double)((long)d)) / 1000.0;
161             unit = "us";
162         }
163         long dd = (long)d;
164         String JavaDoc ds = String.valueOf(dd);
165         long df = (long)((d - dd) * 100);
166         String JavaDoc fs = String.valueOf(df);
167         if (fs.length() < 2) fs = "0" + fs;
168         String JavaDoc s = ds + "." + fs + " " + unit;
169         if (pad > 0) {
170             while (s.length() < pad) s = " " + s;
171         }
172         return s;
173     }
174
175     String JavaDoc rpad(String JavaDoc s, int n) {
176         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(s);
177         while (sb.length() < n) sb.append(' ');
178         return sb.toString();
179     }
180
181     class StatCompare implements Comparator JavaDoc {
182         public int compare(Object JavaDoc a, Object JavaDoc b) {
183             double da = ((Stat)a).getTotal();
184             double db = ((Stat)b).getTotal();
185             if (da > db) return -1;
186             if (da < db) return 1;
187             return 0;
188         }
189     }
190
191     final public void clearStats(String JavaDoc x) {
192         if (x == null) {
193             stats = new HashMap JavaDoc();
194         } else {
195             Iterator JavaDoc iter = stats.values().iterator();
196             while (iter.hasNext()) {
197                 Stat s = (Stat)iter.next();
198                 if (s.getName().startsWith(x)) {
199                     iter.remove();
200                 }
201             }
202         }
203         jni.reset();
204     }
205
206     final public void dumpStats(String JavaDoc x) {
207         //#ifdef ENABLE_JNI
208
//- ArrayList ss = new ArrayList();
209
//- ss.addAll(stats.values());
210
//- Collections.sort(ss, new StatCompare());
211
//- if (x == null) {
212
//- System.out.println("ALL Stats:");
213
//- } else {
214
//- System.out.println("Stats for " + x);
215
//- }
216
//- Iterator iter = ss.iterator();
217
//- while (iter.hasNext()) {
218
//- Stat stat = (Stat)iter.next();
219
//- String s = stat.getName();
220
//- if (x == null || s.startsWith(x)) {
221
//- double d = stat.getAdjustedMean();
222
//- double total = stat.getTotal();
223
//- System.out.println(
224
//- formatInterval(total, 14) + "[" +
225
//- formatInterval(d, 12) + "/" + stat.getCount() + "] " +
226
//- rpad(stat.toString(), 66));
227
//- }
228
//- }
229
//#endif
230
}
231
232     public void finalize() throws Throwable JavaDoc {
233         dumpStats(null);
234         super.finalize();
235     }
236
237     //#ifdef ENABLE_JNI
238
//- public static void main(String args[]) {
239
//- Jni t = new Jni("main");
240
//- Object lock = new Object();
241
//- long freq = Jni.getHiResFrequency();
242
//- double F = (double)freq;
243
//- long start = Jni.getHiResTimer();
244
//- for (int i = 0; i < 1000; i++) continue;
245
//- double ticks = (double)(Jni.getHiResTimer() - start);
246
//- double elap = (ticks/freq) * 1000000.0;
247
//- System.out.println("elapsed: " + elap + " us");
248
//- System.out.println("freq: " + (freq / 1000000.0) + " Mhz");
249
//- t.reset();
250
//- for (int i = 0; i < 1000; i++) {
251
//- System.currentTimeMillis();
252
//- t.stat("currentTimeMillis");
253
//- System.currentTimeMillis();
254
//- t.stat("currentTimeMillis");
255
//- System.currentTimeMillis();
256
//- t.stat("currentTimeMillis");
257
//- System.currentTimeMillis();
258
//- t.stat("currentTimeMillis");
259
//- System.currentTimeMillis();
260
//- t.stat("currentTimeMillis");
261
//- synchronized (lock) {
262
//- t.stat("synchronized");
263
//- }
264
//- synchronized (lock) {
265
//- t.stat("synchronized");
266
//- }
267
//- synchronized (lock) {
268
//- t.stat("synchronized");
269
//- }
270
//- synchronized (lock) {
271
//- t.stat("synchronized");
272
//- }
273
//- t.stat("noop");
274
//- t.stat("noop");
275
//- t.stat("noop");
276
//- t.stat("noop");
277
//- }
278
//- t.dumpStats(null);
279
//- }
280
//#endif
281
}
282
Popular Tags