Search in sources :

Example 16 with MemoryUsage

use of java.lang.management.MemoryUsage 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 17 with MemoryUsage

use of java.lang.management.MemoryUsage in project bazel by bazelbuild.

the class MemoryProfiler method markPhase.

public synchronized void markPhase(ProfilePhase nextPhase) {
    if (memoryProfile != null) {
        String name = currentPhase.description;
        ManagementFactory.getMemoryMXBean().gc();
        MemoryUsage memoryUsage = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
        memoryProfile.println(name + ":heap:init:" + memoryUsage.getInit());
        memoryProfile.println(name + ":heap:used:" + memoryUsage.getUsed());
        memoryProfile.println(name + ":heap:commited:" + memoryUsage.getCommitted());
        memoryProfile.println(name + ":heap:max:" + memoryUsage.getMax());
        memoryUsage = ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage();
        memoryProfile.println(name + ":non-heap:init:" + memoryUsage.getInit());
        memoryProfile.println(name + ":non-heap:used:" + memoryUsage.getUsed());
        memoryProfile.println(name + ":non-heap:commited:" + memoryUsage.getCommitted());
        memoryProfile.println(name + ":non-heap:max:" + memoryUsage.getMax());
        currentPhase = nextPhase;
    }
}
Also used : MemoryUsage(java.lang.management.MemoryUsage)

Example 18 with MemoryUsage

use of java.lang.management.MemoryUsage in project gradle by gradle.

the class GarbageCollectionStats method calculateAverageUsage.

static long calculateAverageUsage(Set<GarbageCollectionEvent> events) {
    if (events.size() < 1) {
        return -1;
    }
    long total = 0;
    long firstCount = 0;
    long lastCount = 0;
    for (GarbageCollectionEvent event : events) {
        // Skip if the garbage collector did not fire in between events
        if (event.getCount() == lastCount || event.getCount() == 0) {
            continue;
        }
        MemoryUsage usage = event.getUsage();
        if (firstCount == 0) {
            firstCount = event.getCount();
            total += usage.getUsed();
        } else {
            total += usage.getUsed() * (event.getCount() - lastCount);
        }
        lastCount = event.getCount();
    }
    if (lastCount == 0 || lastCount == firstCount) {
        return -1;
    } else {
        long totalCount = lastCount - firstCount + 1;
        return total / totalCount;
    }
}
Also used : MemoryUsage(java.lang.management.MemoryUsage)

Example 19 with MemoryUsage

use of java.lang.management.MemoryUsage in project jdk8u_jdk by JetBrains.

the class MemoryNotifInfoCompositeData method createGoodCompositeData.

public static void createGoodCompositeData() throws Exception {
    // get the CompositeType for MemoryUsage
    validItemTypes[USAGE] = OpenTypeConverter.toOpenType(MemoryUsage.class);
    CompositeType ct = new CompositeType("MyCompositeType", "CompositeType for MemoryNotificationInfo", validItemNames, validItemNames, validItemTypes);
    CompositeData cd = new CompositeDataSupport(ct, validItemNames, values);
    MemoryNotificationInfo info = MemoryNotificationInfo.from(cd);
    if (!info.getPoolName().equals(values[POOL_NAME])) {
        throw new RuntimeException("pool name = " + info.getPoolName() + " expected = " + values[POOL_NAME]);
    }
    if (info.getCount() != ((Long) values[COUNT]).longValue()) {
        throw new RuntimeException("count = " + info.getCount() + " expected = " + values[COUNT]);
    }
    if (info.getUsage().getInit() != 0) {
        throw new RuntimeException("usage init = " + info.getUsage().getInit() + " expected = 0");
    }
    if (info.getUsage().getUsed() != 100) {
        throw new RuntimeException("usage used = " + info.getUsage().getUsed() + " expected = 100");
    }
    if (info.getUsage().getCommitted() != 1000) {
        throw new RuntimeException("usage committed = " + info.getUsage().getCommitted() + " expected = 1000");
    }
    if (info.getUsage().getMax() != 5000) {
        throw new RuntimeException("usage max = " + info.getUsage().getMax() + " expected = 5000");
    }
    System.out.print("Pool name = " + info.getPoolName());
    System.out.println(" Count = " + info.getCount());
    System.out.println("Usage = " + info.getUsage());
}
Also used : MemoryNotificationInfo(java.lang.management.MemoryNotificationInfo) MemoryUsageCompositeData(sun.management.MemoryUsageCompositeData) MemoryUsage(java.lang.management.MemoryUsage)

Example 20 with MemoryUsage

use of java.lang.management.MemoryUsage in project jdk8u_jdk by JetBrains.

the class LastGCInfo method checkGcInfo.

private static void checkGcInfo(String name, GcInfo info) throws Exception {
    System.out.println("GC statistic for : " + name);
    System.out.print("GC #" + info.getId());
    System.out.print(" start:" + info.getStartTime());
    System.out.print(" end:" + info.getEndTime());
    System.out.println(" (" + info.getDuration() + "ms)");
    Map usage = info.getMemoryUsageBeforeGc();
    List pnames = new ArrayList();
    for (Iterator iter = usage.entrySet().iterator(); iter.hasNext(); ) {
        Map.Entry entry = (Map.Entry) iter.next();
        String poolname = (String) entry.getKey();
        pnames.add(poolname);
        MemoryUsage busage = (MemoryUsage) entry.getValue();
        MemoryUsage ausage = (MemoryUsage) info.getMemoryUsageAfterGc().get(poolname);
        if (ausage == null) {
            throw new RuntimeException("After Gc Memory does not exist" + " for " + poolname);
        }
        System.out.println("Usage for pool " + poolname);
        System.out.println("   Before GC: " + busage);
        System.out.println("   After GC: " + ausage);
    }
    // check if memory usage for all memory pools are returned
    List pools = ManagementFactory.getMemoryPoolMXBeans();
    for (Iterator iter = pools.iterator(); iter.hasNext(); ) {
        MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
        if (!pnames.contains(p.getName())) {
            throw new RuntimeException("GcInfo does not contain " + "memory usage for pool " + p.getName());
        }
    }
}
Also used : MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) MemoryUsage(java.lang.management.MemoryUsage)

Aggregations

MemoryUsage (java.lang.management.MemoryUsage)63 MemoryMXBean (java.lang.management.MemoryMXBean)13 MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)11 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)6 RuntimeMXBean (java.lang.management.RuntimeMXBean)6 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)4 ThreadMXBean (java.lang.management.ThreadMXBean)4 HashMap (java.util.HashMap)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 NotNull (org.jetbrains.annotations.NotNull)3 ApplicationInfo (com.intellij.openapi.application.ApplicationInfo)2 GarbageCollectionNotificationInfo (com.sun.management.GarbageCollectionNotificationInfo)2 BufferPoolMXBean (java.lang.management.BufferPoolMXBean)2 ClassLoadingMXBean (java.lang.management.ClassLoadingMXBean)2 Date (java.util.Date)2 CacheServiceMBean (org.apache.cassandra.service.CacheServiceMBean)2 Metric (com.codahale.metrics.Metric)1 RatioGauge (com.codahale.metrics.RatioGauge)1