org/objectweb/proactive/core/util/timer/MicroTimer.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.util.timer;
00032 
00033 public class MicroTimer implements Timeable {
00034     private static boolean nativeMode;
00035 
00036     static {
00037         try {
00038             System.loadLibrary("MicroTimer");
00039             nativeMode = true;
00040         } catch (Throwable e) {
00041             e.printStackTrace();
00042             System.err.println(
00043                 "WARNING: couldn't load native lib, falling back to milliseconds");
00044             nativeMode = false;
00045         }
00046     }
00047 
00048     private long[] startTime;
00049     private long[] endTime;
00050 
00051     public native long[] currentTime();
00052 
00053     public long startTime2;
00054     public long endTime2;
00055 
00056     public MicroTimer() {
00057     }
00058 
00059     public void start() {
00060         if (nativeMode) {
00061             this.startTime = currentTime();
00062             this.endTime = currentTime();
00063         } else {
00064             this.startTime2 = System.currentTimeMillis();
00065             this.endTime2 = this.startTime2;
00066         }
00067     }
00068 
00073     public void stop() {
00074         if (MicroTimer.nativeMode) {
00075             this.endTime = currentTime();
00076         } else {
00077             this.endTime2 = System.currentTimeMillis();
00078         }
00079     }
00080 
00086     public long getCumulatedTime() {
00087         if (nativeMode) {
00088             long[] tmp = this.updateCumulatedTime(startTime, endTime); //this.stop();
00089             return (tmp[0] * 1000000) + tmp[1];
00090         } else {
00091             return (endTime2 - startTime2);
00092         }
00093     }
00094 
00095     protected long[] updateCumulatedTime(long[] t1, long[] t2) {
00096         long[] tmp = new long[2]; // this.cumulatedTime;
00097         tmp[0] = t2[0] - t1[0];
00098 
00099         if ((t2[1] - t1[1]) < 0) {
00100             //one second has gone
00101             tmp[0]--;
00102             tmp[1] = (t2[1] + 1000000) - t1[1];
00103         } else {
00104             tmp[1] += (t2[1] - t1[1]);
00105         }
00106         return tmp;
00107     }
00108 
00109     public String getUnit() {
00110         if (nativeMode) {
00111             return "micros";
00112         } else {
00113             return "millis";
00114         }
00115     }
00116 
00117     public static void main(String[] args) {
00118         MicroTimer micro = new MicroTimer();
00119         System.out.println("Test starting...");
00120         micro.start();
00121         try {
00122             Thread.sleep(1000);
00123         } catch (InterruptedException e) {
00124             e.printStackTrace();
00125         }
00126         micro.stop();
00127         System.out.println("After 1000ms : " + micro.getCumulatedTime() +
00128             micro.getUnit());
00129     }
00130 }

Generated on Mon Jan 22 15:16:10 2007 for ProActive by  doxygen 1.5.1