From 19ca615e385e005319120d6f976987327d9decbc Mon Sep 17 00:00:00 2001 From: liujianhuan Date: Mon, 25 Apr 2022 19:28:29 +0800 Subject: [PATCH 1/6] jvm gc to mxbeans --- .../druid/java/util/metrics/JvmMonitor.java | 200 +++++++----------- 1 file changed, 82 insertions(+), 118 deletions(-) diff --git a/core/src/main/java/org/apache/druid/java/util/metrics/JvmMonitor.java b/core/src/main/java/org/apache/druid/java/util/metrics/JvmMonitor.java index c36d0f92b210..09dd259b1c90 100644 --- a/core/src/main/java/org/apache/druid/java/util/metrics/JvmMonitor.java +++ b/core/src/main/java/org/apache/druid/java/util/metrics/JvmMonitor.java @@ -22,17 +22,14 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableMap; -import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.java.util.common.Pair; import org.apache.druid.java.util.common.logger.Logger; import org.apache.druid.java.util.emitter.service.ServiceEmitter; import org.apache.druid.java.util.emitter.service.ServiceMetricEvent; -import org.gridkit.lab.jvm.perfdata.JStatData; -import org.gridkit.lab.jvm.perfdata.JStatData.LongCounter; -import org.gridkit.lab.jvm.perfdata.JStatData.StringCounter; -import org.gridkit.lab.jvm.perfdata.JStatData.TickCounter; import javax.annotation.Nullable; import java.lang.management.BufferPoolMXBean; +import java.lang.management.GarbageCollectorMXBean; import java.lang.management.ManagementFactory; import java.lang.management.MemoryPoolMXBean; import java.lang.management.MemoryType; @@ -46,7 +43,6 @@ public class JvmMonitor extends FeedDefiningMonitor private static final Logger log = new Logger(JvmMonitor.class); private final Map dimensions; - private final long pid; @VisibleForTesting @Nullable @@ -66,16 +62,10 @@ public JvmMonitor(Map dimensions) } public JvmMonitor(Map dimensions, String feed) - { - this(dimensions, feed, JvmPidDiscoverer.instance()); - } - - public JvmMonitor(Map dimensions, String feed, PidDiscoverer pidDiscoverer) { super(feed); Preconditions.checkNotNull(dimensions); this.dimensions = ImmutableMap.copyOf(dimensions); - this.pid = Preconditions.checkNotNull(pidDiscoverer).getPid(); this.collector = AllocationMetricCollectors.getAllocationMetricCollector(); this.gcCounters = tryCreateGcCounters(); } @@ -101,10 +91,6 @@ private void emitThreadAllocationMetrics(ServiceEmitter emitter) } } - /** - * These metrics are going to be replaced by new jvm/gc/mem/* metrics - */ - @Deprecated private void emitJvmMemMetrics(ServiceEmitter emitter) { // I have no idea why, but jvm/mem is slightly more than the sum of jvm/pool. Let's just include @@ -183,95 +169,117 @@ private GcCounters tryCreateGcCounters() */ private class GcCounters { - private final List generations = new ArrayList<>(); + private final List generations = new ArrayList<>(); + private final List spaces = new ArrayList<>(); GcCounters() { - // connect to itself - final JStatData jStatData = JStatData.connect(pid); - final Map> jStatCounters = jStatData.getAllCounters(); - - generations.add(new GcGeneration(jStatCounters, 0, "young")); - generations.add(new GcGeneration(jStatCounters, 1, "old")); - // Removed in Java 8 but still actual for previous Java versions - if (jStatCounters.containsKey("sun.gc.generation.2.name")) { - generations.add(new GcGeneration(jStatCounters, 2, "perm")); + List collectorMxBeans = ManagementFactory.getGarbageCollectorMXBeans(); + for (GarbageCollectorMXBean collectorMxBean : collectorMxBeans) { + generations.add(new GcCollector(collectorMxBean)); + } + + List memoryPoolMxBeans = ManagementFactory.getMemoryPoolMXBeans(); + for (MemoryPoolMXBean memoryPoolMxBean : memoryPoolMxBeans) { + MemoryUsage collectionUsage = memoryPoolMxBean.getCollectionUsage(); + if (collectionUsage != null) { + spaces.add(new GcGenerationSpace(collectionUsage, memoryPoolMxBean.getName())); + } } } void emit(ServiceEmitter emitter, Map dimensions) { - for (GcGeneration generation : generations) { + for (GcCollector generation : generations) { generation.emit(emitter, dimensions); } + + for (GcGenerationSpace space : spaces) { + space.emit(emitter, dimensions); + } } } - private class GcGeneration + private class GcCollector { - private final String name; + private final String generation; + private final String collectorName; private final GcGenerationCollector collector; - private final List spaces = new ArrayList<>(); - GcGeneration(Map> jStatCounters, long genIndex, String name) + private static final String GC_YOUNG_GENERATION_NAME = "young"; + private static final String GC_OLD_GENERATION_NAME = "old"; + private static final String GC_CMS_NAME = "cms"; + private static final String GC_G1_NAME = "g1"; + private static final String GC_PARALLEL_NAME = "parallel"; + private static final String GC_SERIAL_NAME = "serial"; + + GcCollector(GarbageCollectorMXBean gcBean) { - this.name = StringUtils.toLowerCase(name); + Pair gcNamePair = getReadableName(gcBean.getName()); + this.generation = gcNamePair.lhs; + this.collectorName = gcNamePair.rhs; - long spacesCount = ((JStatData.LongCounter) jStatCounters.get( - StringUtils.format("sun.gc.generation.%d.spaces", genIndex) - )).getLong(); - for (long spaceIndex = 0; spaceIndex < spacesCount; spaceIndex++) { - spaces.add(new GcGenerationSpace(jStatCounters, genIndex, spaceIndex)); - } + collector = new GcGenerationCollector(gcBean); + } + + private Pair getReadableName(String name) + { + switch (name) { + //CMS + case "ParNew": + return new Pair<>(GC_YOUNG_GENERATION_NAME, GC_CMS_NAME); + case "ConcurrentMarkSweep": + return new Pair<>(GC_OLD_GENERATION_NAME, GC_CMS_NAME); + + // G1 + case "G1 Young Generation": + return new Pair<>(GC_YOUNG_GENERATION_NAME, GC_G1_NAME); + case "G1 Old Generation": + return new Pair<>(GC_OLD_GENERATION_NAME, GC_G1_NAME); + + // Parallel + case "PS Scavenge": + return new Pair<>(GC_YOUNG_GENERATION_NAME, GC_PARALLEL_NAME); + case "PS MarkSweep": + return new Pair<>(GC_OLD_GENERATION_NAME, GC_PARALLEL_NAME); + + // Serial + case "Copy": + return new Pair<>(GC_YOUNG_GENERATION_NAME, GC_SERIAL_NAME); + case "MarkSweepCompact": + return new Pair<>(GC_OLD_GENERATION_NAME, GC_SERIAL_NAME); - if (jStatCounters.containsKey(StringUtils.format("sun.gc.collector.%d.name", genIndex))) { - collector = new GcGenerationCollector(jStatCounters, genIndex); - } else { - collector = null; + default: + return new Pair<>(name, name); } } void emit(ServiceEmitter emitter, Map dimensions) { ImmutableMap.Builder dimensionsCopyBuilder = ImmutableMap - .builder() - .putAll(dimensions) - .put("gcGen", new String[]{name}); + .builder() + .putAll(dimensions) + .put("gcGen", new String[]{generation}); - if (collector != null) { - dimensionsCopyBuilder.put("gcName", new String[]{collector.name}); - } + dimensionsCopyBuilder.put("gcName", new String[]{collectorName}); Map dimensionsCopy = dimensionsCopyBuilder.build(); if (collector != null) { collector.emit(emitter, dimensionsCopy); } - - for (GcGenerationSpace space : spaces) { - space.emit(emitter, dimensionsCopy); - } } } private class GcGenerationCollector { - private final String name; - private final LongCounter invocationsCounter; - private final TickCounter cpuCounter; private long lastInvocations = 0; private long lastCpuNanos = 0; + private final GarbageCollectorMXBean gcBean; - GcGenerationCollector(Map> jStatCounters, long genIndex) + GcGenerationCollector(GarbageCollectorMXBean gcBean) { - String collectorKeyPrefix = StringUtils.format("sun.gc.collector.%d", genIndex); - - String nameKey = StringUtils.format("%s.name", collectorKeyPrefix); - StringCounter nameCounter = (StringCounter) jStatCounters.get(nameKey); - name = getReadableName(nameCounter.getString()); - - invocationsCounter = (LongCounter) jStatCounters.get(StringUtils.format("%s.invocations", collectorKeyPrefix)); - cpuCounter = (TickCounter) jStatCounters.get(StringUtils.format("%s.time", collectorKeyPrefix)); + this.gcBean = gcBean; } void emit(ServiceEmitter emitter, Map dimensions) @@ -279,69 +287,25 @@ void emit(ServiceEmitter emitter, Map dimensions) final ServiceMetricEvent.Builder builder = builder(); MonitorUtils.addDimensionsToBuilder(builder, dimensions); - long newInvocations = invocationsCounter.getLong(); + long newInvocations = gcBean.getCollectionCount(); emitter.emit(builder.build("jvm/gc/count", newInvocations - lastInvocations)); lastInvocations = newInvocations; - long newCpuNanos = cpuCounter.getNanos(); + long newCpuNanos = gcBean.getCollectionTime(); emitter.emit(builder.build("jvm/gc/cpu", newCpuNanos - lastCpuNanos)); lastCpuNanos = newCpuNanos; } - - private String getReadableName(String name) - { - switch (name) { - // Young gen - case "Copy": - return "serial"; - case "PSScavenge": - return "parallel"; - case "PCopy": - return "cms"; - case "G1 incremental collections": - return "g1"; - case "Shenandoah partial": - return "shenandoah"; - - // Old gen - case "MCS": - return "serial"; - case "PSParallelCompact": - return "parallel"; - case "CMS": - return "cms"; - case "G1 stop-the-world full collections": - return "g1"; - case "Shenandoah full": - return "shenandoah"; - - default: - return name; - } - } } private class GcGenerationSpace { private final String name; + private final MemoryUsage memoryUsage; - private final LongCounter maxCounter; - private final LongCounter capacityCounter; - private final LongCounter usedCounter; - private final LongCounter initCounter; - - GcGenerationSpace(Map> jStatCounters, long genIndex, long spaceIndex) + public GcGenerationSpace(MemoryUsage memoryUsage, String name) { - String spaceKeyPrefix = StringUtils.format("sun.gc.generation.%d.space.%d", genIndex, spaceIndex); - - String nameKey = StringUtils.format("%s.name", spaceKeyPrefix); - StringCounter nameCounter = (StringCounter) jStatCounters.get(nameKey); - name = StringUtils.toLowerCase(nameCounter.toString()); - - maxCounter = (LongCounter) jStatCounters.get(StringUtils.format("%s.maxCapacity", spaceKeyPrefix)); - capacityCounter = (LongCounter) jStatCounters.get(StringUtils.format("%s.capacity", spaceKeyPrefix)); - usedCounter = (LongCounter) jStatCounters.get(StringUtils.format("%s.used", spaceKeyPrefix)); - initCounter = (LongCounter) jStatCounters.get(StringUtils.format("%s.initCapacity", spaceKeyPrefix)); + this.memoryUsage = memoryUsage; + this.name = name; } void emit(ServiceEmitter emitter, Map dimensions) @@ -351,10 +315,10 @@ void emit(ServiceEmitter emitter, Map dimensions) builder.setDimension("gcGenSpaceName", name); - emitter.emit(builder.build("jvm/gc/mem/max", maxCounter.getLong())); - emitter.emit(builder.build("jvm/gc/mem/capacity", capacityCounter.getLong())); - emitter.emit(builder.build("jvm/gc/mem/used", usedCounter.getLong())); - emitter.emit(builder.build("jvm/gc/mem/init", initCounter.getLong())); + emitter.emit(builder.build("jvm/gc/mem/max", memoryUsage.getMax())); + emitter.emit(builder.build("jvm/gc/mem/capacity", memoryUsage.getCommitted())); + emitter.emit(builder.build("jvm/gc/mem/used", memoryUsage.getUsed())); + emitter.emit(builder.build("jvm/gc/mem/init", memoryUsage.getInit())); } } } From c0253d3d2dd0596f593c2b736e9d84d538f2af85 Mon Sep 17 00:00:00 2001 From: liujianhuan Date: Tue, 26 Apr 2022 10:59:35 +0800 Subject: [PATCH 2/6] add zgc and shenandoah #12476 --- .../druid/java/util/metrics/JvmMonitor.java | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/org/apache/druid/java/util/metrics/JvmMonitor.java b/core/src/main/java/org/apache/druid/java/util/metrics/JvmMonitor.java index 09dd259b1c90..b83e668ebdc1 100644 --- a/core/src/main/java/org/apache/druid/java/util/metrics/JvmMonitor.java +++ b/core/src/main/java/org/apache/druid/java/util/metrics/JvmMonitor.java @@ -170,7 +170,6 @@ private GcCounters tryCreateGcCounters() private class GcCounters { private final List generations = new ArrayList<>(); - private final List spaces = new ArrayList<>(); GcCounters() { @@ -179,13 +178,6 @@ private class GcCounters generations.add(new GcCollector(collectorMxBean)); } - List memoryPoolMxBeans = ManagementFactory.getMemoryPoolMXBeans(); - for (MemoryPoolMXBean memoryPoolMxBean : memoryPoolMxBeans) { - MemoryUsage collectionUsage = memoryPoolMxBean.getCollectionUsage(); - if (collectionUsage != null) { - spaces.add(new GcGenerationSpace(collectionUsage, memoryPoolMxBean.getName())); - } - } } void emit(ServiceEmitter emitter, Map dimensions) @@ -193,10 +185,6 @@ void emit(ServiceEmitter emitter, Map dimensions) for (GcCollector generation : generations) { generation.emit(emitter, dimensions); } - - for (GcGenerationSpace space : spaces) { - space.emit(emitter, dimensions); - } } } @@ -205,13 +193,18 @@ private class GcCollector private final String generation; private final String collectorName; private final GcGenerationCollector collector; + private final List spaces = new ArrayList<>(); private static final String GC_YOUNG_GENERATION_NAME = "young"; private static final String GC_OLD_GENERATION_NAME = "old"; + private static final String GC_ZGC_GENERATION_NAME = "zgc"; + private static final String GC_CMS_NAME = "cms"; private static final String GC_G1_NAME = "g1"; private static final String GC_PARALLEL_NAME = "parallel"; private static final String GC_SERIAL_NAME = "serial"; + private static final String GC_ZGC_NAME = "zgc"; + private static final String GC_SHENANDOAN_NAME = "shenandoah"; GcCollector(GarbageCollectorMXBean gcBean) { @@ -220,6 +213,14 @@ private class GcCollector this.collectorName = gcNamePair.rhs; collector = new GcGenerationCollector(gcBean); + + List memoryPoolMxBeans = ManagementFactory.getMemoryPoolMXBeans(); + for (MemoryPoolMXBean memoryPoolMxBean : memoryPoolMxBeans) { + MemoryUsage collectionUsage = memoryPoolMxBean.getCollectionUsage(); + if (collectionUsage != null) { + spaces.add(new GcGenerationSpace(collectionUsage, memoryPoolMxBean.getName())); + } + } } private Pair getReadableName(String name) @@ -249,6 +250,16 @@ private Pair getReadableName(String name) case "MarkSweepCompact": return new Pair<>(GC_OLD_GENERATION_NAME, GC_SERIAL_NAME); + //zgc + case "ZGC": + return new Pair<>(GC_ZGC_GENERATION_NAME, GC_ZGC_NAME); + + //shenanoan + case "Shenandoah Cycles": + return new Pair<>(GC_YOUNG_GENERATION_NAME, GC_SHENANDOAN_NAME); + case "Shenandoah Pauses": + return new Pair<>(GC_OLD_GENERATION_NAME, GC_SHENANDOAN_NAME); + default: return new Pair<>(name, name); } @@ -268,6 +279,10 @@ void emit(ServiceEmitter emitter, Map dimensions) if (collector != null) { collector.emit(emitter, dimensionsCopy); } + + for (GcGenerationSpace space : spaces) { + space.emit(emitter, dimensionsCopy); + } } } From 0c4bb94b4cddec9d3279956c7b899a4efeeee0b7 Mon Sep 17 00:00:00 2001 From: liujianhuan Date: Wed, 27 Apr 2022 14:30:00 +0800 Subject: [PATCH 3/6] remove tryCreateGcCounter --- .../druid/java/util/metrics/JvmMonitor.java | 30 +++++-------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/core/src/main/java/org/apache/druid/java/util/metrics/JvmMonitor.java b/core/src/main/java/org/apache/druid/java/util/metrics/JvmMonitor.java index b83e668ebdc1..e5b5f9c8ba8d 100644 --- a/core/src/main/java/org/apache/druid/java/util/metrics/JvmMonitor.java +++ b/core/src/main/java/org/apache/druid/java/util/metrics/JvmMonitor.java @@ -67,7 +67,7 @@ public JvmMonitor(Map dimensions, String feed) Preconditions.checkNotNull(dimensions); this.dimensions = ImmutableMap.copyOf(dimensions); this.collector = AllocationMetricCollectors.getAllocationMetricCollector(); - this.gcCounters = tryCreateGcCounters(); + this.gcCounters = new GcCounters(); } @Override @@ -146,22 +146,6 @@ private void emitGcMetrics(ServiceEmitter emitter) } } - @Nullable - private GcCounters tryCreateGcCounters() - { - try { - return new GcCounters(); - } - catch (RuntimeException e) { - // in JDK11 jdk.internal.perf.Perf is not accessible, unless - // --add-exports java.base/jdk.internal.perf=ALL-UNNAMED is set - log.warn("Cannot initialize GC counters. If running JDK11 and above," - + " add --add-exports java.base/jdk.internal.perf=ALL-UNNAMED" - + " to the JVM arguments to enable GC counters."); - } - return null; - } - /** * The following GC-related code is partially based on * https://github.com/aragozin/jvm-tools/blob/e0e37692648951440aa1a4ea5046261cb360df70/ @@ -169,26 +153,26 @@ private GcCounters tryCreateGcCounters() */ private class GcCounters { - private final List generations = new ArrayList<>(); + private final List generations = new ArrayList<>(); GcCounters() { List collectorMxBeans = ManagementFactory.getGarbageCollectorMXBeans(); for (GarbageCollectorMXBean collectorMxBean : collectorMxBeans) { - generations.add(new GcCollector(collectorMxBean)); + generations.add(new GcCollectors(collectorMxBean)); } } void emit(ServiceEmitter emitter, Map dimensions) { - for (GcCollector generation : generations) { + for (GcCollectors generation : generations) { generation.emit(emitter, dimensions); } } } - private class GcCollector + private class GcCollectors { private final String generation; private final String collectorName; @@ -206,7 +190,7 @@ private class GcCollector private static final String GC_ZGC_NAME = "zgc"; private static final String GC_SHENANDOAN_NAME = "shenandoah"; - GcCollector(GarbageCollectorMXBean gcBean) + GcCollectors(GarbageCollectorMXBean gcBean) { Pair gcNamePair = getReadableName(gcBean.getName()); this.generation = gcNamePair.lhs; @@ -254,7 +238,7 @@ private Pair getReadableName(String name) case "ZGC": return new Pair<>(GC_ZGC_GENERATION_NAME, GC_ZGC_NAME); - //shenanoan + //Shenandoah case "Shenandoah Cycles": return new Pair<>(GC_YOUNG_GENERATION_NAME, GC_SHENANDOAN_NAME); case "Shenandoah Pauses": From 841fa8f73f454e3cb1d4644920a806a536719e74 Mon Sep 17 00:00:00 2001 From: liujianhuan Date: Thu, 5 May 2022 11:07:04 +0800 Subject: [PATCH 4/6] separate the space collector --- .../druid/java/util/metrics/JvmMonitor.java | 110 ++++++++++-------- .../java/util/metrics/JvmMonitorTest.java | 2 +- 2 files changed, 65 insertions(+), 47 deletions(-) diff --git a/core/src/main/java/org/apache/druid/java/util/metrics/JvmMonitor.java b/core/src/main/java/org/apache/druid/java/util/metrics/JvmMonitor.java index 8c91605b7314..af271dda0f08 100644 --- a/core/src/main/java/org/apache/druid/java/util/metrics/JvmMonitor.java +++ b/core/src/main/java/org/apache/druid/java/util/metrics/JvmMonitor.java @@ -44,7 +44,7 @@ public class JvmMonitor extends FeedDefiningMonitor @VisibleForTesting @Nullable - final GcCounters gcCounters; + final GcCollectors gcCollectors; @Nullable private final AllocationMetricCollector collector; @@ -65,7 +65,7 @@ public JvmMonitor(Map dimensions, String feed) Preconditions.checkNotNull(dimensions); this.dimensions = ImmutableMap.copyOf(dimensions); this.collector = AllocationMetricCollectors.getAllocationMetricCollector(); - this.gcCounters = new GcCounters(); + this.gcCollectors = new GcCollectors(); } @Override @@ -89,6 +89,10 @@ private void emitThreadAllocationMetrics(ServiceEmitter emitter) } } + /** + * These metrics are going to be replaced by new jvm/gc/mem/* metrics + */ + @Deprecated private void emitJvmMemMetrics(ServiceEmitter emitter) { // I have no idea why, but jvm/mem is slightly more than the sum of jvm/pool. Let's just include @@ -139,70 +143,66 @@ private void emitDirectMemMetrics(ServiceEmitter emitter) private void emitGcMetrics(ServiceEmitter emitter) { - if (gcCounters != null) { - gcCounters.emit(emitter, dimensions); + if (gcCollectors != null) { + gcCollectors.emit(emitter, dimensions); } } - /** - * The following GC-related code is partially based on - * https://github.com/aragozin/jvm-tools/blob/e0e37692648951440aa1a4ea5046261cb360df70/ - * sjk-core/src/main/java/org/gridkit/jvmtool/PerfCounterGcCpuUsageMonitor.java - */ - private class GcCounters + private class GcCollectors { - private final List generations = new ArrayList<>(); + private final List collectors = new ArrayList<>(); + private final List spaceCollectors = new ArrayList<>(); - GcCounters() + GcCollectors() { List collectorMxBeans = ManagementFactory.getGarbageCollectorMXBeans(); for (GarbageCollectorMXBean collectorMxBean : collectorMxBeans) { - generations.add(new GcCollectors(collectorMxBean)); + collectors.add(new GcCollector(collectorMxBean)); + } + + List memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans(); + for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) { + spaceCollectors.add(new GcSpaceCollector(memoryPoolMXBean)); } } void emit(ServiceEmitter emitter, Map dimensions) { - for (GcCollectors generation : generations) { - generation.emit(emitter, dimensions); + for (GcCollector collector : collectors) { + collector.emit(emitter, dimensions); + } + + for (GcSpaceCollector spaceCollector : spaceCollectors) { + spaceCollector.emit(emitter, dimensions); } } } - private class GcCollectors + private class GcCollector { private final String generation; private final String collectorName; private final GcGenerationCollector collector; - private final List spaces = new ArrayList<>(); private static final String GC_YOUNG_GENERATION_NAME = "young"; private static final String GC_OLD_GENERATION_NAME = "old"; private static final String GC_ZGC_GENERATION_NAME = "zgc"; - private static final String GC_CMS_NAME = "cms"; - private static final String GC_G1_NAME = "g1"; - private static final String GC_PARALLEL_NAME = "parallel"; - private static final String GC_SERIAL_NAME = "serial"; - private static final String GC_ZGC_NAME = "zgc"; - private static final String GC_SHENANDOAN_NAME = "shenandoah"; + private static final String CMS_COLLECTOR_NAME = "cms"; + private static final String G1_COLLECTOR_NAME = "g1"; + private static final String PARALLEL_COLLECTOR_NAME = "parallel"; + private static final String SERIAL_COLLECTOR_NAME = "serial"; + private static final String ZGC_COLLECTOR_NAME = "zgc"; + private static final String SHENANDOAN_COLLECTOR_NAME = "shenandoah"; - GcCollectors(GarbageCollectorMXBean gcBean) + GcCollector(GarbageCollectorMXBean gcBean) { Pair gcNamePair = getReadableName(gcBean.getName()); this.generation = gcNamePair.lhs; this.collectorName = gcNamePair.rhs; collector = new GcGenerationCollector(gcBean); - - List memoryPoolMxBeans = ManagementFactory.getMemoryPoolMXBeans(); - for (MemoryPoolMXBean memoryPoolMxBean : memoryPoolMxBeans) { - MemoryUsage collectionUsage = memoryPoolMxBean.getCollectionUsage(); - if (collectionUsage != null) { - spaces.add(new GcGenerationSpace(collectionUsage, memoryPoolMxBean.getName())); - } - } } private Pair getReadableName(String name) @@ -210,37 +210,37 @@ private Pair getReadableName(String name) switch (name) { //CMS case "ParNew": - return new Pair<>(GC_YOUNG_GENERATION_NAME, GC_CMS_NAME); + return new Pair<>(GC_YOUNG_GENERATION_NAME, CMS_COLLECTOR_NAME); case "ConcurrentMarkSweep": - return new Pair<>(GC_OLD_GENERATION_NAME, GC_CMS_NAME); + return new Pair<>(GC_OLD_GENERATION_NAME, CMS_COLLECTOR_NAME); // G1 case "G1 Young Generation": - return new Pair<>(GC_YOUNG_GENERATION_NAME, GC_G1_NAME); + return new Pair<>(GC_YOUNG_GENERATION_NAME, G1_COLLECTOR_NAME); case "G1 Old Generation": - return new Pair<>(GC_OLD_GENERATION_NAME, GC_G1_NAME); + return new Pair<>(GC_OLD_GENERATION_NAME, G1_COLLECTOR_NAME); // Parallel case "PS Scavenge": - return new Pair<>(GC_YOUNG_GENERATION_NAME, GC_PARALLEL_NAME); + return new Pair<>(GC_YOUNG_GENERATION_NAME, PARALLEL_COLLECTOR_NAME); case "PS MarkSweep": - return new Pair<>(GC_OLD_GENERATION_NAME, GC_PARALLEL_NAME); + return new Pair<>(GC_OLD_GENERATION_NAME, PARALLEL_COLLECTOR_NAME); // Serial case "Copy": - return new Pair<>(GC_YOUNG_GENERATION_NAME, GC_SERIAL_NAME); + return new Pair<>(GC_YOUNG_GENERATION_NAME, SERIAL_COLLECTOR_NAME); case "MarkSweepCompact": - return new Pair<>(GC_OLD_GENERATION_NAME, GC_SERIAL_NAME); + return new Pair<>(GC_OLD_GENERATION_NAME, SERIAL_COLLECTOR_NAME); //zgc case "ZGC": - return new Pair<>(GC_ZGC_GENERATION_NAME, GC_ZGC_NAME); + return new Pair<>(GC_ZGC_GENERATION_NAME, ZGC_COLLECTOR_NAME); //Shenandoah case "Shenandoah Cycles": - return new Pair<>(GC_YOUNG_GENERATION_NAME, GC_SHENANDOAN_NAME); + return new Pair<>(GC_YOUNG_GENERATION_NAME, SHENANDOAN_COLLECTOR_NAME); case "Shenandoah Pauses": - return new Pair<>(GC_OLD_GENERATION_NAME, GC_SHENANDOAN_NAME); + return new Pair<>(GC_OLD_GENERATION_NAME, SHENANDOAN_COLLECTOR_NAME); default: return new Pair<>(name, name); @@ -262,9 +262,6 @@ void emit(ServiceEmitter emitter, Map dimensions) collector.emit(emitter, dimensionsCopy); } - for (GcGenerationSpace space : spaces) { - space.emit(emitter, dimensionsCopy); - } } } @@ -294,6 +291,27 @@ void emit(ServiceEmitter emitter, Map dimensions) } } + private class GcSpaceCollector + { + + private final List spaces = new ArrayList<>(); + + public GcSpaceCollector(MemoryPoolMXBean memoryPoolMxBean) + { + MemoryUsage collectionUsage = memoryPoolMxBean.getCollectionUsage(); + if (collectionUsage != null) { + spaces.add(new GcGenerationSpace(collectionUsage, memoryPoolMxBean.getName())); + } + } + + void emit(ServiceEmitter emitter, Map dimensions) + { + for (GcGenerationSpace space : spaces) { + space.emit(emitter, dimensions); + } + } + } + private class GcGenerationSpace { private final String name; diff --git a/core/src/test/java/org/apache/druid/java/util/metrics/JvmMonitorTest.java b/core/src/test/java/org/apache/druid/java/util/metrics/JvmMonitorTest.java index 1e5cff5016b2..24af5db4c56f 100644 --- a/core/src/test/java/org/apache/druid/java/util/metrics/JvmMonitorTest.java +++ b/core/src/test/java/org/apache/druid/java/util/metrics/JvmMonitorTest.java @@ -41,7 +41,7 @@ public void testGcCounts() throws InterruptedException serviceEmitter.start(); final JvmMonitor jvmMonitor = new JvmMonitor(); // skip tests if gc counters fail to initialize with this JDK - Assume.assumeNotNull(jvmMonitor.gcCounters); + Assume.assumeNotNull(jvmMonitor.gcCollectors); while (true) { // generate some garbage to see gc counters incremented From 673afb6b862de08ab7d9b5db84ff3e540da24778 Mon Sep 17 00:00:00 2001 From: liujianhuan Date: Wed, 29 Jun 2022 17:40:30 +0800 Subject: [PATCH 5/6] blend GcGenerationCollector into GcCollector --- .../druid/java/util/metrics/JvmMonitor.java | 63 +++++++------------ 1 file changed, 22 insertions(+), 41 deletions(-) diff --git a/core/src/main/java/org/apache/druid/java/util/metrics/JvmMonitor.java b/core/src/main/java/org/apache/druid/java/util/metrics/JvmMonitor.java index af271dda0f08..7c9f6be99dfb 100644 --- a/core/src/main/java/org/apache/druid/java/util/metrics/JvmMonitor.java +++ b/core/src/main/java/org/apache/druid/java/util/metrics/JvmMonitor.java @@ -143,34 +143,35 @@ private void emitDirectMemMetrics(ServiceEmitter emitter) private void emitGcMetrics(ServiceEmitter emitter) { - if (gcCollectors != null) { - gcCollectors.emit(emitter, dimensions); - } + gcCollectors.emit(emitter, dimensions); } private class GcCollectors { - private final List collectors = new ArrayList<>(); + private final List generationCollectors = new ArrayList<>(); private final List spaceCollectors = new ArrayList<>(); GcCollectors() { List collectorMxBeans = ManagementFactory.getGarbageCollectorMXBeans(); for (GarbageCollectorMXBean collectorMxBean : collectorMxBeans) { - collectors.add(new GcCollector(collectorMxBean)); + generationCollectors.add(new GcGenerationCollector(collectorMxBean)); } - List memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans(); - for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) { - spaceCollectors.add(new GcSpaceCollector(memoryPoolMXBean)); + List memoryPoolMxBeans = ManagementFactory.getMemoryPoolMXBeans(); + for (MemoryPoolMXBean memoryPoolMxBean : memoryPoolMxBeans) { + MemoryUsage collectionUsage = memoryPoolMxBean.getCollectionUsage(); + if (collectionUsage != null) { + spaceCollectors.add(new GcSpaceCollector(collectionUsage, memoryPoolMxBean.getName())); + } } } void emit(ServiceEmitter emitter, Map dimensions) { - for (GcCollector collector : collectors) { - collector.emit(emitter, dimensions); + for (GcGenerationCollector generationCollector : generationCollectors) { + generationCollector.emit(emitter, dimensions); } for (GcSpaceCollector spaceCollector : spaceCollectors) { @@ -179,11 +180,14 @@ void emit(ServiceEmitter emitter, Map dimensions) } } - private class GcCollector + private class GcGenerationCollector { private final String generation; private final String collectorName; - private final GcGenerationCollector collector; + private final GarbageCollectorMXBean gcBean; + + private long lastInvocations = 0; + private long lastCpuNanos = 0; private static final String GC_YOUNG_GENERATION_NAME = "young"; private static final String GC_OLD_GENERATION_NAME = "old"; @@ -196,13 +200,12 @@ private class GcCollector private static final String ZGC_COLLECTOR_NAME = "zgc"; private static final String SHENANDOAN_COLLECTOR_NAME = "shenandoah"; - GcCollector(GarbageCollectorMXBean gcBean) + GcGenerationCollector(GarbageCollectorMXBean gcBean) { Pair gcNamePair = getReadableName(gcBean.getName()); this.generation = gcNamePair.lhs; this.collectorName = gcNamePair.rhs; - - collector = new GcGenerationCollector(gcBean); + this.gcBean = gcBean; } private Pair getReadableName(String name) @@ -258,28 +261,8 @@ void emit(ServiceEmitter emitter, Map dimensions) Map dimensionsCopy = dimensionsCopyBuilder.build(); - if (collector != null) { - collector.emit(emitter, dimensionsCopy); - } - - } - } - - private class GcGenerationCollector - { - private long lastInvocations = 0; - private long lastCpuNanos = 0; - private final GarbageCollectorMXBean gcBean; - - GcGenerationCollector(GarbageCollectorMXBean gcBean) - { - this.gcBean = gcBean; - } - - void emit(ServiceEmitter emitter, Map dimensions) - { final ServiceMetricEvent.Builder builder = builder(); - MonitorUtils.addDimensionsToBuilder(builder, dimensions); + MonitorUtils.addDimensionsToBuilder(builder, dimensionsCopy); long newInvocations = gcBean.getCollectionCount(); emitter.emit(builder.build("jvm/gc/count", newInvocations - lastInvocations)); @@ -288,6 +271,7 @@ void emit(ServiceEmitter emitter, Map dimensions) long newCpuNanos = gcBean.getCollectionTime(); emitter.emit(builder.build("jvm/gc/cpu", newCpuNanos - lastCpuNanos)); lastCpuNanos = newCpuNanos; + } } @@ -296,12 +280,9 @@ private class GcSpaceCollector private final List spaces = new ArrayList<>(); - public GcSpaceCollector(MemoryPoolMXBean memoryPoolMxBean) + public GcSpaceCollector(MemoryUsage collectionUsage, String name) { - MemoryUsage collectionUsage = memoryPoolMxBean.getCollectionUsage(); - if (collectionUsage != null) { - spaces.add(new GcGenerationSpace(collectionUsage, memoryPoolMxBean.getName())); - } + spaces.add(new GcGenerationSpace(collectionUsage, name)); } void emit(ServiceEmitter emitter, Map dimensions) From abe50f67b0500af5c0c9053a4ec50e2405f8d2c7 Mon Sep 17 00:00:00 2001 From: liujianhuan Date: Fri, 8 Jul 2022 10:18:14 +0800 Subject: [PATCH 6/6] add jdk surefire argLine --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index c05ac73e1932..5d29c54cb883 100644 --- a/pom.xml +++ b/pom.xml @@ -120,6 +120,7 @@ 1.26.0 v1-rev20190607-${com.google.apis.client.version} v1-rev20190523-${com.google.apis.client.version} + apache.snapshots Apache Snapshot Repository https://repository.apache.org/snapshots