Skip to content
This repository has been archived by the owner on Apr 22, 2021. It is now read-only.

Commit

Permalink
Fix: priority functionality not working properly (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
luguina authored May 28, 2020
1 parent a6cdb2c commit be37abf
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 39 deletions.
41 changes: 32 additions & 9 deletions src/com/sheepit/client/os/FreeBSD.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@
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 Boolean hasNiceBinary;
private final String ID_COMMAND_INVOCATION = "id -u";

public FreeBSD() {
super();
this.hasNiceBinary = null;
}

public String name() {
Expand Down Expand Up @@ -155,10 +155,7 @@ public String name() {
}

List<String> 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"));
Expand All @@ -183,23 +180,49 @@ 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 {
if (process != null) {
process.destroy();
}
}
return hasNiceBinary;
}
}
47 changes: 29 additions & 18 deletions src/com/sheepit/client/os/Linux.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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";
Expand All @@ -168,10 +168,7 @@ public String name() {
}

List<String> 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"));
Expand All @@ -197,38 +194,52 @@ 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 {
if (process != null) {
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);
Expand All @@ -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()));
}
}
Expand Down
41 changes: 32 additions & 9 deletions src/com/sheepit/client/os/Mac.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -143,10 +143,7 @@ public String name() {

@Override public Process exec(List<String> command, Map<String, String> env) throws IOException {
List<String> 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"));
Expand All @@ -172,23 +169,49 @@ 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 {
if (process != null) {
process.destroy();
}
}
return hasNiceBinary;
}
}
6 changes: 3 additions & 3 deletions src/com/sheepit/client/os/OS.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> command, Map<String, String> env) throws IOException {
ProcessBuilder builder = new ProcessBuilder(command);
Expand Down
9 changes: 9 additions & 0 deletions src/com/sheepit/client/os/Windows.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down

0 comments on commit be37abf

Please sign in to comment.