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.awt.Color;
019import java.io.IOException;
020import java.lang.management.ManagementFactory;
021import java.lang.management.ThreadMXBean;
022import java.util.HashMap;
023import java.util.Map;
024
025import org.rrd4j.ConsolFun;
026import org.rrd4j.DsType;
027import org.rrd4j.core.RrdDef;
028import org.rrd4j.core.Sample;
029import org.rrd4j.graph.RrdGraphDef;
030
031import org.ametys.runtime.plugins.admin.jvmstatus.monitoring.SampleManager;
032
033/**
034 * {@link SampleManager} for collecting the number of live threads.
035 */
036public class ThreadSampleManager extends AbstractSampleManager
037{
038    @Override
039    protected void _configureDatasources(RrdDef rrdDef)
040    {
041        _registerDatasources(rrdDef, "daemon", DsType.GAUGE, 0, Double.NaN);
042        _registerDatasources(rrdDef, "total", DsType.GAUGE, 0, Double.NaN);
043    }
044
045    @Override
046    protected Map<String, Object> _internalCollect(Sample sample) throws IOException
047    {
048        Map<String, Object> result = new HashMap<>();
049        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
050
051        int daemon = threadMXBean.getDaemonThreadCount();
052        sample.setValue("daemon", daemon);
053        result.put("daemon", daemon);
054        
055        int total = threadMXBean.getThreadCount();
056        sample.setValue("total", total);
057        result.put("total", total);
058        
059        return result;
060    }
061
062    @Override
063    protected String _getGraphTitle()
064    {
065        return "Live thread";
066    }
067    
068    @Override
069    protected void _populateGraphDefinition(RrdGraphDef graphDef, String rrdFilePath)
070    {
071        graphDef.datasource("daemon", rrdFilePath, "daemon", ConsolFun.AVERAGE);
072        graphDef.datasource("total", rrdFilePath, "total", ConsolFun.AVERAGE);
073        // User thread are not daemon thread ;)
074        graphDef.datasource("user", "total,daemon,-");
075        
076
077        graphDef.area("total", new Color(229, 229, 229), "Total thread count");
078        graphDef.line("daemon", new Color(28, 76, 128), "Daemon thread count", 2);
079        graphDef.line("user", new Color(148, 30, 109), "User thread count", 2);
080
081        graphDef.gprint("user", ConsolFun.LAST, "Cur user: %.0f");
082        graphDef.gprint("user", ConsolFun.MAX, "Max user: %.0f");
083        graphDef.gprint("daemon", ConsolFun.LAST, "Cur daemon: %.0f");
084        graphDef.gprint("daemon", ConsolFun.MAX, "Max daemon: %.0f");
085        graphDef.gprint("total", ConsolFun.LAST, "Cur total: %.0f");
086        graphDef.gprint("total", ConsolFun.MAX, "Max total: %.0f");
087
088        // Do not scale units
089        graphDef.setUnitsExponent(0);
090        graphDef.setVerticalLabel("thread count");
091    }
092}