Quadcap Embeddable Database

com/quadcap/jni/Jni.java

Go to the documentation of this file.
00001 package com.quadcap.jni; 00002 00003 /* Copyright 2000 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.util.ArrayList; 00042 import java.util.Collections; 00043 import java.util.Comparator; 00044 import java.util.HashMap; 00045 import java.util.Iterator; 00046 00047 /** 00048 * Interface to native hi-res timers. 00049 * 00050 * @author Stan Bailes 00051 */ 00052 public class Jni { 00053 //#set defs(ENABLE_JNIxx) 1 00054 //#autogen begin 00055 //#autogen end 00056 00057 //#ifdef ENABLE_JNI 00058 //- static { 00059 //- System.loadLibrary("Jni"); 00060 //- System.out.println("Jni loaded"); 00061 //- } 00062 //- public static native long getHiResTimer(); 00063 //- public static native long getHiResFrequency(); 00064 //- static boolean silent = false; 00065 //#else 00066 public static long getHiResFrequency() { 00067 return 1000; 00068 } 00069 public static long getHiResTimer() { 00070 return System.currentTimeMillis(); 00071 } 00072 static boolean silent = true; 00073 //#endif 00074 00075 long start = getHiResTimer(); 00076 00077 static int deferCount = 0; 00078 00079 static double F = (double)getHiResFrequency(); 00080 public static Jni jni = new Jni(null); 00081 static String lastStat; 00082 00083 public Jni() { 00084 } 00085 00086 String name; 00087 00088 public Jni(String name) { 00089 this.name = name; 00090 } 00091 00092 public final double next() { 00093 //#ifdef ENABLE_JNI 00094 //- long mark = getHiResTimer(); 00095 //- double d = 1000000000.0 * ((double)(mark - start)) / F; 00096 //- d = ((double)((long)d)) / 1000.0; 00097 //- start = getHiResTimer(); 00098 //- return d; 00099 //#else 00100 return 0; 00101 //#endif 00102 } 00103 00104 public final static double D() { 00105 return jni.next(); 00106 } 00107 00108 public final void reset() { 00109 //#ifdef ENABLE_JNI 00110 //- start = getHiResTimer(); 00111 //#endif 00112 } 00113 00114 public String toString() { 00115 return name + ": " + next(); 00116 } 00117 00118 int count = 0; 00119 00120 final public void dump(String s) { 00121 //#ifdef ENABLE_JNI 00122 //- long mark = getHiResTimer(); 00123 //- if (!silent && deferCount-- <= 0) { 00124 //- double d = 1000000000.0 * ((double)(mark - start)) / F; 00125 //- System.out.println(Thread.currentThread().getName() + ": " + 00126 //- name + ": " + formatInterval(d, -1) + " " + 00127 //- s + " " + (count++)); 00128 //- start = getHiResTimer(); 00129 //- } 00130 //#endif 00131 } 00132 00133 static HashMap stats = new HashMap(); 00134 00135 final public void stat(String s) { 00136 //#ifdef ENABLE_JNI 00137 //- long mark = getHiResTimer(); 00138 //- double d = 1000000000.0 * ((double)(mark - start)) / F; 00139 //- if (name != null) s = name + ": " + s; 00140 //- Stat stat = (Stat)stats.get(s); 00141 //- if (stat == null) { 00142 //- stat = new Stat(s); 00143 //- stats.put(s, stat); 00144 //- } 00145 //- if (lastStat != null) { 00146 //- stat.addPrev(lastStat); 00147 //- } 00148 //- lastStat = s; 00149 //- stat.add(d); 00150 //- start = getHiResTimer(); 00151 //#endif 00152 } 00153 00154 public static String formatInterval(double d, int pad) { 00155 String unit = "ns"; 00156 if (false) { 00157 d = ((double)((long)d)) / 1000000.0; 00158 unit = "ms"; 00159 } else { 00160 d = ((double)((long)d)) / 1000.0; 00161 unit = "us"; 00162 } 00163 long dd = (long)d; 00164 String ds = String.valueOf(dd); 00165 long df = (long)((d - dd) * 100); 00166 String fs = String.valueOf(df); 00167 if (fs.length() < 2) fs = "0" + fs; 00168 String s = ds + "." + fs + " " + unit; 00169 if (pad > 0) { 00170 while (s.length() < pad) s = " " + s; 00171 } 00172 return s; 00173 } 00174 00175 String rpad(String s, int n) { 00176 StringBuffer sb = new StringBuffer(s); 00177 while (sb.length() < n) sb.append(' '); 00178 return sb.toString(); 00179 } 00180 00181 class StatCompare implements Comparator { 00182 public int compare(Object a, Object b) { 00183 double da = ((Stat)a).getTotal(); 00184 double db = ((Stat)b).getTotal(); 00185 if (da > db) return -1; 00186 if (da < db) return 1; 00187 return 0; 00188 } 00189 } 00190 00191 final public void clearStats(String x) { 00192 if (x == null) { 00193 stats = new HashMap(); 00194 } else { 00195 Iterator iter = stats.values().iterator(); 00196 while (iter.hasNext()) { 00197 Stat s = (Stat)iter.next(); 00198 if (s.getName().startsWith(x)) { 00199 iter.remove(); 00200 } 00201 } 00202 } 00203 jni.reset(); 00204 } 00205 00206 final public void dumpStats(String x) { 00207 //#ifdef ENABLE_JNI 00208 //- ArrayList ss = new ArrayList(); 00209 //- ss.addAll(stats.values()); 00210 //- Collections.sort(ss, new StatCompare()); 00211 //- if (x == null) { 00212 //- System.out.println("ALL Stats:"); 00213 //- } else { 00214 //- System.out.println("Stats for " + x); 00215 //- } 00216 //- Iterator iter = ss.iterator(); 00217 //- while (iter.hasNext()) { 00218 //- Stat stat = (Stat)iter.next(); 00219 //- String s = stat.getName(); 00220 //- if (x == null || s.startsWith(x)) { 00221 //- double d = stat.getAdjustedMean(); 00222 //- double total = stat.getTotal(); 00223 //- System.out.println( 00224 //- formatInterval(total, 14) + "[" + 00225 //- formatInterval(d, 12) + "/" + stat.getCount() + "] " + 00226 //- rpad(stat.toString(), 66)); 00227 //- } 00228 //- } 00229 //#endif 00230 } 00231 00232 public void finalize() throws Throwable { 00233 dumpStats(null); 00234 super.finalize(); 00235 } 00236 00237 //#ifdef ENABLE_JNI 00238 //- public static void main(String args[]) { 00239 //- Jni t = new Jni("main"); 00240 //- Object lock = new Object(); 00241 //- long freq = Jni.getHiResFrequency(); 00242 //- double F = (double)freq; 00243 //- long start = Jni.getHiResTimer(); 00244 //- for (int i = 0; i < 1000; i++) continue; 00245 //- double ticks = (double)(Jni.getHiResTimer() - start); 00246 //- double elap = (ticks/freq) * 1000000.0; 00247 //- System.out.println("elapsed: " + elap + " us"); 00248 //- System.out.println("freq: " + (freq / 1000000.0) + " Mhz"); 00249 //- t.reset(); 00250 //- for (int i = 0; i < 1000; i++) { 00251 //- System.currentTimeMillis(); 00252 //- t.stat("currentTimeMillis"); 00253 //- System.currentTimeMillis(); 00254 //- t.stat("currentTimeMillis"); 00255 //- System.currentTimeMillis(); 00256 //- t.stat("currentTimeMillis"); 00257 //- System.currentTimeMillis(); 00258 //- t.stat("currentTimeMillis"); 00259 //- System.currentTimeMillis(); 00260 //- t.stat("currentTimeMillis"); 00261 //- synchronized (lock) { 00262 //- t.stat("synchronized"); 00263 //- } 00264 //- synchronized (lock) { 00265 //- t.stat("synchronized"); 00266 //- } 00267 //- synchronized (lock) { 00268 //- t.stat("synchronized"); 00269 //- } 00270 //- synchronized (lock) { 00271 //- t.stat("synchronized"); 00272 //- } 00273 //- t.stat("noop"); 00274 //- t.stat("noop"); 00275 //- t.stat("noop"); 00276 //- t.stat("noop"); 00277 //- } 00278 //- t.dumpStats(null); 00279 //- } 00280 //#endif 00281 }