Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,38 @@
import org.apache.iotdb.commons.service.metric.enums.Tag;
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 com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.BaseTSD.SIZE_T;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.WinNT;
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.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ProcessMetrics implements IMetricSet {
private static final Logger LOGGER = LoggerFactory.getLogger(ProcessMetrics.class);
private static final MetricConfig CONFIG = MetricConfigDescriptor.getInstance().getMetricConfig();
private final OperatingSystemMXBean sunOsMxBean;
private final Runtime runtime;
private static final String PROCESS = "process";
private static final String LINUX_RSS_PREFIX = "VmRSS:";
private long lastUpdateTime = 0L;
private volatile long processCpuLoad = 0L;
private volatile long processCpuTime = 0L;
Expand Down Expand Up @@ -213,7 +232,68 @@
}

private long getProcessUsedMemory() {
return runtime.totalMemory() - runtime.freeMemory();
long residentMemory = getResidentMemory();
return residentMemory > 0 ? residentMemory : runtime.totalMemory() - runtime.freeMemory();
}

private long getResidentMemory() {
if (CONFIG.getPid().isEmpty()) {
return 0L;
}
try {
switch (CONFIG.getSystemType()) {
case LINUX:
return getLinuxResidentMemory();
case MAC:
return getMacResidentMemory();
case WINDOWS:
return getWindowsResidentMemory();
default:
return 0L;
}
} catch (Exception e) {

Check warning on line 254 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/ProcessMetrics.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Either re-interrupt this method or rethrow the "InterruptedException" that can be caught here.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ2PExkYqLdx4W_fPnE_&open=AZ2PExkYqLdx4W_fPnE_&pullRequest=17482
LOGGER.debug("Failed to get process resident memory for pid {}", CONFIG.getPid(), e);
return 0L;
}
}

private long getLinuxResidentMemory() throws IOException {
Path statusPath = Paths.get(String.format("/proc/%s/status", CONFIG.getPid()));
if (!Files.exists(statusPath)) {
return 0L;
}
for (String line : Files.readAllLines(statusPath)) {
if (line.startsWith(LINUX_RSS_PREFIX)) {
String[] parts = line.trim().split("\\s+");
if (parts.length >= 2) {
return Long.parseLong(parts[1]) * 1024L;
}
}
}
return 0L;
}

private long getMacResidentMemory() throws IOException, InterruptedException {
Process process = runtime.exec(new String[] {"ps", "-o", "rss=", "-p", CONFIG.getPid()});
try (BufferedReader input =
new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line = input.readLine();
int exitCode = process.waitFor();
if (exitCode == 0 && line != null && !line.trim().isEmpty()) {
return Long.parseLong(line.trim()) * 1024L;
}
}
return 0L;
}

private long getWindowsResidentMemory() {
WinNT.HANDLE hProcess = Kernel32.INSTANCE.GetCurrentProcess();

Check warning on line 290 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/ProcessMetrics.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Local variable name 'hProcess' must match pattern '^[a-z]([a-z0-9][a-zA-Z0-9]*)?$'.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ2PExkYqLdx4W_fPnFD&open=AZ2PExkYqLdx4W_fPnFD&pullRequest=17482
ProcessMemoryCounters counters = new ProcessMemoryCounters();
boolean success = PsapiExt.INSTANCE.GetProcessMemoryInfo(hProcess, counters, counters.size());
if (!success) {
return 0L;
}
return counters.workingSetSize.longValue();
}

private long getProcessStatus() {
Expand All @@ -233,4 +313,35 @@
long totalPhysicalMemorySize = sunOsMxBean.getTotalPhysicalMemorySize();
return (double) processUsedMemory / (double) totalPhysicalMemorySize * 100;
}

public interface PsapiExt extends Library {
PsapiExt INSTANCE = Native.load("psapi", PsapiExt.class);

boolean GetProcessMemoryInfo(WinNT.HANDLE process, ProcessMemoryCounters counters, int size);

Check warning on line 320 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/ProcessMetrics.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Method name 'GetProcessMemoryInfo' must match pattern '^[a-z][a-z0-9][a-zA-Z0-9_]*$'.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ2PExkYqLdx4W_fPnFE&open=AZ2PExkYqLdx4W_fPnFE&pullRequest=17482

Check warning on line 320 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/ProcessMetrics.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this method name to match the regular expression '^[a-z][a-zA-Z0-9]*$'.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ2PExkYqLdx4W_fPnFB&open=AZ2PExkYqLdx4W_fPnFB&pullRequest=17482
}

@Structure.FieldOrder({
"cb",
"pageFaultCount",
"peakWorkingSetSize",
"workingSetSize",
"quotaPeakPagedPoolUsage",
"quotaPagedPoolUsage",
"quotaPeakNonPagedPoolUsage",
"quotaNonPagedPoolUsage",
"pagefileUsage",
"peakPagefileUsage"
})
public static class ProcessMemoryCounters extends Structure {

Check warning on line 335 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/ProcessMetrics.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Override the "equals" method in this class.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ2PExkYqLdx4W_fPnFC&open=AZ2PExkYqLdx4W_fPnFC&pullRequest=17482
public int cb = size();

Check warning on line 336 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/ProcessMetrics.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make cb a static final constant or non-public and provide accessors if needed.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ2PExkYqLdx4W_fPnE1&open=AZ2PExkYqLdx4W_fPnE1&pullRequest=17482
public int pageFaultCount;

Check warning on line 337 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/ProcessMetrics.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make pageFaultCount a static final constant or non-public and provide accessors if needed.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ2PExkYqLdx4W_fPnE2&open=AZ2PExkYqLdx4W_fPnE2&pullRequest=17482
public SIZE_T peakWorkingSetSize;

Check warning on line 338 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/ProcessMetrics.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make peakWorkingSetSize a static final constant or non-public and provide accessors if needed.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ2PExkYqLdx4W_fPnE3&open=AZ2PExkYqLdx4W_fPnE3&pullRequest=17482
public SIZE_T workingSetSize;

Check warning on line 339 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/ProcessMetrics.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make workingSetSize a static final constant or non-public and provide accessors if needed.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ2PExkYqLdx4W_fPnE4&open=AZ2PExkYqLdx4W_fPnE4&pullRequest=17482
public SIZE_T quotaPeakPagedPoolUsage;

Check warning on line 340 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/ProcessMetrics.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make quotaPeakPagedPoolUsage a static final constant or non-public and provide accessors if needed.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ2PExkYqLdx4W_fPnE5&open=AZ2PExkYqLdx4W_fPnE5&pullRequest=17482
public SIZE_T quotaPagedPoolUsage;

Check warning on line 341 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/ProcessMetrics.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make quotaPagedPoolUsage a static final constant or non-public and provide accessors if needed.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ2PExkYqLdx4W_fPnE6&open=AZ2PExkYqLdx4W_fPnE6&pullRequest=17482
public SIZE_T quotaPeakNonPagedPoolUsage;

Check warning on line 342 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/ProcessMetrics.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make quotaPeakNonPagedPoolUsage a static final constant or non-public and provide accessors if needed.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ2PExkYqLdx4W_fPnE7&open=AZ2PExkYqLdx4W_fPnE7&pullRequest=17482
public SIZE_T quotaNonPagedPoolUsage;

Check warning on line 343 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/ProcessMetrics.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make quotaNonPagedPoolUsage a static final constant or non-public and provide accessors if needed.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ2PExkYqLdx4W_fPnE8&open=AZ2PExkYqLdx4W_fPnE8&pullRequest=17482
public SIZE_T pagefileUsage;

Check warning on line 344 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/ProcessMetrics.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make pagefileUsage a static final constant or non-public and provide accessors if needed.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ2PExkYqLdx4W_fPnE9&open=AZ2PExkYqLdx4W_fPnE9&pullRequest=17482
public SIZE_T peakPagefileUsage;

Check warning on line 345 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/ProcessMetrics.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Make peakPagefileUsage a static final constant or non-public and provide accessors if needed.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AZ2PExkYqLdx4W_fPnE-&open=AZ2PExkYqLdx4W_fPnE-&pullRequest=17482
}
}
Loading