8317433: Async UL: Only grab lock once when write():ing

Reviewed-by: dholmes, xliu
This commit is contained in:
Johan Sjölen 2023-10-13 13:21:34 +00:00
parent 266636deab
commit 45b7aedabf
2 changed files with 21 additions and 27 deletions

View File

@ -116,29 +116,7 @@ AsyncLogWriter::AsyncLogWriter()
}
}
void AsyncLogWriter::write() {
ResourceMark rm;
AsyncLogMap<AnyObj::RESOURCE_AREA> 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<AnyObj::RESOURCE_AREA>& 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<AnyObj::RESOURCE_AREA> 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);
}
}

View File

@ -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<AnyObj::RESOURCE_AREA>& snapshot);
void run() override;
void pre_run() override {
NonJavaThread::pre_run();