diff options
author | 2017-06-06 15:10:31 -0400 | |
---|---|---|
committer | 2017-06-07 09:49:11 -0400 | |
commit | 7aacb779c8c2ee72a297bad9c82ba6872100ec17 (patch) | |
tree | 2c01d52e25db70a55e565054ed2e1cf2fb6cbbf0 /src/main/java/com/google/devtools | |
parent | 50b59cec885c0a66ff3a45c51402c2d786796610 (diff) |
Internally track changes to stdout / stderr files, so we can avoid filesystem operations to check whether they've been written. In the common case, they haven't, and we can avoid checking that.
PiperOrigin-RevId: 158172182
Diffstat (limited to 'src/main/java/com/google/devtools')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/util/io/FileOutErr.java | 39 |
1 files changed, 28 insertions, 11 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/util/io/FileOutErr.java b/src/main/java/com/google/devtools/build/lib/util/io/FileOutErr.java index 07949e4be5..2124f97b52 100644 --- a/src/main/java/com/google/devtools/build/lib/util/io/FileOutErr.java +++ b/src/main/java/com/google/devtools/build/lib/util/io/FileOutErr.java @@ -292,18 +292,20 @@ public class FileOutErr extends OutErr { } /** - * An output stream that captures all output into a file. - * The file is created only if output is received. + * An output stream that captures all output into a file. The file is created only if output is + * received. * - * The user must take care that nobody else is writing to the - * file that is backing the output stream. + * The user must take care that nobody else is writing to the file that is backing the output + * stream. * - * The write() methods of type are synchronized to ensure - * that writes from different threads are not mixed up. + * The write() methods of type are synchronized to ensure that writes from different threads are + * not mixed up. Note that this class is otherwise + * {@link com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadCompatible}. Only the + * write() methods are allowed to be concurrently, and only concurrently with each other. All + * other calls must be serialized. * - * The outputStream is here only for the benefit of the pumping - * IO we're currently using for execution - Once that is gone, - * we can remove this output stream and fold its code into the + * The outputStream is here only for the benefit of the pumping IO we're currently using for + * execution - Once that is gone we can remove this output stream and fold its code into the * FileOutErr. */ @ThreadSafety.ThreadCompatible @@ -313,6 +315,7 @@ public class FileOutErr extends OutErr { private OutputStream outputStream; private String error; private OutputFilter outputFilter; + private boolean mightHaveOutput = false; protected FileRecordingOutputStream(Path outputFile) { this.outputFile = outputFile; @@ -325,9 +328,16 @@ public class FileOutErr extends OutErr { @Override Path getFile() { + // The caller is getting a reference to the filesystem path, so conservatively assume the + // file has been modified. + markDirty(); return outputFile; } + private void markDirty() { + mightHaveOutput = true; + } + private OutputStream getOutputStream() throws IOException { // you should hold the lock before you invoke this method if (outputStream == null) { @@ -348,6 +358,7 @@ public class FileOutErr extends OutErr { close(); outputStream = null; outputFile.delete(); + mightHaveOutput = false; } @Override @@ -368,6 +379,9 @@ public class FileOutErr extends OutErr { if (hadError()) { return true; } + if (!mightHaveOutput) { + return false; + } if (!outputFile.exists()) { return false; } @@ -383,7 +397,7 @@ public class FileOutErr extends OutErr { String getRecordedOutput() { StringBuilder result = new StringBuilder(); try { - if (getFile().exists()) { + if (mightHaveOutput && getFile().exists()) { result.append(FileSystemUtils.readContentAsLatin1(getFile())); } } catch (IOException ex) { @@ -399,7 +413,7 @@ public class FileOutErr extends OutErr { @Override void dumpOut(OutputStream out) { try { - if (getFile().exists()) { + if (mightHaveOutput && getFile().exists()) { try (InputStream in = getFile().getInputStream()) { ByteStreams.copy(in, out); } @@ -418,6 +432,7 @@ public class FileOutErr extends OutErr { @Override public synchronized void write(byte[] b, int off, int len) { if (len > 0) { + markDirty(); try { getOutputStream().write(b, off, len); } catch (IOException ex) { @@ -428,6 +443,7 @@ public class FileOutErr extends OutErr { @Override public synchronized void write(int b) { + markDirty(); try { getOutputStream().write(b); } catch (IOException ex) { @@ -438,6 +454,7 @@ public class FileOutErr extends OutErr { @Override public synchronized void write(byte[] b) throws IOException { if (b.length > 0) { + markDirty(); getOutputStream().write(b); } } |