Fix: priority functionality not working properly (#232)

This commit is contained in:
Luis Uguina
2020-05-28 23:05:52 +10:00
committed by GitHub
parent 737377098f
commit cdf552f708
6 changed files with 109 additions and 39 deletions

View File

@@ -24,6 +24,7 @@ import java.util.HashMap;
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 @@ 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() {
@@ -155,10 +155,7 @@ public class FreeBSD extends OS {
}
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"));
@@ -183,17 +180,42 @@ public class FreeBSD extends OS {
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 @@ public class FreeBSD extends OS {
process.destroy();
}
}
return hasNiceBinary;
}
}

View File

@@ -20,6 +20,7 @@ package com.sheepit.client.os;
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 @@ import com.sheepit.client.hardware.cpu.CPU;
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 class Linux extends OS {
// 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 class Linux extends OS {
}
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"));
@@ -197,28 +194,41 @@ public class Linux extends OS {
}
@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 @@ public class Linux extends OS {
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 @@ public class Linux extends OS {
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()));
}
}

View File

@@ -21,6 +21,7 @@ package com.sheepit.client.os;
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 @@ import com.sheepit.client.hardware.cpu.CPU;
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 class Mac extends OS {
@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"));
@@ -172,17 +169,42 @@ public class Mac extends OS {
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 @@ public class Mac extends OS {
process.destroy();
}
}
return hasNiceBinary;
}
}

View File

@@ -41,9 +41,9 @@ public abstract class OS {
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);

View File

@@ -218,4 +218,13 @@ public class Windows extends OS {
}
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;
}
}