Don't keep "outResultsFile" open in nanobench

There is a bug on Pixel and Pixel2 devices where the program
eventually terminates with a non-zero exit code. Closing the
outResultsFile between JSON flushes seems to fix it (for whatever
reason).

Bug: b/143074513
Change-Id: I935e982e88758fda19292129c8031f8501cca615
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/249821
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Chris Dalton <csmartdalton@google.com>
This commit is contained in:
Chris Dalton 2019-10-23 14:35:19 -06:00 committed by Skia Commit-Bot
parent 37bc8f9652
commit 2e1381ec86
4 changed files with 66 additions and 3 deletions

View File

@ -12,6 +12,7 @@
#include "include/core/SkString.h"
#include "include/core/SkTypes.h"
#include "src/core/SkOSFile.h"
#include "src/utils/SkJSONWriter.h"
/**
@ -54,4 +55,60 @@ public:
}
};
/**
NanoFILEAppendAndCloseStream: re-open the file, append the data, then close on every write() call.
The purpose of this class is to not keep the file handle open between JSON flushes. SkJSONWriter
uses a 32k in-memory cache already, so it only flushes occasionally and is well equipped for a
steam like this.
See: https://b.corp.google.com/issues/143074513
*/
class NanoFILEAppendAndCloseStream : public SkWStream {
public:
NanoFILEAppendAndCloseStream(const char* filePath) : fFilePath(filePath) {
// Open the file as "write" to ensure it exists and clear any contents before we begin
// appending.
FILE* file = sk_fopen(fFilePath.c_str(), kWrite_SkFILE_Flag);
if (!file) {
SkDebugf("Failed to open file %s for write.\n", fFilePath.c_str());
fFilePath.reset();
return;
}
sk_fclose(file);
}
size_t bytesWritten() const override { return fBytesWritten; }
bool write(const void* buffer, size_t size) override {
if (fFilePath.isEmpty()) {
return false;
}
FILE* file = sk_fopen(fFilePath.c_str(), kAppend_SkFILE_Flag);
if (!file) {
SkDebugf("Failed to open file %s for append.\n", fFilePath.c_str());
return false;
}
size_t bytesWritten = sk_fwrite(buffer, size, file);
fBytesWritten += bytesWritten;
sk_fclose(file);
if (bytesWritten != size) {
SkDebugf("NanoFILEAppendAndCloseStream failed writing %d bytes (wrote %d instead)\n",
size, bytesWritten);
return false;
}
return true;
}
void flush() override {}
private:
SkString fFilePath;
size_t fBytesWritten = 0;
};
#endif

View File

@ -1164,7 +1164,9 @@ int main(int argc, char** argv) {
std::unique_ptr<SkWStream> logStream(new SkNullWStream);
if (!FLAGS_outResultsFile.isEmpty()) {
#if defined(SK_RELEASE)
logStream.reset(new SkFILEWStream(FLAGS_outResultsFile[0]));
// SkJSONWriter uses a 32k in-memory cache, so it only flushes occasionally and is well
// equipped for a stream that re-opens, appends, and closes the file on every write.
logStream.reset(new NanoFILEAppendAndCloseStream(FLAGS_outResultsFile[0]));
#else
SkDebugf("I'm ignoring --outResultsFile because this is a Debug build.");
return 1;

View File

@ -16,8 +16,9 @@
#include "include/core/SkString.h"
enum SkFILE_Flags {
kRead_SkFILE_Flag = 0x01,
kWrite_SkFILE_Flag = 0x02
kRead_SkFILE_Flag = 0x01,
kWrite_SkFILE_Flag = 0x02,
kAppend_SkFILE_Flag = 0x04
};
FILE* sk_fopen(const char path[], SkFILE_Flags);

View File

@ -72,7 +72,10 @@ FILE* sk_fopen(const char path[], SkFILE_Flags flags) {
*p++ = 'r';
}
if (flags & kWrite_SkFILE_Flag) {
SkASSERT(!(flags & kAppend_SkFILE_Flag));
*p++ = 'w';
} else if (flags & kAppend_SkFILE_Flag) {
*p++ = 'a';
}
*p = 'b';