oshi.hardware.common.AbstractGlobalMemory Maven / Gradle / Ivy
/**
* MIT License
*
* Copyright (c) 2010 - 2020 The OSHI Project Contributors: https://github.com/oshi/oshi/graphs/contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package oshi.hardware.common;
import java.util.ArrayList;
import java.util.List;
import oshi.annotation.concurrent.ThreadSafe;
import oshi.hardware.GlobalMemory;
import oshi.hardware.PhysicalMemory;
import oshi.util.Constants;
import oshi.util.ExecutingCommand;
import oshi.util.FormatUtil;
import oshi.util.ParseUtil;
/**
* Memory info.
*/
@ThreadSafe
public abstract class AbstractGlobalMemory implements GlobalMemory {
@Override
public List getPhysicalMemory() {
// dmidecode requires sudo permission but is the only option on Linux
// and Unix
List pmList = new ArrayList<>();
List dmi = ExecutingCommand.runNative("dmidecode --type 17");
int bank = 0;
String bankLabel = Constants.UNKNOWN;
String locator = "";
long capacity = 0L;
long speed = 0L;
String manufacturer = Constants.UNKNOWN;
String memoryType = Constants.UNKNOWN;
for (String line : dmi) {
if (line.trim().contains("DMI type 17")) {
// Save previous bank
if (bank++ > 0) {
if (capacity > 0) {
pmList.add(new PhysicalMemory(bankLabel + locator, capacity, speed, manufacturer, memoryType));
}
bankLabel = Constants.UNKNOWN;
locator = "";
capacity = 0L;
speed = 0L;
}
} else if (bank > 0) {
String[] split = line.trim().split(":");
if (split.length == 2) {
switch (split[0]) {
case "Bank Locator":
bankLabel = split[1].trim();
break;
case "Locator":
locator = "/" + split[1].trim();
break;
case "Size":
capacity = ParseUtil.parseDecimalMemorySizeToBinary(split[1].trim());
break;
case "Type":
memoryType = split[1].trim();
break;
case "Speed":
speed = ParseUtil.parseHertz(split[1]);
break;
case "Manufacturer":
manufacturer = split[1].trim();
break;
default:
break;
}
}
}
}
if (capacity > 0) {
pmList.add(new PhysicalMemory(bankLabel + locator, capacity, speed, manufacturer, memoryType));
}
return pmList;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Available: ");
sb.append(FormatUtil.formatBytes(getAvailable()));
sb.append("/");
sb.append(FormatUtil.formatBytes(getTotal()));
return sb.toString();
}
}