Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8256008: UL does not report anything if disk writing fails
Reviewed-by: stuefe
  • Loading branch information
YaSuenag committed Dec 2, 2020
1 parent fb139cf commit 3e3745c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
18 changes: 11 additions & 7 deletions src/hotspot/share/logging/logFileOutput.cpp
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -292,10 +292,12 @@ int LogFileOutput::write(const LogDecorations& decorations, const char* msg) {

_rotation_semaphore.wait();
int written = LogFileStreamOutput::write(decorations, msg);
_current_size += written;
if (written > 0) {
_current_size += written;

if (should_rotate()) {
rotate();
if (should_rotate()) {
rotate();
}
}
_rotation_semaphore.signal();

Expand All @@ -310,10 +312,12 @@ int LogFileOutput::write(LogMessageBuffer::Iterator msg_iterator) {

_rotation_semaphore.wait();
int written = LogFileStreamOutput::write(msg_iterator);
_current_size += written;
if (written > 0) {
_current_size += written;

if (should_rotate()) {
rotate();
if (should_rotate()) {
rotate();
}
}
_rotation_semaphore.signal();

Expand Down
48 changes: 38 additions & 10 deletions src/hotspot/share/logging/logFileStreamOutput.cpp
Expand Up @@ -28,6 +28,7 @@
#include "logging/logFileStreamOutput.hpp"
#include "logging/logMessageBuffer.hpp"
#include "memory/allocation.inline.hpp"
#include "utilities/defaultStream.hpp"

static bool initialized;
static union {
Expand Down Expand Up @@ -86,19 +87,47 @@ class FileLocker : public StackObj {
}
};

bool LogFileStreamOutput::flush() {
bool result = true;
if (fflush(_stream) != 0) {
if (!_write_error_is_shown) {
jio_fprintf(defaultStream::error_stream(),
"Could not flush log: %s (%s (%d))\n", name(), os::strerror(errno), errno);
jio_fprintf(_stream, "\nERROR: Could not flush log (%d)\n", errno);
_write_error_is_shown = true;
}
result = false;
}
return result;
}

#define WRITE_LOG_WITH_RESULT_CHECK(op, total) \
{ \
int result = op; \
if (result < 0) { \
if (!_write_error_is_shown) { \
jio_fprintf(defaultStream::error_stream(), \
"Could not write log: %s\n", name()); \
jio_fprintf(_stream, "\nERROR: Could not write log\n"); \
_write_error_is_shown = true; \
return -1; \
} \
} \
total += result; \
}

int LogFileStreamOutput::write(const LogDecorations& decorations, const char* msg) {
const bool use_decorations = !_decorators.is_empty();

int written = 0;
FileLocker flocker(_stream);
if (use_decorations) {
written += write_decorations(decorations);
written += jio_fprintf(_stream, " ");
WRITE_LOG_WITH_RESULT_CHECK(write_decorations(decorations), written);
WRITE_LOG_WITH_RESULT_CHECK(jio_fprintf(_stream, " "), written);
}
written += jio_fprintf(_stream, "%s\n", msg);
fflush(_stream);
WRITE_LOG_WITH_RESULT_CHECK(jio_fprintf(_stream, "%s\n", msg), written);

return written;
return flush() ? written : -1;
}

int LogFileStreamOutput::write(LogMessageBuffer::Iterator msg_iterator) {
Expand All @@ -108,12 +137,11 @@ int LogFileStreamOutput::write(LogMessageBuffer::Iterator msg_iterator) {
FileLocker flocker(_stream);
for (; !msg_iterator.is_at_end(); msg_iterator++) {
if (use_decorations) {
written += write_decorations(msg_iterator.decorations());
written += jio_fprintf(_stream, " ");
WRITE_LOG_WITH_RESULT_CHECK(write_decorations(msg_iterator.decorations()), written);
WRITE_LOG_WITH_RESULT_CHECK(jio_fprintf(_stream, " "), written);
}
written += jio_fprintf(_stream, "%s\n", msg_iterator.message());
WRITE_LOG_WITH_RESULT_CHECK(jio_fprintf(_stream, "%s\n", msg_iterator.message()), written);
}
fflush(_stream);

return written;
return flush() ? written : -1;
}
7 changes: 5 additions & 2 deletions src/hotspot/share/logging/logFileStreamOutput.hpp
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -40,17 +40,20 @@ static LogFileStreamInitializer log_stream_initializer;

// Base class for all FileStream-based log outputs.
class LogFileStreamOutput : public LogOutput {
private:
bool _write_error_is_shown;
protected:
FILE* _stream;
size_t _decorator_padding[LogDecorators::Count];

LogFileStreamOutput(FILE *stream) : _stream(stream) {
LogFileStreamOutput(FILE *stream) : _write_error_is_shown(false), _stream(stream) {
for (size_t i = 0; i < LogDecorators::Count; i++) {
_decorator_padding[i] = 0;
}
}

int write_decorations(const LogDecorations& decorations);
bool flush();

public:
virtual int write(const LogDecorations& decorations, const char* msg);
Expand Down

1 comment on commit 3e3745c

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.