1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.jour.timer;
22
23 import org.apache.log4j.Logger;
24
25
26
27
28
29
30
31
32
33
34
35 /***
36 * Use system high-resolution performance counter.
37 * Base on article http://www.javaworld.com/javaworld/javaqa/2003-01/01-qa-0110-timing.html
38 * @author vlads
39 * @version $Revision: 1.8 $ ($Author: vlads $)
40 */
41 public class NativeTimer implements Timer {
42 /***
43 * The name of the .so or .dll file
44 */
45 public static final String JOUR_LIB = "libJourNative";
46 public static boolean triedToLoadAlredy;
47 public static boolean libraryAvailable;
48 /***
49 * logger.
50 */
51 protected static final Logger log = Logger.getLogger(NativeTimer.class);
52
53 /***
54 * Instantiating utility classes does not make sense.
55 */
56 public NativeTimer() {
57 }
58
59 /***
60 *
61 * @return Returns the current high-resolution performance counter in milliseconds.
62 * If the installed hardware does not support a high-resolution performance counter, this value can be zero.
63 */
64 public static native double getTime();
65
66 public static double getTimeSafe() {
67 try {
68 if (!isAvalabel()) {
69 return System.currentTimeMillis();
70 }
71 return getTime();
72 } catch (UnsatisfiedLinkError e) {
73 log.error("Error", e);
74 return System.currentTimeMillis();
75 } catch (RuntimeException e) {
76 log.error("Error", e);
77 return System.currentTimeMillis();
78 }
79
80 }
81
82 public double getTimeCounter() {
83 return getTime();
84 }
85
86 private static native double setSystemTime(long millis);
87
88 public static boolean isAvalabel() {
89 if (triedToLoadAlredy) {
90 return libraryAvailable;
91 }
92 try {
93 System.loadLibrary(JOUR_LIB);
94 setSystemTime(System.currentTimeMillis());
95 libraryAvailable = true;
96 } catch (UnsatisfiedLinkError e) {
97 log.error(e);
98 log.info("java.library.path=" + System.getProperty ("java.library.path"));
99 libraryAvailable = false;
100 }
101 triedToLoadAlredy = true;
102 return libraryAvailable;
103 }
104
105 static {
106 isAvalabel();
107 }
108
109 }