001/*
002 *  Copyright 2012 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.runtime.plugins.admin.jvmstatus.monitoring.sample;
017
018import java.io.IOException;
019import java.lang.management.ManagementFactory;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.rrd4j.DsType;
024import org.rrd4j.core.RrdDef;
025import org.rrd4j.core.Sample;
026
027import org.ametys.runtime.plugins.admin.jvmstatus.monitoring.SampleManager;
028
029import com.sun.management.OperatingSystemMXBean;
030
031/**
032 * {@link SampleManager} for collecting JVM heap memory status.
033 */
034public class CPUSampleManager extends AbstractSampleManager
035{
036    private static MXBeanCPUMonitor _mxBeanCPUMonitor;
037    
038    @Override
039    protected void _configureDatasources(RrdDef rrdDef)
040    {
041        _registerDatasources(rrdDef, "current", DsType.GAUGE, 0, Double.NaN);
042    }
043
044    @Override
045    protected Map<String, Object> _internalCollect(Sample sample) throws IOException
046    {
047        Map<String, Object> result = new HashMap<>();
048        if (_mxBeanCPUMonitor == null)
049        {
050            _mxBeanCPUMonitor = new MXBeanCPUMonitor();
051        }
052        
053        double cpuUsage = _mxBeanCPUMonitor.getCpuUsage();
054        sample.setValue("current", cpuUsage);
055        result.put("current", cpuUsage);
056        
057        return result;
058    }
059
060    @Override
061    protected String _getGraphTitle()
062    {
063        return "CPU use";
064    }
065    
066    /**
067     * MXBean to monitor CPU 
068     */
069    public class MXBeanCPUMonitor
070    {
071        private int _availableProcessors = getOperatingSystemMXBean().getAvailableProcessors();
072        private long _lastProcessCpuTime;
073        private long _lastSystemTime;
074
075        MXBeanCPUMonitor()
076        {
077            // empty
078        }
079
080        private void baselineCounters()
081        {
082            _lastSystemTime = System.nanoTime();
083
084            if (getOperatingSystemMXBean() != null)
085            {
086                _lastProcessCpuTime = (getOperatingSystemMXBean()).getProcessCpuTime();
087            }
088        }
089
090        private OperatingSystemMXBean getOperatingSystemMXBean()
091        {
092            Object bean = ManagementFactory.getOperatingSystemMXBean();
093            if (bean instanceof OperatingSystemMXBean)
094            {
095                return (OperatingSystemMXBean) bean;
096            }
097            else
098            {
099                return null;
100            }
101        }
102
103        /**
104         * The cpu usage
105         * @return The cpu usage
106         */
107        public synchronized double getCpuUsage()
108        {
109            if (_lastSystemTime == 0)
110            {
111                baselineCounters();
112                return 0;
113            }
114
115            long systemTime = System.nanoTime();
116            long processCpuTime = 0;
117
118            if (getOperatingSystemMXBean() != null)
119            {
120                processCpuTime = (getOperatingSystemMXBean()).getProcessCpuTime();
121            }
122            else
123            {
124                return 0;
125            }
126
127            double cpuUsage = (double) (processCpuTime - _lastProcessCpuTime) / (systemTime - _lastSystemTime);
128
129            _lastSystemTime = systemTime;
130            _lastProcessCpuTime = processCpuTime;
131
132            return (cpuUsage / _availableProcessors) * 100;
133        }
134    }
135}