< prev index next >

src/hotspot/share/logging/logDecorations.cpp

Print this page
rev 55638 : 8227527: LogDecorations should lazily resolve host name
Reviewed-by: gziemski

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2019, 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.

@@ -23,28 +23,42 @@
  */
 #include "precompiled.hpp"
 #include "jvm.h"
 #include "logging/logConfiguration.hpp"
 #include "logging/logDecorations.hpp"
+#include "runtime/atomic.hpp"
+#include "runtime/orderAccess.hpp"
 #include "runtime/os.inline.hpp"
 #include "runtime/thread.inline.hpp"
 #include "services/management.hpp"
 
 jlong LogDecorations::_vm_start_time_millis = 0;
-const char* LogDecorations::_host_name = "";
+const char* volatile LogDecorations::_host_name = NULL;
 
 LogDecorations::LogDecorations(LogLevelType level, const LogTagSet &tagset, const LogDecorators &decorators)
     : _level(level), _tagset(tagset), _millis(-1) {
   create_decorations(decorators);
 }
 
 void LogDecorations::initialize(jlong vm_start_time) {
+  _vm_start_time_millis = vm_start_time;
+}
+
+const char* LogDecorations::host_name() {
+  const char* host_name = OrderAccess::load_acquire(&_host_name);
+  if (host_name == NULL) {
   char buffer[1024];
-  if (os::get_host_name(buffer, sizeof(buffer))){
-    _host_name = os::strdup_check_oom(buffer);
+    if (os::get_host_name(buffer, sizeof(buffer))) {
+      host_name = os::strdup_check_oom(buffer);
+      const char* old_value = Atomic::cmpxchg(host_name, &_host_name, (const char*)NULL);
+      if (old_value != NULL) {
+        os::free((void *) host_name);
+        host_name = old_value;
   }
-  _vm_start_time_millis = vm_start_time;
+    }
+  }
+  return host_name;
 }
 
 void LogDecorations::create_decorations(const LogDecorators &decorators) {
   char* position = _decorations_buffer;
   #define DECORATOR(full_name, abbr) \

@@ -126,9 +140,9 @@
   int written = _tagset.label(pos, DecorationsBufferSize - (pos - _decorations_buffer));
   ASSERT_AND_RETURN(written, pos)
 }
 
 char* LogDecorations::create_hostname_decoration(char* pos) {
-  int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), "%s", _host_name);
+  int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), "%s", host_name());
   ASSERT_AND_RETURN(written, pos)
 }
 
< prev index next >