]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9802 add memory and cpu metrics to JvmStateSection
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 24 Sep 2017 20:11:05 +0000 (22:11 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 26 Sep 2017 21:49:38 +0000 (23:49 +0200)
server/sonar-process/src/main/java/org/sonar/process/systeminfo/JvmStateSection.java
server/sonar-process/src/test/java/org/sonar/process/systeminfo/JvmStateSectionTest.java

index 804e2f0de5e90f9d574dad5d54ab216b0c70449c..b5cf9c90f4652e23a92d77f7181599a3efa28718 100644 (file)
@@ -22,9 +22,11 @@ package org.sonar.process.systeminfo;
 import java.lang.management.ManagementFactory;
 import java.lang.management.MemoryMXBean;
 import java.lang.management.MemoryUsage;
+import java.lang.management.OperatingSystemMXBean;
 import java.lang.management.ThreadMXBean;
 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
 
+import static java.lang.String.format;
 import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute;
 
 /**
@@ -47,6 +49,8 @@ public class JvmStateSection implements SystemInfoSection {
   ProtobufSystemInfo.Section toProtobuf(MemoryMXBean memoryBean) {
     ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder();
     protobuf.setName(name);
+    addAttributeInMb(protobuf,"Max Memory (MB)", Runtime.getRuntime().maxMemory());
+    addAttributeInMb(protobuf, "Free Memory (MB)", Runtime.getRuntime().freeMemory());
     MemoryUsage heap = memoryBean.getHeapMemoryUsage();
     addAttributeInMb(protobuf, "Heap Committed (MB)", heap.getCommitted());
     addAttributeInMb(protobuf, "Heap Init (MB)", heap.getInit());
@@ -57,7 +61,8 @@ public class JvmStateSection implements SystemInfoSection {
     addAttributeInMb(protobuf, "Non Heap Init (MB)", nonHeap.getInit());
     addAttributeInMb(protobuf, "Non Heap Max (MB)", nonHeap.getMax());
     addAttributeInMb(protobuf, "Non Heap Used (MB)", nonHeap.getUsed());
-
+    OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
+    setAttribute(protobuf,"System Load Average", format("%.1f%% (last minute)", os.getSystemLoadAverage() * 100.0));
     ThreadMXBean thread = ManagementFactory.getThreadMXBean();
     setAttribute(protobuf, "Threads", thread.getThreadCount());
 
index 77da5df5719eea46f6312dce7e062768eb66babe..c2513a418d460cc66fe094b6baa3b91381976097 100644 (file)
@@ -39,7 +39,11 @@ public class JvmStateSectionTest {
 
     assertThat(section.getName()).isEqualTo(PROCESS_NAME);
     assertThat(section.getAttributesCount()).isGreaterThan(0);
-    assertThat(section.getAttributesList()).extracting("key").contains("Threads", "Heap Max (MB)");
+    assertThat(section.getAttributesList()).extracting("key")
+      .contains(
+        "Max Memory (MB)", "Free Memory (MB)",
+        "Heap Max (MB)",
+        "System Load Average", "Threads");
   }
 
   @Test