package ptrman.meter.resource;


import ptrman.meter.event.DoubleMeter;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;

/**
 * CPU Time, in milliseconds
 *
 * @author The Stajistics Project
 */
public class ThreadCPUTimeTracker extends DoubleMeter {

    //private long startCPUTime; // nanos

    public ThreadCPUTimeTracker(final String id) {
        super(id);

        //ensureCPUTimeMonitoringEnabled();
    }

    public static double getCPUTime() {
        return getThreadMXBean().getCurrentThreadCpuTime();
    }

    @Override
    protected Double getValue(Object key, int index) {
        if (isCPUTimeMonitoringEnabled()) {
            return getCPUTime();
        }
        return null;
    }

 

    
   private static volatile boolean hasSetContentionMonitoringEnabled = false;
    private static volatile boolean hasSetCPUTimeMonitoringEnabled = false;

    private static boolean contentionMonitoringEnabled = false;
    private static boolean cpuTimeMonitoringEnabled = true;

    private static final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();    

    protected static ThreadMXBean getThreadMXBean() {
        return threadMXBean;
    }

    protected static void ensureContentionMonitoringEnabled() {
        if (!hasSetContentionMonitoringEnabled) {
            hasSetContentionMonitoringEnabled = true;

            if (threadMXBean.isThreadContentionMonitoringSupported()) {
                threadMXBean.setThreadContentionMonitoringEnabled(true);
                contentionMonitoringEnabled = true;

                //logger.info("Enabling thread contention monitoring");

            } else {
                System.err.println("Thread contention monitoring is not supported in this JVM; "
                        + "Thread contention related trackers will be silent");
            }
        }
    }

    protected static boolean isContentionMonitoringEnabled() {
        return contentionMonitoringEnabled;
    }

    protected static void ensureCPUTimeMonitoringEnabled() {
        if (!hasSetCPUTimeMonitoringEnabled) {
            hasSetCPUTimeMonitoringEnabled = true;

            if (threadMXBean.isCurrentThreadCpuTimeSupported()) {
                threadMXBean.setThreadCpuTimeEnabled(true);
                cpuTimeMonitoringEnabled = true;

                //logger.info("Enabling thread CPU time monitoring");
            } else {
                System.err.println("Thread CPU time monitoring is not supported in this JVM; "
                        + "Thread CPU time related trackers will be silent");
            }
        }
    }

    protected static boolean isCPUTimeMonitoringEnabled() {
        return cpuTimeMonitoringEnabled;
    }

    protected static ThreadInfo getCurrentThreadInfo() {
        if (contentionMonitoringEnabled) {
            return threadMXBean.getThreadInfo(Thread.currentThread().getId(), 0);
        }

        return null;
    }
}

@@ -1,8 +1,617 @@
+/* Copyright 2009 - 2010 The Stajistics Project%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *    http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 
@@ -634,17 +634,16 @@
 ource;%0A%0A
-%0A
 import p

add proper license to ThreadCPUTimeTracker