From: Jim Procter Date: Mon, 17 Sep 2018 09:49:16 +0000 (+0100) Subject: refactored worker and made the launch work on OSX/16G machine X-Git-Url: http://source.jalview.org/gitweb/?p=jalview.git;a=commitdiff_plain;h=652c5487a5a2b3a31c4bba5f0a3425a26c9369aa refactored worker and made the launch work on OSX/16G machine --- diff --git a/src/jalview/bin/Jalview.java b/src/jalview/bin/Jalview.java index f776b27..062a22a 100755 --- a/src/jalview/bin/Jalview.java +++ b/src/jalview/bin/Jalview.java @@ -39,6 +39,7 @@ import jalview.schemes.ColourSchemeI; import jalview.schemes.ColourSchemeProperty; import jalview.util.MessageManager; import jalview.util.Platform; +import jalview.util.Worker; import jalview.ws.jws2.Jws2Discoverer; import java.io.BufferedReader; @@ -57,7 +58,10 @@ import java.security.CodeSource; import java.security.PermissionCollection; import java.security.Permissions; import java.security.Policy; +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Vector; @@ -188,15 +192,62 @@ public class Jalview float ONE_MB = 1048576f; Runtime runtime = Runtime.getRuntime(); float maxMemory = runtime.maxMemory() / ONE_MB; - if (maxMemory < 1024 * 15) + boolean is9or10 = System.getProperty("java.version").startsWith("10.") + || System.getProperty("java.version").startsWith("9.") + || System.getProperty("java.version").startsWith("10.") + || System.getProperty("java.version").startsWith("1.10.") + || System.getProperty("java.version").startsWith("1.9."); + if (maxMemory < 1024 * 14) { - System.exit(); + String cmd = ""; + for (String r : args) + { + cmd += (cmd.length() == 0) ? r : " " + r; + } + if (cmd.indexOf("-Xmx") == -1) + { + cmd = "-Xmx15G " + cmd; + System.err.println("relaunching with 15G: " + cmd); + Worker worker = Worker.jalviewDesktopRunner(false, cmd, 0, + "-Xmx15G" + + (is9or10 ? " --add-modules=\"java.se.ee\"" : "")); + worker.doEcho(); + try + { + while (worker.getExitValue() == null) + { + try + { + Thread.sleep(2000); + } catch (Exception q) + { + } + ; + } + } catch (Exception q) + { + } + ; + + System.exit(worker.getExitValue()); + } } - if (System.ge) + List rgs = new ArrayList(); + rgs.addAll(Arrays.asList(args)); + int i = 0; + while (i < rgs.size()) { - instance = new Jalview(); + if (rgs.get(i).indexOf("-Xmx") > -1) + { + rgs.remove(i); + } + else + { + i++; + } } + instance = new Jalview(); instance.doMain(args); } diff --git a/src/jalview/util/Worker.java b/src/jalview/util/Worker.java new file mode 100644 index 0000000..68f4174 --- /dev/null +++ b/src/jalview/util/Worker.java @@ -0,0 +1,193 @@ +package jalview.util; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintStream; + +import io.github.classgraph.ClassGraph; +import io.github.classgraph.ScanResult; + +/*** + * from + * http://stackoverflow.com/questions/808276/how-to-add-a-timeout-value-when + * -using-javas-runtime-exec + * + * @author jimp + * + */ +public class Worker extends Thread +{ + final Process process; + + private BufferedReader outputReader; + + private BufferedReader errorReader; + + Integer exit; + + public Worker(Process process) + { + this.process = process; + } + + @Override + public void run() + { + try + { + exit = process.waitFor(); + } catch (InterruptedException ignore) + { + return; + } + } + + public BufferedReader getOutputReader() + { + return outputReader; + } + + public void setOutputReader(BufferedReader outputReader) + { + this.outputReader = outputReader; + } + + public BufferedReader getErrorReader() + { + return errorReader; + } + + public void setErrorReader(BufferedReader errorReader) + { + this.errorReader = errorReader; + } + + /** + * + * @return null or process exit code + */ + public Integer getExitValue() + { + return exit; + } + + /** + * terminate with prejudice + */ + public void destroyProcess() + { + interrupt(); + currentThread().interrupt(); + process.destroy(); + } + + private static ClassGraph scanner = null; + + private static String classpath = null; + + public synchronized static String getClassPath() + { + if (scanner == null) + { + scanner = new ClassGraph(); + ScanResult scan = scanner.scan(); + classpath = scan.getClasspath(); + } + while (classpath == null) + { + try + { + Thread.sleep(10); + } catch (InterruptedException x) + { + + } + } + return classpath; + } + + public static Worker jalviewDesktopRunner(boolean withAwt, String cmd, + int timeout) + { + return jalviewDesktopRunner(withAwt, cmd, timeout, ""); + } + + public static Worker jalviewDesktopRunner(boolean withAwt, String cmd, + int timeout, String javaArgs) + { + // Note: JAL-3065 - don't include quotes for lib/* because the arguments are + // not expanded by the shell + String classpath = getClassPath(); + String _cmd = "java " + (withAwt ? "-Djava.awt.headless=true" : "") + + " -classpath " + classpath + " " + javaArgs + + " jalview.bin.Jalview "; + Process ls2_proc = null; + Worker worker = null; + try + { + ls2_proc = Runtime.getRuntime().exec(_cmd + cmd); + } catch (IOException e1) + { + e1.printStackTrace(); + } + if (ls2_proc != null) + { + BufferedReader outputReader = new BufferedReader( + new InputStreamReader(ls2_proc.getInputStream())); + BufferedReader errorReader = new BufferedReader( + new InputStreamReader(ls2_proc.getErrorStream())); + worker = new Worker(ls2_proc); + worker.start(); + try + { + worker.join(timeout); + } catch (InterruptedException e) + { + // e.printStackTrace(); + } + worker.setOutputReader(outputReader); + worker.setErrorReader(errorReader); + } + return worker; + } + + class Echo + { + public Echo(final PrintStream out, final BufferedReader source) + { + try + { + new Thread(new Runnable() + { + @Override + public void run() + { + String line; + try + { + while ((line = source.readLine()) != null) + { + out.print(line); + } + } catch (IOException q) + { + } + ; + }; + }).start(); + } catch (Exception ex) + { + } + ; + + } + + } + + public void doEcho() + { + new Echo(System.out, getOutputReader()); + new Echo(System.err, getErrorReader()); + } +} \ No newline at end of file diff --git a/test/jalview/bin/CommandLineOperations.java b/test/jalview/bin/CommandLineOperations.java index b3e6af9..a6c3bea 100644 --- a/test/jalview/bin/CommandLineOperations.java +++ b/test/jalview/bin/CommandLineOperations.java @@ -21,7 +21,7 @@ package jalview.bin; import jalview.gui.JvOptionPane; -import jalview.ws.utils.Worker; +import jalview.util.Worker; import java.io.File; import java.io.IOException;