diff --git a/src/hotspot/share/logging/logAsyncWriter.cpp b/src/hotspot/share/logging/logAsyncWriter.cpp index 178adf22d49..3d987d04b8d 100644 --- a/src/hotspot/share/logging/logAsyncWriter.cpp +++ b/src/hotspot/share/logging/logAsyncWriter.cpp @@ -116,29 +116,7 @@ AsyncLogWriter::AsyncLogWriter() } } -void AsyncLogWriter::write() { - ResourceMark rm; - AsyncLogMap snapshot; - - // lock protection. This guarantees I/O jobs don't block logsites. - { - AsyncLogLocker locker; - - _buffer_staging->reset(); - swap(_buffer, _buffer_staging); - - // move counters to snapshot and reset them. - _stats.iterate([&] (LogFileStreamOutput* output, uint32_t& counter) { - if (counter > 0) { - bool created = snapshot.put(output, counter); - assert(created == true, "sanity check"); - counter = 0; - } - return true; - }); - _data_available = false; - } - +void AsyncLogWriter::write(AsyncLogMap& snapshot) { int req = 0; auto it = _buffer_staging->iterator(); while (it.hasNext()) { @@ -159,7 +137,7 @@ void AsyncLogWriter::write() { if (counter > 0) { stringStream ss; ss.print(UINT32_FORMAT_W(6) " messages dropped due to async logging", counter); - output->write_blocking(decorations, ss.as_string(false)); + output->write_blocking(decorations, ss.freeze()); } return true; }); @@ -172,15 +150,31 @@ void AsyncLogWriter::write() { void AsyncLogWriter::run() { while (true) { + ResourceMark rm; + AsyncLogMap snapshot; { AsyncLogLocker locker; while (!_data_available) { _lock.wait(0/* no timeout */); } - } + // Only doing a swap and statistics under the lock to + // guarantee that I/O jobs don't block logsites. + _buffer_staging->reset(); + swap(_buffer, _buffer_staging); - write(); + // move counters to snapshot and reset them. + _stats.iterate([&] (LogFileStreamOutput* output, uint32_t& counter) { + if (counter > 0) { + bool created = snapshot.put(output, counter); + assert(created == true, "sanity check"); + counter = 0; + } + return true; + }); + _data_available = false; + } + write(snapshot); } } diff --git a/src/hotspot/share/logging/logAsyncWriter.hpp b/src/hotspot/share/logging/logAsyncWriter.hpp index bd6fcd20a52..5015ddfe1a9 100644 --- a/src/hotspot/share/logging/logAsyncWriter.hpp +++ b/src/hotspot/share/logging/logAsyncWriter.hpp @@ -172,7 +172,7 @@ class AsyncLogWriter : public NonJavaThread { AsyncLogWriter(); void enqueue_locked(LogFileStreamOutput* output, const LogDecorations& decorations, const char* msg); - void write(); + void write(AsyncLogMap& snapshot); void run() override; void pre_run() override { NonJavaThread::pre_run();