Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/changelog/115117.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 115117
summary: Report JVM stats for all memory pools (97046)
area: Infra/Core
type: bug
issues:
- 97046
15 changes: 14 additions & 1 deletion server/src/main/java/org/elasticsearch/monitor/jvm/GcNames.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ public class GcNames {
public static final String OLD = "old";
public static final String SURVIVOR = "survivor";

private GcNames() {}

/**
* Resolves the GC type by its memory pool name ({@link java.lang.management.MemoryPoolMXBean#getName()}.
* Resolves the memory area name by the memory pool name provided by {@link java.lang.management.MemoryPoolMXBean#getName()}
*
* @param poolName the name of the memory pool from {@link java.lang.management.MemoryPoolMXBean}
* @param defaultName the name to return if the pool name does not match any known memory area
* @return memory area name corresponding to the pool name or {@code defaultName} if no match is found
*/
public static String getByMemoryPoolName(String poolName, String defaultName) {
if ("Eden Space".equals(poolName)
Expand All @@ -40,6 +46,13 @@ public static String getByMemoryPoolName(String poolName, String defaultName) {
return defaultName;
}

/**
* Resolves the GC type by the GC name provided by {@link java.lang.management.GarbageCollectorMXBean#getName()}
*
* @param gcName the name of the GC from {@link java.lang.management.GarbageCollectorMXBean}
* @param defaultName the name to return if the GC name does not match any known GC type
* @return GC type corresponding to the GC name or {@code defaultName} if no match is found
*/
public static String getByGcName(String gcName, String defaultName) {
if ("Copy".equals(gcName) || "PS Scavenge".equals(gcName) || "ParNew".equals(gcName) || "G1 Young Generation".equals(gcName)) {
return YOUNG;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ public static JvmStats jvmStats() {
List<MemoryPool> pools = new ArrayList<>();
for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) {
try {
String name = GcNames.getByMemoryPoolName(memoryPoolMXBean.getName(), null);
if (name == null) { // if we can't resolve it, its not interesting.... (Per Gen, Code Cache)
continue;
}
String name = GcNames.getByMemoryPoolName(memoryPoolMXBean.getName(), memoryPoolMXBean.getName());
MemoryUsage usage = memoryPoolMXBean.getUsage();
MemoryUsage peakUsage = memoryPoolMXBean.getPeakUsage();
pools.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.test.ESTestCase;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.hasSize;

public class JvmStatsTests extends ESTestCase {
public void testJvmStats() throws IOException {
public void testJvmStats() {
JvmStats stats = JvmStats.jvmStats();
assertNotNull(stats);
assertNotNull(stats.getUptime());
Expand All @@ -40,6 +45,17 @@ public void testJvmStats() throws IOException {
assertNotNull(mem.getHeapUsedPercent());
assertThat(mem.getHeapUsedPercent(), anyOf(equalTo((short) -1), greaterThanOrEqualTo((short) 0)));

// Memory pools
Map<String, JvmStats.MemoryPool> memoryPools = StreamSupport.stream(stats.getMem().spliterator(), false)
.collect(Collectors.toMap(JvmStats.MemoryPool::getName, Function.identity()));
assertThat(memoryPools, hasKey(GcNames.YOUNG));
assertThat(memoryPools, hasKey(GcNames.OLD));
assertThat(memoryPools, hasKey("Metaspace"));
assertThat(memoryPools.keySet(), hasSize(greaterThan(3)));
for (JvmStats.MemoryPool memoryPool : memoryPools.values()) {
assertThat(memoryPool.getUsed().getBytes(), greaterThan(0L));
}

// Threads
JvmStats.Threads threads = stats.getThreads();
assertNotNull(threads);
Expand Down