View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.timer;
22  
23  import org.apache.log4j.Logger;
24  //import org.apache.commons.logging.Log;
25  //import org.apache.commons.logging.LogFactory;
26  
27  /*
28   * Created on 17.11.2004
29   *
30   * Contributing Author(s):
31   *
32   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
33   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
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 }