00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package org.objectweb.proactive.calcium.statistics;
00032
00033 import java.io.Serializable;
00034 import java.lang.management.ManagementFactory;
00035 import java.lang.management.ThreadMXBean;
00036
00037 public class Timer implements Serializable{
00038
00039 long t, accumulated;
00040 int numberActivatedTimes;
00041 boolean cpuTime;
00042
00043 public Timer(boolean useCPUTime){
00044 cpuTime=useCPUTime;
00045 reset();
00046 }
00047
00048 public Timer() {
00049 this(false);
00050 }
00051
00056 public void start() {
00057 reset();
00058 }
00059
00064 public long stop(){
00065 if(t>0)
00066 accumulated+=getCurrentTime() - t;
00067 t=-1;
00068 return accumulated;
00069 }
00070
00076 public void resume(){
00077 t=getCurrentTime();
00078 numberActivatedTimes++;
00079 }
00080
00084 private void reset() {
00085 t = getCurrentTime();
00086 accumulated=0;
00087 numberActivatedTimes=1;
00088 }
00089
00093 public long getTime(){
00094 if(t>0) return accumulated + getCurrentTime() - t;
00095
00096 return accumulated;
00097 }
00098
00103 public int getNumberOfActivatedTimes() {
00104 return numberActivatedTimes;
00105 }
00106
00107 private long getCurrentTime(){
00108
00109 if(!cpuTime) return System.currentTimeMillis();
00110
00111 ThreadMXBean tmb=ManagementFactory.getThreadMXBean();
00112 if(tmb.isThreadCpuTimeSupported()){
00113
00114 if(!tmb.isThreadCpuTimeEnabled()){
00115 tmb.setThreadCpuTimeEnabled(true);
00116 }
00117 return tmb.getCurrentThreadCpuTime()/1000000;
00118 }
00119
00120 return System.currentTimeMillis();
00121 }
00122 }