Search in sources :

Example 76 with OperatingSystemMXBean

use of java.lang.management.OperatingSystemMXBean in project java-chassis by ServiceComb.

the class HealthMonitorDataProvider method exactProcessInfo.

private void exactProcessInfo(MonitorData monitorData) {
    MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
    MemoryUsage memoryHeapUsage = memoryMXBean.getHeapMemoryUsage();
    MemoryUsage memoryNonHeapUsage = memoryMXBean.getNonHeapMemoryUsage();
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    int threadCount = threadMXBean.getThreadCount();
    OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
    double cpu = operatingSystemMXBean.getSystemLoadAverage();
    monitorData.setCpu(CPUMonitorCalc.getInstance().getProcessCpu());
    monitorData.setLoadAverage(cpu);
    monitorData.setThreadCount(threadCount);
    monitorData.setUptime(runtimeMXBean.getUptime());
    Map<String, Long> memoryInfo = new HashMap<>();
    memoryInfo.put("heapInit", memoryHeapUsage.getInit());
    memoryInfo.put("headMax", memoryHeapUsage.getMax());
    memoryInfo.put("heapCommit", memoryHeapUsage.getCommitted());
    memoryInfo.put("heapUsed", memoryHeapUsage.getUsed());
    memoryInfo.put("nonHeapInit", memoryNonHeapUsage.getInit());
    memoryInfo.put("nonHeapCommit", memoryNonHeapUsage.getCommitted());
    memoryInfo.put("nonHeapUsed", memoryNonHeapUsage.getUsed());
    monitorData.setMemory(memoryInfo);
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) MemoryMXBean(java.lang.management.MemoryMXBean) HashMap(java.util.HashMap) RuntimeMXBean(java.lang.management.RuntimeMXBean) MemoryUsage(java.lang.management.MemoryUsage) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 77 with OperatingSystemMXBean

use of java.lang.management.OperatingSystemMXBean in project commons by twitter.

the class JvmStats method export.

/**
 * Exports stats related to the JVM and runtime environment.
 */
public static void export() {
    final OperatingSystemMXBean osMbean = ManagementFactory.getOperatingSystemMXBean();
    if (osMbean instanceof com.sun.management.OperatingSystemMXBean) {
        final com.sun.management.OperatingSystemMXBean sunOsMbean = (com.sun.management.OperatingSystemMXBean) osMbean;
        Stats.exportAll(ImmutableList.<Stat<? extends Number>>builder().add(new StatImpl<Long>("system_free_physical_memory_mb") {

            @Override
            public Long read() {
                return sunOsMbean.getFreePhysicalMemorySize() / BYTES_PER_MB;
            }
        }).add(new StatImpl<Long>("system_free_swap_mb") {

            @Override
            public Long read() {
                return sunOsMbean.getFreeSwapSpaceSize() / BYTES_PER_MB;
            }
        }).add(Rate.of(new StatImpl<Long>("process_cpu_time_nanos") {

            @Override
            public Long read() {
                return sunOsMbean.getProcessCpuTime();
            }
        }).withName("process_cpu_cores_utilized").withScaleFactor(SECS_PER_NANO).build()).build());
    }
    if (osMbean instanceof com.sun.management.UnixOperatingSystemMXBean) {
        final com.sun.management.UnixOperatingSystemMXBean unixOsMbean = (com.sun.management.UnixOperatingSystemMXBean) osMbean;
        Stats.exportAll(ImmutableList.<Stat<? extends Number>>builder().add(new StatImpl<Long>("process_max_fd_count") {

            @Override
            public Long read() {
                return unixOsMbean.getMaxFileDescriptorCount();
            }
        }).add(new StatImpl<Long>("process_open_fd_count") {

            @Override
            public Long read() {
                return unixOsMbean.getOpenFileDescriptorCount();
            }
        }).build());
    }
    final Runtime runtime = Runtime.getRuntime();
    final ClassLoadingMXBean classLoadingBean = ManagementFactory.getClassLoadingMXBean();
    final MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
    final ThreadMXBean threads = ManagementFactory.getThreadMXBean();
    final RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    Stats.exportAll(ImmutableList.<Stat<? extends Number>>builder().add(new StatImpl<Long>("jvm_time_ms") {

        @Override
        public Long read() {
            return System.currentTimeMillis();
        }
    }).add(new StatImpl<Integer>("jvm_available_processors") {

        @Override
        public Integer read() {
            return runtime.availableProcessors();
        }
    }).add(new StatImpl<Long>("jvm_memory_free_mb") {

        @Override
        public Long read() {
            return runtime.freeMemory() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_memory_max_mb") {

        @Override
        public Long read() {
            return runtime.maxMemory() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_memory_mb_total") {

        @Override
        public Long read() {
            return runtime.totalMemory() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Integer>("jvm_class_loaded_count") {

        @Override
        public Integer read() {
            return classLoadingBean.getLoadedClassCount();
        }
    }).add(new StatImpl<Long>("jvm_class_total_loaded_count") {

        @Override
        public Long read() {
            return classLoadingBean.getTotalLoadedClassCount();
        }
    }).add(new StatImpl<Long>("jvm_class_unloaded_count") {

        @Override
        public Long read() {
            return classLoadingBean.getUnloadedClassCount();
        }
    }).add(new StatImpl<Long>("jvm_gc_collection_time_ms") {

        @Override
        public Long read() {
            long collectionTimeMs = 0;
            for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
                collectionTimeMs += bean.getCollectionTime();
            }
            return collectionTimeMs;
        }
    }).add(new StatImpl<Long>("jvm_gc_collection_count") {

        @Override
        public Long read() {
            long collections = 0;
            for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
                collections += bean.getCollectionCount();
            }
            return collections;
        }
    }).add(new StatImpl<Long>("jvm_memory_heap_mb_used") {

        @Override
        public Long read() {
            return memoryBean.getHeapMemoryUsage().getUsed() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_memory_heap_mb_committed") {

        @Override
        public Long read() {
            return memoryBean.getHeapMemoryUsage().getCommitted() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_memory_heap_mb_max") {

        @Override
        public Long read() {
            return memoryBean.getHeapMemoryUsage().getMax() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_memory_non_heap_mb_used") {

        @Override
        public Long read() {
            return memoryBean.getNonHeapMemoryUsage().getUsed() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_memory_non_heap_mb_committed") {

        @Override
        public Long read() {
            return memoryBean.getNonHeapMemoryUsage().getCommitted() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_memory_non_heap_mb_max") {

        @Override
        public Long read() {
            return memoryBean.getNonHeapMemoryUsage().getMax() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_uptime_secs") {

        @Override
        public Long read() {
            return runtimeMXBean.getUptime() / 1000;
        }
    }).add(new StatImpl<Double>("system_load_avg") {

        @Override
        public Double read() {
            return osMbean.getSystemLoadAverage();
        }
    }).add(new StatImpl<Integer>("jvm_threads_peak") {

        @Override
        public Integer read() {
            return threads.getPeakThreadCount();
        }
    }).add(new StatImpl<Long>("jvm_threads_started") {

        @Override
        public Long read() {
            return threads.getTotalStartedThreadCount();
        }
    }).add(new StatImpl<Integer>("jvm_threads_daemon") {

        @Override
        public Integer read() {
            return threads.getDaemonThreadCount();
        }
    }).add(new StatImpl<Integer>("jvm_threads_active") {

        @Override
        public Integer read() {
            return threads.getThreadCount();
        }
    }).build());
    // Export per memory pool gc time and cycle count like Ostrich
    // This is based on code in Bridcage: https://cgit.twitter.biz/birdcage/tree/ \
    // ostrich/src/main/scala/com/twitter/ostrich/stats/StatsCollection.scala
    Stats.exportAll(Iterables.transform(ManagementFactory.getGarbageCollectorMXBeans(), new Function<GarbageCollectorMXBean, Stat<? extends Number>>() {

        @Override
        public Stat<? extends Number> apply(final GarbageCollectorMXBean gcMXBean) {
            return new StatImpl<Long>("jvm_gc_" + Stats.normalizeName(gcMXBean.getName()) + "_collection_count") {

                @Override
                public Long read() {
                    return gcMXBean.getCollectionCount();
                }
            };
        }
    }));
    Stats.exportAll(Iterables.transform(ManagementFactory.getGarbageCollectorMXBeans(), new Function<GarbageCollectorMXBean, Stat<? extends Number>>() {

        @Override
        public Stat<? extends Number> apply(final GarbageCollectorMXBean gcMXBean) {
            return new StatImpl<Long>("jvm_gc_" + Stats.normalizeName(gcMXBean.getName()) + "_collection_time_ms") {

                @Override
                public Long read() {
                    return gcMXBean.getCollectionTime();
                }
            };
        }
    }));
    Stats.exportString(new StatImpl<String>("jvm_input_arguments") {

        @Override
        public String read() {
            return runtimeMXBean.getInputArguments().toString();
        }
    });
    for (final String property : System.getProperties().stringPropertyNames()) {
        Stats.exportString(new StatImpl<String>("jvm_prop_" + Stats.normalizeName(property)) {

            @Override
            public String read() {
                return System.getProperty(property);
            }
        });
    }
    for (final Map.Entry<String, String> environmentVariable : System.getenv().entrySet()) {
        Stats.exportString(new StatImpl<String>("system_env_" + Stats.normalizeName(environmentVariable.getKey())) {

            @Override
            public String read() {
                return environmentVariable.getValue();
            }
        });
    }
}
Also used : GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) Function(com.google.common.base.Function) MemoryMXBean(java.lang.management.MemoryMXBean) ClassLoadingMXBean(java.lang.management.ClassLoadingMXBean) ThreadMXBean(java.lang.management.ThreadMXBean) RuntimeMXBean(java.lang.management.RuntimeMXBean) Map(java.util.Map) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 78 with OperatingSystemMXBean

use of java.lang.management.OperatingSystemMXBean in project streamsx.topology by IBMStreams.

the class BundleUserStreamsContext method useRemoteBuild.

/**
 * See if the remote build service should be used.
 * If STREAMS_INSTALL was not set is handled elsewhere,
 * so this path assumes that STREAMS_INSTALL is set and not empty.
 *
 * Remote build if:
 *  - FORCE_REMOTE_BUILD is set to true.
 *  - Architecture cannot be determined as x86_64. (assumption that service is always x86_64)
 *  - OS is not Linux.
 *  - OS is not centos or redhat
 *  - OS is less than version 6.
 *  - OS service base does not match local os version
 */
protected final boolean useRemoteBuild(AppEntity entity, Function<AppEntity, Integer> serviceBaseGetter) {
    assert System.getenv("STREAMS_INSTALL") != null;
    assert !System.getenv("STREAMS_INSTALL").isEmpty();
    final JsonObject deploy = deploy(entity.submission);
    if (jboolean(deploy, FORCE_REMOTE_BUILD))
        return true;
    try {
        final OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
        final String arch = os.getArch();
        boolean x86_64 = "x86_64".equalsIgnoreCase(arch) || "amd64".equalsIgnoreCase(arch);
        if (!x86_64)
            return true;
        if (!"Linux".equalsIgnoreCase(os.getName()))
            return true;
        // /etc/system-release-cpe examples
        // 
        // cpe:/o:centos:linux:6:GA
        // cpe:/o/redhat:enterprise_linux:7.2:ga:server
        File cpeFile = new File("/etc/system-release-cpe");
        if (!cpeFile.isFile())
            return true;
        List<String> lines = Files.readAllLines(cpeFile.toPath());
        String osVendor = null;
        String osVersion = null;
        @SuppressWarnings("unused") String osUpdate = null;
        for (String cpe : lines) {
            cpe = cpe.trim();
            if (!cpe.startsWith("cpe:"))
                continue;
            String[] comps = cpe.split(":");
            if (comps.length < 5)
                continue;
            if (!"cpe".equals(comps[0]))
                continue;
            if (!"/o".equals(comps[1]))
                continue;
            if (!comps[2].isEmpty()) {
                osVendor = comps[2];
                osVersion = comps[4];
                if (comps.length >= 6)
                    osUpdate = comps[5];
            }
        }
        // If we can' determine the info, force remote
        if (osVendor == null || osVendor.isEmpty())
            return true;
        if (osVersion == null || osVersion.isEmpty())
            return true;
        if (!"centos".equals(osVendor) && !"redhat".equals(osVendor))
            return true;
        double version = Double.valueOf(osVersion);
        if (version < 6)
            return true;
        if (serviceBaseGetter != null) {
            // Compare base versions, ir it doesn't exactly match the
            // service force remote build.
            int serviceBase = serviceBaseGetter.apply(entity);
            if (((int) version) != serviceBase)
                return true;
        }
    } catch (SecurityException | IOException | NumberFormatException e) {
        // Can't determine information, force remote.
        return true;
    }
    return false;
}
Also used : JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) GsonUtilities.jboolean(com.ibm.streamsx.topology.internal.gson.GsonUtilities.jboolean) File(java.io.File) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 79 with OperatingSystemMXBean

use of java.lang.management.OperatingSystemMXBean in project flink by apache.

the class Hardware method getSizeOfPhysicalMemory.

/**
 * Returns the size of the physical memory in bytes.
 *
 * @return the size of the physical memory in bytes or {@code -1}, if the size could not be
 *     determined.
 */
public static long getSizeOfPhysicalMemory() {
    // this works only on Oracle JVMs
    try {
        Class<?> clazz = Class.forName("com.sun.management.OperatingSystemMXBean");
        Method method = clazz.getMethod("getTotalPhysicalMemorySize");
        OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
        // is in fact the sun management bean
        if (clazz.isInstance(operatingSystemMXBean)) {
            return (Long) method.invoke(operatingSystemMXBean);
        }
    } catch (ClassNotFoundException e) {
    // this happens on non-Oracle JVMs, do nothing and use the alternative code paths
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        LOG.warn("Access to physical memory size: " + "com.sun.management.OperatingSystemMXBean incompatibly changed.", e);
    }
    // we now try the OS specific access paths
    switch(OperatingSystem.getCurrentOperatingSystem()) {
        case LINUX:
            return getSizeOfPhysicalMemoryForLinux();
        case WINDOWS:
            return getSizeOfPhysicalMemoryForWindows();
        case MAC_OS:
            return getSizeOfPhysicalMemoryForMac();
        case FREE_BSD:
            return getSizeOfPhysicalMemoryForFreeBSD();
        case UNKNOWN:
            LOG.error("Cannot determine size of physical memory for unknown operating system");
            return -1;
        default:
            LOG.error("Unrecognized OS: " + OperatingSystem.getCurrentOperatingSystem());
            return -1;
    }
}
Also used : Method(java.lang.reflect.Method) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 80 with OperatingSystemMXBean

use of java.lang.management.OperatingSystemMXBean in project XRTB by benmfaul.

the class Performance method getCpuPerfAsString.

/**
 * Get CPU performance as a String, adjusted by cores
 *
 * @return String. Returns a cpu percentage string
 */
public static String getCpuPerfAsString() {
    OperatingSystemMXBean mx = java.lang.management.ManagementFactory.getOperatingSystemMXBean();
    int cores = Runtime.getRuntime().availableProcessors();
    double d = mx.getSystemLoadAverage() * 100 / cores;
    return formatter.format(d);
}
Also used : OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Aggregations

OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)89 RuntimeMXBean (java.lang.management.RuntimeMXBean)27 Method (java.lang.reflect.Method)20 IOException (java.io.IOException)15 MemoryMXBean (java.lang.management.MemoryMXBean)12 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