From d2b9985be8ea4282a231578bbc1cf45c7932c046 Mon Sep 17 00:00:00 2001 From: DaCool <8727384-DaCool@users.noreply.gitlab.com> Date: Sun, 12 Nov 2023 13:57:18 +0000 Subject: [PATCH] Rework and document OS class --- .../java/com/sheepit/client/os/Linux.java | 8 +- src/main/java/com/sheepit/client/os/Mac.java | 8 +- src/main/java/com/sheepit/client/os/OS.java | 92 +++++++++++++++---- .../java/com/sheepit/client/os/Windows.java | 4 +- .../standalone/swing/activity/Settings.java | 6 +- 5 files changed, 89 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/sheepit/client/os/Linux.java b/src/main/java/com/sheepit/client/os/Linux.java index 7a165b2..9abae53 100644 --- a/src/main/java/com/sheepit/client/os/Linux.java +++ b/src/main/java/com/sheepit/client/os/Linux.java @@ -73,7 +73,7 @@ public class Linux extends OS { } List 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); diff --git a/src/main/java/com/sheepit/client/os/Mac.java b/src/main/java/com/sheepit/client/os/Mac.java index b692950..63e9a15 100644 --- a/src/main/java/com/sheepit/client/os/Mac.java +++ b/src/main/java/com/sheepit/client/os/Mac.java @@ -46,7 +46,7 @@ public class Mac extends OS { @Override public Process exec(List command, Map env) throws IOException { List 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); diff --git a/src/main/java/com/sheepit/client/os/OS.java b/src/main/java/com/sheepit/client/os/OS.java index e8a2c5f..7b3310c 100644 --- a/src/main/java/com/sheepit/client/os/OS.java +++ b/src/main/java/com/sheepit/client/os/OS.java @@ -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 command, Map 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 command, Map 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 XDG specification + * @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"); diff --git a/src/main/java/com/sheepit/client/os/Windows.java b/src/main/java/com/sheepit/client/os/Windows.java index 09e1e16..fdb01d7 100644 --- a/src/main/java/com/sheepit/client/os/Windows.java +++ b/src/main/java/com/sheepit/client/os/Windows.java @@ -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; } diff --git a/src/main/java/com/sheepit/client/standalone/swing/activity/Settings.java b/src/main/java/com/sheepit/client/standalone/swing/activity/Settings.java index 13bfbf0..d39b22b 100644 --- a/src/main/java/com/sheepit/client/standalone/swing/activity/Settings.java +++ b/src/main/java/com/sheepit/client/standalone/swing/activity/Settings.java @@ -392,7 +392,7 @@ public class Settings implements Activity { // priority // ui display low -> high but the actual values are reversed // -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); Hashtable labelTablePriority = new Hashtable<>(); labelTablePriority.put(high_priority_support ? -19 : 0, new JLabel("Low")); @@ -418,7 +418,7 @@ public class Settings implements Activity { JLabel priorityLabel = new JLabel("Priority"); priorityLabel.setToolTipText(SwingTooltips.PRIORITY.getText()); - boolean showPrioritySlider = os.checkNiceAvailability(); + boolean showPrioritySlider = os.isNiceAvailable(); priority.setVisible(showPrioritySlider); priorityLabel.setVisible(showPrioritySlider); @@ -746,7 +746,7 @@ public class Settings implements Activity { // ui display low -> high but the actual values are reversed // -19 is highest priority OS os = OS.getOS(); - boolean high_priority_support = os.getSupportHighPriority(); + boolean high_priority_support = os.isHighPrioritySupported(); if (high_priority_support) { // inverse config.setUsePriority(-1 * priority.getValue());