You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2021/01/19 06:25:07 UTC

[karaf-decanter] branch master updated: [KARAF-7000] Add System-Wide CPU Load to the oshi collector (#224)

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

jbonofre pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/karaf-decanter.git


The following commit(s) were added to refs/heads/master by this push:
     new 01c3e75  [KARAF-7000] Add System-Wide CPU Load to the oshi collector (#224)
01c3e75 is described below

commit 01c3e757d41e402959b53d8629e6c6a7b26a1d29
Author: paulsp <pa...@apache.org>
AuthorDate: Tue Jan 19 01:24:56 2021 -0500

    [KARAF-7000] Add System-Wide CPU Load to the oshi collector (#224)
---
 .../decanter/collector/oshi/OshiCollector.java     |  5 +++++
 .../decanter/collector/oshi/OshiCollectorTest.java | 25 ++++++++++++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/collector/oshi/src/main/java/org/apache/karaf/decanter/collector/oshi/OshiCollector.java b/collector/oshi/src/main/java/org/apache/karaf/decanter/collector/oshi/OshiCollector.java
index 3ea7a4f..e725fe3 100644
--- a/collector/oshi/src/main/java/org/apache/karaf/decanter/collector/oshi/OshiCollector.java
+++ b/collector/oshi/src/main/java/org/apache/karaf/decanter/collector/oshi/OshiCollector.java
@@ -51,6 +51,8 @@ public class OshiCollector implements Runnable {
 
     private Dictionary<String, Object> properties;
 
+    long[] lastSystemCpuLoadTicks = null;
+
     @Activate
     public void activate(ComponentContext componentContext) {
         activate(componentContext.getProperties());
@@ -58,6 +60,7 @@ public class OshiCollector implements Runnable {
 
     public void activate(Dictionary<String, Object> properties) {
         this.properties = properties;
+        this.lastSystemCpuLoadTicks = null;
     }
 
     @Override
@@ -110,6 +113,8 @@ public class OshiCollector implements Runnable {
                 data.put("processor.interrupts", hardwareAbstractionLayer.getProcessor().getInterrupts());
                 data.put("processor.logicalProcessorCount", hardwareAbstractionLayer.getProcessor().getLogicalProcessorCount());
                 data.put("processor.maxFreq", hardwareAbstractionLayer.getProcessor().getMaxFreq());
+                data.put("processor.systemCpuLoadBetweenTicks", lastSystemCpuLoadTicks != null ? hardwareAbstractionLayer.getProcessor().getSystemCpuLoadBetweenTicks(lastSystemCpuLoadTicks) : null);
+                lastSystemCpuLoadTicks = hardwareAbstractionLayer.getProcessor().getSystemCpuLoadTicks();
                 boolean processorsLogical = properties.get("processors.logical") != null ? Boolean.parseBoolean(properties.get("processors.logical").toString()) : true;
                 if (processorsLogical) {
                     int i = 0;
diff --git a/collector/oshi/src/test/java/org/apache/karaf/decanter/collector/oshi/OshiCollectorTest.java b/collector/oshi/src/test/java/org/apache/karaf/decanter/collector/oshi/OshiCollectorTest.java
index 8936848..c2c5473 100644
--- a/collector/oshi/src/test/java/org/apache/karaf/decanter/collector/oshi/OshiCollectorTest.java
+++ b/collector/oshi/src/test/java/org/apache/karaf/decanter/collector/oshi/OshiCollectorTest.java
@@ -27,6 +27,8 @@ import java.util.List;
 
 public class OshiCollectorTest {
 
+    static final String SYSTEM_CPU_LOAD_PROPERTY = "processor.systemCpuLoadBetweenTicks";
+
     @Test
     public void test() throws Exception {
         DispatcherMock dispatcherMock = new DispatcherMock();
@@ -45,6 +47,29 @@ public class OshiCollectorTest {
         }
     }
 
+   /**
+    * Verify the SYSTEM_CPU_LOAD_PROPERTY is null on the initial collector run
+    * and not null on subsequent collector runs.
+    */
+   @Test
+   public void testSystemCpuLoad() throws Exception {
+        DispatcherMock dispatcherMock = new DispatcherMock();
+
+        OshiCollector collector = new OshiCollector();
+        collector.setDispatcher(dispatcherMock);
+        collector.activate(new Hashtable<>());
+
+        collector.run();
+        collector.run();
+
+        Assert.assertEquals(2, dispatcherMock.postedEvents.size());
+        Event firstEvent = dispatcherMock.postedEvents.get(0);
+        Assert.assertNull("Initial systemCpuLoadBetweenTicks is null", firstEvent.getProperty(SYSTEM_CPU_LOAD_PROPERTY));
+        Event secondEvent = dispatcherMock.postedEvents.get(1);
+        Assert.assertNotNull("Second systemCpuLoadBetweenTicks is not null", secondEvent.getProperty(SYSTEM_CPU_LOAD_PROPERTY));
+        System.out.println(SYSTEM_CPU_LOAD_PROPERTY + ":" + secondEvent.getProperty(SYSTEM_CPU_LOAD_PROPERTY));
+    }
+
     class DispatcherMock implements EventAdmin {
 
         public List<Event> postedEvents = new ArrayList<>();