Search in sources :

Example 1 with UnixOperatingSystemMXBean

use of com.sun.management.UnixOperatingSystemMXBean in project commons by twitter.

the class JvmStats method register.

/**
   * Add a series of system and jvm-level stats to the given registry.
   */
public static void register(MetricRegistry registry) {
    final MetricRegistry stats = registry.scope("jvm");
    final MemoryMXBean mem = ManagementFactory.getMemoryMXBean();
    // memory stats
    final MetricRegistry heapRegistry = stats.scope("heap");
    registerMemoryStats(heapRegistry, new MemoryReporter() {

        @Override
        public MemoryUsage getUsage() {
            return mem.getHeapMemoryUsage();
        }
    });
    final MetricRegistry nonHeapRegistry = stats.scope("nonheap");
    registerMemoryStats(nonHeapRegistry, new MemoryReporter() {

        @Override
        public MemoryUsage getUsage() {
            return mem.getNonHeapMemoryUsage();
        }
    });
    // threads
    final ThreadMXBean threads = ManagementFactory.getThreadMXBean();
    final MetricRegistry threadRegistry = stats.scope("thread");
    threadRegistry.register(new AbstractGauge<Integer>("daemon_count") {

        @Override
        public Integer read() {
            return threads.getDaemonThreadCount();
        }
    });
    threadRegistry.register(new AbstractGauge<Integer>("count") {

        @Override
        public Integer read() {
            return threads.getThreadCount();
        }
    });
    threadRegistry.register(new AbstractGauge<Integer>("peak_count") {

        @Override
        public Integer read() {
            return threads.getPeakThreadCount();
        }
    });
    // class loading bean
    final ClassLoadingMXBean classLoadingBean = ManagementFactory.getClassLoadingMXBean();
    stats.register(new AbstractGauge<Integer>("classes_loaded") {

        @Override
        public Integer read() {
            return classLoadingBean.getLoadedClassCount();
        }
    });
    stats.register(new AbstractGauge<Long>("total_classes_loaded") {

        @Override
        public Long read() {
            return classLoadingBean.getTotalLoadedClassCount();
        }
    });
    stats.register(new AbstractGauge<Long>("classes_unloaded") {

        @Override
        public Long read() {
            return classLoadingBean.getUnloadedClassCount();
        }
    });
    // runtime
    final RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
    stats.register(new AbstractGauge<Long>("start_time") {

        @Override
        public Long read() {
            return runtime.getStartTime();
        }
    });
    stats.register(new AbstractGauge<Long>("uptime") {

        @Override
        public Long read() {
            return runtime.getUptime();
        }
    });
    //stats.register(new AbstractGauge<String>)
    // os
    final OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
    stats.register(new AbstractGauge<Integer>("num_cpus") {

        @Override
        public Integer read() {
            return os.getAvailableProcessors();
        }
    });
    if (os instanceof com.sun.management.OperatingSystemMXBean) {
        final com.sun.management.OperatingSystemMXBean sunOsMbean = (com.sun.management.OperatingSystemMXBean) os;
        // if this is indeed an operating system
        stats.register(new AbstractGauge<Long>("free_physical_memory") {

            @Override
            public Long read() {
                return sunOsMbean.getFreePhysicalMemorySize();
            }
        });
        stats.register(new AbstractGauge<Long>("free_swap") {

            @Override
            public Long read() {
                return sunOsMbean.getFreeSwapSpaceSize();
            }
        });
        stats.register(new AbstractGauge<Long>("process_cpu_time") {

            @Override
            public Long read() {
                return sunOsMbean.getProcessCpuTime();
            }
        });
    }
    if (os instanceof com.sun.management.UnixOperatingSystemMXBean) {
        // it's a unix system... I know this!
        final UnixOperatingSystemMXBean unix = (UnixOperatingSystemMXBean) os;
        stats.register(new AbstractGauge<Long>("fd_count") {

            @Override
            public Long read() {
                return unix.getOpenFileDescriptorCount();
            }
        });
        stats.register(new AbstractGauge<Long>("fd_limit") {

            @Override
            public Long read() {
                return unix.getMaxFileDescriptorCount();
            }
        });
    }
    // mem
    final List<MemoryPoolMXBean> memPool = ManagementFactory.getMemoryPoolMXBeans();
    final MetricRegistry memRegistry = stats.scope("mem");
    final MetricRegistry currentMem = memRegistry.scope("current");
    final MetricRegistry postGCRegistry = memRegistry.scope("postGC");
    for (final MemoryPoolMXBean pool : memPool) {
        String name = normalizeName(pool.getName());
        registerMemoryStats(currentMem.scope(name), new MemoryReporter() {

            @Override
            public MemoryUsage getUsage() {
                return pool.getUsage();
            }
        });
        registerMemoryStats(postGCRegistry.scope(name), new MemoryReporter() {

            @Override
            public MemoryUsage getUsage() {
                return pool.getCollectionUsage();
            }
        });
    }
    currentMem.register(new AbstractGauge<Long>("used") {

        @Override
        public Long read() {
            long sum = 0;
            for (MemoryPoolMXBean pool : memPool) {
                MemoryUsage usage = pool.getUsage();
                if (usage != null) {
                    sum += usage.getUsed();
                }
            }
            return sum;
        }
    });
    AbstractGauge<Long> totalPostGCGauge = new AbstractGauge<Long>("used") {

        @Override
        public Long read() {
            long sum = 0;
            for (MemoryPoolMXBean pool : memPool) {
                MemoryUsage usage = pool.getCollectionUsage();
                if (usage != null) {
                    sum += usage.getUsed();
                }
            }
            return sum;
        }
    };
    postGCRegistry.register(totalPostGCGauge);
    // java 1.7 specific buffer pool gauges
    Multimap<String, AbstractGauge<Long>> java17gauges = buildJava17Gauges();
    if (!java17gauges.isEmpty()) {
        MetricRegistry bufferRegistry = stats.scope("buffer");
        for (String scope : java17gauges.keySet()) {
            MetricRegistry pool = bufferRegistry.scope(scope);
            for (AbstractGauge<Long> gauge : java17gauges.get(scope)) {
                pool.register(gauge);
            }
        }
    }
    // gc
    final List<GarbageCollectorMXBean> gcPool = ManagementFactory.getGarbageCollectorMXBeans();
    MetricRegistry gcRegistry = stats.scope("gc");
    for (final GarbageCollectorMXBean gc : gcPool) {
        String name = normalizeName(gc.getName());
        MetricRegistry scoped = memRegistry.scope(name);
        scoped.register(new AbstractGauge<Long>("cycles") {

            @Override
            public Long read() {
                return gc.getCollectionCount();
            }
        });
        scoped.register(new AbstractGauge<Long>("msec") {

            @Override
            public Long read() {
                return gc.getCollectionTime();
            }
        });
    }
    gcRegistry.register(new AbstractGauge<Long>("cycles") {

        @Override
        public Long read() {
            long sum = 0;
            for (GarbageCollectorMXBean pool : gcPool) {
                long count = pool.getCollectionCount();
                if (count > 0) {
                    sum += count;
                }
            }
            return sum;
        }
    });
    gcRegistry.register(new AbstractGauge<Long>("msec") {

        @Override
        public Long read() {
            long sum = 0;
            for (GarbageCollectorMXBean pool : gcPool) {
                long msec = pool.getCollectionTime();
                if (msec > 0) {
                    sum += msec;
                }
            }
            return sum;
        }
    });
}
Also used : GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) UnixOperatingSystemMXBean(com.sun.management.UnixOperatingSystemMXBean) MemoryMXBean(java.lang.management.MemoryMXBean) ClassLoadingMXBean(java.lang.management.ClassLoadingMXBean) ThreadMXBean(java.lang.management.ThreadMXBean) RuntimeMXBean(java.lang.management.RuntimeMXBean) MemoryUsage(java.lang.management.MemoryUsage) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) UnixOperatingSystemMXBean(com.sun.management.UnixOperatingSystemMXBean)

Example 2 with UnixOperatingSystemMXBean

use of com.sun.management.UnixOperatingSystemMXBean in project hive by apache.

the class LlapDaemonJvmMetrics method getJvmMetrics.

private void getJvmMetrics(final MetricsRecordBuilder rb) {
    List<BufferPoolMXBean> pools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
    long directBufferCount = 0;
    long directBufferTotalCapacity = 0;
    long directBufferMemoryUsed = 0;
    long mappedBufferCount = 0;
    long mappedBufferTotalCapacity = 0;
    long mappedBufferMemoryUsed = 0;
    for (BufferPoolMXBean pool : pools) {
        if (pool.getName().equals("direct")) {
            directBufferCount = pool.getCount();
            directBufferTotalCapacity = pool.getTotalCapacity();
            directBufferMemoryUsed = pool.getMemoryUsed();
        } else if (pool.getName().equals("mapped")) {
            mappedBufferCount = pool.getCount();
            mappedBufferTotalCapacity = pool.getTotalCapacity();
            mappedBufferMemoryUsed = pool.getMemoryUsed();
        }
    }
    OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
    long openFileHandles = 0;
    long maxFileHandles = 0;
    if (os instanceof UnixOperatingSystemMXBean) {
        openFileHandles = ((UnixOperatingSystemMXBean) os).getOpenFileDescriptorCount();
        maxFileHandles = ((UnixOperatingSystemMXBean) os).getMaxFileDescriptorCount();
    }
    rb.addGauge(LlapDaemonDirectBufferCount, directBufferCount).addGauge(LlapDaemonDirectBufferTotalCapacity, directBufferTotalCapacity).addGauge(LlapDaemonDirectBufferMemoryUsed, directBufferMemoryUsed).addGauge(LlapDaemonMappedBufferCount, mappedBufferCount).addGauge(LlapDaemonMappedBufferTotalCapacity, mappedBufferTotalCapacity).addGauge(LlapDaemonMappedBufferMemoryUsed, mappedBufferMemoryUsed).addGauge(LlapDaemonOpenFileDescriptorCount, openFileHandles).addGauge(LlapDaemonMaxFileDescriptorCount, maxFileHandles);
}
Also used : UnixOperatingSystemMXBean(com.sun.management.UnixOperatingSystemMXBean) BufferPoolMXBean(java.lang.management.BufferPoolMXBean) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) UnixOperatingSystemMXBean(com.sun.management.UnixOperatingSystemMXBean)

Aggregations

UnixOperatingSystemMXBean (com.sun.management.UnixOperatingSystemMXBean)2 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)2 BufferPoolMXBean (java.lang.management.BufferPoolMXBean)1 ClassLoadingMXBean (java.lang.management.ClassLoadingMXBean)1 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)1 MemoryMXBean (java.lang.management.MemoryMXBean)1 MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)1 MemoryUsage (java.lang.management.MemoryUsage)1 RuntimeMXBean (java.lang.management.RuntimeMXBean)1 ThreadMXBean (java.lang.management.ThreadMXBean)1