001: package com.quadcap.jni;
002:
003: /* Copyright 2000 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.util.ArrayList;
042: import java.util.Collections;
043: import java.util.Comparator;
044: import java.util.HashMap;
045: import java.util.Iterator;
046:
047: /**
048: * Interface to native hi-res timers.
049: *
050: * @author Stan Bailes
051: */
052: public class Jni {
053: //#set defs(ENABLE_JNIxx) 1
054: //#autogen begin
055: //#autogen end
056:
057: //#ifdef ENABLE_JNI
058: //- static {
059: //- System.loadLibrary("Jni");
060: //- System.out.println("Jni loaded");
061: //- }
062: //- public static native long getHiResTimer();
063: //- public static native long getHiResFrequency();
064: //- static boolean silent = false;
065: //#else
066: public static long getHiResFrequency() {
067: return 1000;
068: }
069:
070: public static long getHiResTimer() {
071: return System.currentTimeMillis();
072: }
073:
074: static boolean silent = true;
075: //#endif
076:
077: long start = getHiResTimer();
078:
079: static int deferCount = 0;
080:
081: static double F = (double) getHiResFrequency();
082: public static Jni jni = new Jni(null);
083: static String lastStat;
084:
085: public Jni() {
086: }
087:
088: String name;
089:
090: public Jni(String name) {
091: this .name = name;
092: }
093:
094: public final double next() {
095: //#ifdef ENABLE_JNI
096: //- long mark = getHiResTimer();
097: //- double d = 1000000000.0 * ((double)(mark - start)) / F;
098: //- d = ((double)((long)d)) / 1000.0;
099: //- start = getHiResTimer();
100: //- return d;
101: //#else
102: return 0;
103: //#endif
104: }
105:
106: public final static double D() {
107: return jni.next();
108: }
109:
110: public final void reset() {
111: //#ifdef ENABLE_JNI
112: //- start = getHiResTimer();
113: //#endif
114: }
115:
116: public String toString() {
117: return name + ": " + next();
118: }
119:
120: int count = 0;
121:
122: final public void dump(String s) {
123: //#ifdef ENABLE_JNI
124: //- long mark = getHiResTimer();
125: //- if (!silent && deferCount-- <= 0) {
126: //- double d = 1000000000.0 * ((double)(mark - start)) / F;
127: //- System.out.println(Thread.currentThread().getName() + ": " +
128: //- name + ": " + formatInterval(d, -1) + " " +
129: //- s + " " + (count++));
130: //- start = getHiResTimer();
131: //- }
132: //#endif
133: }
134:
135: static HashMap stats = new HashMap();
136:
137: final public void stat(String s) {
138: //#ifdef ENABLE_JNI
139: //- long mark = getHiResTimer();
140: //- double d = 1000000000.0 * ((double)(mark - start)) / F;
141: //- if (name != null) s = name + ": " + s;
142: //- Stat stat = (Stat)stats.get(s);
143: //- if (stat == null) {
144: //- stat = new Stat(s);
145: //- stats.put(s, stat);
146: //- }
147: //- if (lastStat != null) {
148: //- stat.addPrev(lastStat);
149: //- }
150: //- lastStat = s;
151: //- stat.add(d);
152: //- start = getHiResTimer();
153: //#endif
154: }
155:
156: public static String formatInterval(double d, int pad) {
157: String unit = "ns";
158: if (false) {
159: d = ((double) ((long) d)) / 1000000.0;
160: unit = "ms";
161: } else {
162: d = ((double) ((long) d)) / 1000.0;
163: unit = "us";
164: }
165: long dd = (long) d;
166: String ds = String.valueOf(dd);
167: long df = (long) ((d - dd) * 100);
168: String fs = String.valueOf(df);
169: if (fs.length() < 2)
170: fs = "0" + fs;
171: String s = ds + "." + fs + " " + unit;
172: if (pad > 0) {
173: while (s.length() < pad)
174: s = " " + s;
175: }
176: return s;
177: }
178:
179: String rpad(String s, int n) {
180: StringBuffer sb = new StringBuffer(s);
181: while (sb.length() < n)
182: sb.append(' ');
183: return sb.toString();
184: }
185:
186: class StatCompare implements Comparator {
187: public int compare(Object a, Object b) {
188: double da = ((Stat) a).getTotal();
189: double db = ((Stat) b).getTotal();
190: if (da > db)
191: return -1;
192: if (da < db)
193: return 1;
194: return 0;
195: }
196: }
197:
198: final public void clearStats(String x) {
199: if (x == null) {
200: stats = new HashMap();
201: } else {
202: Iterator iter = stats.values().iterator();
203: while (iter.hasNext()) {
204: Stat s = (Stat) iter.next();
205: if (s.getName().startsWith(x)) {
206: iter.remove();
207: }
208: }
209: }
210: jni.reset();
211: }
212:
213: final public void dumpStats(String x) {
214: //#ifdef ENABLE_JNI
215: //- ArrayList ss = new ArrayList();
216: //- ss.addAll(stats.values());
217: //- Collections.sort(ss, new StatCompare());
218: //- if (x == null) {
219: //- System.out.println("ALL Stats:");
220: //- } else {
221: //- System.out.println("Stats for " + x);
222: //- }
223: //- Iterator iter = ss.iterator();
224: //- while (iter.hasNext()) {
225: //- Stat stat = (Stat)iter.next();
226: //- String s = stat.getName();
227: //- if (x == null || s.startsWith(x)) {
228: //- double d = stat.getAdjustedMean();
229: //- double total = stat.getTotal();
230: //- System.out.println(
231: //- formatInterval(total, 14) + "[" +
232: //- formatInterval(d, 12) + "/" + stat.getCount() + "] " +
233: //- rpad(stat.toString(), 66));
234: //- }
235: //- }
236: //#endif
237: }
238:
239: public void finalize() throws Throwable {
240: dumpStats(null);
241: super .finalize();
242: }
243:
244: //#ifdef ENABLE_JNI
245: //- public static void main(String args[]) {
246: //- Jni t = new Jni("main");
247: //- Object lock = new Object();
248: //- long freq = Jni.getHiResFrequency();
249: //- double F = (double)freq;
250: //- long start = Jni.getHiResTimer();
251: //- for (int i = 0; i < 1000; i++) continue;
252: //- double ticks = (double)(Jni.getHiResTimer() - start);
253: //- double elap = (ticks/freq) * 1000000.0;
254: //- System.out.println("elapsed: " + elap + " us");
255: //- System.out.println("freq: " + (freq / 1000000.0) + " Mhz");
256: //- t.reset();
257: //- for (int i = 0; i < 1000; i++) {
258: //- System.currentTimeMillis();
259: //- t.stat("currentTimeMillis");
260: //- System.currentTimeMillis();
261: //- t.stat("currentTimeMillis");
262: //- System.currentTimeMillis();
263: //- t.stat("currentTimeMillis");
264: //- System.currentTimeMillis();
265: //- t.stat("currentTimeMillis");
266: //- System.currentTimeMillis();
267: //- t.stat("currentTimeMillis");
268: //- synchronized (lock) {
269: //- t.stat("synchronized");
270: //- }
271: //- synchronized (lock) {
272: //- t.stat("synchronized");
273: //- }
274: //- synchronized (lock) {
275: //- t.stat("synchronized");
276: //- }
277: //- synchronized (lock) {
278: //- t.stat("synchronized");
279: //- }
280: //- t.stat("noop");
281: //- t.stat("noop");
282: //- t.stat("noop");
283: //- t.stat("noop");
284: //- }
285: //- t.dumpStats(null);
286: //- }
287: //#endif
288: }
|