From a3945d0f474b51cc52712d79715e03f681578204 Mon Sep 17 00:00:00 2001 From: Xin Liu Date: Mon, 22 Mar 2021 08:58:28 +0000 Subject: 8229517: Support for optional asynchronous/buffered logging Add an option async for the file-based outputs. The option conforms to output-option in JEP-158. --- src/hotspot/share/logging/logConfiguration.cpp | 3 ++- src/hotspot/share/logging/logFileOutput.cpp | 18 +++++++++++++++--- src/hotspot/share/logging/logFileOutput.hpp | 4 +++- test/hotspot/gtest/logging/test_asynclog.cpp | 2 +- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/hotspot/share/logging/logConfiguration.cpp b/src/hotspot/share/logging/logConfiguration.cpp index 556a34f3672..af5be442ba2 100644 --- a/src/hotspot/share/logging/logConfiguration.cpp +++ b/src/hotspot/share/logging/logConfiguration.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2021, 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 @@ -541,6 +541,7 @@ void LogConfiguration::print_command_line_help(outputStream* out) { out->print_cr(" filecount=.. - Number of files to keep in rotation (not counting the active file)." " If set to 0, log rotation is disabled." " This will cause existing log files to be overwritten."); + out->print_cr(" async=true|false - write asynchronously or not."); out->cr(); out->print_cr("Some examples:"); diff --git a/src/hotspot/share/logging/logFileOutput.cpp b/src/hotspot/share/logging/logFileOutput.cpp index 762d7872bc8..13ed569ca75 100644 --- a/src/hotspot/share/logging/logFileOutput.cpp +++ b/src/hotspot/share/logging/logFileOutput.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2021, 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 @@ -40,13 +40,14 @@ const char* const LogFileOutput::TimestampFilenamePlaceholder = "%t"; const char* const LogFileOutput::TimestampFormat = "%Y-%m-%d_%H-%M-%S"; const char* const LogFileOutput::FileSizeOptionKey = "filesize"; const char* const LogFileOutput::FileCountOptionKey = "filecount"; +const char* const LogFileOutput::AsyncOptionKey = "async"; char LogFileOutput::_pid_str[PidBufferSize]; char LogFileOutput::_vm_start_time_str[StartTimeBufferSize]; LogFileOutput::LogFileOutput(const char* name) : LogFileStreamOutput(NULL), _name(os::strdup_check_oom(name, mtLogging)), _file_name(NULL), _archive_name(NULL), _current_file(0), - _file_count(DefaultFileCount), _is_default_file_count(true), _archive_name_len(0), + _file_count(DefaultFileCount), _is_default_file_count(true), _async_mode(AsyncLogging), _archive_name_len(0), _rotate_size(DefaultFileSize), _current_size(0), _rotation_semaphore(1) { assert(strstr(name, Prefix) == name, "invalid output name '%s': missing prefix: %s", name, Prefix); _file_name = make_file_name(name + strlen(Prefix), _pid_str, _vm_start_time_str); @@ -216,6 +217,17 @@ bool LogFileOutput::parse_options(const char* options, outputStream* errstream) break; } _rotate_size = static_cast(value); + } else if (strcmp(AsyncOptionKey, key) == 0) { + if (strcasecmp(value_str, "true") == 0) { + _async_mode = true; + } else if (strcasecmp(value_str, "false") == 0) { + _async_mode =false; + } else { + errstream->print_cr("Invalid option: %s must be either true or false.", + AsyncOptionKey); + success = false; + break; + } } else { errstream->print_cr("Invalid option '%s' for log file output.", key); success = false; @@ -307,7 +319,7 @@ int LogFileOutput::write(const LogDecorations& decorations, const char* msg) { } LogAsyncFlusher* flusher = LogAsyncFlusher::instance(); - if (AsyncLogging && flusher != NULL) { + if (_async_mode && flusher != NULL) { flusher->enqueue(*this, decorations, msg); return 0; } diff --git a/src/hotspot/share/logging/logFileOutput.hpp b/src/hotspot/share/logging/logFileOutput.hpp index ea578fc3a59..3afb4b96af1 100644 --- a/src/hotspot/share/logging/logFileOutput.hpp +++ b/src/hotspot/share/logging/logFileOutput.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2021, 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 @@ -37,6 +37,7 @@ class LogFileOutput : public LogFileStreamOutput { static const char* const FileOpenMode; static const char* const FileCountOptionKey; static const char* const FileSizeOptionKey; + static const char* const AsyncOptionKey; static const char* const PidFilenamePlaceholder; static const char* const TimestampFilenamePlaceholder; static const char* const TimestampFormat; @@ -56,6 +57,7 @@ class LogFileOutput : public LogFileStreamOutput { uint _file_count; uint _file_count_max_digits; bool _is_default_file_count; + bool _async_mode; size_t _archive_name_len; size_t _rotate_size; diff --git a/test/hotspot/gtest/logging/test_asynclog.cpp b/test/hotspot/gtest/logging/test_asynclog.cpp index 89db983d45d..e9b1ec03b2b 100644 --- a/test/hotspot/gtest/logging/test_asynclog.cpp +++ b/test/hotspot/gtest/logging/test_asynclog.cpp @@ -145,7 +145,7 @@ LOG_LEVEL_LIST }; TEST_VM_F(AsyncLogTest, asynclog) { - set_log_config(TestLogFileName, "logging=debug"); + set_log_config(TestLogFileName, "logging=debug", NULL, "async=true"); VM_TestFlusher op; ThreadInVMfromNative invm(JavaThread::current()); -- 2.23.3