Skip to content

Commit

Permalink
Create standard gc and memory_pool names for Jvm stats
Browse files Browse the repository at this point in the history
fixes #4661
  • Loading branch information
kimchy committed Jan 8, 2014
1 parent 6e4586f commit efa59f3
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 36 deletions.
12 changes: 6 additions & 6 deletions config/elasticsearch.yml
Expand Up @@ -355,10 +355,10 @@

################################## GC Logging ################################

#monitor.jvm.gc.ParNew.warn: 1000ms
#monitor.jvm.gc.ParNew.info: 700ms
#monitor.jvm.gc.ParNew.debug: 400ms
#monitor.jvm.gc.young.warn: 1000ms
#monitor.jvm.gc.young.info: 700ms
#monitor.jvm.gc.young.debug: 400ms

#monitor.jvm.gc.ConcurrentMarkSweep.warn: 10s
#monitor.jvm.gc.ConcurrentMarkSweep.info: 5s
#monitor.jvm.gc.ConcurrentMarkSweep.debug: 2s
#monitor.jvm.gc.old.warn: 10s
#monitor.jvm.gc.old.info: 5s
#monitor.jvm.gc.old.debug: 2s
55 changes: 55 additions & 0 deletions src/main/java/org/elasticsearch/monitor/jvm/GcNames.java
@@ -0,0 +1,55 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.monitor.jvm;

/**
*/
public class GcNames {

public static String YOUNG = "young";
public static String OLD = "old";
public static String SURVIVOR = "survivor";

/**
* Resolves the GC type by its memory pool name ({@link java.lang.management.MemoryPoolMXBean#getName()}.
*/
public static String getByMemoryPoolName(String poolName, String defaultName) {
if ("Eden Space".equals(poolName) || "PS Eden Space".equals(poolName) || "Par Eden Space".equals(poolName) || "G1 Eden Space".equals(poolName)) {
return YOUNG;
}
if ("Survivor Space".equals(poolName) || "PS Survivor Space".equals(poolName) || "Par Survivor Space".equals(poolName) || "G1 Survivor Space".equals(poolName)) {
return SURVIVOR;
}
if ("Tenured Gen".equals(poolName) || "PS Old Gen".equals(poolName) || "CMS Old Gen".equals(poolName) || "G1 Old Gen".equals(poolName)) {
return OLD;
}
return defaultName;
}

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;
}
if ("MarkSweepCompact".equals(gcName) || "PS MarkSweep".equals(gcName) || "ConcurrentMarkSweep".equals(gcName) || "G1 Old Generation".equals(gcName)) {
return OLD;
}
return defaultName;
}
}
37 changes: 34 additions & 3 deletions src/main/java/org/elasticsearch/monitor/jvm/JvmInfo.java
Expand Up @@ -19,6 +19,8 @@

package org.elasticsearch.monitor.jvm;

import org.elasticsearch.Version;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
Expand All @@ -29,10 +31,9 @@

import java.io.IOException;
import java.io.Serializable;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.RuntimeMXBean;
import java.lang.management.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -78,6 +79,20 @@ public class JvmInfo implements Streamable, Serializable, ToXContent {
info.classPath = runtimeMXBean.getClassPath();
info.systemProperties = runtimeMXBean.getSystemProperties();

List<GarbageCollectorMXBean> gcMxBeans = ManagementFactory.getGarbageCollectorMXBeans();
info.gcCollectors = new String[gcMxBeans.size()];
for (int i = 0; i < gcMxBeans.size(); i++) {
GarbageCollectorMXBean gcMxBean = gcMxBeans.get(i);
info.gcCollectors[i] = gcMxBean.getName();
}

List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
info.memoryPools = new String[memoryPoolMXBeans.size()];
for (int i = 0; i < memoryPoolMXBeans.size(); i++) {
MemoryPoolMXBean memoryPoolMXBean = memoryPoolMXBeans.get(i);
info.memoryPools[i] = memoryPoolMXBean.getName();
}

INSTANCE = info;
}

Expand All @@ -104,6 +119,9 @@ public static JvmInfo jvmInfo() {

Map<String, String> systemProperties;

String[] gcCollectors = Strings.EMPTY_ARRAY;
String[] memoryPools = Strings.EMPTY_ARRAY;

private JvmInfo() {
}

Expand Down Expand Up @@ -281,6 +299,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
builder.byteSizeField(Fields.DIRECT_MAX_IN_BYTES, Fields.DIRECT_MAX, mem.directMemoryMax);
builder.endObject();

builder.field(Fields.GC_COLLECTORS, gcCollectors);
builder.field(Fields.MEMORY_POOLS, memoryPools);

builder.endObject();
return builder;
}
Expand All @@ -305,6 +326,8 @@ static final class Fields {
static final XContentBuilderString NON_HEAP_MAX_IN_BYTES = new XContentBuilderString("non_heap_max_in_bytes");
static final XContentBuilderString DIRECT_MAX = new XContentBuilderString("direct_max");
static final XContentBuilderString DIRECT_MAX_IN_BYTES = new XContentBuilderString("direct_max_in_bytes");
static final XContentBuilderString GC_COLLECTORS = new XContentBuilderString("gc_collectors");
static final XContentBuilderString MEMORY_POOLS = new XContentBuilderString("memory_pools");
}

public static JvmInfo readJvmInfo(StreamInput in) throws IOException {
Expand Down Expand Up @@ -334,6 +357,10 @@ public void readFrom(StreamInput in) throws IOException {
}
mem = new Mem();
mem.readFrom(in);
if (in.getVersion().after(Version.V_0_90_9)) {
gcCollectors = in.readStringArray();
memoryPools = in.readStringArray();
}
}

@Override
Expand All @@ -356,6 +383,10 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeString(entry.getValue());
}
mem.writeTo(out);
if (out.getVersion().after(Version.V_0_90_9)) {
out.writeStringArray(gcCollectors);
out.writeStringArray(memoryPools);
}
}

public static class Mem implements Streamable, Serializable {
Expand Down
Expand Up @@ -106,11 +106,11 @@ public JvmMonitorService(Settings settings, ThreadPool threadPool, DumpMonitorSe
gcThresholds.put(name, new GcThreshold(name, warn.millis(), info.millis(), debug.millis()));
}
}
if (!gcThresholds.containsKey("ParNew")) {
gcThresholds.put("ParNew", new GcThreshold("ParNew", 1000, 700, 400));
if (!gcThresholds.containsKey(GcNames.YOUNG)) {
gcThresholds.put(GcNames.YOUNG, new GcThreshold(GcNames.YOUNG, 1000, 700, 400));
}
if (!gcThresholds.containsKey("ConcurrentMarkSweep")) {
gcThresholds.put("ConcurrentMarkSweep", new GcThreshold("ConcurrentMarkSweep", 10000, 5000, 2000));
if (!gcThresholds.containsKey(GcNames.OLD)) {
gcThresholds.put(GcNames.OLD, new GcThreshold(GcNames.OLD, 10000, 5000, 2000));
}
if (!gcThresholds.containsKey("default")) {
gcThresholds.put("default", new GcThreshold("default", 10000, 5000, 2000));
Expand Down Expand Up @@ -154,8 +154,12 @@ public JvmMonitor() {

@Override
public void run() {
try {
// monitorDeadlock();
monitorLongGc();
monitorLongGc();
} catch (Throwable t) {
t.printStackTrace();
}
}

private synchronized void monitorLongGc() {
Expand Down
31 changes: 9 additions & 22 deletions src/main/java/org/elasticsearch/monitor/jvm/JvmStats.java
Expand Up @@ -145,18 +145,23 @@ public static JvmStats jvmStats() {
stats.mem.nonHeapCommitted = memUsage.getCommitted() < 0 ? 0 : memUsage.getCommitted();

List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
stats.mem.pools = new MemoryPool[memoryPoolMXBeans.size()];
List<MemoryPool> pools = new ArrayList<MemoryPool>();
for (int i = 0; i < memoryPoolMXBeans.size(); i++) {
MemoryPoolMXBean memoryPoolMXBean = memoryPoolMXBeans.get(i);
MemoryUsage usage = memoryPoolMXBean.getUsage();
MemoryUsage peakUsage = memoryPoolMXBean.getPeakUsage();
stats.mem.pools[i] = new MemoryPool(memoryPoolMXBean.getName(),
String name = GcNames.getByMemoryPoolName(memoryPoolMXBean.getName(), null);
if (name == null) { // if we can't resolve it, its not interesting.... (Per Gen, Code Cache)
continue;
}
pools.add(new MemoryPool(name,
usage.getUsed() < 0 ? 0 : usage.getUsed(),
usage.getMax() < 0 ? 0 : usage.getMax(),
peakUsage.getUsed() < 0 ? 0 : peakUsage.getUsed(),
peakUsage.getMax() < 0 ? 0 : peakUsage.getMax()
);
));
}
stats.mem.pools = pools.toArray(new MemoryPool[pools.size()]);

stats.threads = new Threads();
stats.threads.count = threadMXBean.getThreadCount();
Expand All @@ -168,7 +173,7 @@ public static JvmStats jvmStats() {
for (int i = 0; i < stats.gc.collectors.length; i++) {
GarbageCollectorMXBean gcMxBean = gcMxBeans.get(i);
stats.gc.collectors[i] = new GarbageCollector();
stats.gc.collectors[i].name = gcMxBean.getName();
stats.gc.collectors[i].name = GcNames.getByGcName(gcMxBean.getName(), gcMxBean.getName());
stats.gc.collectors[i].collectionCount = gcMxBean.getCollectionCount();
stats.gc.collectors[i].collectionTime = gcMxBean.getCollectionTime();
if (enableLastGc) {
Expand Down Expand Up @@ -321,8 +326,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
}
if (gc != null) {
builder.startObject(Fields.GC);
builder.field(Fields.COLLECTION_COUNT, gc.collectionCount());
builder.timeValueField(Fields.COLLECTION_TIME_IN_MILLIS, Fields.COLLECTION_TIME, gc.collectionTime());

builder.startObject(Fields.COLLECTORS);
for (GarbageCollector collector : gc) {
Expand Down Expand Up @@ -482,22 +485,6 @@ public GarbageCollector[] collectors() {
public Iterator<GarbageCollector> iterator() {
return Iterators.forArray(collectors);
}

public long collectionCount() {
long collectionCount = 0;
for (GarbageCollector gc : collectors) {
collectionCount += gc.collectionCount();
}
return collectionCount;
}

public TimeValue collectionTime() {
long collectionTime = 0;
for (GarbageCollector gc : collectors) {
collectionTime += gc.collectionTime;
}
return new TimeValue(collectionTime, TimeUnit.MILLISECONDS);
}
}

public static class GarbageCollector implements Streamable, Serializable {
Expand Down

0 comments on commit efa59f3

Please sign in to comment.