Search in sources :

Example 26 with OperatingSystemMXBean

use of java.lang.management.OperatingSystemMXBean in project neo4j by neo4j.

the class MetaSection method retrieve.

static Stream<RetrieveResult> retrieve(String graphToken, Kernel kernel, long numSilentQueryDrops) throws TransactionFailureException {
    Map<String, Object> systemData = new HashMap<>();
    systemData.put("jvmMemoryFree", Runtime.getRuntime().freeMemory());
    systemData.put("jvmMemoryTotal", Runtime.getRuntime().totalMemory());
    systemData.put("jvmMemoryMax", Runtime.getRuntime().maxMemory());
    systemData.put("systemTotalPhysicalMemory", OsBeanUtil.getTotalPhysicalMemory());
    systemData.put("systemFreePhysicalMemory", OsBeanUtil.getFreePhysicalMemory());
    systemData.put("systemCommittedVirtualMemory", OsBeanUtil.getCommittedVirtualMemory());
    systemData.put("systemTotalSwapSpace", OsBeanUtil.getTotalSwapSpace());
    systemData.put("systemFreeSwapSpace", OsBeanUtil.getFreeSwapSpace());
    OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
    systemData.put("osArch", os.getArch());
    systemData.put("osName", os.getName());
    systemData.put("osVersion", os.getVersion());
    systemData.put("availableProcessors", os.getAvailableProcessors());
    systemData.put("byteOrder", ByteOrder.nativeOrder().toString());
    RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
    systemData.put("jvmName", runtime.getVmName());
    systemData.put("jvmVendor", runtime.getVmVendor());
    systemData.put("jvmVersion", runtime.getVmVersion());
    CompilationMXBean compiler = ManagementFactory.getCompilationMXBean();
    systemData.put("jvmJITCompiler", compiler == null ? "unknown" : compiler.getName());
    systemData.put("userLanguage", Locale.getDefault().getLanguage());
    systemData.put("userCountry", Locale.getDefault().getCountry());
    systemData.put("userTimezone", TimeZone.getDefault().getID());
    systemData.put("fileEncoding", System.getProperty("file.encoding"));
    Map<String, Object> internalData = new HashMap<>();
    internalData.put("numSilentQueryCollectionMisses", numSilentQueryDrops);
    Map<String, Object> metaData = new HashMap<>();
    metaData.put("graphToken", graphToken);
    metaData.put("retrieveTime", ZonedDateTime.now());
    metaData.put("system", systemData);
    metaData.put("internal", internalData);
    TokensSection.putTokenCounts(metaData, kernel);
    return Stream.of(new RetrieveResult(Sections.META, metaData));
}
Also used : HashMap(java.util.HashMap) RuntimeMXBean(java.lang.management.RuntimeMXBean) CompilationMXBean(java.lang.management.CompilationMXBean) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 27 with OperatingSystemMXBean

use of java.lang.management.OperatingSystemMXBean in project databus by linkedin.

the class JavaStatsRequestProcessor method processOsStats.

private void processOsStats(DatabusRequest request) throws IOException {
    OperatingSystemMXBean osStats = ManagementFactory.getOperatingSystemMXBean();
    writeJsonObjectToResponse(osStats, request);
}
Also used : OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 28 with OperatingSystemMXBean

use of java.lang.management.OperatingSystemMXBean in project gerrit by GerritCodeReview.

the class OperatingSystemMXBeanFactory method create.

static OperatingSystemMXBeanInterface create() {
    OperatingSystemMXBean sys = ManagementFactory.getOperatingSystemMXBean();
    if (sys instanceof UnixOperatingSystemMXBean) {
        return new OperatingSystemMXBeanUnixNative((UnixOperatingSystemMXBean) sys);
    }
    for (String name : Arrays.asList("com.sun.management.UnixOperatingSystemMXBean", "com.ibm.lang.management.UnixOperatingSystemMXBean")) {
        try {
            Class<?> impl = Class.forName(name);
            if (impl.isInstance(sys)) {
                return new OperatingSystemMXBeanReflectionBased(sys);
            }
        } catch (ReflectiveOperationException e) {
            logger.atFine().withCause(e).log("No implementation for %s", name);
        }
    }
    logger.atWarning().log("No implementation of UnixOperatingSystemMXBean found");
    return null;
}
Also used : UnixOperatingSystemMXBean(com.sun.management.UnixOperatingSystemMXBean) UnixOperatingSystemMXBean(com.sun.management.UnixOperatingSystemMXBean) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 29 with OperatingSystemMXBean

use of java.lang.management.OperatingSystemMXBean 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 30 with OperatingSystemMXBean

use of java.lang.management.OperatingSystemMXBean in project chassis by Kixeye.

the class ChassisConfiguration method metricRegistry.

/**
 * Initializes the metrics registry
 *
 * @return metric registry bean
 */
@Bean
public MetricRegistry metricRegistry() {
    final MetricRegistry bean = new MetricRegistry();
    // add JVM metrics
    bean.register("jvm.gc", new GarbageCollectorMetricSet());
    bean.register("jvm.memory", new MemoryUsageGaugeSet());
    bean.register("jvm.thread-states", new ThreadStatesGaugeSet());
    bean.register("jvm.fd", new FileDescriptorRatioGauge());
    bean.register("jvm.load-average", new Gauge<Double>() {

        private OperatingSystemMXBean mxBean = ManagementFactory.getOperatingSystemMXBean();

        public Double getValue() {
            try {
                return mxBean.getSystemLoadAverage();
            } catch (Exception e) {
                // not supported
                return -1d;
            }
        }
    });
    // add Logback metrics
    final LoggerContext factory = (LoggerContext) LoggerFactory.getILoggerFactory();
    final Logger root = factory.getLogger(Logger.ROOT_LOGGER_NAME);
    final InstrumentedAppender appender = new InstrumentedAppender(bean);
    appender.setContext(root.getLoggerContext());
    appender.start();
    root.addAppender(appender);
    return bean;
}
Also used : InstrumentedAppender(com.codahale.metrics.logback.InstrumentedAppender) FileDescriptorRatioGauge(com.codahale.metrics.jvm.FileDescriptorRatioGauge) MemoryUsageGaugeSet(com.codahale.metrics.jvm.MemoryUsageGaugeSet) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(ch.qos.logback.classic.Logger) ThreadStatesGaugeSet(com.codahale.metrics.jvm.ThreadStatesGaugeSet) LoggerContext(ch.qos.logback.classic.LoggerContext) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) GarbageCollectorMetricSet(com.codahale.metrics.jvm.GarbageCollectorMetricSet) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)88 RuntimeMXBean (java.lang.management.RuntimeMXBean)26 Method (java.lang.reflect.Method)20 IOException (java.io.IOException)15 MemoryMXBean (java.lang.management.MemoryMXBean)11 MemoryUsage (java.lang.management.MemoryUsage)8 ThreadMXBean (java.lang.management.ThreadMXBean)8 HashMap (java.util.HashMap)8 UnixOperatingSystemMXBean (com.sun.management.UnixOperatingSystemMXBean)7 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)6 Status (com.alibaba.dubbo.common.status.Status)5 File (java.io.File)5 LinkedHashMap (java.util.LinkedHashMap)5 Map (java.util.Map)5 Test (org.junit.Test)5 Test (org.testng.annotations.Test)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 UnknownHostException (java.net.UnknownHostException)4 Date (java.util.Date)4 ExtendedOperatingSystemMXBeanImpl (com.ibm.lang.management.internal.ExtendedOperatingSystemMXBeanImpl)3