Search in sources :

Example 41 with GarbageCollectorMXBean

use of java.lang.management.GarbageCollectorMXBean in project DataX by alibaba.

the class VMInfo method getDelta.

public synchronized void getDelta(boolean print) {
    try {
        if (VMInfo.isSunOsMBean(osMXBean)) {
            long curUptime = runtimeMXBean.getUptime();
            long curProcessTime = getLongFromOperatingSystem(osMXBean, "getProcessCpuTime");
            //百分比, uptime是ms,processTime是nano
            if ((curUptime > lastUpTime) && (curProcessTime >= lastProcessCpuTime)) {
                float curDeltaCpu = (float) (curProcessTime - lastProcessCpuTime) / ((curUptime - lastUpTime) * totalProcessorCount * 10000);
                processCpuStatus.setMaxMinCpu(curDeltaCpu);
                processCpuStatus.averageCpu = (float) curProcessTime / (curUptime * totalProcessorCount * 10000);
                lastUpTime = curUptime;
                lastProcessCpuTime = curProcessTime;
            }
        }
        for (GarbageCollectorMXBean garbage : garbageCollectorMXBeanList) {
            GCStatus gcStatus = processGCStatus.gcStatusMap.get(garbage.getName());
            if (gcStatus == null) {
                gcStatus = new GCStatus();
                gcStatus.name = garbage.getName();
                processGCStatus.gcStatusMap.put(garbage.getName(), gcStatus);
            }
            long curTotalGcCount = garbage.getCollectionCount();
            gcStatus.setCurTotalGcCount(curTotalGcCount);
            long curtotalGcTime = garbage.getCollectionTime();
            gcStatus.setCurTotalGcTime(curtotalGcTime);
        }
        if (memoryPoolMXBeanList != null && !memoryPoolMXBeanList.isEmpty()) {
            for (MemoryPoolMXBean pool : memoryPoolMXBeanList) {
                MemoryStatus memoryStatus = processMomoryStatus.memoryStatusMap.get(pool.getName());
                if (memoryStatus == null) {
                    memoryStatus = new MemoryStatus();
                    memoryStatus.name = pool.getName();
                    processMomoryStatus.memoryStatusMap.put(pool.getName(), memoryStatus);
                }
                memoryStatus.commitedSize = pool.getUsage().getCommitted();
                memoryStatus.setMaxMinUsedSize(pool.getUsage().getUsed());
                long maxMemory = memoryStatus.commitedSize > 0 ? memoryStatus.commitedSize : memoryStatus.maxSize;
                memoryStatus.setMaxMinPercent(maxMemory > 0 ? (float) 100 * memoryStatus.usedSize / maxMemory : -1);
            }
        }
        if (print) {
            LOG.info(processCpuStatus.getDeltaString() + processMomoryStatus.getDeltaString() + processGCStatus.getDeltaString());
        }
    } catch (Exception e) {
        LOG.warn("no need care, the fail is ignored : vmInfo getDelta failed " + e.getMessage(), e);
    }
}
Also used : GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Example 42 with GarbageCollectorMXBean

use of java.lang.management.GarbageCollectorMXBean in project heron by twitter.

the class JVMMetrics method updateGcTimes.

private void updateGcTimes() {
    long totalTimeMs = 0;
    for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
        long collectionTimeMs = bean.getCollectionTime();
        totalTimeMs += collectionTimeMs;
        // Replace all non alpha-numeric characters to '-'
        String normalizedKeyName = bean.getName().replaceAll("[^\\w]", "-");
        jvmGCTimeMsPerGCType.safeScope(normalizedKeyName).setValue(collectionTimeMs);
    }
    jvmGCTimeMs.setValue(totalTimeMs);
}
Also used : GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean)

Example 43 with GarbageCollectorMXBean

use of java.lang.management.GarbageCollectorMXBean 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 44 with GarbageCollectorMXBean

use of java.lang.management.GarbageCollectorMXBean in project graylog2-server by Graylog2.

the class GarbageCollectionWarningThread method doRun.

@Override
public void doRun() {
    for (final GarbageCollectorMXBean gc : garbageCollectors) {
        switch(gc.getName()) {
            case "ParNew":
            case "ConcurrentMarkSweep":
                LOG.debug("Skipping GC warning listener for concurrent collector {}.", gc.getName());
                continue;
        }
        LOG.debug("Installing GC warning listener for collector {}, total runtime threshold is {}.", gc.getName(), gcWarningThreshold);
        final NotificationEmitter emitter = (NotificationEmitter) gc;
        final NotificationListener listener = new NotificationListener() {

            @Override
            public void handleNotification(javax.management.Notification notification, Object handback) {
                if (GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION.equals(notification.getType())) {
                    final GcInfo gcInfo = GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData()).getGcInfo();
                    final Duration duration = Duration.milliseconds(gcInfo.getDuration());
                    if (duration.compareTo(gcWarningThreshold) > 0) {
                        LOG.warn("Last GC run with {} took longer than {} (last duration={})", gc.getName(), gcWarningThreshold, duration);
                        final Notification systemNotification = notificationService.buildNow().addNode(nodeId.toString()).addTimestamp(Tools.nowUTC()).addSeverity(Notification.Severity.URGENT).addType(Notification.Type.GC_TOO_LONG).addDetail("gc_name", gc.getName()).addDetail("gc_duration_ms", duration.toMilliseconds()).addDetail("gc_threshold_ms", gcWarningThreshold.toMilliseconds()).addDetail("gc_collection_count", gc.getCollectionCount()).addDetail("gc_collection_time", gc.getCollectionTime());
                        if (!notificationService.publishIfFirst(systemNotification)) {
                            LOG.debug("Couldn't publish notification: {}", notification);
                        }
                    }
                }
            }
        };
        emitter.addNotificationListener(listener, null, null);
    }
}
Also used : NotificationEmitter(javax.management.NotificationEmitter) GcInfo(com.sun.management.GcInfo) CompositeData(javax.management.openmbean.CompositeData) GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) Duration(com.github.joschi.jadconfig.util.Duration) Notification(org.graylog2.notifications.Notification) NotificationListener(javax.management.NotificationListener)

Example 45 with GarbageCollectorMXBean

use of java.lang.management.GarbageCollectorMXBean in project spring-boot by spring-projects.

the class SystemPublicMetrics method addGarbageCollectionMetrics.

/**
	 * Add garbage collection metrics.
	 * @param result the result
	 */
protected void addGarbageCollectionMetrics(Collection<Metric<?>> result) {
    List<GarbageCollectorMXBean> garbageCollectorMxBeans = ManagementFactory.getGarbageCollectorMXBeans();
    for (GarbageCollectorMXBean garbageCollectorMXBean : garbageCollectorMxBeans) {
        String name = beautifyGcName(garbageCollectorMXBean.getName());
        result.add(new Metric<>("gc." + name + ".count", garbageCollectorMXBean.getCollectionCount()));
        result.add(new Metric<>("gc." + name + ".time", garbageCollectorMXBean.getCollectionTime()));
    }
}
Also used : GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean)

Aggregations

GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)62 MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)10 RuntimeMXBean (java.lang.management.RuntimeMXBean)10 MemoryMXBean (java.lang.management.MemoryMXBean)8 HashMap (java.util.HashMap)8 MemoryUsage (java.lang.management.MemoryUsage)6 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)5 ThreadMXBean (java.lang.management.ThreadMXBean)4 ClassLoadingMXBean (java.lang.management.ClassLoadingMXBean)3 ObjectName (javax.management.ObjectName)3 Date (java.util.Date)2 LinkedHashMap (java.util.LinkedHashMap)2 NotificationEmitter (javax.management.NotificationEmitter)2 MetricGroup (org.apache.flink.metrics.MetricGroup)2 MetricsInfo (org.apache.hadoop.metrics2.MetricsInfo)2 Metric (com.codahale.metrics.Metric)1 Checkpoint (com.datatorrent.stram.api.Checkpoint)1 ContainerStatsEvent (com.datatorrent.stram.api.ContainerEvent.ContainerStatsEvent)1