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

View File

@@ -38,45 +38,90 @@ public abstract class OS {
private static OS instance = null; private static OS instance = null;
/**
* Operating system name
* @return a string representing the OS name.
*/
public abstract String 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()); } 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" * 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() { public String getVersion() {
return (name() + " " + operatingSystem.getVersionInfo()).toLowerCase(); return (name() + " " + operatingSystem.getVersionInfo()).toLowerCase();
} }
/**
* @see oshi.hardware.GlobalMemory#getTotal()
* @return long of getTotal() in kilobytes
*/
public long getTotalMemory() { public long getTotalMemory() {
return hardwareAbstractionLayer.getMemory().getTotal() / 1024; return hardwareAbstractionLayer.getMemory().getTotal() / 1024;
} }
/**
* @see oshi.hardware.GlobalMemory#getAvailable()
* @return long of getAvailable() in kilobytes
*/
public long getFreeMemory() { public long getFreeMemory() {
return hardwareAbstractionLayer.getMemory().getAvailable() / 1024; 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(); 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() { public String getCUDALib() {
return null; 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() { public String getNVMLLib() {
return null; return null;
} }
public abstract boolean getSupportHighPriority(); /**
* Determines if the platform supports setting a higher priority
public abstract boolean checkNiceAvailability(); * @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); public abstract void shutdownComputer(int delayInMinutes);
/**
* Creates and populates a CPU object
* @return a populated CPU object
*/
public CPU getCPU() { public CPU getCPU() {
CentralProcessor.ProcessorIdentifier cpuID = hardwareAbstractionLayer.getProcessor().getProcessorIdentifier(); CentralProcessor.ProcessorIdentifier cpuID = hardwareAbstractionLayer.getProcessor().getProcessorIdentifier();
CPU ret = new CPU(); CPU ret = new CPU();
@@ -86,15 +131,20 @@ public abstract class OS {
return ret; return ret;
} }
public Process exec(List<String> command, Map<String, String> env) throws IOException { /**
ProcessBuilder builder = new ProcessBuilder(command); * Executes a Process
builder.redirectErrorStream(true); * @param command List of strings of the command and the respective command line arguments
if (env != null) { * @param env Map of strings of environment variables and their values for the execution
builder.environment().putAll(env); * @return a Process object reflecting the execution of the command
} * @throws IOException if an I/O error occurs
return builder.start(); */
} 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) { public boolean kill(Process proc) {
if (proc != null) { if (proc != null) {
proc.destroy(); proc.destroy();
@@ -103,6 +153,10 @@ public abstract class OS {
return false; return false;
} }
/**
* Get the operating system currently in use
* @return object instance of OS specific to the used operating system.
*/
public static OS getOS() { public static OS getOS() {
if (instance == null) { if (instance == null) {
switch (operatingSystem.getManufacturer()){ switch (operatingSystem.getManufacturer()){
@@ -110,7 +164,7 @@ public abstract class OS {
instance = new Windows(); instance = new Windows();
break; break;
case "Apple": 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(); instance = new MacM1();
} }
else { else {
@@ -125,6 +179,12 @@ public abstract class OS {
return instance; 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() { public String getDefaultConfigFilePath() {
String xdgConfigHome = System.getenv("XDG_CONFIG_HOME"); String xdgConfigHome = System.getenv("XDG_CONFIG_HOME");
String userHome = System.getProperty("user.home"); String userHome = System.getProperty("user.home");

View File

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

View File

@@ -392,7 +392,7 @@ public class Settings implements Activity {
// priority // priority
// ui display low -> high but the actual values are reversed // ui display low -> high but the actual values are reversed
// -19 is highest priority // -19 is highest priority
boolean high_priority_support = os.getSupportHighPriority(); boolean high_priority_support = os.isHighPrioritySupported();
priority = new JSlider(high_priority_support ? -19 : 0, 19); priority = new JSlider(high_priority_support ? -19 : 0, 19);
Hashtable<Integer, JLabel> labelTablePriority = new Hashtable<>(); Hashtable<Integer, JLabel> labelTablePriority = new Hashtable<>();
labelTablePriority.put(high_priority_support ? -19 : 0, new JLabel("Low")); labelTablePriority.put(high_priority_support ? -19 : 0, new JLabel("Low"));
@@ -418,7 +418,7 @@ public class Settings implements Activity {
JLabel priorityLabel = new JLabel("Priority"); JLabel priorityLabel = new JLabel("Priority");
priorityLabel.setToolTipText(SwingTooltips.PRIORITY.getText()); priorityLabel.setToolTipText(SwingTooltips.PRIORITY.getText());
boolean showPrioritySlider = os.checkNiceAvailability(); boolean showPrioritySlider = os.isNiceAvailable();
priority.setVisible(showPrioritySlider); priority.setVisible(showPrioritySlider);
priorityLabel.setVisible(showPrioritySlider); priorityLabel.setVisible(showPrioritySlider);
@@ -746,7 +746,7 @@ public class Settings implements Activity {
// ui display low -> high but the actual values are reversed // ui display low -> high but the actual values are reversed
// -19 is highest priority // -19 is highest priority
OS os = OS.getOS(); OS os = OS.getOS();
boolean high_priority_support = os.getSupportHighPriority(); boolean high_priority_support = os.isHighPrioritySupported();
if (high_priority_support) { if (high_priority_support) {
// inverse // inverse
config.setUsePriority(-1 * priority.getValue()); config.setUsePriority(-1 * priority.getValue());