Skip to content

Commit

Permalink
Make sys and dev paths on Linux configurable (#2626)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwiddis committed Apr 13, 2024
1 parent 8f4c08d commit eafc488
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 88 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

##### New Features
* [#2603](https://github.com/oshi/oshi/pull/2603),
[#2603](https://github.com/oshi/oshi/pull/2625): Add part number to Physical Memory - [@BartekDziurowicz](https://github.com/BartekDziurowicz), [@dbwiddis](https://github.com/dbwiddis).
[#2625](https://github.com/oshi/oshi/pull/2625): Add part number to Physical Memory - [@BartekDziurowicz](https://github.com/BartekDziurowicz), [@dbwiddis](https://github.com/dbwiddis).

##### Bug fixes / Improvements
* [#2605](https://github.com/oshi/oshi/pull/2605): Reduce CpuStat.getSystemCpuLoadticks memory allocation pressure - [@chrisribble](https://github.com/chrisribble).
* [#2612](https://github.com/oshi/oshi/pull/2612): Use 1k buffer in FileUtils.readLines to reduce heap allocation pressure - [@chrisribble](https://github.com/chrisribble).
* [#2621](https://github.com/oshi/oshi/pull/2621): Cache thread counters when updating OS Process with suspended state - [@dbwiddis](https://github.com/dbwiddis).
* [#2626](https://github.com/oshi/oshi/pull/2626): Make sys and dev paths on Linux configurable - [@dbwiddis](https://github.com/dbwiddis).

# 6.5.0 (2024-03-10)

Expand Down
5 changes: 3 additions & 2 deletions oshi-core/src/main/java/oshi/driver/linux/Devicetree.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/*
* Copyright 2020-2022 The OSHI Project Contributors
* Copyright 2020-2024 The OSHI Project Contributors
* SPDX-License-Identifier: MIT
*/
package oshi.driver.linux;

import oshi.annotation.concurrent.ThreadSafe;
import oshi.util.FileUtil;
import oshi.util.platform.linux.SysPath;

/**
* Utility to read info from the devicetree
Expand All @@ -22,7 +23,7 @@ private Devicetree() {
* @return The model if available, null otherwise
*/
public static String queryModel() {
String modelStr = FileUtil.getStringFromFile("/sys/firmware/devicetree/base/model");
String modelStr = FileUtil.getStringFromFile(SysPath.MODEL);
if (!modelStr.isEmpty()) {
return modelStr.replace("Machine: ", "");
}
Expand Down
31 changes: 15 additions & 16 deletions oshi-core/src/main/java/oshi/driver/linux/Sysfs.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/*
* Copyright 2020-2022 The OSHI Project Contributors
* Copyright 2020-2024 The OSHI Project Contributors
* SPDX-License-Identifier: MIT
*/
package oshi.driver.linux;

import oshi.annotation.concurrent.ThreadSafe;
import oshi.util.Constants;
import oshi.util.FileUtil;
import oshi.util.ParseUtil;
import oshi.util.Util;
import oshi.util.platform.linux.SysPath;

/**
* Utility to read info from {@code sysfs}
Expand All @@ -25,7 +25,7 @@ private Sysfs() {
* @return The vendor if available, null otherwise
*/
public static String querySystemVendor() {
final String sysVendor = FileUtil.getStringFromFile(Constants.SYSFS_SERIAL_PATH + "sys_vendor").trim();
final String sysVendor = FileUtil.getStringFromFile(SysPath.DMI_ID + "sys_vendor").trim();
if (!sysVendor.isEmpty()) {
return sysVendor;
}
Expand All @@ -38,9 +38,8 @@ public static String querySystemVendor() {
* @return The model if available, null otherwise
*/
public static String queryProductModel() {
final String productName = FileUtil.getStringFromFile(Constants.SYSFS_SERIAL_PATH + "product_name").trim();
final String productVersion = FileUtil.getStringFromFile(Constants.SYSFS_SERIAL_PATH + "product_version")
.trim();
final String productName = FileUtil.getStringFromFile(SysPath.DMI_ID + "product_name").trim();
final String productVersion = FileUtil.getStringFromFile(SysPath.DMI_ID + "product_version").trim();
if (productName.isEmpty()) {
if (!productVersion.isEmpty()) {
return productVersion;
Expand All @@ -62,7 +61,7 @@ public static String queryProductModel() {
public static String queryProductSerial() {
// These sysfs files accessible by root, or can be chmod'd at boot time
// to enable access without root
String serial = FileUtil.getStringFromFile(Constants.SYSFS_SERIAL_PATH + "product_serial");
String serial = FileUtil.getStringFromFile(SysPath.DMI_ID + "product_serial");
if (!serial.isEmpty() && !"None".equals(serial)) {
return serial;
}
Expand All @@ -77,7 +76,7 @@ public static String queryProductSerial() {
public static String queryUUID() {
// These sysfs files accessible by root, or can be chmod'd at boot time
// to enable access without root
String uuid = FileUtil.getStringFromFile(Constants.SYSFS_SERIAL_PATH + "product_uuid");
String uuid = FileUtil.getStringFromFile(SysPath.DMI_ID + "product_uuid");
if (!uuid.isEmpty() && !"None".equals(uuid)) {
return uuid;
}
Expand All @@ -90,7 +89,7 @@ public static String queryUUID() {
* @return The board vendor if available, null otherwise
*/
public static String queryBoardVendor() {
final String boardVendor = FileUtil.getStringFromFile(Constants.SYSFS_SERIAL_PATH + "board_vendor").trim();
final String boardVendor = FileUtil.getStringFromFile(SysPath.DMI_ID + "board_vendor").trim();
if (!boardVendor.isEmpty()) {
return boardVendor;
}
Expand All @@ -103,7 +102,7 @@ public static String queryBoardVendor() {
* @return The board model if available, null otherwise
*/
public static String queryBoardModel() {
final String boardName = FileUtil.getStringFromFile(Constants.SYSFS_SERIAL_PATH + "board_name").trim();
final String boardName = FileUtil.getStringFromFile(SysPath.DMI_ID + "board_name").trim();
if (!boardName.isEmpty()) {
return boardName;
}
Expand All @@ -116,7 +115,7 @@ public static String queryBoardModel() {
* @return The board version if available, null otherwise
*/
public static String queryBoardVersion() {
final String boardVersion = FileUtil.getStringFromFile(Constants.SYSFS_SERIAL_PATH + "board_version").trim();
final String boardVersion = FileUtil.getStringFromFile(SysPath.DMI_ID + "board_version").trim();
if (!boardVersion.isEmpty()) {
return boardVersion;
}
Expand All @@ -129,7 +128,7 @@ public static String queryBoardVersion() {
* @return The board serial number if available, null otherwise
*/
public static String queryBoardSerial() {
final String boardSerial = FileUtil.getStringFromFile(Constants.SYSFS_SERIAL_PATH + "board_serial").trim();
final String boardSerial = FileUtil.getStringFromFile(SysPath.DMI_ID + "board_serial").trim();
if (!boardSerial.isEmpty()) {
return boardSerial;
}
Expand All @@ -142,7 +141,7 @@ public static String queryBoardSerial() {
* @return The bios vendor if available, null otherwise
*/
public static String queryBiosVendor() {
final String biosVendor = FileUtil.getStringFromFile(Constants.SYSFS_SERIAL_PATH + "bios_vendor").trim();
final String biosVendor = FileUtil.getStringFromFile(SysPath.DMI_ID + "bios_vendor").trim();
if (biosVendor.isEmpty()) {
return biosVendor;
}
Expand All @@ -155,7 +154,7 @@ public static String queryBiosVendor() {
* @return The bios description if available, null otherwise
*/
public static String queryBiosDescription() {
final String modalias = FileUtil.getStringFromFile(Constants.SYSFS_SERIAL_PATH + "modalias").trim();
final String modalias = FileUtil.getStringFromFile(SysPath.DMI_ID + "modalias").trim();
if (!modalias.isEmpty()) {
return modalias;
}
Expand All @@ -169,7 +168,7 @@ public static String queryBiosDescription() {
* @return The bios version if available, null otherwise
*/
public static String queryBiosVersion(String biosRevision) {
final String biosVersion = FileUtil.getStringFromFile(Constants.SYSFS_SERIAL_PATH + "bios_version").trim();
final String biosVersion = FileUtil.getStringFromFile(SysPath.DMI_ID + "bios_version").trim();
if (!biosVersion.isEmpty()) {
return biosVersion + (Util.isBlank(biosRevision) ? "" : " (revision " + biosRevision + ")");
}
Expand All @@ -182,7 +181,7 @@ public static String queryBiosVersion(String biosRevision) {
* @return The bios release date if available, null otherwise
*/
public static String queryBiosReleaseDate() {
final String biosDate = FileUtil.getStringFromFile(Constants.SYSFS_SERIAL_PATH + "bios_date").trim();
final String biosDate = FileUtil.getStringFromFile(SysPath.DMI_ID + "bios_date").trim();
if (!biosDate.isEmpty()) {
return ParseUtil.parseMmDdYyyyToYyyyMmDD(biosDate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import oshi.util.FileUtil;
import oshi.util.ParseUtil;
import oshi.util.Util;
import oshi.util.platform.linux.SysPath;
import oshi.util.tuples.Quartet;

/**
Expand Down Expand Up @@ -241,9 +242,8 @@ private static Quartet<List<LogicalProcessor>, List<ProcessorCache>, Map<Integer
Set<ProcessorCache> caches = new HashSet<>();
Map<Integer, Integer> coreEfficiencyMap = new HashMap<>();
Map<Integer, String> modAliasMap = new HashMap<>();
String cpuPath = "/sys/devices/system/cpu/";
try {
try (Stream<Path> cpuFiles = Files.find(Paths.get(cpuPath), Integer.MAX_VALUE,
try (Stream<Path> cpuFiles = Files.find(Paths.get(SysPath.CPU), Integer.MAX_VALUE,
(path, basicFileAttributes) -> path.toFile().getName().matches("cpu\\d+"))) {
cpuFiles.forEach(cpu -> {
String syspath = cpu.toString(); // /sys/devices/system/cpu/cpuX
Expand All @@ -256,7 +256,7 @@ private static Quartet<List<LogicalProcessor>, List<ProcessorCache>, Map<Integer
}
} catch (IOException e) {
// No udev and no cpu info in sysfs? Bad.
LOG.warn("Unable to find CPU information in sysfs at path {}", cpuPath);
LOG.warn("Unable to find CPU information in sysfs at path {}", SysPath.CPU);
}
return new Quartet<>(logProcs, orderedProcCaches(caches), coreEfficiencyMap, modAliasMap);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2022 The OSHI Project Contributors
* Copyright 2020-2024 The OSHI Project Contributors
* SPDX-License-Identifier: MIT
*/
package oshi.hardware.platform.linux;
Expand Down Expand Up @@ -32,6 +32,7 @@
import oshi.util.Constants;
import oshi.util.FileUtil;
import oshi.util.ParseUtil;
import oshi.util.platform.linux.DevPath;
import oshi.util.platform.linux.ProcPath;

/**
Expand Down Expand Up @@ -60,8 +61,6 @@ public final class LinuxHWDiskStore extends AbstractHWDiskStore {
private static final String DM_VG_NAME = "DM_VG_NAME";
private static final String DM_LV_NAME = "DM_LV_NAME";
private static final String LOGICAL_VOLUME_GROUP = "Logical Volume Group";
private static final String DEV_LOCATION = "/dev/";
private static final String DEV_MAPPER = DEV_LOCATION + "mapper/";

private static final int SECTORSIZE = 512;

Expand Down Expand Up @@ -171,15 +170,15 @@ private static List<HWDiskStore> getDisks(LinuxHWDiskStore storeToUpdate) {
// devnode is what we use as name, like /dev/sda
String devnode = device.getDevnode();
// Ignore loopback and ram disks; do nothing
if (devnode != null && !devnode.startsWith("/dev/loop")
&& !devnode.startsWith("/dev/ram")) {
if (devnode != null && !devnode.startsWith(DevPath.LOOP)
&& !devnode.startsWith(DevPath.RAM)) {
if (DISK.equals(device.getDevtype())) {
// Null model and serial in virtual environments
String devModel = device.getPropertyValue(ID_MODEL);
String devSerial = device.getPropertyValue(ID_SERIAL_SHORT);
long devSize = ParseUtil.parseLongOrDefault(device.getSysattrValue(SIZE), 0L)
* SECTORSIZE;
if (devnode.startsWith("/dev/dm")) {
if (devnode.startsWith(DevPath.DM)) {
devModel = LOGICAL_VOLUME_GROUP;
devSerial = device.getPropertyValue(DM_UUID);
store = new LinuxHWDiskStore(devnode, devModel,
Expand Down Expand Up @@ -271,7 +270,7 @@ private static Map<String, String> readMountsMap() {
List<String> mounts = FileUtil.readFile(ProcPath.MOUNTS);
for (String mount : mounts) {
String[] split = ParseUtil.whitespaces.split(mount);
if (split.length < 2 || !split[0].startsWith(DEV_LOCATION)) {
if (split.length < 2 || !split[0].startsWith(DevPath.DEV)) {
continue;
}
mountsMap.put(split[0], split[1]);
Expand All @@ -293,11 +292,11 @@ private static void computeDiskStats(LinuxHWDiskStore store, String devstat) {
}

private static String getPartitionNameForDmDevice(String vgName, String lvName) {
return new StringBuilder().append(DEV_LOCATION).append(vgName).append('/').append(lvName).toString();
return new StringBuilder().append(DevPath.DEV).append(vgName).append('/').append(lvName).toString();
}

private static String getMountPointOfDmDevice(String vgName, String lvName) {
return new StringBuilder().append(DEV_MAPPER).append(vgName).append('-').append(lvName).toString();
return new StringBuilder().append(DevPath.MAPPER).append(vgName).append('-').append(lvName).toString();
}

private static String getDependentNamesFromHoldersDirectory(String sysPath) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2022 The OSHI Project Contributors
* Copyright 2021-2024 The OSHI Project Contributors
* SPDX-License-Identifier: MIT
*/
package oshi.hardware.platform.linux;
Expand All @@ -25,6 +25,7 @@
import oshi.util.ExecutingCommand;
import oshi.util.ParseUtil;
import oshi.util.Util;
import oshi.util.platform.linux.DevPath;

final class LinuxLogicalVolumeGroup extends AbstractLogicalVolumeGroup {

Expand All @@ -34,7 +35,6 @@ final class LinuxLogicalVolumeGroup extends AbstractLogicalVolumeGroup {
private static final String DM_UUID = "DM_UUID";
private static final String DM_VG_NAME = "DM_VG_NAME";
private static final String DM_LV_NAME = "DM_LV_NAME";
private static final String DEV_LOCATION = "/dev/";

LinuxLogicalVolumeGroup(String name, Map<String, Set<String>> lvMap, Set<String> pvSet) {
super(name, lvMap, pvSet);
Expand All @@ -52,7 +52,7 @@ static List<LogicalVolumeGroup> getLogicalVolumeGroups() {
// This requires elevated permissions and may fail
for (String s : ExecutingCommand.runNative("pvs -o vg_name,pv_name")) {
String[] split = ParseUtil.whitespaces.split(s.trim());
if (split.length == 2 && split[1].startsWith(DEV_LOCATION)) {
if (split.length == 2 && split[1].startsWith(DevPath.DEV)) {
physicalVolumesMap.computeIfAbsent(split[0], k -> new HashSet<>()).add(split[1]);
}
}
Expand All @@ -70,7 +70,7 @@ static List<LogicalVolumeGroup> getLogicalVolumeGroups() {
if (device != null) {
try {
String devnode = device.getDevnode();
if (devnode != null && devnode.startsWith("/dev/dm")) {
if (devnode != null && devnode.startsWith(DevPath.DM)) {
String uuid = device.getPropertyValue(DM_UUID);
if (uuid != null && uuid.startsWith("LVM-")) {
String vgName = device.getPropertyValue(DM_VG_NAME);
Expand All @@ -88,10 +88,10 @@ static List<LogicalVolumeGroup> getLogicalVolumeGroups() {
for (File f : slaves) {
String pvName = f.getName();
lvMapForGroup.computeIfAbsent(lvName, k -> new HashSet<>())
.add(DEV_LOCATION + pvName);
.add(DevPath.DEV + pvName);
// Backup to add to pv set if pvs command failed
// Added /dev/ to remove duplicates like /dev/sda1 and sda1
pvSetForGroup.add(DEV_LOCATION + pvName);
pvSetForGroup.add(DevPath.DEV + pvName);
}
}
}
Expand Down

0 comments on commit eafc488

Please sign in to comment.