From 7ef41eefea41ba241f238f50afb8949c357327be Mon Sep 17 00:00:00 2001 From: kimchy Date: Sun, 10 Jul 2011 05:13:35 +0300 Subject: [PATCH] Node Info / Stats: Add max_file_descriptors (info) and open_file_descriptors (stats), closes #1113. --- .../elasticsearch/monitor/jvm/JvmStats.java | 7 +- .../monitor/process/JmxProcessProbe.java | 54 +++++++++++++++- .../monitor/process/ProcessInfo.java | 16 ++++- .../monitor/process/ProcessStats.java | 64 ++----------------- .../monitor/process/SigarProcessProbe.java | 18 ++---- 5 files changed, 85 insertions(+), 74 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/monitor/jvm/JvmStats.java b/modules/elasticsearch/src/main/java/org/elasticsearch/monitor/jvm/JvmStats.java index f6cb322fb7a..bde1634ba5e 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/monitor/jvm/JvmStats.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/monitor/jvm/JvmStats.java @@ -30,7 +30,12 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import java.io.IOException; import java.io.Serializable; -import java.lang.management.*; +import java.lang.management.GarbageCollectorMXBean; +import java.lang.management.ManagementFactory; +import java.lang.management.MemoryMXBean; +import java.lang.management.MemoryUsage; +import java.lang.management.RuntimeMXBean; +import java.lang.management.ThreadMXBean; import java.lang.reflect.Method; import java.util.Iterator; import java.util.List; diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/monitor/process/JmxProcessProbe.java b/modules/elasticsearch/src/main/java/org/elasticsearch/monitor/process/JmxProcessProbe.java index 18056edc14c..962a75ee3fa 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/monitor/process/JmxProcessProbe.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/monitor/process/JmxProcessProbe.java @@ -23,6 +23,10 @@ import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; +import java.lang.management.ManagementFactory; +import java.lang.management.OperatingSystemMXBean; +import java.lang.reflect.Method; + import static org.elasticsearch.monitor.jvm.JvmInfo.*; /** @@ -30,17 +34,65 @@ import static org.elasticsearch.monitor.jvm.JvmInfo.*; */ public class JmxProcessProbe extends AbstractComponent implements ProcessProbe { + private static final OperatingSystemMXBean osMxBean = ManagementFactory.getOperatingSystemMXBean(); + + private static final Method getMaxFileDescriptorCountField; + private static final Method getOpenFileDescriptorCountField; + + static { + Method method = null; + try { + method = osMxBean.getClass().getDeclaredMethod("getMaxFileDescriptorCount"); + method.setAccessible(true); + } catch (Exception e) { + // not available + } + getMaxFileDescriptorCountField = method; + + method = null; + try { + method = osMxBean.getClass().getDeclaredMethod("getOpenFileDescriptorCount"); + method.setAccessible(true); + } catch (Exception e) { + // not available + } + getOpenFileDescriptorCountField = method; + } + + public static long getMaxFileDescriptorCount() { + if (getMaxFileDescriptorCountField == null) { + return -1; + } + try { + return (Long) getMaxFileDescriptorCountField.invoke(osMxBean); + } catch (Exception e) { + return -1; + } + } + + public static long getOpenFileDescriptorCount() { + if (getOpenFileDescriptorCountField == null) { + return -1; + } + try { + return (Long) getOpenFileDescriptorCountField.invoke(osMxBean); + } catch (Exception e) { + return -1; + } + } + @Inject public JmxProcessProbe(Settings settings) { super(settings); } @Override public ProcessInfo processInfo() { - return new ProcessInfo(jvmInfo().pid()); + return new ProcessInfo(jvmInfo().pid(), getMaxFileDescriptorCount()); } @Override public ProcessStats processStats() { ProcessStats stats = new ProcessStats(); stats.timestamp = System.currentTimeMillis(); + stats.openFileDescriptors = getOpenFileDescriptorCount(); return stats; } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/monitor/process/ProcessInfo.java b/modules/elasticsearch/src/main/java/org/elasticsearch/monitor/process/ProcessInfo.java index 4dfb9e190d2..bd64c1947c0 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/monitor/process/ProcessInfo.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/monitor/process/ProcessInfo.java @@ -37,12 +37,15 @@ public class ProcessInfo implements Streamable, Serializable, ToXContent { private long id; + private long maxFileDescriptors = -1; + ProcessInfo() { } - public ProcessInfo(long id) { + public ProcessInfo(long id, long maxFileDescriptors) { this.id = id; + this.maxFileDescriptors = maxFileDescriptors; } public long refreshInterval() { @@ -67,10 +70,19 @@ public class ProcessInfo implements Streamable, Serializable, ToXContent { return id(); } + public long maxFileDescriptors() { + return this.maxFileDescriptors; + } + + public long getMaxFileDescriptors() { + return maxFileDescriptors; + } + @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject("process"); builder.field("refresh_interval", refreshInterval); builder.field("id", id); + builder.field("max_file_descriptors", maxFileDescriptors); builder.endObject(); return builder; } @@ -84,10 +96,12 @@ public class ProcessInfo implements Streamable, Serializable, ToXContent { @Override public void readFrom(StreamInput in) throws IOException { refreshInterval = in.readLong(); id = in.readLong(); + maxFileDescriptors = in.readLong(); } @Override public void writeTo(StreamOutput out) throws IOException { out.writeLong(refreshInterval); out.writeLong(id); + out.writeLong(maxFileDescriptors); } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/monitor/process/ProcessStats.java b/modules/elasticsearch/src/main/java/org/elasticsearch/monitor/process/ProcessStats.java index bd00018c255..cb696a7eeb0 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/monitor/process/ProcessStats.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/monitor/process/ProcessStats.java @@ -37,12 +37,12 @@ public class ProcessStats implements Streamable, Serializable, ToXContent { long timestamp = -1; + long openFileDescriptors; + Cpu cpu = null; Mem mem = null; - Fd fd; - ProcessStats() { } @@ -70,17 +70,10 @@ public class ProcessStats implements Streamable, Serializable, ToXContent { return mem(); } - public Fd fd() { - return fd; - } - - public Fd getFd() { - return fd(); - } - @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject("process"); builder.field("timestamp", timestamp); + builder.field("open_file_descriptors", openFileDescriptors); if (cpu != null) { builder.startObject("cpu"); builder.field("percent", cpu.percent()); @@ -102,11 +95,6 @@ public class ProcessStats implements Streamable, Serializable, ToXContent { builder.field("total_virtual_in_bytes", mem.totalVirtual().bytes()); builder.endObject(); } - if (fd != null) { - builder.startObject("fd"); - builder.field("total", fd.total()); - builder.endObject(); - } builder.endObject(); return builder; } @@ -119,19 +107,18 @@ public class ProcessStats implements Streamable, Serializable, ToXContent { @Override public void readFrom(StreamInput in) throws IOException { timestamp = in.readVLong(); + openFileDescriptors = in.readLong(); if (in.readBoolean()) { cpu = Cpu.readCpu(in); } if (in.readBoolean()) { mem = Mem.readMem(in); } - if (in.readBoolean()) { - fd = Fd.readFd(in); - } } @Override public void writeTo(StreamOutput out) throws IOException { out.writeVLong(timestamp); + out.writeLong(openFileDescriptors); if (cpu == null) { out.writeBoolean(false); } else { @@ -144,47 +131,6 @@ public class ProcessStats implements Streamable, Serializable, ToXContent { out.writeBoolean(true); mem.writeTo(out); } - if (fd == null) { - out.writeBoolean(false); - } else { - out.writeBoolean(true); - fd.writeTo(out); - } - } - - public static class Fd implements Streamable, Serializable { - - long total = -1; - - Fd() { - } - - public static Fd readFd(StreamInput in) throws IOException { - Fd fd = new Fd(); - fd.readFrom(in); - return fd; - } - - @Override public void readFrom(StreamInput in) throws IOException { - total = in.readLong(); - } - - @Override public void writeTo(StreamOutput out) throws IOException { - out.writeLong(total); - } - - /** - * Get the Total number of open file descriptors. - * - *

Supported Platforms: AIX, HPUX, Linux, Solaris, Win32. - */ - public long total() { - return total; - } - - public long getTotal() { - return total(); - } } public static class Mem implements Streamable, Serializable { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/monitor/process/SigarProcessProbe.java b/modules/elasticsearch/src/main/java/org/elasticsearch/monitor/process/SigarProcessProbe.java index ae4b1eb8fff..b6cca7f77cc 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/monitor/process/SigarProcessProbe.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/monitor/process/SigarProcessProbe.java @@ -23,7 +23,10 @@ import org.elasticsearch.common.component.AbstractComponent; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.monitor.sigar.SigarService; -import org.hyperic.sigar.*; +import org.hyperic.sigar.ProcCpu; +import org.hyperic.sigar.ProcMem; +import org.hyperic.sigar.Sigar; +import org.hyperic.sigar.SigarException; /** * @author kimchy (shay.banon) @@ -38,13 +41,14 @@ public class SigarProcessProbe extends AbstractComponent implements ProcessProbe } @Override public synchronized ProcessInfo processInfo() { - return new ProcessInfo(sigarService.sigar().getPid()); + return new ProcessInfo(sigarService.sigar().getPid(), JmxProcessProbe.getMaxFileDescriptorCount()); } @Override public synchronized ProcessStats processStats() { Sigar sigar = sigarService.sigar(); ProcessStats stats = new ProcessStats(); stats.timestamp = System.currentTimeMillis(); + stats.openFileDescriptors = JmxProcessProbe.getOpenFileDescriptorCount(); try { ProcCpu cpu = sigar.getProcCpu(sigar.getPid()); @@ -66,16 +70,6 @@ public class SigarProcessProbe extends AbstractComponent implements ProcessProbe // ignore } - try { - ProcFd fd = sigar.getProcFd(sigar.getPid()); - if (fd.getTotal() != Sigar.FIELD_NOTIMPL) { - stats.fd = new ProcessStats.Fd(); - stats.fd.total = fd.getTotal(); - } - } catch (SigarException e) { - // ignore - } - return stats; } }