8227527: LogDecorations should lazily resolve host name
Reviewed-by: gziemski, lfoltan, stuefe
This commit is contained in:
parent
5e24afc868
commit
987fad27b7
@ -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,13 +40,25 @@ LogDecorations::LogDecorations(LogLevelType level, const LogTagSet &tagset, cons
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
return host_name;
|
||||
}
|
||||
|
||||
void LogDecorations::create_decorations(const LogDecorators &decorators) {
|
||||
char* position = _decorations_buffer;
|
||||
#define DECORATOR(full_name, abbr) \
|
||||
@ -128,7 +142,7 @@ char* LogDecorations::create_tags_decoration(char* 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)
|
||||
}
|
||||
|
||||
|
@ -38,8 +38,9 @@ class LogDecorations {
|
||||
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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user