more work on monitoring support

This commit is contained in:
kimchy 2010-05-07 22:49:36 +03:00
parent 4ed93d20f6
commit e222d38b84
3 changed files with 302 additions and 188 deletions

View File

@ -19,7 +19,6 @@
package org.elasticsearch.monitor.jvm;
import org.elasticsearch.util.SizeValue;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.io.stream.Streamable;
@ -52,64 +51,50 @@ public class JvmInfo implements Streamable, Serializable {
} catch (Exception e) {
pid = -1;
}
INSTANCE = new JvmInfo(pid, runtimeMXBean.getVmName(), System.getProperty("java.version"), System.getProperty("java.vendor"),
runtimeMXBean.getStartTime(),
memoryMXBean.getHeapMemoryUsage().getInit(), memoryMXBean.getHeapMemoryUsage().getMax(),
memoryMXBean.getNonHeapMemoryUsage().getInit(), memoryMXBean.getNonHeapMemoryUsage().getMax(),
runtimeMXBean.getInputArguments().toArray(new String[runtimeMXBean.getInputArguments().size()]), runtimeMXBean.getBootClassPath(), runtimeMXBean.getClassPath(), runtimeMXBean.getSystemProperties());
JvmInfo info = new JvmInfo();
info.pid = pid;
info.startTime = runtimeMXBean.getStartTime();
info.vmName = runtimeMXBean.getVmName();
info.vmVendor = runtimeMXBean.getVmVendor();
info.vmVersion = runtimeMXBean.getVmVersion();
info.mem = new Mem();
info.mem.heapInit = memoryMXBean.getHeapMemoryUsage().getInit();
info.mem.heapMax = memoryMXBean.getHeapMemoryUsage().getMax();
info.mem.nonHeapInit = memoryMXBean.getNonHeapMemoryUsage().getInit();
info.mem.nonHeapMax = memoryMXBean.getNonHeapMemoryUsage().getMax();
info.inputArguments = runtimeMXBean.getInputArguments().toArray(new String[runtimeMXBean.getInputArguments().size()]);
info.bootClassPath = runtimeMXBean.getBootClassPath();
info.classPath = runtimeMXBean.getClassPath();
info.systemProperties = runtimeMXBean.getSystemProperties();
INSTANCE = info;
}
public static JvmInfo jvmInfo() {
return INSTANCE;
}
private long pid = -1;
long pid = -1;
private String vmName = "";
String vmName = "";
String vmVersion = "";
String vmVendor = "";
private String vmVersion = "";
long startTime = -1;
private String vmVendor = "";
Mem mem;
private long startTime = -1;
String[] inputArguments;
private long memoryHeapInit = -1;
String bootClassPath;
private long memoryHeapMax = -1;
String classPath;
private long memoryNonHeapInit = -1;
private long memoryNonHeapMax = -1;
private String[] inputArguments;
private String bootClassPath;
private String classPath;
private Map<String, String> systemProperties;
Map<String, String> systemProperties;
private JvmInfo() {
}
public JvmInfo(long pid, String vmName, String vmVersion, String vmVendor, long startTime,
long memoryHeapInit, long memoryHeapMax, long memoryNonHeapInit, long memoryNonHeapMax,
String[] inputArguments, String bootClassPath, String classPath, Map<String, String> systemProperties) {
this.pid = pid;
this.vmName = vmName;
this.vmVersion = vmVersion;
this.vmVendor = vmVendor;
this.startTime = startTime;
this.memoryHeapInit = memoryHeapInit;
this.memoryHeapMax = memoryHeapMax;
this.memoryNonHeapInit = memoryNonHeapInit;
this.memoryNonHeapMax = memoryNonHeapMax;
this.inputArguments = inputArguments;
this.bootClassPath = bootClassPath;
this.classPath = classPath;
this.systemProperties = systemProperties;
}
/**
* The process id.
*/
@ -156,36 +141,12 @@ public class JvmInfo implements Streamable, Serializable {
return startTime;
}
public SizeValue memoryHeapInit() {
return new SizeValue(memoryHeapInit);
public Mem mem() {
return mem;
}
public SizeValue getMemoryHeapInit() {
return memoryHeapInit();
}
public SizeValue memoryHeapMax() {
return new SizeValue(memoryHeapMax);
}
public SizeValue getMemoryHeapMax() {
return memoryHeapMax();
}
public SizeValue memoryNonHeapInit() {
return new SizeValue(memoryNonHeapInit);
}
public SizeValue getMemoryNonHeapInit() {
return memoryNonHeapInit();
}
public SizeValue memoryNonHeapMax() {
return new SizeValue(memoryNonHeapMax);
}
public SizeValue getMemoryNonHeapMax() {
return memoryNonHeapMax();
public Mem getMem() {
return mem();
}
public String[] inputArguments() {
@ -232,10 +193,6 @@ public class JvmInfo implements Streamable, Serializable {
vmVersion = in.readUTF();
vmVendor = in.readUTF();
startTime = in.readLong();
memoryHeapInit = in.readLong();
memoryHeapMax = in.readLong();
memoryNonHeapInit = in.readLong();
memoryNonHeapMax = in.readLong();
inputArguments = new String[in.readInt()];
for (int i = 0; i < inputArguments.length; i++) {
inputArguments[i] = in.readUTF();
@ -255,10 +212,6 @@ public class JvmInfo implements Streamable, Serializable {
out.writeUTF(vmVersion);
out.writeUTF(vmVendor);
out.writeLong(startTime);
out.writeLong(memoryHeapInit);
out.writeLong(memoryHeapMax);
out.writeLong(memoryNonHeapInit);
out.writeLong(memoryNonHeapMax);
out.writeInt(inputArguments.length);
for (String inputArgument : inputArguments) {
out.writeUTF(inputArgument);
@ -271,4 +224,35 @@ public class JvmInfo implements Streamable, Serializable {
out.writeUTF(entry.getValue());
}
}
public static class Mem implements Streamable, Serializable {
long heapInit = -1;
long heapMax = -1;
long nonHeapInit = -1;
long nonHeapMax = -1;
Mem() {
}
public static Mem readMem(StreamInput in) throws IOException {
Mem mem = new Mem();
mem.readFrom(in);
return mem;
}
@Override public void readFrom(StreamInput in) throws IOException {
heapInit = in.readVLong();
heapMax = in.readVLong();
nonHeapInit = in.readVLong();
nonHeapMax = in.readVLong();
}
@Override public void writeTo(StreamOutput out) throws IOException {
out.writeVLong(heapInit);
out.writeVLong(heapMax);
out.writeVLong(nonHeapInit);
out.writeVLong(nonHeapMax);
}
}
}

View File

@ -99,7 +99,7 @@ public class JvmMonitorService extends AbstractLifecycleComponent<JvmMonitorServ
private void monitorLongGc() {
JvmStats currentJvmStats = jvmStats();
long collectionTime = currentJvmStats.gcCollectionTime().millis() - lastJvmStats.gcCollectionTime().millis();
long collectionTime = currentJvmStats.gc().collectionTime().millis() - lastJvmStats.gc().collectionTime().millis();
if (collectionTime > gcCollectionWarning.millis()) {
logger.warn("Long GC collection occurred, took [" + new TimeValue(collectionTime) + "], breached threshold [" + gcCollectionWarning + "]");
}

View File

@ -21,6 +21,7 @@ package org.elasticsearch.monitor.jvm;
import org.elasticsearch.util.SizeValue;
import org.elasticsearch.util.TimeValue;
import org.elasticsearch.util.collect.Iterators;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.io.stream.Streamable;
@ -28,6 +29,7 @@ import org.elasticsearch.util.io.stream.Streamable;
import java.io.IOException;
import java.io.Serializable;
import java.lang.management.*;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;
@ -47,61 +49,49 @@ public class JvmStats implements Streamable, Serializable {
}
public static JvmStats jvmStats() {
long gcCollectionCount = 0;
long gcCollectionTime = 0;
JvmStats stats = new JvmStats(System.currentTimeMillis(), runtimeMXBean.getUptime());
stats.mem = new Mem();
MemoryUsage memUsage = memoryMXBean.getHeapMemoryUsage();
stats.mem.heapUsed = memUsage.getUsed();
stats.mem.heapCommitted = memUsage.getCommitted();
memUsage = memoryMXBean.getNonHeapMemoryUsage();
stats.mem.nonHeapUsed = memUsage.getUsed();
stats.mem.nonHeapCommitted = memUsage.getCommitted();
stats.threads = new Threads();
stats.threads.count = threadMXBean.getThreadCount();
stats.threads.peakCount = threadMXBean.getPeakThreadCount();
List<GarbageCollectorMXBean> gcMxBeans = ManagementFactory.getGarbageCollectorMXBeans();
for (GarbageCollectorMXBean gcMxBean : gcMxBeans) {
long tmp = gcMxBean.getCollectionCount();
if (tmp != -1) {
gcCollectionCount += tmp;
}
tmp = gcMxBean.getCollectionTime();
if (tmp != -1) {
gcCollectionTime += tmp;
}
}
return new JvmStats(System.currentTimeMillis(), runtimeMXBean.getUptime(),
memoryMXBean.getHeapMemoryUsage().getCommitted(), memoryMXBean.getHeapMemoryUsage().getUsed(),
memoryMXBean.getNonHeapMemoryUsage().getCommitted(), memoryMXBean.getNonHeapMemoryUsage().getUsed(),
threadMXBean.getThreadCount(), threadMXBean.getPeakThreadCount(), gcCollectionCount, gcCollectionTime);
stats.gc = new GarbageCollectors();
stats.gc.collectors = new GarbageCollector[gcMxBeans.size()];
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].collectionCount = gcMxBean.getCollectionCount();
stats.gc.collectors[i].collectionTime = gcMxBean.getCollectionTime();
}
private long timestamp = -1;
return stats;
}
private long uptime;
long timestamp = -1;
private long memoryHeapCommitted;
long uptime;
private long memoryHeapUsed;
Mem mem;
private long memoryNonHeapCommitted;
Threads threads;
private long memoryNonHeapUsed;
private int threadCount;
private int peakThreadCount;
private long gcCollectionCount;
private long gcCollectionTime;
GarbageCollectors gc;
private JvmStats() {
}
public JvmStats(long timestamp, long uptime,
long memoryHeapCommitted, long memoryHeapUsed, long memoryNonHeapCommitted, long memoryNonHeapUsed,
int threadCount, int peakThreadCount, long gcCollectionCount, long gcCollectionTime) {
public JvmStats(long timestamp, long uptime) {
this.timestamp = timestamp;
this.uptime = uptime;
this.memoryHeapCommitted = memoryHeapCommitted;
this.memoryHeapUsed = memoryHeapUsed;
this.memoryNonHeapCommitted = memoryNonHeapCommitted;
this.memoryNonHeapUsed = memoryNonHeapUsed;
this.threadCount = threadCount;
this.peakThreadCount = peakThreadCount;
this.gcCollectionCount = gcCollectionCount;
this.gcCollectionTime = gcCollectionTime;
}
public long timestamp() {
@ -120,68 +110,28 @@ public class JvmStats implements Streamable, Serializable {
return uptime();
}
public SizeValue memoryHeapCommitted() {
return new SizeValue(memoryHeapCommitted);
public Mem mem() {
return this.mem;
}
public SizeValue getMemoryHeapCommitted() {
return memoryHeapCommitted();
public Mem getMem() {
return mem();
}
public SizeValue memoryHeapUsed() {
return new SizeValue(memoryHeapUsed);
public Threads threads() {
return threads;
}
public SizeValue getMemoryHeapUsed() {
return memoryHeapUsed();
public Threads getThreads() {
return threads();
}
public SizeValue memoryNonHeapCommitted() {
return new SizeValue(memoryNonHeapCommitted);
public GarbageCollectors gc() {
return gc;
}
public SizeValue getMemoryNonHeapCommitted() {
return memoryNonHeapCommitted();
}
public SizeValue memoryNonHeapUsed() {
return new SizeValue(memoryNonHeapUsed);
}
public SizeValue getMemoryNonHeapUsed() {
return memoryNonHeapUsed();
}
public int threadCount() {
return threadCount;
}
public int getThreadCount() {
return threadCount;
}
public int peakThreadCount() {
return peakThreadCount;
}
public int getPeakThreadCount() {
return peakThreadCount;
}
public long gcCollectionCount() {
return gcCollectionCount;
}
public long getGcCollectionCount() {
return gcCollectionCount;
}
public TimeValue gcCollectionTime() {
return new TimeValue(gcCollectionTime, TimeUnit.MILLISECONDS);
}
public TimeValue getGcCollectionTime() {
return gcCollectionTime();
public GarbageCollectors getGc() {
return gc();
}
public static JvmStats readJvmStats(StreamInput in) throws IOException {
@ -193,26 +143,206 @@ public class JvmStats implements Streamable, Serializable {
@Override public void readFrom(StreamInput in) throws IOException {
timestamp = in.readVLong();
uptime = in.readVLong();
memoryHeapCommitted = in.readVLong();
memoryHeapUsed = in.readVLong();
memoryNonHeapCommitted = in.readVLong();
memoryNonHeapUsed = in.readVLong();
threadCount = in.readVInt();
peakThreadCount = in.readVInt();
gcCollectionCount = in.readVLong();
gcCollectionTime = in.readVLong();
mem = Mem.readMem(in);
threads = Threads.readThreads(in);
gc = GarbageCollectors.readGarbageCollectors(in);
}
@Override public void writeTo(StreamOutput out) throws IOException {
out.writeVLong(timestamp);
out.writeVLong(uptime);
out.writeVLong(memoryHeapCommitted);
out.writeVLong(memoryHeapUsed);
out.writeVLong(memoryNonHeapCommitted);
out.writeVLong(memoryNonHeapUsed);
out.writeVInt(threadCount);
out.writeVInt(peakThreadCount);
out.writeVLong(gcCollectionCount);
out.writeVLong(gcCollectionTime);
mem.writeTo(out);
threads.writeTo(out);
gc.writeTo(out);
}
public static class GarbageCollectors implements Streamable, Serializable, Iterable {
private GarbageCollector[] collectors;
GarbageCollectors() {
}
public static GarbageCollectors readGarbageCollectors(StreamInput in) throws IOException {
GarbageCollectors collectors = new GarbageCollectors();
collectors.readFrom(in);
return collectors;
}
@Override public void readFrom(StreamInput in) throws IOException {
collectors = new GarbageCollector[in.readVInt()];
for (int i = 0; i < collectors.length; i++) {
collectors[i] = GarbageCollector.readGarbageCollector(in);
}
}
@Override public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(collectors.length);
for (GarbageCollector gc : collectors) {
gc.writeTo(out);
}
}
@Override public Iterator 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 {
String name;
long collectionCount;
long collectionTime;
GarbageCollector() {
}
public static GarbageCollector readGarbageCollector(StreamInput in) throws IOException {
GarbageCollector gc = new GarbageCollector();
gc.readFrom(in);
return gc;
}
@Override public void readFrom(StreamInput in) throws IOException {
name = in.readUTF();
collectionCount = in.readVLong();
collectionTime = in.readVLong();
}
@Override public void writeTo(StreamOutput out) throws IOException {
out.writeUTF(name);
out.writeVLong(collectionCount);
out.writeVLong(collectionTime);
}
public String name() {
return name;
}
public String getName() {
return name();
}
public long collectionCount() {
return collectionCount;
}
public long getCollectionCount() {
return collectionCount();
}
public TimeValue collectionTime() {
return new TimeValue(collectionTime, TimeUnit.MILLISECONDS);
}
public TimeValue getCollectionTime() {
return collectionTime();
}
}
public static class Threads implements Streamable, Serializable {
int count;
int peakCount;
Threads() {
}
public static Threads readThreads(StreamInput in) throws IOException {
Threads threads = new Threads();
threads.readFrom(in);
return threads;
}
@Override public void readFrom(StreamInput in) throws IOException {
count = in.readVInt();
peakCount = in.readVInt();
}
@Override public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(count);
out.writeVInt(peakCount);
}
}
public static class Mem implements Streamable, Serializable {
long heapCommitted;
long heapUsed;
long nonHeapCommitted;
long nonHeapUsed;
Mem() {
}
public static Mem readMem(StreamInput in) throws IOException {
Mem mem = new Mem();
mem.readFrom(in);
return mem;
}
@Override public void readFrom(StreamInput in) throws IOException {
heapCommitted = in.readVLong();
heapUsed = in.readVLong();
nonHeapCommitted = in.readVLong();
nonHeapUsed = in.readVLong();
}
@Override public void writeTo(StreamOutput out) throws IOException {
out.writeVLong(heapCommitted);
out.writeVLong(heapUsed);
out.writeVLong(nonHeapCommitted);
out.writeVLong(nonHeapUsed);
}
public SizeValue heapCommitted() {
return new SizeValue(heapCommitted);
}
public SizeValue getHeapCommitted() {
return heapCommitted();
}
public SizeValue heapUsed() {
return new SizeValue(heapUsed);
}
public SizeValue getHeapUsed() {
return heapUsed();
}
public SizeValue nonHeapCommitted() {
return new SizeValue(nonHeapCommitted);
}
public SizeValue getNonHeapCommitted() {
return nonHeapCommitted();
}
public SizeValue nonHeapUsed() {
return new SizeValue(nonHeapUsed);
}
public SizeValue getNonHeapUsed() {
return nonHeapUsed();
}
}
}