aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/worker/Worker.java
diff options
context:
space:
mode:
authorGravatar Philipp Wollermann <philwo@google.com>2015-06-09 12:35:06 +0000
committerGravatar Philipp Wollermann <philwo@google.com>2015-06-10 16:02:07 +0000
commit6c5688d121cba9e68259be13a0ce39dc4f026f9a (patch)
treea2390eafe8a41160d7df545498f0284f7a57e3c1 /src/main/java/com/google/devtools/build/lib/worker/Worker.java
parentfa759e2fe6698cabfe250a77bdd446f7eaca8a5f (diff)
Implement persistent worker processes and a spawn strategy that uses them.
This will be used by the persistent JavaBuilder, which improves performance of Java compilation by 4x due to profiting from JVM JIT optimizations and not having to relaunch the JVM for every spawn. It is completely generic though, so as long as a tool (ProGuard? Dexer? Whatever.) conforms to the Worker process protocol, it can use the new spawn strategy. -- MOS_MIGRATED_REVID=95527132
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/worker/Worker.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/worker/Worker.java86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/worker/Worker.java b/src/main/java/com/google/devtools/build/lib/worker/Worker.java
new file mode 100644
index 0000000000..409394da20
--- /dev/null
+++ b/src/main/java/com/google/devtools/build/lib/worker/Worker.java
@@ -0,0 +1,86 @@
+// Copyright 2015 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package com.google.devtools.build.lib.worker;
+
+import com.google.common.base.Preconditions;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.ProcessBuilder.Redirect;
+
+/**
+ * Interface to a worker process running as a child process.
+ *
+ * <p>A worker process must follow this protocol to be usable via this class: The worker process is
+ * spawned on demand. The worker process is free to exit whenever necessary, as new instances will
+ * be relaunched automatically. Communication happens via the WorkerProtocol protobuf, sent to and
+ * received from the worker process via stdin / stdout.
+ *
+ * <p>Other code in Blaze can talk to the worker process via input / output streams provided by this
+ * class.
+ */
+final class Worker {
+ private final Process process;
+ private final Thread shutdownHook;
+
+ private Worker(Process process, Thread shutdownHook) {
+ this.process = process;
+ this.shutdownHook = shutdownHook;
+ }
+
+ static Worker create(WorkerKey key) throws IOException {
+ Preconditions.checkNotNull(key);
+ ProcessBuilder processBuilder = new ProcessBuilder(key.getArgs().toArray(new String[0]))
+ .directory(key.getWorkDir().getPathFile())
+ .redirectError(Redirect.INHERIT);
+ processBuilder.environment().putAll(key.getEnv());
+
+ final Process process = processBuilder.start();
+
+ Thread shutdownHook = new Thread() {
+ @Override
+ public void run() {
+ process.destroy();
+ }
+ };
+ Runtime.getRuntime().addShutdownHook(shutdownHook);
+
+ return new Worker(process, shutdownHook);
+ }
+
+ void destroy() {
+ Runtime.getRuntime().removeShutdownHook(shutdownHook);
+ process.destroy();
+ }
+
+ boolean isAlive() {
+ // This is horrible, but Process.isAlive() is only available from Java 8 on and this is the
+ // best we can do prior to that.
+ try {
+ process.exitValue();
+ return false;
+ } catch (IllegalThreadStateException e) {
+ return true;
+ }
+ }
+
+ InputStream getInputStream() {
+ return process.getInputStream();
+ }
+
+ OutputStream getOutputStream() {
+ return process.getOutputStream();
+ }
+}