8152637: Create a stack allocatable LogStream class
authorstefank
Tue, 05 Apr 2016 10:35:39 +0200
changeset 37254 8631304f255c
parent 37252 d9b7f60c0b98
child 37255 100d4a515df0
8152637: Create a stack allocatable LogStream class Reviewed-by: rehn, brutisso
hotspot/src/share/vm/logging/log.cpp
hotspot/src/share/vm/logging/log.hpp
hotspot/src/share/vm/logging/logStream.cpp
hotspot/src/share/vm/logging/logStream.hpp
hotspot/src/share/vm/logging/logStream.inline.hpp
hotspot/src/share/vm/memory/binaryTreeDictionary.cpp
hotspot/src/share/vm/utilities/internalVMTests.cpp
hotspot/src/share/vm/utilities/ostream.cpp
hotspot/src/share/vm/utilities/ostream.hpp
--- a/hotspot/src/share/vm/logging/log.cpp	Tue Apr 05 00:41:55 2016 +0200
+++ b/hotspot/src/share/vm/logging/log.cpp	Tue Apr 05 10:35:39 2016 +0200
@@ -31,6 +31,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) \
@@ -315,4 +316,58 @@
   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 != NULL, "File read error");
+
+  char output[256 /* Large enough buffer */];
+  char* res = fgets(output, sizeof(output), fp);
+  assert(res != NULL, "assert");
+
+  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
--- a/hotspot/src/share/vm/logging/log.hpp	Tue Apr 05 00:41:55 2016 +0200
+++ b/hotspot/src/share/vm/logging/log.hpp	Tue Apr 05 10:35:39 2016 +0200
@@ -31,7 +31,6 @@
 #include "memory/allocation.hpp"
 #include "runtime/os.hpp"
 #include "utilities/debug.hpp"
-#include "utilities/ostream.hpp"
 
 //
 // Logging macros
@@ -104,6 +103,13 @@
 //
 #define LogTarget(level, ...) LogTargetImpl<LogLevel::level, LOG_TAGS(__VA_ARGS__)>
 
+// Forward declaration to decouple this file from the outputStream API.
+class outputStream;
+outputStream* create_log_stream(LogLevelType level, LogTagSet* tagset);
+
+template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
+class LogTargetImpl;
+
 template <LogTagType T0, LogTagType T1 = LogTag::__NO_TAG, LogTagType T2 = LogTag::__NO_TAG, LogTagType T3 = LogTag::__NO_TAG,
           LogTagType T4 = LogTag::__NO_TAG, LogTagType GuardTag = LogTag::__NO_TAG>
 class LogImpl VALUE_OBJ_CLASS_SPEC {
@@ -162,7 +168,10 @@
     return is_level(LogLevel::level); \
   } \
   static outputStream* name##_stream() { \
-    return new logStream(write<LogLevel::level>); \
+    return create_log_stream(LogLevel::level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()); \
+  } \
+  static LogTargetImpl<LogLevel::level, T0, T1, T2, T3, T4, GuardTag>* name() { \
+    return (LogTargetImpl<LogLevel::level, T0, T1, T2, T3, T4, GuardTag>*)NULL; \
   }
   LOG_LEVEL_LIST
 #undef LOG_LEVEL
@@ -190,7 +199,7 @@
   }
 
   static outputStream* stream() {
-    return new logStream(&LogImpl<T0, T1, T2, T3, T4, GuardTag>::template write<level>);
+    return create_log_stream(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset());
   }
 };
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/src/share/vm/logging/logStream.cpp	Tue Apr 05 10:35:39 2016 +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(LogLevelType level, LogTagSet* tagset) {
+  return new LogStreamNoResourceMark(level, tagset);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/src/share/vm/logging/logStream.hpp	Tue Apr 05 10:35:39 2016 +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 "logging/log.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;
+  LogLevelType _level;
+  LogTagSet*   _tagset;
+
+public:
+  LogStreamNoResourceMark(LogLevelType level, LogTagSet* tagset) : _level(level), _tagset(tagset) {}
+  ~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	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/src/share/vm/logging/logStream.inline.hpp	Tue Apr 05 10:35:39 2016 +0200
@@ -0,0 +1,84 @@
+/*
+ * 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::write(const char* s, size_t len) {
+  if (len > 0 && s[len - 1] == '\n') {
+    _current_line.write(s, len - 1);
+    _tagset->write(_level, "%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 <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
+  LogStream(const LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>& type_carrier) :
+      _embedded_resource_mark(),
+      _stream(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}
+
+  // Constructor to support creation from typed (likely NULL) pointer. Mostly used by the logging framework.
+  //
+  // LogStream stream(log.debug());
+  // LogStream stream((LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>*)NULL);
+  template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
+  LogStream(const LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>* type_carrier) :
+      _embedded_resource_mark(),
+      _stream(level, &LogTagSetMapping<T0, T1, T2, T3, T4>::tagset()) {}
+
+  // 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<LogLevel::level, LOG_TAGS(__VA_ARGS__)>
+
+template <LogLevelType level, LogTagType T0, LogTagType T1, LogTagType T2, LogTagType T3, LogTagType T4, LogTagType GuardTag>
+class LogStreamTemplate : public LogStream {
+public:
+  LogStreamTemplate() : LogStream((LogTargetImpl<level, T0, T1, T2, T3, T4, GuardTag>*)NULL) {}
+};
+
+#endif // SHARE_VM_LOGGING_LOGSTREAM_INLINE_HPP
--- a/hotspot/src/share/vm/memory/binaryTreeDictionary.cpp	Tue Apr 05 00:41:55 2016 +0200
+++ b/hotspot/src/share/vm/memory/binaryTreeDictionary.cpp	Tue Apr 05 10:35:39 2016 +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) log;
+  if (log.is_enabled()) {
+    LogStream out(log);
+    report_statistics(&out);
   }
   clear_tree_census();
 }
@@ -1232,27 +1233,26 @@
   FreeList_t* total() { return &_total; }
   size_t total_free() { return _total_free; }
   void do_list(FreeList<Chunk_t>* 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<Chunk_t>* 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()    );
--- a/hotspot/src/share/vm/utilities/internalVMTests.cpp	Tue Apr 05 00:41:55 2016 +0200
+++ b/hotspot/src/share/vm/utilities/internalVMTests.cpp	Tue Apr 05 10:35:39 2016 +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);
--- a/hotspot/src/share/vm/utilities/ostream.cpp	Tue Apr 05 00:41:55 2016 +0200
+++ b/hotspot/src/share/vm/utilities/ostream.cpp	Tue Apr 05 10:35:39 2016 +0200
@@ -1097,14 +1097,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);
-}
--- a/hotspot/src/share/vm/utilities/ostream.hpp	Tue Apr 05 00:41:55 2016 +0200
+++ b/hotspot/src/share/vm/utilities/ostream.hpp	Tue Apr 05 10:35:39 2016 +0200
@@ -246,18 +246,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();