src/hotspot/share/logging/logStream.cpp
author ihse
Tue, 13 Feb 2018 10:37:33 +0100
branchihse-remove-mapfiles-branch
changeset 56106 40e61db323c2
parent 47216 71c04702a3d5
child 49961 7379e6f906ae
permissions -rw-r--r--
First stab at removing mapfiles.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37254
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
     1
/*
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
     4
 *
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
     8
 *
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    13
 * accompanied this code).
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    14
 *
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    18
 *
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    21
 * questions.
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    22
 *
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    23
 */
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    24
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    25
#include "precompiled.hpp"
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    26
#include "logging/log.hpp"
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    27
#include "logging/logStream.hpp"
37254
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    28
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    29
LogStream::LineBuffer::LineBuffer()
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    30
 : _buf(_smallbuf), _cap(sizeof(_smallbuf)), _pos(0)
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    31
{
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    32
  _buf[0] = '\0';
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    33
}
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    34
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    35
LogStream::LineBuffer::~LineBuffer() {
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    36
  assert(_pos == 0, "still outstanding bytes in the line buffer");
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    37
  if (_buf != _smallbuf) {
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    38
    os::free(_buf);
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    39
  }
37254
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    40
}
8631304f255c 8152637: Create a stack allocatable LogStream class
stefank
parents:
diff changeset
    41
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    42
// try_ensure_cap tries to enlarge the capacity of the internal buffer
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    43
// to the given atleast value. May fail if either OOM happens or atleast
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    44
// is larger than a reasonable max of 1 M. Caller must not assume
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    45
// capacity without checking.
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    46
void LogStream::LineBuffer::try_ensure_cap(size_t atleast) {
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    47
  assert(_cap >= sizeof(_smallbuf), "sanity");
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    48
  if (_cap < atleast) {
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    49
    // Cap out at a reasonable max to prevent runaway leaks.
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    50
    const size_t reasonable_max = 1 * M;
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    51
    assert(_cap <= reasonable_max, "sanity");
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    52
    if (_cap == reasonable_max) {
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    53
      return;
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    54
    }
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    55
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    56
    const size_t additional_expansion = 256;
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    57
    size_t newcap = align_up(atleast + additional_expansion, additional_expansion);
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    58
    if (newcap > reasonable_max) {
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    59
      log_info(logging)("Suspiciously long log line: \"%.100s%s",
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    60
              _buf, (_pos >= 100 ? "..." : ""));
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    61
      newcap = reasonable_max;
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    62
    }
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    63
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    64
    char* const newbuf = (char*) os::malloc(newcap, mtLogging);
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    65
    if (newbuf == NULL) { // OOM. Leave object unchanged.
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    66
      return;
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    67
    }
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    68
    if (_pos > 0) { // preserve old content
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    69
      memcpy(newbuf, _buf, _pos + 1); // ..including trailing zero
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    70
    }
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    71
    if (_buf != _smallbuf) {
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    72
      os::free(_buf);
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    73
    }
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    74
    _buf = newbuf;
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    75
    _cap = newcap;
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    76
  }
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    77
  assert(_cap >= atleast, "sanity");
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    78
}
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    79
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    80
void LogStream::LineBuffer::append(const char* s, size_t len) {
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    81
  assert(_buf[_pos] == '\0', "sanity");
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    82
  assert(_pos < _cap, "sanity");
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    83
  const size_t minimum_capacity_needed = _pos + len + 1;
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    84
  try_ensure_cap(minimum_capacity_needed);
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    85
  // try_ensure_cap may not have enlarged the capacity to the full requested
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    86
  // extend or may have not worked at all. In that case, just gracefully work
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    87
  // with what we have already; just truncate if necessary.
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    88
  if (_cap < minimum_capacity_needed) {
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    89
    len = _cap - _pos - 1;
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    90
    if (len == 0) {
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    91
      return;
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    92
    }
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    93
  }
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    94
  memcpy(_buf + _pos, s, len);
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    95
  _pos += len;
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    96
  _buf[_pos] = '\0';
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    97
}
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    98
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
    99
void LogStream::LineBuffer::reset() {
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
   100
  _pos = 0;
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
   101
  _buf[_pos] = '\0';
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
   102
}
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
   103
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
   104
void LogStream::write(const char* s, size_t len) {
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
   105
  if (len > 0 && s[len - 1] == '\n') {
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
   106
    _current_line.append(s, len - 1); // omit the newline.
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
   107
    _log_handle.print("%s", _current_line.buffer());
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
   108
    _current_line.reset();
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
   109
  } else {
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
   110
    _current_line.append(s, len);
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
   111
  }
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
   112
  update_position(s, len);
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
   113
}
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 37461
diff changeset
   114