Added CPU to top command.
Project: http://git-wip-us.apache.org/repos/asf/incubator-blur/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-blur/commit/7ce55960 Tree: http://git-wip-us.apache.org/repos/asf/incubator-blur/tree/7ce55960 Diff: http://git-wip-us.apache.org/repos/asf/incubator-blur/diff/7ce55960 Branch: refs/heads/master Commit: 7ce55960667ef37259c94ff4e1b52ac99a463bfc Parents: 92577b4 Author: Aaron McCurry <[email protected]> Authored: Wed Aug 7 13:19:43 2013 -0400 Committer: Aaron McCurry <[email protected]> Committed: Wed Aug 7 13:19:43 2013 -0400 ---------------------------------------------------------------------- .../org/apache/blur/thrift/ThriftServer.java | 45 ++++++++++++++++++-- .../org/apache/blur/shell/top.properties | 7 ++- .../apache/blur/metrics/MetricsConstants.java | 1 + 3 files changed, 48 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/7ce55960/blur-core/src/main/java/org/apache/blur/thrift/ThriftServer.java ---------------------------------------------------------------------- diff --git a/blur-core/src/main/java/org/apache/blur/thrift/ThriftServer.java b/blur-core/src/main/java/org/apache/blur/thrift/ThriftServer.java index d7e6ee4..12b58cb 100644 --- a/blur-core/src/main/java/org/apache/blur/thrift/ThriftServer.java +++ b/blur-core/src/main/java/org/apache/blur/thrift/ThriftServer.java @@ -16,6 +16,7 @@ package org.apache.blur.thrift; * See the License for the specific language governing permissions and * limitations under the License. */ +import static org.apache.blur.metrics.MetricsConstants.CPU_USED; import static org.apache.blur.metrics.MetricsConstants.HEAP_USED; import static org.apache.blur.metrics.MetricsConstants.JVM; import static org.apache.blur.metrics.MetricsConstants.LOAD_AVERAGE; @@ -30,6 +31,7 @@ import java.lang.management.ManagementFactory; import java.lang.management.MemoryMXBean; import java.lang.management.MemoryUsage; import java.lang.management.OperatingSystemMXBean; +import java.lang.reflect.Method; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.UnknownHostException; @@ -73,7 +75,7 @@ public class ThriftServer { private TServerEventHandler _eventHandler; public static void printUlimits() throws IOException { - ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c" ,"ulimit -a"); + ProcessBuilder processBuilder = new ProcessBuilder("bash", "-c", "ulimit -a"); Process process; try { process = processBuilder.start(); @@ -90,11 +92,11 @@ public class ThriftServer { } reader.close(); } - + public static void setupJvmMetrics() { final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); final OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean(); - + Metrics.newGauge(new MetricName(ORG_APACHE_BLUR, SYSTEM, LOAD_AVERAGE), new Gauge<Double>() { @Override public Double value() { @@ -108,8 +110,45 @@ public class ThriftServer { return usage.getUsed(); } }); + Method processCpuTimeMethod = null; + for (Method method : operatingSystemMXBean.getClass().getDeclaredMethods()) { + if (method.getName().equals("getProcessCpuTime")) { + method.setAccessible(true); + processCpuTimeMethod = method; + } + } + final double availableProcessors = operatingSystemMXBean.getAvailableProcessors(); + if (processCpuTimeMethod != null) { + final Method pctm = processCpuTimeMethod; + Metrics.newGauge(new MetricName(ORG_APACHE_BLUR, JVM, CPU_USED), new Gauge<Double>() { + private long start = System.nanoTime(); + private long lastCpuTime = getProcessCputTime(pctm, operatingSystemMXBean); + + @Override + public Double value() { + long now = System.nanoTime(); + long cpuTime = getProcessCputTime(pctm, operatingSystemMXBean); + long time = now - start; + long processTime = cpuTime - lastCpuTime; + try { + return ((processTime / (double) time) / availableProcessors) * 100.0; + } finally { + lastCpuTime = cpuTime; + start = System.nanoTime(); + } + } + }); + } } + private static long getProcessCputTime(Method processCpuTimeMethod, OperatingSystemMXBean operatingSystemMXBean) { + try { + return (Long) processCpuTimeMethod.invoke(operatingSystemMXBean, new Object[] {}); + } catch (Exception e) { + LOG.error("Unknown Error", e); + return 0; + } + } public synchronized void close() { if (!_closed) { http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/7ce55960/blur-shell/src/main/resources/org/apache/blur/shell/top.properties ---------------------------------------------------------------------- diff --git a/blur-shell/src/main/resources/org/apache/blur/shell/top.properties b/blur-shell/src/main/resources/org/apache/blur/shell/top.properties index c1d765c..0487cca 100644 --- a/blur-shell/src/main/resources/org/apache/blur/shell/top.properties +++ b/blur-shell/src/main/resources/org/apache/blur/shell/top.properties @@ -1,4 +1,4 @@ -top.columns=SHARD_SERVER,SL,HU,IM,EQ,IQ,RO,RE,WO,WE,CH,CM,CE,CS,TC,IC,SC +top.columns=SHARD_SERVER,CP,SL,HU,IM,EQ,IQ,RO,RE,WO,WE,CH,CM,CE,CS,TC,IC,SC top.sizes=IM,SL top.SHARD_SERVER.shortname=Shard Server @@ -18,6 +18,7 @@ top.IQ.shortname=in qry top.EQ.shortname=ex qry top.HU.shortname=heap usd top.SL.shortname=sys load +top.CP.shortname=cpu % top.SHARD_SERVER.longname=Shard Server top.IQ.longname="org.apache.blur":type="Blur",name="Internal Queries/s" @@ -36,6 +37,7 @@ top.IC.longname="org.apache.blur":type="Blur",scope="default",name="Index Count" top.SC.longname="org.apache.blur":type="Blur",scope="default",name="Segment Count" top.SL.longname="org.apache.blur":type="System",name="Load Average" top.HU.longname="org.apache.blur":type="JVM",name="Heap Used" +top.CP.longname="org.apache.blur":type="JVM",name="Cpu Used" top.SHARD_SERVER.help=Shard Server - * indicates that the server is offline top.IQ.help=Internal Queries/s - This counts every shard query (One Minute Average) @@ -53,4 +55,5 @@ top.TC.help=Table Count - The number of tables top.IC.help=Index Count - The number of indexes top.SC.help=Segment Count - The number of index segments top.SL.help=Load Average - Current server load average -top.HU.help=Heap Used - Current heap size \ No newline at end of file +top.HU.help=Heap Used - Current heap size +top.CP.help=Cpu Used - Current System CPU % \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/7ce55960/blur-util/src/main/java/org/apache/blur/metrics/MetricsConstants.java ---------------------------------------------------------------------- diff --git a/blur-util/src/main/java/org/apache/blur/metrics/MetricsConstants.java b/blur-util/src/main/java/org/apache/blur/metrics/MetricsConstants.java index 2e94412..dd8d9b2 100644 --- a/blur-util/src/main/java/org/apache/blur/metrics/MetricsConstants.java +++ b/blur-util/src/main/java/org/apache/blur/metrics/MetricsConstants.java @@ -39,6 +39,7 @@ public class MetricsConstants { public static final String CACHE = "Cache"; public static final String JVM = "JVM"; public static final String HEAP_USED = "Heap Used"; + public static final String CPU_USED = "Cpu Used"; public static final String EVICTION = "Eviction"; public static final String TABLE_COUNT = "Table Count"; public static final String FILES_IN_QUEUE_TO_BE_DELETED = "Files in Queue to be Deleted";
