src/hotspot/share/logging/logDecorations.cpp
author stefank
Tue, 26 Nov 2019 10:47:46 +0100
changeset 59290 97d13893ec3c
parent 59252 623722a6aeb9
permissions -rw-r--r--
8234748: Clean up atomic and orderAccess includes Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     1
/*
55656
1346086863a3 8227527: LogDecorations should lazily resolve host name
redestad
parents: 47765
diff changeset
     2
 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     4
 *
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     7
 * published by the Free Software Foundation.
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     8
 *
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    13
 * accompanied this code).
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    14
 *
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    18
 *
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    21
 * questions.
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    22
 *
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    23
 */
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    24
#include "precompiled.hpp"
47765
b7c7428eaab9 8189610: Reconcile jvm.h and all jvm_md.h between java.base and hotspot
coleenp
parents: 47216
diff changeset
    25
#include "jvm.h"
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    26
#include "logging/logConfiguration.hpp"
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    27
#include "logging/logDecorations.hpp"
55656
1346086863a3 8227527: LogDecorations should lazily resolve host name
redestad
parents: 47765
diff changeset
    28
#include "runtime/atomic.hpp"
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    29
#include "runtime/os.inline.hpp"
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    30
#include "runtime/thread.inline.hpp"
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    31
#include "services/management.hpp"
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    32
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    33
jlong LogDecorations::_vm_start_time_millis = 0;
55656
1346086863a3 8227527: LogDecorations should lazily resolve host name
redestad
parents: 47765
diff changeset
    34
const char* volatile LogDecorations::_host_name = NULL;
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    35
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    36
LogDecorations::LogDecorations(LogLevelType level, const LogTagSet &tagset, const LogDecorators &decorators)
38263
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents: 36174
diff changeset
    37
    : _level(level), _tagset(tagset), _millis(-1) {
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    38
  create_decorations(decorators);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    39
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    40
36174
481391df586b 8148219: Add decorator hostname to UL
mlarsson
parents: 34646
diff changeset
    41
void LogDecorations::initialize(jlong vm_start_time) {
55656
1346086863a3 8227527: LogDecorations should lazily resolve host name
redestad
parents: 47765
diff changeset
    42
  _vm_start_time_millis = vm_start_time;
1346086863a3 8227527: LogDecorations should lazily resolve host name
redestad
parents: 47765
diff changeset
    43
}
1346086863a3 8227527: LogDecorations should lazily resolve host name
redestad
parents: 47765
diff changeset
    44
1346086863a3 8227527: LogDecorations should lazily resolve host name
redestad
parents: 47765
diff changeset
    45
const char* LogDecorations::host_name() {
59247
56bf71d64d51 8234562: Move OrderAccess::release_store*/load_acquire to Atomic
stefank
parents: 58884
diff changeset
    46
  const char* host_name = Atomic::load_acquire(&_host_name);
55656
1346086863a3 8227527: LogDecorations should lazily resolve host name
redestad
parents: 47765
diff changeset
    47
  if (host_name == NULL) {
1346086863a3 8227527: LogDecorations should lazily resolve host name
redestad
parents: 47765
diff changeset
    48
    char buffer[1024];
1346086863a3 8227527: LogDecorations should lazily resolve host name
redestad
parents: 47765
diff changeset
    49
    if (os::get_host_name(buffer, sizeof(buffer))) {
1346086863a3 8227527: LogDecorations should lazily resolve host name
redestad
parents: 47765
diff changeset
    50
      host_name = os::strdup_check_oom(buffer);
59252
623722a6aeb9 8234740: Harmonize parameter order in Atomic - cmpxchg
stefank
parents: 59247
diff changeset
    51
      const char* old_value = Atomic::cmpxchg(&_host_name, (const char*)NULL, host_name);
55656
1346086863a3 8227527: LogDecorations should lazily resolve host name
redestad
parents: 47765
diff changeset
    52
      if (old_value != NULL) {
1346086863a3 8227527: LogDecorations should lazily resolve host name
redestad
parents: 47765
diff changeset
    53
        os::free((void *) host_name);
1346086863a3 8227527: LogDecorations should lazily resolve host name
redestad
parents: 47765
diff changeset
    54
        host_name = old_value;
1346086863a3 8227527: LogDecorations should lazily resolve host name
redestad
parents: 47765
diff changeset
    55
      }
1346086863a3 8227527: LogDecorations should lazily resolve host name
redestad
parents: 47765
diff changeset
    56
    }
36174
481391df586b 8148219: Add decorator hostname to UL
mlarsson
parents: 34646
diff changeset
    57
  }
55656
1346086863a3 8227527: LogDecorations should lazily resolve host name
redestad
parents: 47765
diff changeset
    58
  return host_name;
36174
481391df586b 8148219: Add decorator hostname to UL
mlarsson
parents: 34646
diff changeset
    59
}
481391df586b 8148219: Add decorator hostname to UL
mlarsson
parents: 34646
diff changeset
    60
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    61
void LogDecorations::create_decorations(const LogDecorators &decorators) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    62
  char* position = _decorations_buffer;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    63
  #define DECORATOR(full_name, abbr) \
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    64
  if (decorators.is_decorator(LogDecorators::full_name##_decorator)) { \
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    65
    _decoration_offset[LogDecorators::full_name##_decorator] = position; \
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    66
    position = create_##full_name##_decoration(position) + 1; \
58884
562df5d69eed 8233386: Initialize NULL fields for unused decorations
coleenp
parents: 55656
diff changeset
    67
  } else { \
562df5d69eed 8233386: Initialize NULL fields for unused decorations
coleenp
parents: 55656
diff changeset
    68
    _decoration_offset[LogDecorators::full_name##_decorator] = NULL; \
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    69
  }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    70
  DECORATOR_LIST
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    71
#undef DECORATOR
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    72
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    73
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    74
jlong LogDecorations::java_millis() {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    75
  if (_millis < 0) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    76
    _millis = os::javaTimeMillis();
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    77
  }
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    78
  return _millis;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    79
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    80
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    81
#define ASSERT_AND_RETURN(written, pos) \
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    82
    assert(written >= 0, "Decorations buffer overflow"); \
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    83
    return pos + written;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    84
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    85
char* LogDecorations::create_time_decoration(char* pos) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    86
  char* buf = os::iso8601_time(pos, 29);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    87
  int written = buf == NULL ? -1 : 29;
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    88
  ASSERT_AND_RETURN(written, pos)
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    89
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    90
42066
46f6db750b17 8166117: Add UTC timestamp decorator for UL
mlarsson
parents: 38263
diff changeset
    91
char* LogDecorations::create_utctime_decoration(char* pos) {
46f6db750b17 8166117: Add UTC timestamp decorator for UL
mlarsson
parents: 38263
diff changeset
    92
  char* buf = os::iso8601_time(pos, 29, true);
46f6db750b17 8166117: Add UTC timestamp decorator for UL
mlarsson
parents: 38263
diff changeset
    93
  int written = buf == NULL ? -1 : 29;
46f6db750b17 8166117: Add UTC timestamp decorator for UL
mlarsson
parents: 38263
diff changeset
    94
  ASSERT_AND_RETURN(written, pos)
46f6db750b17 8166117: Add UTC timestamp decorator for UL
mlarsson
parents: 38263
diff changeset
    95
}
46f6db750b17 8166117: Add UTC timestamp decorator for UL
mlarsson
parents: 38263
diff changeset
    96
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    97
char * LogDecorations::create_uptime_decoration(char* pos) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    98
  int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), "%.3fs", os::elapsedTime());
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
    99
  ASSERT_AND_RETURN(written, pos)
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   100
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   101
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   102
char * LogDecorations::create_timemillis_decoration(char* pos) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   103
  int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), INT64_FORMAT "ms", java_millis());
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   104
  ASSERT_AND_RETURN(written, pos)
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   105
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   106
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   107
char * LogDecorations::create_uptimemillis_decoration(char* pos) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   108
  int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer),
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   109
                             INT64_FORMAT "ms", java_millis() - _vm_start_time_millis);
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   110
  ASSERT_AND_RETURN(written, pos)
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   111
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   112
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   113
char * LogDecorations::create_timenanos_decoration(char* pos) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   114
  int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), INT64_FORMAT "ns", os::javaTimeNanos());
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   115
  ASSERT_AND_RETURN(written, pos)
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   116
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   117
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   118
char * LogDecorations::create_uptimenanos_decoration(char* pos) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   119
  int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), INT64_FORMAT "ns", os::elapsed_counter());
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   120
  ASSERT_AND_RETURN(written, pos)
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   121
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   122
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   123
char * LogDecorations::create_pid_decoration(char* pos) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   124
  int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), "%d", os::current_process_id());
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   125
  ASSERT_AND_RETURN(written, pos)
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   126
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   127
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   128
char * LogDecorations::create_tid_decoration(char* pos) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   129
  int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer),
34646
e2442a5c90e4 8144702: Using tid decorator in Unified Logging may crash VM
stuefe
parents: 33097
diff changeset
   130
                             INTX_FORMAT, os::current_thread_id());
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   131
  ASSERT_AND_RETURN(written, pos)
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   132
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   133
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   134
char* LogDecorations::create_level_decoration(char* pos) {
38263
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents: 36174
diff changeset
   135
  // Avoid generating the level decoration because it may change.
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents: 36174
diff changeset
   136
  // The decoration() method has a special case for level decorations.
a7488329ad27 8145934: Make ttyLocker equivalent for Unified Logging framework
mlarsson
parents: 36174
diff changeset
   137
  return pos;
33097
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   138
}
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   139
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   140
char* LogDecorations::create_tags_decoration(char* pos) {
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   141
  int written = _tagset.label(pos, DecorationsBufferSize - (pos - _decorations_buffer));
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   142
  ASSERT_AND_RETURN(written, pos)
96e348cb0442 8046148: JEP 158: Unified JVM Logging
mlarsson
parents:
diff changeset
   143
}
36174
481391df586b 8148219: Add decorator hostname to UL
mlarsson
parents: 34646
diff changeset
   144
481391df586b 8148219: Add decorator hostname to UL
mlarsson
parents: 34646
diff changeset
   145
char* LogDecorations::create_hostname_decoration(char* pos) {
55656
1346086863a3 8227527: LogDecorations should lazily resolve host name
redestad
parents: 47765
diff changeset
   146
  int written = jio_snprintf(pos, DecorationsBufferSize - (pos - _decorations_buffer), "%s", host_name());
36174
481391df586b 8148219: Add decorator hostname to UL
mlarsson
parents: 34646
diff changeset
   147
  ASSERT_AND_RETURN(written, pos)
481391df586b 8148219: Add decorator hostname to UL
mlarsson
parents: 34646
diff changeset
   148
}