--- old/src/share/vm/logging/log.cpp 2016-03-29 15:25:02.229115610 +0200 +++ new/src/share/vm/logging/log.cpp 2016-03-29 15:25:02.077110500 +0200 @@ -25,6 +25,7 @@ #include "precompiled.hpp" #include "logging/log.hpp" #include "memory/allocation.inline.hpp" +#include "utilities/ostream.hpp" void LogWriteHelper::write_large(LogTagSet& lts, LogLevelType level, @@ -49,6 +50,7 @@ #include "logging/log.hpp" #include "logging/logConfiguration.hpp" #include "logging/logOutput.hpp" +#include "logging/logStream.inline.hpp" #include "memory/resourceArea.hpp" #define assert_str_eq(s1, s2) \ @@ -320,4 +322,56 @@ Test_logtarget_off(); } + +static void Test_logstream_helper(outputStream* stream) { + TestLogFile log_file("log_stream"); + TestLogSavedConfig tlsc(log_file.name(), "gc=debug"); + + // Try to log, but expect this to be filtered out. + stream->print("%d ", 3); stream->print("workers"); stream->cr(); + + FILE* fp = fopen(log_file.name(), "r"); + assert(fp, "File read error"); + + char output[256 /* Large enough buffer */]; + if (fgets(output, sizeof(output), fp) != NULL) { + assert(strstr(output, "3 workers") != NULL, "log line missing"); + } + fclose(fp); +} + +static void Test_logstream_log() { + Log(gc) log; + LogStream stream(log.debug()); + + Test_logstream_helper(&stream); +} + +static void Test_logstream_logtarget() { + LogTarget(Debug, gc) log; + LogStream stream(log); + + Test_logstream_helper(&stream); +} + +static void Test_logstream_logstreamhandle() { + LogStreamHandle(Debug, gc) stream; + + Test_logstream_helper(&stream); +} + +static void Test_logstream_no_rm() { + ResourceMark rm; + outputStream* stream = LogTarget(Debug, gc)::stream(); + + Test_logstream_helper(stream); +} + +void Test_logstream() { + Test_logstream_log(); + Test_logstream_logtarget(); + Test_logstream_logstreamhandle(); + Test_logstream_no_rm(); +} + #endif // PRODUCT --- old/src/share/vm/logging/log.hpp 2016-03-29 15:25:02.493124486 +0200 +++ new/src/share/vm/logging/log.hpp 2016-03-29 15:25:02.333119107 +0200 @@ -31,7 +31,6 @@ #include "memory/allocation.hpp" #include "runtime/os.hpp" #include "utilities/debug.hpp" -#include "utilities/ostream.hpp" // // Logging macros @@ -124,6 +123,13 @@ // #define LogTarget(level, ...) LogTargetImpl +// Forward declaration to decouple this file from the outputStream API. +class outputStream; +outputStream* create_log_stream(void (*log_func)(const char* fmt, va_list args) ATTRIBUTE_PRINTF(1, 0)); + +template +class LogTargetImpl; + template class LogImpl VALUE_OBJ_CLASS_SPEC { @@ -144,6 +150,11 @@ return LogTagSetMapping::tagset().is_level(level); } + template + static bool is_level() { + return is_level(Level); + } + ATTRIBUTE_PRINTF(2, 3) static void write(LogLevelType level, const char* fmt, ...) { va_list args; @@ -180,6 +191,12 @@ va_end(saved_args); } + template + ATTRIBUTE_PRINTF(1, 0) + static void vwrite(const char* fmt, va_list args) { + vwrite(Level, fmt, args); + } + #define LOG_LEVEL(level, name) ATTRIBUTE_PRINTF(2, 0) \ LogImpl& v##name(const char* fmt, va_list args) { \ vwrite(LogLevel::level, fmt, args); \ @@ -196,7 +213,10 @@ return is_level(LogLevel::level); \ } \ static outputStream* name##_stream() { \ - return new logStream(write); \ + return create_log_stream(&LogImpl::template vwrite); \ + } \ + static LogTargetImpl* name() { \ + return (LogTargetImpl*)NULL; \ } LOG_LEVEL_LIST #undef LOG_LEVEL @@ -224,7 +244,7 @@ } static outputStream* stream() { - return new logStream(&LogImpl::template write); + return create_log_stream(&LogImpl::template vwrite); } }; --- old/src/share/vm/memory/binaryTreeDictionary.cpp 2016-03-29 15:25:02.789134437 +0200 +++ new/src/share/vm/memory/binaryTreeDictionary.cpp 2016-03-29 15:25:02.609128386 +0200 @@ -25,6 +25,7 @@ #include "precompiled.hpp" #include "gc/cms/allocationStats.hpp" #include "gc/shared/spaceDecorator.hpp" +#include "logging/logStream.inline.hpp" #include "memory/binaryTreeDictionary.hpp" #include "memory/freeBlockDictionary.hpp" #include "memory/freeList.hpp" @@ -1190,10 +1191,10 @@ // Does walking the tree 3 times hurt? set_tree_surplus(splitSurplusPercent); set_tree_hints(); - Log(gc, freelist, stats) log; - if (log.is_trace()) { - ResourceMark rm; - report_statistics(log.trace_stream()); + LogTarget(Trace, gc, freelist, stats) out; + if (out.is_enabled()) { + LogStream stream(out); + report_statistics(&stream); } clear_tree_census(); } @@ -1232,27 +1233,26 @@ FreeList_t* total() { return &_total; } size_t total_free() { return _total_free; } void do_list(FreeList* fl) { - Log(gc, freelist, census) log; - outputStream* out = log.debug_stream(); + LogStreamHandle(Debug, gc, freelist, census) out; + if (++_print_line >= 40) { - ResourceMark rm; - FreeList_t::print_labels_on(out, "size"); + FreeList_t::print_labels_on(&out, "size"); _print_line = 0; } - fl->print_on(out); + fl->print_on(&out); _total_free += fl->count() * fl->size(); total()->set_count(total()->count() + fl->count()); } #if INCLUDE_ALL_GCS void do_list(AdaptiveFreeList* fl) { - Log(gc, freelist, census) log; - outputStream* out = log.debug_stream(); + LogStreamHandle(Debug, gc, freelist, census) out; + if (++_print_line >= 40) { - FreeList_t::print_labels_on(out, "size"); + FreeList_t::print_labels_on(&out, "size"); _print_line = 0; } - fl->print_on(out); + fl->print_on(&out); _total_free += fl->count() * fl->size() ; total()->set_count( total()->count() + fl->count() ); total()->set_bfr_surp( total()->bfr_surp() + fl->bfr_surp() ); --- old/src/share/vm/utilities/internalVMTests.cpp 2016-03-29 15:25:03.085144389 +0200 +++ new/src/share/vm/utilities/internalVMTests.cpp 2016-03-29 15:25:02.929139144 +0200 @@ -68,6 +68,7 @@ run_unit_test(TestChunkedList_test); run_unit_test(JSON_test); run_unit_test(Test_logtarget); + run_unit_test(Test_logstream); run_unit_test(Test_configure_stdout); run_unit_test(Test_logconfiguration_subscribe); run_unit_test(Test_log_prefix); --- old/src/share/vm/utilities/ostream.cpp 2016-03-29 15:25:03.333152726 +0200 +++ new/src/share/vm/utilities/ostream.cpp 2016-03-29 15:25:03.177147482 +0200 @@ -1099,14 +1099,3 @@ } #endif - -void logStream::write(const char* s, size_t len) { - if (len > 0 && s[len - 1] == '\n') { - _current_line.write(s, len - 1); - _log_func("%s", _current_line.as_string()); - _current_line.reset(); - } else { - _current_line.write(s, len); - } - update_position(s, len); -} --- old/src/share/vm/utilities/ostream.hpp 2016-03-29 15:25:03.633162812 +0200 +++ new/src/share/vm/utilities/ostream.hpp 2016-03-29 15:25:03.457156895 +0200 @@ -247,18 +247,6 @@ void flush() {}; }; -class logStream : public outputStream { -private: - stringStream _current_line; - void (*_log_func)(const char* fmt, ...) ATTRIBUTE_PRINTF(1, 2); -public: - void write(const char* s, size_t len); - logStream(void (*log_func)(const char* fmt, ...)) : _log_func(log_func) {} - ~logStream() { - guarantee(_current_line.size() == 0, "Buffer not flushed. Missing call to print_cr()?"); - } -}; - void ostream_init(); void ostream_init_log(); void ostream_exit(); --- /dev/null 2016-01-16 19:30:22.947416487 +0100 +++ new/src/share/vm/logging/logStream.cpp 2016-03-29 15:25:03.745166577 +0200 @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2016, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + * + */ + +#include "precompiled.hpp" +#include "logging/log.hpp" +#include "logging/logStream.hpp" + +// Create a log stream without an embedded ResourceMark. +// The function is placed here to be called out-of-line in log.hpp. +outputStream* create_log_stream(void (*log_func)(const char* fmt, va_list args) ATTRIBUTE_PRINTF(1, 0)) { + return new LogStreamNoResourceMark(log_func); +} + --- /dev/null 2016-01-16 19:30:22.947416487 +0100 +++ new/src/share/vm/logging/logStream.hpp 2016-03-29 15:25:03.969174108 +0200 @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2016, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + * + */ + +#ifndef SHARE_VM_LOGGING_LOGSTREAM_HPP +#define SHARE_VM_LOGGING_LOGSTREAM_HPP + +#include "utilities/ostream.hpp" + +// An output stream that logs to the logging framework. +// Requires a ResourceMark on the stack. +class LogStreamNoResourceMark : public outputStream { +private: + stringStream _current_line; + void (*_log_func)(const char* fmt, va_list args) ATTRIBUTE_PRINTF(1, 0); + + void print(const char* fmt, ...) ATTRIBUTE_PRINTF(2, 3); + +public: + LogStreamNoResourceMark(void (*log_func)(const char* fmt, va_list args)) : _log_func(log_func) {} + ~LogStreamNoResourceMark() { + guarantee(_current_line.size() == 0, "Buffer not flushed. Missing call to print_cr()?"); + } + + void write(const char* s, size_t len); +}; + +#endif // SHARE_VM_LOGGING_LOGSTREAM_HPP --- /dev/null 2016-01-16 19:30:22.947416487 +0100 +++ new/src/share/vm/logging/logStream.inline.hpp 2016-03-29 15:25:04.173180967 +0200 @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2016, 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + * + */ +#ifndef SHARE_VM_LOGGING_LOGSTREAM_INLINE_HPP +#define SHARE_VM_LOGGING_LOGSTREAM_INLINE_HPP + +#include "logging/log.hpp" +#include "logging/logStream.hpp" +#include "memory/resourceArea.hpp" +#include "utilities/ostream.hpp" + +inline void LogStreamNoResourceMark::print(const char* fmt, ...) { + va_list args; + va_start(args, fmt); + _log_func(fmt, args); + va_end(args); +} + +inline void LogStreamNoResourceMark::write(const char* s, size_t len) { + if (len > 0 && s[len - 1] == '\n') { + _current_line.write(s, len - 1); + print("%s", _current_line.as_string()); + _current_line.reset(); + } else { + _current_line.write(s, len); + } + update_position(s, len); +} + +// An output stream that logs to the logging framework, and embeds a ResourceMark. +// +// The class is intended to be stack allocated. +// Care needs to be taken when nested ResourceMarks are used. +class LogStream : public outputStream { +private: + ResourceMark _embedded_resource_mark; + LogStreamNoResourceMark _stream; + +public: + // Constructor to support creation from a LogTarget instance. + // + // LogTarget(Debug, gc) log; + // LogStream(log) stream; + template + LogStream(const LogTargetImpl& type_carrier) : + _embedded_resource_mark(), + _stream(&LogImpl::template vwrite) {} + + // Constructor to support creation from typed (likely NULL) pointer. Mostly used by the logging framework. + // + // LogStream stream(log.debug()); + // LogStream stream((LogTargetImpl*)NULL); + template + LogStream(const LogTargetImpl* type_carrier) : + _embedded_resource_mark(), + _stream(&LogImpl::template vwrite) {} + + // Override of outputStream::write. + void write(const char* s, size_t len) { _stream.write(s, len); } +}; + +// Support creation of a LogStream without having to provide a LogTarget pointer. +#define LogStreamHandle(level, ...) LogStreamTemplate + +template +class LogStreamTemplate : public LogStream { +public: + LogStreamTemplate() : LogStream((LogTargetImpl*)NULL) {} +}; + +#endif // SHARE_VM_LOGGING_LOGSTREAM_INLINE_HPP