This is an automated email from the ASF dual-hosted git repository.
tanxinyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/master by this push:
new 89ebd32b1be Add Linux Memory Metrics (#12332)
89ebd32b1be is described below
commit 89ebd32b1be59be10aeb7ad4853f4f280fdd1a51
Author: ZhangHongYin <[email protected]>
AuthorDate: Tue Apr 16 10:10:17 2024 +0800
Add Linux Memory Metrics (#12332)
* update system metrics with linux
* fix command
* add \n
* update union
* update memory
* modify according to review
* modify according to review
* fix number
---
.../metrics/metricsets/system/SystemMetrics.java | 100 ++++++++++++++++++++-
.../apache/iotdb/metrics/utils/SystemMetric.java | 1 +
2 files changed, 100 insertions(+), 1 deletion(-)
diff --git
a/iotdb-core/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/system/SystemMetrics.java
b/iotdb-core/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/system/SystemMetrics.java
index aed6ee78074..f23d4bb7034 100644
---
a/iotdb-core/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/system/SystemMetrics.java
+++
b/iotdb-core/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/system/SystemMetrics.java
@@ -20,19 +20,24 @@
package org.apache.iotdb.metrics.metricsets.system;
import org.apache.iotdb.metrics.AbstractMetricService;
+import org.apache.iotdb.metrics.MetricConstant;
+import org.apache.iotdb.metrics.config.MetricConfig;
import org.apache.iotdb.metrics.config.MetricConfigDescriptor;
import org.apache.iotdb.metrics.metricsets.IMetricSet;
import org.apache.iotdb.metrics.utils.MetricLevel;
import org.apache.iotdb.metrics.utils.MetricType;
import org.apache.iotdb.metrics.utils.SystemMetric;
import org.apache.iotdb.metrics.utils.SystemTag;
+import org.apache.iotdb.metrics.utils.SystemType;
import org.apache.iotdb.tsfile.utils.FSUtils;
import com.sun.management.OperatingSystemMXBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.io.BufferedReader;
import java.io.IOException;
+import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.nio.file.FileStore;
import java.nio.file.Files;
@@ -44,7 +49,18 @@ import java.util.Set;
public class SystemMetrics implements IMetricSet {
private static final Logger logger =
LoggerFactory.getLogger(SystemMetrics.class);
- private static final String SYSTEM = "system";
+ private static final MetricConfig CONFIG =
MetricConfigDescriptor.getInstance().getMetricConfig();
+ private final Runtime runtime = Runtime.getRuntime();
+ private final String[] getSystemMemoryCommand = new String[] {"/bin/sh",
"-c", "free"};
+ private final String[] linuxMemoryTitles =
+ new String[] {"Total", "Used", "Free", "Shared", "Buff/Cache",
"Available"};
+ private long lastUpdateTime = 0L;
+ private volatile long usedMemory = 0L;
+ private volatile long sharedMemory = 0L;
+ private volatile long buffCacheMemory = 0L;
+ private volatile long availableMemory = 0L;
+
+ static final String SYSTEM = "system";
private final com.sun.management.OperatingSystemMXBean osMxBean;
private Set<FileStore> fileStores = new HashSet<>();
private static final String FAILED_TO_STATISTIC = "Failed to statistic the
size of {}, because";
@@ -167,6 +183,79 @@ public class SystemMetrics implements IMetricSet {
a -> osMxBean.getCommittedVirtualMemorySize(),
SystemTag.NAME.toString(),
SYSTEM);
+ if (CONFIG.getSystemType() == SystemType.LINUX) {
+ metricService.createAutoGauge(
+ SystemMetric.LINUX_MEMORY_SIZE.toString(),
+ MetricLevel.CORE,
+ this,
+ a -> {
+ updateLinuxSystemMemInfo();
+ return usedMemory;
+ },
+ SystemTag.NAME.toString(),
+ linuxMemoryTitles[1]);
+ metricService.createAutoGauge(
+ SystemMetric.LINUX_MEMORY_SIZE.toString(),
+ MetricLevel.CORE,
+ this,
+ a -> {
+ updateLinuxSystemMemInfo();
+ return sharedMemory;
+ },
+ SystemTag.NAME.toString(),
+ linuxMemoryTitles[3]);
+ metricService.createAutoGauge(
+ SystemMetric.LINUX_MEMORY_SIZE.toString(),
+ MetricLevel.CORE,
+ this,
+ a -> {
+ updateLinuxSystemMemInfo();
+ return buffCacheMemory;
+ },
+ SystemTag.NAME.toString(),
+ linuxMemoryTitles[4]);
+ metricService.createAutoGauge(
+ SystemMetric.LINUX_MEMORY_SIZE.toString(),
+ MetricLevel.CORE,
+ this,
+ a -> {
+ updateLinuxSystemMemInfo();
+ return availableMemory;
+ },
+ SystemTag.NAME.toString(),
+ linuxMemoryTitles[5]);
+ }
+ }
+
+ private void updateLinuxSystemMemInfo() {
+ long time = System.currentTimeMillis();
+ if (time - lastUpdateTime > MetricConstant.UPDATE_INTERVAL) {
+ lastUpdateTime = time;
+ try {
+ Process process = runtime.exec(getSystemMemoryCommand);
+ StringBuilder result = new StringBuilder();
+ try (BufferedReader input =
+ new BufferedReader(new
InputStreamReader(process.getInputStream()))) {
+ String line;
+ while ((line = input.readLine()) != null) {
+ result.append(line).append("\n");
+ }
+ }
+ String[] lines = result.toString().split("\n");
+ // if failed to get result
+ if (lines.length >= 2) {
+ String[] memParts = lines[1].trim().split("\\s+");
+ if (memParts.length == linuxMemoryTitles.length) {
+ usedMemory = Long.parseLong(memParts[2]) * 1024;
+ sharedMemory = Long.parseLong(memParts[4]) * 1024;
+ buffCacheMemory = Long.parseLong(memParts[5]) * 1024;
+ availableMemory = Long.parseLong(memParts[6]) * 1024;
+ }
+ }
+ } catch (IOException e) {
+ logger.debug("Failed to get memory, because ", e);
+ }
+ }
}
private void removeSystemMemInfo(AbstractMetricService metricService) {
@@ -195,6 +284,15 @@ public class SystemMetrics implements IMetricSet {
SystemMetric.SYS_COMMITTED_VM_SIZE.toString(),
SystemTag.NAME.toString(),
SYSTEM);
+ if (CONFIG.getSystemType() == SystemType.LINUX) {
+ for (String title : linuxMemoryTitles) {
+ metricService.remove(
+ MetricType.GAUGE,
+ SystemMetric.LINUX_MEMORY_SIZE.toString(),
+ SystemTag.NAME.toString(),
+ title);
+ }
+ }
}
private void collectSystemDiskInfo(AbstractMetricService metricService) {
diff --git
a/iotdb-core/metrics/interface/src/main/java/org/apache/iotdb/metrics/utils/SystemMetric.java
b/iotdb-core/metrics/interface/src/main/java/org/apache/iotdb/metrics/utils/SystemMetric.java
index 50d884f68bb..357def37845 100644
---
a/iotdb-core/metrics/interface/src/main/java/org/apache/iotdb/metrics/utils/SystemMetric.java
+++
b/iotdb-core/metrics/interface/src/main/java/org/apache/iotdb/metrics/utils/SystemMetric.java
@@ -51,6 +51,7 @@ public enum SystemMetric {
// system related
SYS_CPU_LOAD("sys_cpu_load"),
SYS_CPU_CORES("sys_cpu_cores"),
+ LINUX_MEMORY_SIZE("linux_memory_size"),
SYS_TOTAL_PHYSICAL_MEMORY_SIZE("sys_total_physical_memory_size"),
SYS_FREE_PHYSICAL_MEMORY_SIZE("sys_free_physical_memory_size"),
SYS_TOTAL_SWAP_SPACE_SIZE("sys_total_swap_space_size"),