diff --git a/src/com/sheepit/client/os/FreeBSD.java b/src/com/sheepit/client/os/FreeBSD.java index fd8bea41..ade4c3c3 100644 --- a/src/com/sheepit/client/os/FreeBSD.java +++ b/src/com/sheepit/client/os/FreeBSD.java @@ -24,6 +24,7 @@ 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; @@ -31,11 +32,10 @@ public class FreeBSD extends OS { private final String NICE_BINARY_PATH = "nice"; - private Boolean hasNiceBinary; + private final String ID_COMMAND_INVOCATION = "id -u"; public FreeBSD() { super(); - this.hasNiceBinary = null; } public String name() { @@ -155,10 +155,7 @@ public String name() { } List actual_command = command; - if (this.hasNiceBinary == null) { - this.checkNiceAvailability(); - } - if (this.hasNiceBinary.booleanValue()) { + if (checkNiceAvailability()) { // launch the process in lowest priority if (env_overight != null) { actual_command.add(0, env_overight.get("PRIORITY")); @@ -183,17 +180,42 @@ public String name() { return builder.start(); } - private void checkNiceAvailability() { + @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(); - this.hasNiceBinary = true; + hasNiceBinary = true; } catch (IOException e) { - this.hasNiceBinary = false; Log.getInstance(null).error("Failed to find low priority binary, will not launch renderer in normal priority (" + e + ")"); } finally { @@ -201,5 +223,6 @@ private void checkNiceAvailability() { process.destroy(); } } + return hasNiceBinary; } } diff --git a/src/com/sheepit/client/os/Linux.java b/src/com/sheepit/client/os/Linux.java index 66a02627..6aacfb1a 100644 --- a/src/com/sheepit/client/os/Linux.java +++ b/src/com/sheepit/client/os/Linux.java @@ -20,6 +20,7 @@ import java.io.BufferedReader; import java.io.File; +import java.io.InputStream; import java.io.InputStreamReader; import java.io.IOException; import java.util.HashMap; @@ -32,11 +33,10 @@ public class Linux extends OS { private final String NICE_BINARY_PATH = "nice"; - private Boolean hasNiceBinary; + private final String ID_COMMAND_INVOCATION = "id -u"; public Linux() { super(); - this.hasNiceBinary = null; } public String name() { @@ -155,7 +155,7 @@ public String name() { // if Blender is already loading an OpenGL library, don't need to load Blender's default one (it will // create system incompatibilities). If no OpenGL library is found, then load the one included in the binary // zip file - if (isOpenGLAreadyInstalled(command.get(0)) == false) { + if (isOpenGLAlreadyInstalled(command.get(0)) == false) { Boolean has_ld_library_path = new_env.containsKey("LD_LIBRARY_PATH"); String lib_dir = (new File(command.get(0))).getParent() + File.separator + "lib"; @@ -168,10 +168,7 @@ public String name() { } List actual_command = command; - if (this.hasNiceBinary == null) { - this.checkNiceAvailability(); - } - if (this.hasNiceBinary.booleanValue()) { + if (checkNiceAvailability()) { // launch the process in lowest priority if (env_overight != null) { actual_command.add(0, env_overight.get("PRIORITY")); @@ -197,28 +194,41 @@ public String name() { } @Override public boolean getSupportHighPriority() { - // only the root user can create process with high (negative nice) value - String logname = System.getenv("LOGNAME"); - String user = System.getenv("USER"); - - if ((logname != null && logname.equals("root")) || (user != null && user.equals("root"))) { - return true; + 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 Linux::getSupportHighPriority Unable to execute id command. IOException %s", e.getMessage())); } return false; } - protected void checkNiceAvailability() { + @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(); - this.hasNiceBinary = true; + hasNiceBinary = true; } catch (IOException e) { - this.hasNiceBinary = false; Log.getInstance(null).error("Failed to find low priority binary, will not launch renderer in normal priority (" + e + ")"); } finally { @@ -226,9 +236,10 @@ protected void checkNiceAvailability() { process.destroy(); } } + return hasNiceBinary; } - protected boolean isOpenGLAreadyInstalled(String pathToRendEXE) { + protected boolean isOpenGLAlreadyInstalled(String pathToRendEXE) { ProcessBuilder processBuilder = new ProcessBuilder(); processBuilder.command("bash", "-c", "ldd '" + pathToRendEXE + "'"); // support for paths with an space processBuilder.redirectErrorStream(true); @@ -255,7 +266,7 @@ protected boolean isOpenGLAreadyInstalled(String pathToRendEXE) { int exitCode = process.waitFor(); if (exitCode != 0) { - System.err.println(String.format("ERROR Linux::isOpenGLAreadyInstalled Unable to execute ldd command. Exit code %d", exitCode)); + System.err.println(String.format("ERROR Linux::isOpenGLAlreadyInstalled Unable to execute ldd command. Exit code %d", exitCode)); System.err.println(String.format("Screen output from ldd execution: %s", screenOutput.toString())); } } diff --git a/src/com/sheepit/client/os/Mac.java b/src/com/sheepit/client/os/Mac.java index a24c3e39..fdcdd41a 100644 --- a/src/com/sheepit/client/os/Mac.java +++ b/src/com/sheepit/client/os/Mac.java @@ -21,6 +21,7 @@ import java.io.BufferedReader; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.util.List; import java.util.Map; @@ -30,11 +31,10 @@ public class Mac extends OS { private final String NICE_BINARY_PATH = "nice"; - private Boolean hasNiceBinary; + private final String ID_COMMAND_INVOCATION = "id -u"; public Mac() { super(); - this.hasNiceBinary = null; } public String name() { @@ -143,10 +143,7 @@ public String name() { @Override public Process exec(List command, Map env) throws IOException { List actual_command = command; - if (this.hasNiceBinary == null) { - this.checkNiceAvailability(); - } - if (this.hasNiceBinary.booleanValue()) { + if (checkNiceAvailability()) { // launch the process in lowest priority if (env != null) { actual_command.add(0, env.get("PRIORITY")); @@ -172,17 +169,42 @@ public String name() { return "/usr/local/cuda/lib/libcuda.dylib"; } - protected void checkNiceAvailability() { + @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 Mac::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(); - this.hasNiceBinary = true; + hasNiceBinary = true; } catch (IOException e) { - this.hasNiceBinary = false; Log.getInstance(null).error("Failed to find low priority binary, will not launch renderer in normal priority (" + e + ")"); } finally { @@ -190,5 +212,6 @@ protected void checkNiceAvailability() { process.destroy(); } } + return hasNiceBinary; } } diff --git a/src/com/sheepit/client/os/OS.java b/src/com/sheepit/client/os/OS.java index 0551f08c..04a337f2 100644 --- a/src/com/sheepit/client/os/OS.java +++ b/src/com/sheepit/client/os/OS.java @@ -41,9 +41,9 @@ public String getCUDALib() { return null; } - public boolean getSupportHighPriority() { - return true; - } + public abstract boolean getSupportHighPriority(); + + public abstract boolean checkNiceAvailability(); public Process exec(List command, Map env) throws IOException { ProcessBuilder builder = new ProcessBuilder(command); diff --git a/src/com/sheepit/client/os/Windows.java b/src/com/sheepit/client/os/Windows.java index 52cd01e7..bc564ef8 100644 --- a/src/com/sheepit/client/os/Windows.java +++ b/src/com/sheepit/client/os/Windows.java @@ -218,4 +218,13 @@ int getPriorityClass(int priority) { } return false; } + + @Override public boolean getSupportHighPriority() { + return true; + } + + @Override public boolean checkNiceAvailability() { + // 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/com/sheepit/client/standalone/swing/activity/Settings.java b/src/com/sheepit/client/standalone/swing/activity/Settings.java index 3350dcf5..92bcfcaa 100644 --- a/src/com/sheepit/client/standalone/swing/activity/Settings.java +++ b/src/com/sheepit/client/standalone/swing/activity/Settings.java @@ -406,6 +406,10 @@ else if (config.getGPUDevice().getType().equals("OPENCL")) { priority.setValue(config.getPriority()); JLabel priorityLabel = new JLabel(high_priority_support ? "Priority (High <-> Low):" : "Priority (Normal <-> Low):"); + boolean showPrioritySlider = os.checkNiceAvailability(); + priority.setVisible(showPrioritySlider); + priorityLabel.setVisible(showPrioritySlider); + compute_devices_constraints.weightx = 1.0 / gpus.size(); compute_devices_constraints.gridx = 0; compute_devices_constraints.gridy++;