|
| 1 | +package io.prometheus.client.hotspot; |
| 2 | + |
| 3 | +import com.sun.management.GarbageCollectionNotificationInfo; |
| 4 | +import com.sun.management.GcInfo; |
| 5 | +import io.prometheus.client.Collector; |
| 6 | +import io.prometheus.client.Counter; |
| 7 | + |
| 8 | +import javax.management.Notification; |
| 9 | +import javax.management.NotificationEmitter; |
| 10 | +import javax.management.NotificationListener; |
| 11 | +import javax.management.openmbean.CompositeData; |
| 12 | +import java.lang.management.GarbageCollectorMXBean; |
| 13 | +import java.lang.management.ManagementFactory; |
| 14 | +import java.lang.management.MemoryUsage; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +public class MemoryAllocationExports extends Collector { |
| 20 | + private final Counter allocatedCounter = Counter.build() |
| 21 | + .name("jvm_memory_pool_allocated_bytes_total") |
| 22 | + .help("Total bytes allocated in a given JVM memory pool. Only updated after GC, not continuously.") |
| 23 | + .labelNames("pool") |
| 24 | + .create(); |
| 25 | + |
| 26 | + public MemoryAllocationExports() { |
| 27 | + AllocationCountingNotificationListener listener = new AllocationCountingNotificationListener(allocatedCounter); |
| 28 | + for (GarbageCollectorMXBean garbageCollectorMXBean : ManagementFactory.getGarbageCollectorMXBeans()) { |
| 29 | + ((NotificationEmitter) garbageCollectorMXBean).addNotificationListener(listener, null, null); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public List<MetricFamilySamples> collect() { |
| 35 | + return allocatedCounter.collect(); |
| 36 | + } |
| 37 | + |
| 38 | + static class AllocationCountingNotificationListener implements NotificationListener { |
| 39 | + private final Map<String, Long> lastMemoryUsage = new HashMap<String, Long>(); |
| 40 | + private final Counter counter; |
| 41 | + |
| 42 | + AllocationCountingNotificationListener(Counter counter) { |
| 43 | + this.counter = counter; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public synchronized void handleNotification(Notification notification, Object handback) { |
| 48 | + GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData()); |
| 49 | + GcInfo gcInfo = info.getGcInfo(); |
| 50 | + Map<String, MemoryUsage> memoryUsageBeforeGc = gcInfo.getMemoryUsageBeforeGc(); |
| 51 | + Map<String, MemoryUsage> memoryUsageAfterGc = gcInfo.getMemoryUsageAfterGc(); |
| 52 | + for (Map.Entry<String, MemoryUsage> entry : memoryUsageBeforeGc.entrySet()) { |
| 53 | + String memoryPool = entry.getKey(); |
| 54 | + long before = entry.getValue().getUsed(); |
| 55 | + long after = memoryUsageAfterGc.get(memoryPool).getUsed(); |
| 56 | + handleMemoryPool(memoryPool, before, after); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Visible for testing |
| 61 | + void handleMemoryPool(String memoryPool, long before, long after) { |
| 62 | + /* |
| 63 | + * Calculate increase in the memory pool by comparing memory used |
| 64 | + * after last GC, before this GC, and after this GC. |
| 65 | + * See ascii illustration below. |
| 66 | + * Make sure to count only increases and ignore decreases. |
| 67 | + * (Typically a pool will only increase between GCs or during GCs, not both. |
| 68 | + * E.g. eden pools between GCs. Survivor and old generation pools during GCs.) |
| 69 | + * |
| 70 | + * |<-- diff1 -->|<-- diff2 -->| |
| 71 | + * Timeline: |-- last GC --| |---- GC -----| |
| 72 | + * ___^__ ___^____ ___^___ |
| 73 | + * Mem. usage vars: / last \ / before \ / after \ |
| 74 | + */ |
| 75 | + |
| 76 | + // Get last memory usage after GC and remember memory used after for next time |
| 77 | + long last = getAndSet(lastMemoryUsage, memoryPool, after); |
| 78 | + // Difference since last GC |
| 79 | + long diff1 = before - last; |
| 80 | + // Difference during this GC |
| 81 | + long diff2 = after - before; |
| 82 | + // Make sure to only count increases |
| 83 | + if (diff1 < 0) { |
| 84 | + diff1 = 0; |
| 85 | + } |
| 86 | + if (diff2 < 0) { |
| 87 | + diff2 = 0; |
| 88 | + } |
| 89 | + long increase = diff1 + diff2; |
| 90 | + if (increase > 0) { |
| 91 | + counter.labels(memoryPool).inc(increase); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private static long getAndSet(Map<String, Long> map, String key, long value) { |
| 96 | + Long last = map.put(key, value); |
| 97 | + return last == null ? 0 : last; |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments