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

marklau99 pushed a commit to branch cpu-monitor
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit a69f654bb8e808e42b9a60b1571d694e56cae007
Author: Liu Xuxin <[email protected]>
AuthorDate: Sun Apr 9 22:26:14 2023 +0800

    temp
---
 .../iotdb/metrics/metricsets/cpu/CpuMetrics.java   |  58 ++++++++++
 .../metrics/metricsets/cpu/ICpuMetricsManager.java |  33 ++++++
 .../metricsets/cpu/LinuxCpuMetricsManager.java     | 120 +++++++++++++++++++++
 .../metricsets/cpu/MacCpuMetricsManager.java       |  22 ++++
 .../metricsets/cpu/WindowsCpuMetricsManager.java   |  22 ++++
 5 files changed, 255 insertions(+)

diff --git 
a/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/cpu/CpuMetrics.java
 
b/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/cpu/CpuMetrics.java
new file mode 100644
index 0000000000..a5bf639203
--- /dev/null
+++ 
b/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/cpu/CpuMetrics.java
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.metrics.metricsets.cpu;
+
+import org.apache.iotdb.metrics.AbstractMetricService;
+import org.apache.iotdb.metrics.metricsets.IMetricSet;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class CpuMetrics implements IMetricSet {
+  private final ICpuMetricsManager cpuMetricsManager = 
ICpuMetricsManager.getCpuMetricsManager();
+  private static final Logger log = LoggerFactory.getLogger(CpuMetrics.class);
+  private final String processName;
+  private static final String MODULE = "module";
+  private static final String CPU_USAGE = "cpu_usage";
+  private final String dataNode = "datanode";
+  private final String configNode = "confignode";
+  private final String[] dataNodeModules =
+      new String[] {"query", "write", "flush", "compaction", "consensus", 
"metadata", "sync"};
+  private final String[] configNodeModules = new String[] {"consensus", "rpc"};
+  private final String[] modules;
+
+  public CpuMetrics(String processName) {
+    this.processName = processName;
+    if (this.processName.equals(dataNode)) {
+      this.modules = dataNodeModules;
+    } else if (this.processName.equals(configNode)) {
+      this.modules = configNodeModules;
+    } else {
+      log.error("Invalid process name: {}", processName);
+      modules = new String[0];
+    }
+  }
+
+  @Override
+  public void bindTo(AbstractMetricService metricService) {}
+
+  @Override
+  public void unbindFrom(AbstractMetricService metricService) {}
+}
diff --git 
a/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/cpu/ICpuMetricsManager.java
 
b/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/cpu/ICpuMetricsManager.java
new file mode 100644
index 0000000000..bbffb29513
--- /dev/null
+++ 
b/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/cpu/ICpuMetricsManager.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.metrics.metricsets.cpu;
+
+import java.util.Collections;
+import java.util.Map;
+
+public interface ICpuMetricsManager {
+  static ICpuMetricsManager getCpuMetricsManager(String processName, String[] 
modules) {
+    return null;
+  }
+
+  default Map<String, Double> getCpuUsage() {
+    return Collections.emptyMap();
+  }
+}
diff --git 
a/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/cpu/LinuxCpuMetricsManager.java
 
b/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/cpu/LinuxCpuMetricsManager.java
new file mode 100644
index 0000000000..ae1dd42dbe
--- /dev/null
+++ 
b/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/cpu/LinuxCpuMetricsManager.java
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.metrics.metricsets.cpu;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.lang.management.ThreadMXBean;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+public class LinuxCpuMetricsManager implements ICpuMetricsManager {
+  private static final Logger log = 
LoggerFactory.getLogger(LinuxCpuMetricsManager.class);
+  private final String processName;
+  private final String pid;
+  private final String[] modules;
+
+  @SuppressWarnings("squid:S1075")
+  private static final String SYSTEM_STAT_FILE = "/proc/stat";
+
+  @SuppressWarnings("squid:S1075")
+  private static final String THREAD_STAT_FILE = "/proc/%s/stat";
+
+  @SuppressWarnings("squid:S1075")
+  private String collectThreadIdsPath = "/proc/%s/task";
+
+  public LinuxCpuMetricsManager(String processName, String[] modules) {
+    this.processName = processName;
+    this.modules = modules;
+    this.pid = String.valueOf(Thread.currentThread().getId());
+    this.collectThreadIdsPath = String.format(collectThreadIdsPath, pid);
+  }
+
+  @Override
+  public Map<String, Double> getCpuUsage() {
+    return null;
+  }
+
+  /**
+   * Get the total CPU time of system.
+   *
+   * @return the total CPU time of system
+   */
+  private long getCpuTimeForSystem() {
+    try {
+      String statLine = Files.readAllLines(Paths.get(SYSTEM_STAT_FILE)).get(0);
+      String[] cpuInfo = statLine.split("\\s+");
+      long totalCpuTime = 0;
+      for (int i = 1; i < cpuInfo.length; i++) {
+        totalCpuTime += Long.parseLong(cpuInfo[i]);
+      }
+      return totalCpuTime;
+    } catch (IOException e) {
+      log.error("Cannot read file {}", SYSTEM_STAT_FILE, e);
+      // do not return 0 to avoid divide by 0
+      return 1;
+    }
+  }
+
+  /**
+   * Collect all threads of this process, and map them to their thread name.
+   *
+   * @return a map from thread id to thread name
+   */
+  private Map<String, String> collectAllThreads() {
+    File threadsDirectory = new File(collectThreadIdsPath);
+    if (!threadsDirectory.exists()) {
+      log.error("Cannot find directory {}", collectThreadIdsPath);
+      return Collections.emptyMap();
+    }
+
+    File[] threadFiles = threadsDirectory.listFiles();
+    if (threadFiles == null || threadFiles.length == 0) {
+      return Collections.emptyMap();
+    }
+
+    // Leaving one free space to avoid rehashing
+    Map<String, String> resultMap = new HashMap<>(threadFiles.length + 1, 1);
+    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
+    for (File threadFile : threadFiles) {
+      String threadId = threadFile.getName();
+      String threadName = 
threadMXBean.getThreadInfo(Long.parseLong(threadId)).getThreadName();
+      resultMap.put(threadId, threadName);
+    }
+    return resultMap;
+  }
+
+  /**
+   * Collect the cpu time for all thread in this process.
+   *
+   * @return a map from thread id to cpu time
+   */
+  private Map<String, Long> collectCpuTimeForPerThread(Set<String> threadIds) {
+    return null;
+  }
+}
diff --git 
a/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/cpu/MacCpuMetricsManager.java
 
b/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/cpu/MacCpuMetricsManager.java
new file mode 100644
index 0000000000..166b57ceda
--- /dev/null
+++ 
b/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/cpu/MacCpuMetricsManager.java
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.metrics.metricsets.cpu;
+
+public class MacCpuMetricsManager {}
diff --git 
a/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/cpu/WindowsCpuMetricsManager.java
 
b/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/cpu/WindowsCpuMetricsManager.java
new file mode 100644
index 0000000000..ce2b249c9e
--- /dev/null
+++ 
b/metrics/interface/src/main/java/org/apache/iotdb/metrics/metricsets/cpu/WindowsCpuMetricsManager.java
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.metrics.metricsets.cpu;
+
+public class WindowsCpuMetricsManager {}

Reply via email to