All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.metrics.jvm.BufferPoolMetricSet Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.metrics.jvm;

import com.alibaba.metrics.JmxAttributeGauge;
import com.alibaba.metrics.Metric;
import com.alibaba.metrics.MetricName;
import com.alibaba.metrics.MetricRegistry;
import com.alibaba.metrics.MetricSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.management.JMException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * A set of gauges for the count, usage, and capacity of the JVM's direct and mapped buffer pools.
 * 

* These JMX objects are only available on Java 7 and above. */ public class BufferPoolMetricSet implements MetricSet { private static final Logger LOGGER = LoggerFactory.getLogger(BufferPoolMetricSet.class); private static final String[] ATTRIBUTES = { "Count", "MemoryUsed", "TotalCapacity" }; private static final String[] NAMES = { "count", "used", "capacity" }; private static final String[] POOLS = { "direct", "mapped" }; private final MBeanServer mBeanServer; public BufferPoolMetricSet(MBeanServer mBeanServer) { this.mBeanServer = mBeanServer; } @Override public Map getMetrics() { final Map gauges = new HashMap(); for (String pool : POOLS) { for (int i = 0; i < ATTRIBUTES.length; i++) { final String attribute = ATTRIBUTES[i]; final String name = NAMES[i]; try { final ObjectName on = new ObjectName("java.nio:type=BufferPool,name=" + pool); mBeanServer.getMBeanInfo(on); gauges.put(MetricRegistry.name(pool, name), new JmxAttributeGauge(mBeanServer, on, attribute)); } catch (JMException ignored) { LOGGER.debug("Unable to load buffer pool MBeans, possibly running on Java 6"); } } } return Collections.unmodifiableMap(gauges); } @Override public long lastUpdateTime() { return System.currentTimeMillis(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy