mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-25 18:55:28 -06:00
[issue-61] Use OSHI library for getting system information
This fixes #61
This commit is contained in:
parent
3262d527ba
commit
2c662ffbe5
@ -112,6 +112,11 @@
|
|||||||
<version>4.1.45.Final</version>
|
<version>4.1.45.Final</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.oshi</groupId>
|
||||||
|
<artifactId>oshi-core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-shade-plugin</artifactId>
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
@ -17,8 +17,11 @@
|
|||||||
|
|
||||||
package io.nosqlbench.engine.api.activityimpl;
|
package io.nosqlbench.engine.api.activityimpl;
|
||||||
|
|
||||||
|
import oshi.SystemInfo;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import oshi.hardware.CentralProcessor;
|
||||||
|
import oshi.hardware.HardwareAbstractionLayer;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
@ -28,88 +31,54 @@ import java.util.*;
|
|||||||
public class CpuInfo {
|
public class CpuInfo {
|
||||||
private final static Logger logger = LoggerFactory.getLogger(CpuInfo.class);
|
private final static Logger logger = LoggerFactory.getLogger(CpuInfo.class);
|
||||||
|
|
||||||
|
final private static SystemInfo SYSTEM_INFO = new SystemInfo();
|
||||||
|
|
||||||
public static Optional<ProcDetails> getProcDetails() {
|
public static Optional<ProcDetails> getProcDetails() {
|
||||||
List<Map<String, String>> cpuinfo = new ArrayList<>();
|
return Optional.of(new ProcDetails(SYSTEM_INFO));
|
||||||
try {
|
|
||||||
String data = Files.readString(Path.of("/proc/cpuinfo"), StandardCharsets.UTF_8);
|
|
||||||
String[] sections = data.split("\n\n");
|
|
||||||
for (String section : sections) {
|
|
||||||
Map<String, String> cpuMap = new HashMap<>();
|
|
||||||
cpuinfo.add(cpuMap);
|
|
||||||
|
|
||||||
String[] props = section.split("\n");
|
|
||||||
for (String prop : props) {
|
|
||||||
String[] assignment = prop.split("\\s*:\\s*");
|
|
||||||
if (assignment.length == 2) {
|
|
||||||
String property = assignment[0].trim();
|
|
||||||
String value = assignment[1].trim();
|
|
||||||
cpuMap.put(property, value);
|
|
||||||
} else {
|
|
||||||
cpuMap.put(assignment[0], "");
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.warn("Unable to learn about CPU architecture: " + e.getMessage());
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
|
||||||
return Optional.of(new ProcDetails(cpuinfo));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static class ProcDetails {
|
public static class ProcDetails {
|
||||||
private final List<Map<String, String>> details;
|
SystemInfo si;
|
||||||
|
HardwareAbstractionLayer hal;
|
||||||
|
CentralProcessor processor;
|
||||||
|
|
||||||
public ProcDetails(List<Map<String, String>> details) {
|
public ProcDetails(SystemInfo si) {
|
||||||
this.details = details;
|
this.si = si;
|
||||||
|
this.hal = si.getHardware();
|
||||||
|
this.processor = hal.getProcessor();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getCoreCount() {
|
public int getCoreCount() {
|
||||||
return (int) details.stream()
|
return processor.getLogicalProcessorCount();
|
||||||
.map(m -> m.get("core id"))
|
|
||||||
.distinct()
|
|
||||||
.count();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getCpuCount() {
|
public int getCpuCount() {
|
||||||
return (int) details.stream()
|
return processor.getPhysicalProcessorCount();
|
||||||
.map(m -> m.get("processor"))
|
|
||||||
.distinct()
|
|
||||||
.count();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getModelName() {
|
public String getModelName() {
|
||||||
return details.stream()
|
return processor.getProcessorIdentifier().toString();
|
||||||
.map(m -> m.get("model name")).findFirst().orElseThrow();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMhz() {
|
public String getMhz() {
|
||||||
return details.stream()
|
// or use processor.getCurrentFreq, and average, or min?
|
||||||
.map(m -> m.get("cpu MHz")).findFirst().orElseThrow();
|
return Long.toString(processor.getMaxFreq()/ (1024*1024));
|
||||||
}
|
|
||||||
|
|
||||||
public String getCacheInfo() {
|
|
||||||
return details.stream()
|
|
||||||
.map(m -> m.get("cache size")).findFirst().orElseThrow();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "cores=" + getCoreCount() +
|
return "cores=" + getCoreCount() +
|
||||||
" cpus=" + getCpuCount() + " mhz=" + getMhz() +
|
" cpus=" + getCpuCount() + " mhz=" + getMhz() +
|
||||||
" speedavg=" + getCurrentSpeed().getAverage() +
|
" speedavg=" + getCurrentSpeed().getAverage() +
|
||||||
" cache=" + getCacheInfo() + " model='" + getModelName() + "'";
|
" model='" + getModelName() + "'";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public double getMaxFreq(int cpu) {
|
public double getMaxFreq(int cpu) {
|
||||||
return readFile("/sys/devices/system/cpu/cpu" + cpu + "/cpufreq/cpuinfo_max_freq",Double.NaN);
|
return (double)processor.getMaxFreq();
|
||||||
}
|
}
|
||||||
public double getCurFreq(int cpu) {
|
public double getCurFreq(int cpu) {
|
||||||
return readFile("/sys/devices/system/cpu/cpu" + cpu + "/cpufreq/cpuinfo_max_freq",Double.NaN);
|
return (double)processor.getCurrentFreq()[cpu];
|
||||||
}
|
|
||||||
public double getMinFreq(int cpu) {
|
|
||||||
return readFile("/sys/devices/system/cpu/cpu" + cpu + "/cpufreq/cpuinfo_min_freq",Double.NaN);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getCurrentSpeed(int cpu) {
|
public double getCurrentSpeed(int cpu) {
|
||||||
@ -132,14 +101,5 @@ public class CpuInfo {
|
|||||||
return dss;
|
return dss;
|
||||||
}
|
}
|
||||||
|
|
||||||
private double readFile(String path, double defaultValue) {
|
|
||||||
try {
|
|
||||||
Path readPath = Path.of(path);
|
|
||||||
String content = Files.readString(readPath);
|
|
||||||
return Double.parseDouble(content);
|
|
||||||
} catch (Exception e) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
package io.nosqlbench.engine.api.activityimpl;
|
package io.nosqlbench.engine.api.activityimpl;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ package io.nosqlbench.engine.cli;
|
|||||||
|
|
||||||
import io.nosqlbench.engine.cli.EBCLIOptions;
|
import io.nosqlbench.engine.cli.EBCLIOptions;
|
||||||
import io.nosqlbench.engine.cli.EBCLIScriptAssembly;
|
import io.nosqlbench.engine.cli.EBCLIScriptAssembly;
|
||||||
import org.junit.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@ -65,8 +65,14 @@
|
|||||||
<artifactId>log4j-core</artifactId>
|
<artifactId>log4j-core</artifactId>
|
||||||
<version>2.13.0</version>
|
<version>2.13.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.oshi</groupId>
|
||||||
|
<artifactId>oshi-core</artifactId>
|
||||||
|
<version>4.5.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
Loading…
Reference in New Issue
Block a user