You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by rg...@apache.org on 2019/11/17 12:31:14 UTC

[qpid-broker-j] branch master updated: QPID-8376 : Report Process CPU usage in Broker Stats

This is an automated email from the ASF dual-hosted git repository.

rgodfrey pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/qpid-broker-j.git


The following commit(s) were added to refs/heads/master by this push:
     new b8d24fc  QPID-8376 : Report Process CPU usage in Broker Stats
b8d24fc is described below

commit b8d24fc93ba41d3a4b4b5b74c04ea444a78e7943
Author: Robert Godfrey <rg...@apache.org>
AuthorDate: Sun Nov 17 13:14:50 2019 +0100

    QPID-8376 : Report Process CPU usage in Broker Stats
---
 .../qpid/server/model/BrokerAttributeInjector.java | 105 +++++++++++++++++++++
 1 file changed, 105 insertions(+)

diff --git a/broker-core/src/main/java/org/apache/qpid/server/model/BrokerAttributeInjector.java b/broker-core/src/main/java/org/apache/qpid/server/model/BrokerAttributeInjector.java
index 42d108e..a2d5278 100644
--- a/broker-core/src/main/java/org/apache/qpid/server/model/BrokerAttributeInjector.java
+++ b/broker-core/src/main/java/org/apache/qpid/server/model/BrokerAttributeInjector.java
@@ -23,13 +23,18 @@ package org.apache.qpid.server.model;
 import java.lang.management.GarbageCollectorMXBean;
 import java.lang.management.ManagementFactory;
 import java.lang.management.MemoryPoolMXBean;
+import java.lang.management.OperatingSystemMXBean;
 import java.lang.management.PlatformManagedObject;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
+import java.util.function.Function;
+import java.util.function.ToLongFunction;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -197,6 +202,95 @@ public class BrokerAttributeInjector implements ConfiguredObjectAttributeInjecto
                 LOGGER.warn("Failed to inject statistic '{}'", jvmGCCollectionCountStatisticName, e);
             }
         }
+
+        OperatingSystemMXBean osMXBean = ManagementFactory.getOperatingSystemMXBean();
+        try
+        {
+            Method method = osMXBean.getClass().getDeclaredMethod("getProcessCpuTime");
+            method.setAccessible(true);
+            ToLongFunction<Broker> supplier  = broker -> {
+                try
+                {
+                    final Object returnValue = method.invoke(osMXBean);
+
+                    if(returnValue instanceof Number)
+                    {
+                        return ((Number)returnValue).longValue();
+                    }
+                }
+                catch (IllegalAccessException | InvocationTargetException e)
+                {
+                    LOGGER.warn("Unable to get cumulative process CPU time");
+                }
+                return -1L;
+            };
+
+            Method getLongValue = BrokerAttributeInjector.class.getDeclaredMethod("getLongValue",
+                                                                                  Broker.class,
+                                                                                  ToLongFunction.class);
+
+            final ConfiguredObjectInjectedStatistic<?, ?> injectedStatistic =
+                    new ConfiguredObjectInjectedStatistic<>("processCpuTime",
+                                                            getLongValue,
+                                                            new Object[]{supplier},
+                                                            "Cumulative process CPU time",
+                                                            _typeValidator,
+                                                            StatisticUnit.TIME_DURATION,
+                                                            StatisticType.CUMULATIVE,
+                                                            osMXBean.getName()
+                                                            + " Process CPU Time");
+            statistics.add(injectedStatistic);
+
+        }
+        catch (NoSuchMethodException e)
+        {
+            LOGGER.warn("Failed to inject statistic 'getProcessCpuTime'", e);
+        }
+
+        try
+        {
+            Method method = osMXBean.getClass().getDeclaredMethod("getProcessCpuLoad");
+            method.setAccessible(true);
+            Function<Broker, BigDecimal> supplier  = broker -> {
+                try
+                {
+                    final Object returnValue = method.invoke(osMXBean);
+
+                    if(returnValue instanceof Number)
+                    {
+                        return BigDecimal.valueOf(((Number) returnValue).doubleValue()).setScale(4,
+                                                                                                 RoundingMode.HALF_UP);
+                    }
+                }
+                catch (IllegalAccessException | InvocationTargetException e)
+                {
+                    LOGGER.warn("Unable to get current process CPU load");
+                }
+                return BigDecimal.valueOf(Double.NaN);
+            };
+
+            Method getBigDecimalValue = BrokerAttributeInjector.class.getDeclaredMethod("getBigDecimalValue",
+                                                                                      Broker.class,
+                                                                                      Function.class);
+
+            final ConfiguredObjectInjectedStatistic<?, ?> injectedStatistic =
+                    new ConfiguredObjectInjectedStatistic<>("processCpuLoad",
+                                                            getBigDecimalValue,
+                                                            new Object[]{supplier},
+                                                            "Current process CPU load",
+                                                            _typeValidator,
+                                                            StatisticUnit.COUNT,
+                                                            StatisticType.POINT_IN_TIME,
+                                                            osMXBean.getName()
+                                                            + " Process CPU Load");
+            statistics.add(injectedStatistic);
+
+        }
+        catch (NoSuchMethodException e)
+        {
+            LOGGER.warn("Failed to inject statistic 'getProcessCpuLoad'", e);
+        }
+
         return statistics;
     }
 
@@ -365,6 +459,17 @@ public class BrokerAttributeInjector implements ConfiguredObjectAttributeInjecto
         }
     }
 
+    private static long getLongValue(Broker broker, ToLongFunction<Broker> supplier)
+    {
+        return supplier.applyAsLong(broker);
+    }
+
+    private static BigDecimal getBigDecimalValue(Broker broker, Function<Broker, BigDecimal> supplier)
+    {
+        return supplier.apply(broker);
+    }
+
+
     public static void dumpHeap(Broker<?> broker,
                                 PlatformManagedObject hotSpotDiagnosticMXBean,
                                 Method dumpHeapMethod,


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org