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; package org.elasticsearch.monitor.jvm;
import org.elasticsearch.util.SizeValue;
import org.elasticsearch.util.io.stream.StreamInput; import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput; import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.io.stream.Streamable; import org.elasticsearch.util.io.stream.Streamable;
@ -52,64 +51,50 @@ public class JvmInfo implements Streamable, Serializable {
} catch (Exception e) { } catch (Exception e) {
pid = -1; pid = -1;
} }
INSTANCE = new JvmInfo(pid, runtimeMXBean.getVmName(), System.getProperty("java.version"), System.getProperty("java.vendor"), JvmInfo info = new JvmInfo();
runtimeMXBean.getStartTime(), info.pid = pid;
memoryMXBean.getHeapMemoryUsage().getInit(), memoryMXBean.getHeapMemoryUsage().getMax(), info.startTime = runtimeMXBean.getStartTime();
memoryMXBean.getNonHeapMemoryUsage().getInit(), memoryMXBean.getNonHeapMemoryUsage().getMax(), info.vmName = runtimeMXBean.getVmName();
runtimeMXBean.getInputArguments().toArray(new String[runtimeMXBean.getInputArguments().size()]), runtimeMXBean.getBootClassPath(), runtimeMXBean.getClassPath(), runtimeMXBean.getSystemProperties()); 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() { public static JvmInfo jvmInfo() {
return INSTANCE; 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; Map<String, String> systemProperties;
private long memoryNonHeapMax = -1;
private String[] inputArguments;
private String bootClassPath;
private String classPath;
private Map<String, String> systemProperties;
private JvmInfo() { 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. * The process id.
*/ */
@ -156,36 +141,12 @@ public class JvmInfo implements Streamable, Serializable {
return startTime; return startTime;
} }
public SizeValue memoryHeapInit() { public Mem mem() {
return new SizeValue(memoryHeapInit); return mem;
} }
public SizeValue getMemoryHeapInit() { public Mem getMem() {
return memoryHeapInit(); return mem();
}
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 String[] inputArguments() { public String[] inputArguments() {
@ -232,10 +193,6 @@ public class JvmInfo implements Streamable, Serializable {
vmVersion = in.readUTF(); vmVersion = in.readUTF();
vmVendor = in.readUTF(); vmVendor = in.readUTF();
startTime = in.readLong(); startTime = in.readLong();
memoryHeapInit = in.readLong();
memoryHeapMax = in.readLong();
memoryNonHeapInit = in.readLong();
memoryNonHeapMax = in.readLong();
inputArguments = new String[in.readInt()]; inputArguments = new String[in.readInt()];
for (int i = 0; i < inputArguments.length; i++) { for (int i = 0; i < inputArguments.length; i++) {
inputArguments[i] = in.readUTF(); inputArguments[i] = in.readUTF();
@ -255,10 +212,6 @@ public class JvmInfo implements Streamable, Serializable {
out.writeUTF(vmVersion); out.writeUTF(vmVersion);
out.writeUTF(vmVendor); out.writeUTF(vmVendor);
out.writeLong(startTime); out.writeLong(startTime);
out.writeLong(memoryHeapInit);
out.writeLong(memoryHeapMax);
out.writeLong(memoryNonHeapInit);
out.writeLong(memoryNonHeapMax);
out.writeInt(inputArguments.length); out.writeInt(inputArguments.length);
for (String inputArgument : inputArguments) { for (String inputArgument : inputArguments) {
out.writeUTF(inputArgument); out.writeUTF(inputArgument);
@ -271,4 +224,35 @@ public class JvmInfo implements Streamable, Serializable {
out.writeUTF(entry.getValue()); 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() { private void monitorLongGc() {
JvmStats currentJvmStats = jvmStats(); 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()) { if (collectionTime > gcCollectionWarning.millis()) {
logger.warn("Long GC collection occurred, took [" + new TimeValue(collectionTime) + "], breached threshold [" + gcCollectionWarning + "]"); 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.SizeValue;
import org.elasticsearch.util.TimeValue; import org.elasticsearch.util.TimeValue;
import org.elasticsearch.util.collect.Iterators;
import org.elasticsearch.util.io.stream.StreamInput; import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput; import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.io.stream.Streamable; 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.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.lang.management.*; import java.lang.management.*;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -47,61 +49,49 @@ public class JvmStats implements Streamable, Serializable {
} }
public static JvmStats jvmStats() { public static JvmStats jvmStats() {
long gcCollectionCount = 0; JvmStats stats = new JvmStats(System.currentTimeMillis(), runtimeMXBean.getUptime());
long gcCollectionTime = 0; 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(); List<GarbageCollectorMXBean> gcMxBeans = ManagementFactory.getGarbageCollectorMXBeans();
for (GarbageCollectorMXBean gcMxBean : gcMxBeans) { stats.gc = new GarbageCollectors();
long tmp = gcMxBean.getCollectionCount(); stats.gc.collectors = new GarbageCollector[gcMxBeans.size()];
if (tmp != -1) { for (int i = 0; i < stats.gc.collectors.length; i++) {
gcCollectionCount += tmp; GarbageCollectorMXBean gcMxBean = gcMxBeans.get(i);
} stats.gc.collectors[i] = new GarbageCollector();
tmp = gcMxBean.getCollectionTime(); stats.gc.collectors[i].name = gcMxBean.getName();
if (tmp != -1) { stats.gc.collectors[i].collectionCount = gcMxBean.getCollectionCount();
gcCollectionTime += tmp; stats.gc.collectors[i].collectionTime = gcMxBean.getCollectionTime();
}
}
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);
} }
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; GarbageCollectors gc;
private int threadCount;
private int peakThreadCount;
private long gcCollectionCount;
private long gcCollectionTime;
private JvmStats() { private JvmStats() {
} }
public JvmStats(long timestamp, long uptime, public JvmStats(long timestamp, long uptime) {
long memoryHeapCommitted, long memoryHeapUsed, long memoryNonHeapCommitted, long memoryNonHeapUsed,
int threadCount, int peakThreadCount, long gcCollectionCount, long gcCollectionTime) {
this.timestamp = timestamp; this.timestamp = timestamp;
this.uptime = uptime; 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() { public long timestamp() {
@ -120,68 +110,28 @@ public class JvmStats implements Streamable, Serializable {
return uptime(); return uptime();
} }
public SizeValue memoryHeapCommitted() { public Mem mem() {
return new SizeValue(memoryHeapCommitted); return this.mem;
} }
public SizeValue getMemoryHeapCommitted() { public Mem getMem() {
return memoryHeapCommitted(); return mem();
} }
public SizeValue memoryHeapUsed() { public Threads threads() {
return new SizeValue(memoryHeapUsed); return threads;
} }
public SizeValue getMemoryHeapUsed() { public Threads getThreads() {
return memoryHeapUsed(); return threads();
} }
public SizeValue memoryNonHeapCommitted() { public GarbageCollectors gc() {
return new SizeValue(memoryNonHeapCommitted); return gc;
} }
public SizeValue getMemoryNonHeapCommitted() { public GarbageCollectors getGc() {
return memoryNonHeapCommitted(); return gc();
}
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 static JvmStats readJvmStats(StreamInput in) throws IOException { 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 { @Override public void readFrom(StreamInput in) throws IOException {
timestamp = in.readVLong(); timestamp = in.readVLong();
uptime = in.readVLong(); uptime = in.readVLong();
memoryHeapCommitted = in.readVLong();
memoryHeapUsed = in.readVLong(); mem = Mem.readMem(in);
memoryNonHeapCommitted = in.readVLong(); threads = Threads.readThreads(in);
memoryNonHeapUsed = in.readVLong(); gc = GarbageCollectors.readGarbageCollectors(in);
threadCount = in.readVInt();
peakThreadCount = in.readVInt();
gcCollectionCount = in.readVLong();
gcCollectionTime = in.readVLong();
} }
@Override public void writeTo(StreamOutput out) throws IOException { @Override public void writeTo(StreamOutput out) throws IOException {
out.writeVLong(timestamp); out.writeVLong(timestamp);
out.writeVLong(uptime); out.writeVLong(uptime);
out.writeVLong(memoryHeapCommitted);
out.writeVLong(memoryHeapUsed); mem.writeTo(out);
out.writeVLong(memoryNonHeapCommitted); threads.writeTo(out);
out.writeVLong(memoryNonHeapUsed); gc.writeTo(out);
out.writeVInt(threadCount); }
out.writeVInt(peakThreadCount);
out.writeVLong(gcCollectionCount); public static class GarbageCollectors implements Streamable, Serializable, Iterable {
out.writeVLong(gcCollectionTime);
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();
}
} }
} }