Rework and document OS class

This commit is contained in:
DaCool
2023-11-12 13:57:18 +00:00
committed by harlekin
parent 1ef0b2f4b7
commit d2b9985be8
5 changed files with 89 additions and 29 deletions

View File

@@ -73,7 +73,7 @@ public class Linux extends OS {
}
List<String> actual_command = command;
if (checkNiceAvailability()) {
if (isNiceAvailable()) {
// launch the process in lowest priority
if (env_overight != null) {
actual_command.add(0, env_overight.get("PRIORITY"));
@@ -109,7 +109,7 @@ public class Linux extends OS {
);
}
@Override public boolean getSupportHighPriority() {
@Override public boolean isHighPrioritySupported() {
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command("bash", "-c", ID_COMMAND_INVOCATION);
@@ -123,7 +123,7 @@ public class Linux extends OS {
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();
return (userLevel.equals("0")) & isNiceAvailable();
}
}
catch (IOException e) {
@@ -133,7 +133,7 @@ public class Linux extends OS {
return false;
}
@Override public boolean checkNiceAvailability() {
@Override public boolean isNiceAvailable() {
ProcessBuilder builder = new ProcessBuilder();
builder.command(NICE_BINARY_PATH);
builder.redirectErrorStream(true);

View File

@@ -46,7 +46,7 @@ public class Mac extends OS {
@Override public Process exec(List<String> command, Map<String, String> env) throws IOException {
List<String> actual_command = command;
if (checkNiceAvailability()) {
if (isNiceAvailable()) {
// launch the process in lowest priority
if (env != null) {
actual_command.add(0, env.get("PRIORITY"));
@@ -78,7 +78,7 @@ public class Mac extends OS {
return "/usr/local/cuda/lib/libcuda.dylib";
}
@Override public boolean getSupportHighPriority() {
@Override public boolean isHighPrioritySupported() {
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command("bash", "-c", ID_COMMAND_INVOCATION);
@@ -92,7 +92,7 @@ public class Mac extends OS {
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();
return (userLevel.equals("0")) & isNiceAvailable();
}
}
catch (IOException e) {
@@ -102,7 +102,7 @@ public class Mac extends OS {
return false;
}
@Override public boolean checkNiceAvailability() {
@Override public boolean isNiceAvailable() {
ProcessBuilder builder = new ProcessBuilder();
builder.command(NICE_BINARY_PATH);
builder.redirectErrorStream(true);

View File

@@ -38,45 +38,90 @@ public abstract class OS {
private static OS instance = null;
/**
* Operating system name
* @return a string representing the OS name.
*/
public abstract String name();
/**
* Blender requires 64 bits
* @return boolean if we are supported i.e. if we run on 64 bits
*/
public boolean isSupported() { return "64bit".equals(getCPU().getArch()); }
/** Get the full version of the os.
/** Get the full version of the operating system.
* For example windows, should give "windows 8.1"
* @return a lowercassed string with the os name and the version info
* @see OperatingSystem#getVersionInfo()
*/
public String getVersion() {
return (name() + " " + operatingSystem.getVersionInfo()).toLowerCase();
}
/**
* @see oshi.hardware.GlobalMemory#getTotal()
* @return long of getTotal() in kilobytes
*/
public long getTotalMemory() {
return hardwareAbstractionLayer.getMemory().getTotal() / 1024;
}
/**
* @see oshi.hardware.GlobalMemory#getAvailable()
* @return long of getAvailable() in kilobytes
*/
public long getFreeMemory() {
return hardwareAbstractionLayer.getMemory().getAvailable() / 1024;
}
/**
* Get the path to the Blender executable
* @return a string representing a path to Blender,
* relative or absolute depends on the implementation
*/
public abstract String getRenderBinaryPath();
/**
* Get the path to the CUDA library
* @return a string representing a path the CUDA library,
* relative or absolute depends on the implementation
*/
public String getCUDALib() {
return null;
}
//path to NVIDIA NVML lib
/**
* Get the path to the NVIDIA NVML library
* @return a string representing a path the NVIDIA NVML library,
* relative or absolute depends on the implementation
*/
public String getNVMLLib() {
return null;
}
public abstract boolean getSupportHighPriority();
public abstract boolean checkNiceAvailability();
/**
* Determines if the platform supports setting a higher priority
* @return a boolean representing if the platform supports setting a higher priority
*/
public abstract boolean isHighPrioritySupported();
/**
* Shutdown the computer waiting delayInMinutes minutes to allow all SheepIt threads to close and exit the app
* Determines if the platform supports being IO nice
* @return a boolean representing if the platform supports being IO nice
*/
public abstract boolean isNiceAvailable();
/**
* Shuts the computer down waiting allow all SheepIt threads to close and exit
* @param delayInMinutes integer repressing the amount minutes to wait before shutting down
*/
public abstract void shutdownComputer(int delayInMinutes);
/**
* Creates and populates a CPU object
* @return a populated CPU object
*/
public CPU getCPU() {
CentralProcessor.ProcessorIdentifier cpuID = hardwareAbstractionLayer.getProcessor().getProcessorIdentifier();
CPU ret = new CPU();
@@ -86,15 +131,20 @@ public abstract class OS {
return ret;
}
public Process exec(List<String> command, Map<String, String> env) throws IOException {
ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectErrorStream(true);
if (env != null) {
builder.environment().putAll(env);
}
return builder.start();
}
/**
* Executes a Process
* @param command List of strings of the command and the respective command line arguments
* @param env Map of strings of environment variables and their values for the execution
* @return a Process object reflecting the execution of the command
* @throws IOException if an I/O error occurs
*/
public abstract Process exec(List<String> command, Map<String, String> env) throws IOException;
/**
* Terminates a process
* @param proc Process to kill
* @return true if proc wasn't null and was destroyed
*/
public boolean kill(Process proc) {
if (proc != null) {
proc.destroy();
@@ -103,6 +153,10 @@ public abstract class OS {
return false;
}
/**
* Get the operating system currently in use
* @return object instance of OS specific to the used operating system.
*/
public static OS getOS() {
if (instance == null) {
switch (operatingSystem.getManufacturer()){
@@ -110,7 +164,7 @@ public abstract class OS {
instance = new Windows();
break;
case "Apple":
if ("aarch64".equalsIgnoreCase(System.getProperty("os.arch"))) { // ARM arch ?
if ("aarch64".equalsIgnoreCase(System.getProperty("os.arch"))) { // Set as M1 Mac if we are on ARM64
instance = new MacM1();
}
else {
@@ -124,7 +178,13 @@ public abstract class OS {
}
return instance;
}
/**
* Returns the default path that SheepIt would place the config file
* This respects the XDG specification for Linux and macOS
* @see <a href="https://www.freedesktop.org/wiki/Software/xdg-user-dirs/">XDG specification</a>
* @return string representing the default path to the sheepit.conf
*/
public String getDefaultConfigFilePath() {
String xdgConfigHome = System.getenv("XDG_CONFIG_HOME");
String userHome = System.getProperty("user.home");

View File

@@ -179,11 +179,11 @@ public class Windows extends OS {
return false;
}
@Override public boolean getSupportHighPriority() {
@Override public boolean isHighPrioritySupported() {
return true;
}
@Override public boolean checkNiceAvailability() {
@Override public boolean isNiceAvailable() {
// In windows, nice is not required and therefore we return always true to show the slider in the Settings GUI
return true;
}