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

spricoder pushed a commit to branch feature/linux-memory
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/feature/linux-memory by this 
push:
     new ae2e7f9c366 update system metrics with linux
ae2e7f9c366 is described below

commit ae2e7f9c3668320902893fdf204667b545d40f3f
Author: spricoder <[email protected]>
AuthorDate: Sat Apr 13 16:00:04 2024 +0800

    update system metrics with linux
---
 .../metrics/metricsets/system/SystemMetrics.java   | 48 ++++++++++++++++++++++
 .../apache/iotdb/metrics/utils/SystemMetric.java   |  2 +
 2 files changed, 50 insertions(+)

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 2450057d219..03a0339cea9 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,23 @@
 package org.apache.iotdb.metrics.metricsets.system;
 
 import org.apache.iotdb.metrics.AbstractMetricService;
+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;
@@ -46,6 +50,9 @@ import java.util.concurrent.atomic.AtomicReference;
 
 public class SystemMetrics implements IMetricSet {
   private static final Logger logger = 
LoggerFactory.getLogger(SystemMetrics.class);
+  private static final MetricConfig CONFIG = 
MetricConfigDescriptor.getInstance().getMetricConfig();
+  private final Runtime runtime = Runtime.getRuntime();
+  private final String[] getSystemMemoryCommand = new String[] {"free"};
   private static final String SYSTEM = "system";
   private final com.sun.management.OperatingSystemMXBean osMxBean;
   private final Set<FileStore> fileStores = new HashSet<>();
@@ -166,6 +173,47 @@ public class SystemMetrics implements IMetricSet {
         a -> osMxBean.getCommittedVirtualMemorySize(),
         SystemTag.NAME.toString(),
         SYSTEM);
+    if (CONFIG.getSystemType() == SystemType.LINUX) {
+      metricService.createAutoGauge(
+          SystemMetric.LINUX_MEMORY_COUNT.toString(),
+          MetricLevel.CORE,
+          this,
+          a -> a.updateLinuxSystemMemInfo(metricService),
+          SystemTag.NAME.toString());
+    }
+  }
+
+  private final String[] linuxMemoryTitles =
+      new String[] {"total", "used", "free", "shared", "buff/cache", 
"available"};
+
+  private long updateLinuxSystemMemInfo(AbstractMetricService metricService) {
+    long count = 0;
+    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);
+        }
+      }
+      String[] lines = result.toString().split("\n");
+      String[] memParts = lines[1].trim().split("\\s+");
+      count = memParts.length;
+      for (int i = 1; i < count; i++) {
+        metricService
+            .getOrCreateGauge(
+                SystemMetric.LINUX_MEMORY_SIZE.toString(),
+                MetricLevel.CORE,
+                SystemTag.NAME.toString(),
+                linuxMemoryTitles[i - 1])
+            .set(Long.parseLong(memParts[i]));
+      }
+    } catch (IOException e) {
+      logger.warn("Failed to get memory, because ", e);
+    }
+    return count;
   }
 
   private void removeSystemMemInfo(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..ca775f8596f 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,8 @@ public enum SystemMetric {
   // system related
   SYS_CPU_LOAD("sys_cpu_load"),
   SYS_CPU_CORES("sys_cpu_cores"),
+  LINUX_MEMORY_SIZE("linux_memory_size"),
+  LINUX_MEMORY_COUNT("linux_memory_count"),
   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"),

Reply via email to