Skip to content

Commit 615345f

Browse files
alexey-ivanov-esgeorgewallace
authored andcommitted
Report JVM stats for all memory pools (97046) (elastic#115117)
This fix allows reporting of all JVM memory pools sizes in JVM stats
1 parent 6e70693 commit 615345f

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

docs/changelog/115117.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 115117
2+
summary: Report JVM stats for all memory pools (97046)
3+
area: Infra/Core
4+
type: bug
5+
issues:
6+
- 97046

server/src/main/java/org/elasticsearch/monitor/jvm/GcNames.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ public class GcNames {
1515
public static final String OLD = "old";
1616
public static final String SURVIVOR = "survivor";
1717

18+
private GcNames() {}
19+
1820
/**
19-
* Resolves the GC type by its memory pool name ({@link java.lang.management.MemoryPoolMXBean#getName()}.
21+
* Resolves the memory area name by the memory pool name provided by {@link java.lang.management.MemoryPoolMXBean#getName()}
22+
*
23+
* @param poolName the name of the memory pool from {@link java.lang.management.MemoryPoolMXBean}
24+
* @param defaultName the name to return if the pool name does not match any known memory area
25+
* @return memory area name corresponding to the pool name or {@code defaultName} if no match is found
2026
*/
2127
public static String getByMemoryPoolName(String poolName, String defaultName) {
2228
if ("Eden Space".equals(poolName)
@@ -40,6 +46,13 @@ public static String getByMemoryPoolName(String poolName, String defaultName) {
4046
return defaultName;
4147
}
4248

49+
/**
50+
* Resolves the GC type by the GC name provided by {@link java.lang.management.GarbageCollectorMXBean#getName()}
51+
*
52+
* @param gcName the name of the GC from {@link java.lang.management.GarbageCollectorMXBean}
53+
* @param defaultName the name to return if the GC name does not match any known GC type
54+
* @return GC type corresponding to the GC name or {@code defaultName} if no match is found
55+
*/
4356
public static String getByGcName(String gcName, String defaultName) {
4457
if ("Copy".equals(gcName) || "PS Scavenge".equals(gcName) || "ParNew".equals(gcName) || "G1 Young Generation".equals(gcName)) {
4558
return YOUNG;

server/src/main/java/org/elasticsearch/monitor/jvm/JvmStats.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ public static JvmStats jvmStats() {
6464
List<MemoryPool> pools = new ArrayList<>();
6565
for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) {
6666
try {
67-
String name = GcNames.getByMemoryPoolName(memoryPoolMXBean.getName(), null);
68-
if (name == null) { // if we can't resolve it, its not interesting.... (Per Gen, Code Cache)
69-
continue;
70-
}
67+
String name = GcNames.getByMemoryPoolName(memoryPoolMXBean.getName(), memoryPoolMXBean.getName());
7168
MemoryUsage usage = memoryPoolMXBean.getUsage();
7269
MemoryUsage peakUsage = memoryPoolMXBean.getPeakUsage();
7370
pools.add(

server/src/test/java/org/elasticsearch/monitor/jvm/JvmStatsTests.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,22 @@
1313
import org.elasticsearch.common.unit.ByteSizeValue;
1414
import org.elasticsearch.test.ESTestCase;
1515

16-
import java.io.IOException;
1716
import java.util.Arrays;
1817
import java.util.List;
18+
import java.util.Map;
19+
import java.util.function.Function;
20+
import java.util.stream.Collectors;
21+
import java.util.stream.StreamSupport;
1922

2023
import static org.hamcrest.Matchers.anyOf;
2124
import static org.hamcrest.Matchers.equalTo;
2225
import static org.hamcrest.Matchers.greaterThan;
2326
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
27+
import static org.hamcrest.Matchers.hasKey;
28+
import static org.hamcrest.Matchers.hasSize;
2429

2530
public class JvmStatsTests extends ESTestCase {
26-
public void testJvmStats() throws IOException {
31+
public void testJvmStats() {
2732
JvmStats stats = JvmStats.jvmStats();
2833
assertNotNull(stats);
2934
assertNotNull(stats.getUptime());
@@ -40,6 +45,17 @@ public void testJvmStats() throws IOException {
4045
assertNotNull(mem.getHeapUsedPercent());
4146
assertThat(mem.getHeapUsedPercent(), anyOf(equalTo((short) -1), greaterThanOrEqualTo((short) 0)));
4247

48+
// Memory pools
49+
Map<String, JvmStats.MemoryPool> memoryPools = StreamSupport.stream(stats.getMem().spliterator(), false)
50+
.collect(Collectors.toMap(JvmStats.MemoryPool::getName, Function.identity()));
51+
assertThat(memoryPools, hasKey(GcNames.YOUNG));
52+
assertThat(memoryPools, hasKey(GcNames.OLD));
53+
assertThat(memoryPools, hasKey("Metaspace"));
54+
assertThat(memoryPools.keySet(), hasSize(greaterThan(3)));
55+
for (JvmStats.MemoryPool memoryPool : memoryPools.values()) {
56+
assertThat(memoryPool.getUsed().getBytes(), greaterThan(0L));
57+
}
58+
4359
// Threads
4460
JvmStats.Threads threads = stats.getThreads();
4561
assertNotNull(threads);

0 commit comments

Comments
 (0)