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;
/**
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());
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());
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