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.lang.management.MemoryMXBean; 021import java.lang.management.MemoryUsage; 022import java.util.HashMap; 023import java.util.Map; 024 025import org.rrd4j.DsType; 026import org.rrd4j.core.RrdDef; 027import org.rrd4j.core.Sample; 028 029import org.ametys.runtime.plugins.admin.jvmstatus.monitoring.SampleManager; 030 031/** 032 * Abstract {@link SampleManager} for collecting JVM memory status. 033 */ 034public abstract class AbstractMemorySampleManager extends AbstractSampleManager 035{ 036 @Override 037 protected void _configureDatasources(RrdDef rrdDef) 038 { 039 rrdDef.addDatasource("commited", DsType.GAUGE, 2 * FEEDING_PERIOD, 0, Double.NaN); 040 rrdDef.addDatasource("used", DsType.GAUGE, 2 * FEEDING_PERIOD, 0, Double.NaN); 041 rrdDef.addDatasource("max", DsType.GAUGE, 2 * FEEDING_PERIOD, 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 MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); 049 MemoryUsage memoryUsage = _getMemoryUsage(memoryMXBean); 050 051 long commited = memoryUsage.getCommitted(); 052 sample.setValue("commited", commited); 053 result.put("commited", commited); 054 055 long used = memoryUsage.getUsed(); 056 sample.setValue("used", used); 057 result.put("used", used); 058 059 long max = memoryUsage.getMax(); 060 sample.setValue("max", max); 061 result.put("max", max); 062 063 return result; 064 } 065 066 /** 067 * Select the memory usage to use. 068 * @param memoryMXBean the memory MXBean. 069 * @return the memory usage. 070 */ 071 protected abstract MemoryUsage _getMemoryUsage(MemoryMXBean memoryMXBean); 072}