Revert "Don't keep "outResultsFile" open in nanobench"

This reverts commit 2e1381ec86.

Reason for revert: This causes corrupted JSON output from
                   nanobench

Bug: skia:12078

Original change's description:
> 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>

# Not skipping CQ checks because original CL landed > 1 day ago.

Bug: b/143074513
Change-Id: Iad8e4423acbc520f49365859020776110087dacc
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/419842
Reviewed-by: Herb Derby <herb@google.com>
Reviewed-by: Chris Dalton <csmartdalton@google.com>
Commit-Queue: Herb Derby <herb@google.com>
This commit is contained in:
Herb Derby 2021-06-18 20:31:43 +00:00 committed by Skia Commit-Bot
parent 685e09b31a
commit efe9df37e0
4 changed files with 3 additions and 66 deletions

View File

@ -12,7 +12,6 @@
#include "include/core/SkString.h" #include "include/core/SkString.h"
#include "include/core/SkTypes.h" #include "include/core/SkTypes.h"
#include "src/core/SkOSFile.h"
#include "src/utils/SkJSONWriter.h" #include "src/utils/SkJSONWriter.h"
#include <cmath> #include <cmath>
@ -56,60 +55,4 @@ 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 #endif

View File

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

View File

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

View File

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