* Add OSHI lib for hardware and os detection

* Raised Windows minimum support to 8.1
* Raised OSX minimum support to 10.13
This commit is contained in:
DaCool
2021-09-10 00:23:57 +00:00
committed by Sheepit Renderfarm
parent 0a149bf1a8
commit a68639ff51
8 changed files with 56 additions and 535 deletions

View File

@@ -325,7 +325,7 @@ import lombok.Data;
}
public boolean checkOSisSupported() {
return OS.getOS() != null;
return OS.getOS() != null && OS.getOS().isSupported();
}
public boolean checkCPUisSupported() {

View File

@@ -80,6 +80,7 @@ public class CPU {
break;
case "amd64":
case "x86_64":
case "x64":
this.arch = "64bit";
break;
default:

View File

@@ -1,239 +0,0 @@
/*
* Copyright (C) 2010-2015 Laurent CLOUET
* Author Laurent CLOUET <laurent.clouet@nopnop.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 2
* of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package com.sheepit.client.os;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.sheepit.client.Log;
import com.sheepit.client.hardware.cpu.CPU;
public class FreeBSD extends OS {
private final String NICE_BINARY_PATH = "nice";
private final String ID_COMMAND_INVOCATION = "id -u";
public FreeBSD() {
super();
}
public String name() {
return "freebsd";
}
@Override public String getRenderBinaryPath() {
return "rend.exe";
}
@Override public CPU getCPU() {
CPU ret = new CPU();
try {
Runtime r = Runtime.getRuntime();
Process p = r.exec("dmesg");
BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = b.readLine()) != null) {
if (line.startsWith("CPU:")) {
String buf[] = line.split(":");
if (buf.length > 1) {
ret.setName(buf[1].trim());
}
}
if (line.contains("Family=") && line.contains("Model=")) {
String buf[] = line.split(" ");
for (int i = 0; i < buf.length; i++) {
if (buf[i].contains("Family")) {
String family = buf[i].split("=")[1];
ret.setFamily(family.split("x")[1]);
}
if (buf[i].contains("Model")) {
String model = buf[i].split("=")[1];
ret.setModel(model.split("x")[1]);
}
}
}
}
b.close();
}
catch (Exception e) {
e.printStackTrace();
}
if (ret.haveData() == false) {
Log.getInstance(null).debug("OS::FreeBSD::getCPU failed to get CPU from dmesg, using partia sysctl");
ret.setModel("0");
ret.setFamily("0");
try {
Runtime run = Runtime.getRuntime();
Process sysctl = run.exec("sysctl -n hw.model");
BufferedReader buf = new BufferedReader(new InputStreamReader(sysctl.getInputStream()));
String name = "";
name = buf.readLine();
buf.close();
if (name == "") {
ret.setName("0");
}
else {
ret.setName(name);
}
}
catch (IOException e) {
Log.getInstance(null).debug("OS::FreeBSD::getCPU exception " + e);
}
}
return ret;
}
@Override public long getMemory() {
try {
Runtime r = Runtime.getRuntime();
Process p = r.exec("sysctl -n hw.usermem");
BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
line = b.readLine();
b.close();
if (line.isEmpty()) {
return 0;
}
Long mem_byte = Long.parseLong(line.trim());
return mem_byte / Long.valueOf(1024);
}
catch (IOException e) {
Log.getInstance(null).debug("OS::FreeBSD::getMemory exception " + e);
}
return 0;
}
@Override public long getFreeMemory() {
return -1;
}
@Override public String getCUDALib() {
return "cuda";
}
@Override public Process exec(List<String> command, Map<String, String> env_overight) throws IOException {
// the renderer have a lib directory so add to the LD_LIBRARY_PATH
// (even if we are not sure that it is the renderer who is launch
Map<String, String> new_env = new HashMap<String, String>();
new_env.putAll(java.lang.System.getenv()); // clone the env
Boolean has_ld_library_path = new_env.containsKey("LD_LIBRARY_PATH");
String lib_dir = (new File(command.get(0))).getParent() + File.separator + "lib";
if (has_ld_library_path == false) {
new_env.put("LD_LIBRARY_PATH", lib_dir);
}
else {
new_env.put("LD_LIBRARY_PATH", new_env.get("LD_LIBRARY_PATH") + ":" + lib_dir);
}
List<String> actual_command = command;
if (checkNiceAvailability()) {
// launch the process in lowest priority
if (env_overight != null) {
actual_command.add(0, env_overight.get("PRIORITY"));
}
else {
actual_command.add(0, "19");
}
actual_command.add(0, "-n");
actual_command.add(0, NICE_BINARY_PATH);
}
else {
Log.getInstance(null).error("No low priority binary, will not launch renderer in normal priority");
}
ProcessBuilder builder = new ProcessBuilder(actual_command);
builder.redirectErrorStream(true);
Map<String, String> env = builder.environment();
env.putAll(new_env);
if (env_overight != null) {
env.putAll(env_overight);
}
return builder.start();
}
@Override public boolean getSupportHighPriority() {
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command("bash", "-c", ID_COMMAND_INVOCATION);
builder.redirectErrorStream(true);
Process process = builder.start();
InputStream is = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String userLevel = null;
if ((userLevel = reader.readLine()) != null) {
// Root user in *ix systems -independently of the alias used to login- has a id value of 0. On top of being a user with root capabilities,
// to support changing the priority the nice tool must be accessible from the current user
return (userLevel.equals("0")) & checkNiceAvailability();
}
}
catch (IOException e) {
System.err.println(String.format("ERROR FreeBSD::getSupportHighPriority Unable to execute id command. IOException %s", e.getMessage()));
}
return false;
}
@Override public boolean checkNiceAvailability() {
ProcessBuilder builder = new ProcessBuilder();
builder.command(NICE_BINARY_PATH);
builder.redirectErrorStream(true);
Process process = null;
boolean hasNiceBinary = false;
try {
process = builder.start();
hasNiceBinary = true;
}
catch (IOException e) {
Log.getInstance(null).error("Failed to find low priority binary, will not launch renderer in normal priority (" + e + ")");
}
finally {
if (process != null) {
process.destroy();
}
}
return hasNiceBinary;
}
@Override public void shutdownComputer(int delayInMinutes) {
try {
// Shutdown the computer waiting 1 minute to allow all SheepIt threads to close and exit the app
ProcessBuilder builder = new ProcessBuilder("shutdown", "-h", String.valueOf(delayInMinutes));
Process process = builder.inheritIO().start();
}
catch (IOException e) {
System.err.println(String.format("FreeBSD::shutdownComputer Unable to execute the 'shutdown -h 1' command. Exception %s", e.getMessage()));
}
}
}

View File

@@ -26,10 +26,8 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import com.sheepit.client.Log;
import com.sheepit.client.hardware.cpu.CPU;
public class Linux extends OS {
private final String NICE_BINARY_PATH = "nice";
@@ -39,7 +37,7 @@ public class Linux extends OS {
super();
}
public String name() {
@Override public String name() {
return "linux";
}
@@ -47,103 +45,6 @@ public class Linux extends OS {
return "rend.exe";
}
@Override public CPU getCPU() {
CPU ret = new CPU();
try {
String filePath = "/proc/cpuinfo";
Scanner scanner = new Scanner(new File(filePath));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("model name")) {
String buf[] = line.split(":");
if (buf.length > 1) {
ret.setName(buf[1].trim());
}
}
if (line.startsWith("cpu family")) {
String buf[] = line.split(":");
if (buf.length > 1) {
ret.setFamily(buf[1].trim());
}
}
if (line.startsWith("model") && line.startsWith("model name") == false) {
String buf[] = line.split(":");
if (buf.length > 1) {
ret.setModel(buf[1].trim());
}
}
}
scanner.close();
}
catch (java.lang.NoClassDefFoundError e) {
System.err.println("OS.Linux::getCPU error " + e + " mostly because Scanner class was introduced by Java 5 and you are running a lower version");
}
catch (Exception e) {
e.printStackTrace();
}
return ret;
}
@Override public long getMemory() {
try {
String filePath = "/proc/meminfo";
Scanner scanner = new Scanner(new File(filePath));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("MemTotal")) {
String buf[] = line.split(":");
if (buf.length > 0) {
Integer buf2 = new Integer(buf[1].trim().split(" ")[0]);
return (((buf2 / 262144) + 1) * 262144); // 256*1024 = 262144
}
}
}
scanner.close();
}
catch (java.lang.NoClassDefFoundError e) {
System.err.println("Machine::type error " + e + " mostly because Scanner class was introducted by Java 5 and you are running a lower version");
}
catch (Exception e) {
e.printStackTrace();
}
return 0;
}
@Override public long getFreeMemory() {
try {
String filePath = "/proc/meminfo";
Scanner scanner = new Scanner(new File(filePath));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("MemAvailable")) {
String buf[] = line.split(":");
if (buf.length > 0) {
Integer buf2 = new Integer(buf[1].trim().split(" ")[0]);
return (((buf2 / 262144) + 1) * 262144); // 256*1024 = 262144
}
}
}
scanner.close();
}
catch (java.lang.NoClassDefFoundError e) {
System.err.println(
"OS::Linux::getFreeMemory error " + e + " mostly because Scanner class was introducted by Java 5 and you are running a lower version");
}
catch (Exception e) {
e.printStackTrace();
}
return 0;
}
@Override public String getCUDALib() {
return "cuda";
}

View File

@@ -27,7 +27,6 @@ import java.util.List;
import java.util.Map;
import com.sheepit.client.Log;
import com.sheepit.client.hardware.cpu.CPU;
public class Mac extends OS {
private final String NICE_BINARY_PATH = "nice";
@@ -37,7 +36,7 @@ public class Mac extends OS {
super();
}
public String name() {
@Override public String name() {
return "mac";
}
@@ -45,102 +44,6 @@ public class Mac extends OS {
return "Blender" + File.separator + "blender.app" + File.separator + "Contents" + File.separator + "MacOS" + File.separator + "blender";
}
@Override public CPU getCPU() {
CPU ret = new CPU();
String command = "sysctl machdep.cpu.family machdep.cpu.brand_string";
Process p = null;
BufferedReader input = null;
try {
String line;
p = Runtime.getRuntime().exec(command);
input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
String option_cpu_family = "machdep.cpu.family:";
String option_model_name = "machdep.cpu.brand_string:";
if (line.startsWith(option_model_name)) {
ret.setName(line.substring(option_model_name.length()).trim());
}
if (line.startsWith(option_cpu_family)) {
ret.setFamily(line.substring(option_cpu_family.length()).trim());
}
}
input.close();
input = null;
}
catch (Exception err) {
System.out.println("exception " + err);
err.printStackTrace();
ret.setName("Unknown Mac name");
ret.setFamily("Unknown Mac family");
}
finally {
if (input != null) {
try {
input.close();
}
catch (IOException e) {
}
}
if (p != null) {
p.destroy();
}
}
ret.setModel("Unknown");
return ret;
}
@Override public long getMemory() {
String command = "sysctl hw.memsize";
Process p = null;
BufferedReader input = null;
try {
String line;
p = Runtime.getRuntime().exec(command);
input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
String option = "hw.memsize:";
if (line.startsWith(option)) {
String memory = line.substring(option.length()).trim(); // memory in bytes
return Long.parseLong(memory) / 1024;
}
}
input.close();
input = null;
}
catch (Exception err) {
System.out.println("exception " + err);
err.printStackTrace();
}
finally {
if (input != null) {
try {
input.close();
}
catch (IOException e) {
}
}
if (p != null) {
p.destroy();
}
}
return -1;
}
@Override public long getFreeMemory() {
return -1;
}
@Override public Process exec(List<String> command, Map<String, String> env) throws IOException {
List<String> actual_command = command;
if (checkNiceAvailability()) {
@@ -165,6 +68,12 @@ public class Mac extends OS {
return builder.start();
}
@Override public boolean isSupported() {
String[] ver = operatingSystem.getVersionInfo().getVersion().split("\\.");
int majorVer = Integer.parseInt(ver[0]), minorVer = Integer.parseInt(ver[1]);
return (majorVer == 10 && minorVer >= 13) || majorVer >= 11;
}
@Override public String getCUDALib() {
return "/usr/local/cuda/lib/libcuda.dylib";
}

View File

@@ -22,25 +22,39 @@ import java.io.IOException;
import java.util.List;
import java.util.Map;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.software.os.OperatingSystem;
import oshi.hardware.HardwareAbstractionLayer;
import com.sheepit.client.hardware.cpu.CPU;
public abstract class OS {
private static SystemInfo systemInfo = new SystemInfo();
static OperatingSystem operatingSystem = systemInfo.getOperatingSystem();
private static HardwareAbstractionLayer hardwareAbstractionLayer = systemInfo.getHardware();
private static OS instance = null;
public abstract String name();
public boolean isSupported() { return true; }
/** Get the full version of the os.
* For example windows, should give "windows 8.1"
*/
public String getVersion() {
return System.getProperty("os.name").toLowerCase();
return (name() + " " + operatingSystem.getVersionInfo()).toLowerCase();
}
public abstract CPU getCPU();
public long getMemory() {
return hardwareAbstractionLayer.getMemory().getTotal() / 1024;
}
public abstract long getMemory();
public abstract long getFreeMemory();
public long getFreeMemory() {
return hardwareAbstractionLayer.getMemory().getAvailable() / 1024;
}
public abstract String getRenderBinaryPath();
@@ -54,6 +68,15 @@ public abstract class OS {
public abstract void shutdownComputer(int delayInMinutes);
public CPU getCPU() {
CentralProcessor.ProcessorIdentifier cpuID = hardwareAbstractionLayer.getProcessor().getProcessorIdentifier();
CPU ret = new CPU();
ret.setName(cpuID.getName());
ret.setModel(cpuID.getModel());
ret.setFamily(cpuID.getFamily());
return ret;
}
public Process exec(List<String> command, Map<String, String> env) throws IOException {
ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectErrorStream(true);
@@ -73,21 +96,18 @@ public abstract class OS {
public static OS getOS() {
if (instance == null) {
String os = System.getProperty("os.name").toLowerCase();
if (os.contains("win")) {
instance = new Windows();
}
else if (os.contains("mac")) {
instance = new Mac();
}
else if (os.contains("nix") || os.contains("nux")) {
instance = new Linux();
}
else if (os.contains("freebsd")) {
instance = new FreeBSD();
switch (operatingSystem.getManufacturer()){
case "Microsoft":
instance = new Windows();
break;
case "Apple":
instance = new Mac();
break;
case "GNU/Linux":
instance = new Linux();
break;
}
}
return instance;
}
}

View File

@@ -22,18 +22,13 @@ import java.io.IOException;
import java.util.List;
import java.util.Map;
import com.sheepit.client.hardware.cpu.CPU;
import com.sheepit.client.os.windows.Kernel32Lib;
import com.sheepit.client.os.windows.WinProcess;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.WinBase.MEMORYSTATUSEX;
import com.sun.jna.platform.win32.WinReg;
public class Windows extends OS {
public String name() {
@Override public String name() {
return "windows";
}
@@ -41,78 +36,6 @@ public class Windows extends OS {
return "rend.exe";
}
@Override public CPU getCPU() {
CPU ret = new CPU();
try {
String[] identifier = java.lang.System.getenv("PROCESSOR_IDENTIFIER").split(" ");
for (int i = 0; i < (identifier.length - 1); i++) {
if (identifier[i].equals("Family")) {
ret.setFamily(identifier[i + 1]);
}
if (identifier[i].equals("Model")) {
ret.setModel(identifier[i + 1]);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
try {
final String cpuRegistryRoot = "HARDWARE\\DESCRIPTION\\System\\CentralProcessor";
String[] processorIds = Advapi32Util.registryGetKeys(WinReg.HKEY_LOCAL_MACHINE, cpuRegistryRoot);
if (processorIds.length > 0) {
String processorId = processorIds[0];
String cpuRegistryPath = cpuRegistryRoot + "\\" + processorId;
ret.setName(Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, cpuRegistryPath, "ProcessorNameString").trim());
}
}
catch (Exception e) {
e.printStackTrace();
}
// override the arch
String env_arch = java.lang.System.getenv("PROCESSOR_ARCHITEW6432");
if (env_arch == null || env_arch.compareTo("") == 0) {
env_arch = java.lang.System.getenv("PROCESSOR_ARCHITECTURE");
}
if (env_arch.compareTo("AMD64") == 0) {
ret.setArch("64bit");
}
else {
ret.setArch("32bit");
}
return ret;
}
@Override public long getMemory() {
try {
MEMORYSTATUSEX _memory = new MEMORYSTATUSEX();
if (Kernel32.INSTANCE.GlobalMemoryStatusEx(_memory)) {
return _memory.ullTotalPhys.longValue() / 1024; // size in KB
}
}
catch (Exception e) {
e.printStackTrace();
}
return 0;
}
@Override public long getFreeMemory() {
try {
MEMORYSTATUSEX _memory = new MEMORYSTATUSEX();
if (Kernel32.INSTANCE.GlobalMemoryStatusEx(_memory)) {
return _memory.ullAvailPhys.longValue() / 1024; // size in KB
}
}
catch (Exception e) {
e.printStackTrace();
}
return -1;
}
@Override public String getCUDALib() {
return "nvcuda";
}
@@ -151,6 +74,11 @@ public class Windows extends OS {
return p;
}
@Override public boolean isSupported() {
String ver = operatingSystem.getVersionInfo().getVersion();
return ver.equals("8.1") || ver.equals("10") || ver.equals("11");
}
int getPriorityClass(int priority) {
int process_class = WinProcess.PRIORITY_IDLE;
switch (priority) {