MOA 12.03
Real Time Analytics for Data Streams
|
00001 /* 00002 * TimingUtils.java 00003 * Copyright (C) 2007 University of Waikato, Hamilton, New Zealand 00004 * @author Richard Kirkby ([email protected]) 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 3 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 * 00019 */ 00020 package moa.core; 00021 00028 public class TimingUtils { 00029 00030 protected static boolean preciseThreadTimesAvailable = false; 00031 00032 public static boolean enablePreciseTiming() { 00033 if (!preciseThreadTimesAvailable) { 00034 try { 00035 java.lang.management.ThreadMXBean tmxb = java.lang.management.ManagementFactory.getThreadMXBean(); 00036 if (tmxb.isCurrentThreadCpuTimeSupported()) { 00037 tmxb.setThreadCpuTimeEnabled(true); 00038 preciseThreadTimesAvailable = true; 00039 } 00040 } catch (Throwable e) { 00041 // ignore problems, just resort to inaccurate timing 00042 } 00043 } 00044 return preciseThreadTimesAvailable; 00045 } 00046 00047 public static long getNanoCPUTimeOfCurrentThread() { 00048 return getNanoCPUTimeOfThread(Thread.currentThread().getId()); 00049 } 00050 00051 public static long getNanoCPUTimeOfThread(long threadID) { 00052 if (preciseThreadTimesAvailable) { 00053 long time = java.lang.management.ManagementFactory.getThreadMXBean().getThreadCpuTime(threadID); 00054 if (time != -1) { 00055 return time; 00056 } 00057 } 00058 return System.nanoTime(); 00059 } 00060 00061 public static double nanoTimeToSeconds(long nanoTime) { 00062 return nanoTime / 1000000000.0; 00063 } 00064 }