8227527: LogDecorations should lazily resolve host name
authorredestad
Thu, 11 Jul 2019 15:38:09 +0200
changeset 55656 1346086863a3
parent 55655 419420eb5230
child 55657 107ebf94ddcc
8227527: LogDecorations should lazily resolve host name Reviewed-by: gziemski, lfoltan, stuefe
src/hotspot/share/logging/logDecorations.cpp
src/hotspot/share/logging/logDecorations.hpp
--- a/src/hotspot/share/logging/logDecorations.cpp	Thu Jul 11 09:26:04 2019 -0400
+++ b/src/hotspot/share/logging/logDecorations.cpp	Thu Jul 11 15:38:09 2019 +0200
@@ -1,5 +1,5 @@
 /*
- * 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
@@ -25,12 +25,14 @@
 #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) {
@@ -38,11 +40,23 @@
 }
 
 void LogDecorations::initialize(jlong vm_start_time) {
-  char buffer[1024];
-  if (os::get_host_name(buffer, sizeof(buffer))){
-    _host_name = os::strdup_check_oom(buffer);
+  _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);
+      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) {
@@ -128,7 +142,7 @@
 }
 
 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)
 }
 
--- a/src/hotspot/share/logging/logDecorations.hpp	Thu Jul 11 09:26:04 2019 -0400
+++ b/src/hotspot/share/logging/logDecorations.hpp	Thu Jul 11 15:38:09 2019 +0200
@@ -38,8 +38,9 @@
   const LogTagSet& _tagset;
   jlong _millis;
   static jlong _vm_start_time_millis;
-  static const char* _host_name;
+  static const char* volatile _host_name;
 
+  const char* host_name();
   jlong java_millis();
   void create_decorations(const LogDecorators& decorators);