src/share/vm/utilities/ostream.hpp

Thu, 09 Apr 2020 13:07:11 +0000

author
phh
date
Thu, 09 Apr 2020 13:07:11 +0000
changeset 9904
4698900b8221
parent 9478
f3108e56b502
child 9931
fd44df5e3bc3
permissions
-rw-r--r--

8191393: Random crashes during cfree+0x1c
Summary: Synchronize gcLogFileStream::write/rotate_log
Reviewed-by: adinn, phh, snazarki
Contributed-by: xxinliu@amazon.com

duke@435 1 /*
drchase@6680 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #ifndef SHARE_VM_UTILITIES_OSTREAM_HPP
stefank@2314 26 #define SHARE_VM_UTILITIES_OSTREAM_HPP
stefank@2314 27
stefank@2314 28 #include "memory/allocation.hpp"
stefank@2314 29 #include "runtime/timer.hpp"
stefank@2314 30
brutisso@6904 31 class GCId;
fparain@5409 32 DEBUG_ONLY(class ResourceMark;)
fparain@5409 33
duke@435 34 // Output streams for printing
duke@435 35 //
duke@435 36 // Printing guidelines:
duke@435 37 // Where possible, please use tty->print() and tty->print_cr().
duke@435 38 // For product mode VM warnings use warning() which internally uses tty.
duke@435 39 // In places where tty is not initialized yet or too much overhead,
duke@435 40 // we may use jio_printf:
duke@435 41 // jio_fprintf(defaultStream::output_stream(), "Message");
duke@435 42 // This allows for redirection via -XX:+DisplayVMOutputToStdout and
duke@435 43 // -XX:+DisplayVMOutputToStderr
duke@435 44 class outputStream : public ResourceObj {
duke@435 45 protected:
duke@435 46 int _indentation; // current indentation
duke@435 47 int _width; // width of the page
duke@435 48 int _position; // position on the current line
duke@435 49 int _newlines; // number of '\n' output so far
duke@435 50 julong _precount; // number of chars output, less _position
duke@435 51 TimeStamp _stamp; // for time stamps
duke@435 52
duke@435 53 void update_position(const char* s, size_t len);
duke@435 54 static const char* do_vsnprintf(char* buffer, size_t buflen,
duke@435 55 const char* format, va_list ap,
duke@435 56 bool add_cr,
drchase@6680 57 size_t& result_len) ATTRIBUTE_PRINTF(3, 0);
duke@435 58
duke@435 59 public:
duke@435 60 // creation
duke@435 61 outputStream(int width = 80);
duke@435 62 outputStream(int width, bool has_time_stamps);
duke@435 63
duke@435 64 // indentation
kamg@3992 65 outputStream& indent();
duke@435 66 void inc() { _indentation++; };
duke@435 67 void dec() { _indentation--; };
kamg@3992 68 void inc(int n) { _indentation += n; };
kamg@3992 69 void dec(int n) { _indentation -= n; };
duke@435 70 int indentation() const { return _indentation; }
duke@435 71 void set_indentation(int i) { _indentation = i; }
duke@435 72 void fill_to(int col);
jrose@535 73 void move_to(int col, int slop = 6, int min_space = 2);
duke@435 74
duke@435 75 // sizing
duke@435 76 int width() const { return _width; }
duke@435 77 int position() const { return _position; }
duke@435 78 int newlines() const { return _newlines; }
duke@435 79 julong count() const { return _precount + _position; }
duke@435 80 void set_count(julong count) { _precount = count - _position; }
duke@435 81 void set_position(int pos) { _position = pos; }
duke@435 82
duke@435 83 // printing
drchase@6680 84 void print(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
drchase@6680 85 void print_cr(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
drchase@6680 86 void vprint(const char *format, va_list argptr) ATTRIBUTE_PRINTF(2, 0);
drchase@6680 87 void vprint_cr(const char* format, va_list argptr) ATTRIBUTE_PRINTF(2, 0);
duke@435 88 void print_raw(const char* str) { write(str, strlen(str)); }
duke@435 89 void print_raw(const char* str, int len) { write(str, len); }
duke@435 90 void print_raw_cr(const char* str) { write(str, strlen(str)); cr(); }
duke@435 91 void print_raw_cr(const char* str, int len){ write(str, len); cr(); }
kamg@3992 92 void print_data(void* data, size_t len, bool with_ascii);
duke@435 93 void put(char ch);
jrose@535 94 void sp(int count = 1);
duke@435 95 void cr();
duke@435 96 void bol() { if (_position > 0) cr(); }
duke@435 97
duke@435 98 // Time stamp
duke@435 99 TimeStamp& time_stamp() { return _stamp; }
duke@435 100 void stamp();
ysr@777 101 void stamp(bool guard, const char* prefix, const char* suffix);
ysr@777 102 void stamp(bool guard) {
ysr@777 103 stamp(guard, "", ": ");
ysr@777 104 }
duke@435 105 // Date stamp
duke@435 106 void date_stamp(bool guard, const char* prefix, const char* suffix);
duke@435 107 // A simplified call that includes a suffix of ": "
duke@435 108 void date_stamp(bool guard) {
duke@435 109 date_stamp(guard, "", ": ");
duke@435 110 }
brutisso@6904 111 void gclog_stamp(const GCId& gc_id);
duke@435 112
duke@435 113 // portable printing of 64 bit integers
duke@435 114 void print_jlong(jlong value);
duke@435 115 void print_julong(julong value);
duke@435 116
duke@435 117 // flushing
duke@435 118 virtual void flush() {}
duke@435 119 virtual void write(const char* str, size_t len) = 0;
minqi@6535 120 virtual void rotate_log(bool force, outputStream* out = NULL) {} // GC log rotation
minqi@2964 121 virtual ~outputStream() {} // close properly on deletion
duke@435 122
duke@435 123 void dec_cr() { dec(); cr(); }
duke@435 124 void inc_cr() { inc(); cr(); }
duke@435 125 };
duke@435 126
duke@435 127 // standard output
minqi@2964 128 // ANSI C++ name collision
duke@435 129 extern outputStream* tty; // tty output
duke@435 130 extern outputStream* gclog_or_tty; // stream for gc log if -Xloggc:<f>, or tty
duke@435 131
kamg@3992 132 class streamIndentor : public StackObj {
kamg@3992 133 private:
kamg@3992 134 outputStream* _str;
kamg@3992 135 int _amount;
kamg@3992 136
kamg@3992 137 public:
kamg@3992 138 streamIndentor(outputStream* str, int amt = 2) : _str(str), _amount(amt) {
kamg@3992 139 _str->inc(_amount);
kamg@3992 140 }
kamg@3992 141 ~streamIndentor() { _str->dec(_amount); }
kamg@3992 142 };
kamg@3992 143
kamg@3992 144
duke@435 145 // advisory locking for the shared tty stream:
duke@435 146 class ttyLocker: StackObj {
never@2570 147 friend class ttyUnlocker;
duke@435 148 private:
duke@435 149 intx _holder;
duke@435 150
duke@435 151 public:
duke@435 152 static intx hold_tty(); // returns a "holder" token
duke@435 153 static void release_tty(intx holder); // must witness same token
never@2570 154 static bool release_tty_if_locked(); // returns true if lock was released
duke@435 155 static void break_tty_lock_for_safepoint(intx holder);
duke@435 156
duke@435 157 ttyLocker() { _holder = hold_tty(); }
duke@435 158 ~ttyLocker() { release_tty(_holder); }
duke@435 159 };
duke@435 160
never@2570 161 // Release the tty lock if it's held and reacquire it if it was
never@2570 162 // locked. Used to avoid lock ordering problems.
never@2570 163 class ttyUnlocker: StackObj {
never@2570 164 private:
never@2570 165 bool _was_locked;
never@2570 166 public:
never@2570 167 ttyUnlocker() {
never@2570 168 _was_locked = ttyLocker::release_tty_if_locked();
never@2570 169 }
never@2570 170 ~ttyUnlocker() {
never@2570 171 if (_was_locked) {
never@2570 172 ttyLocker::hold_tty();
never@2570 173 }
never@2570 174 }
never@2570 175 };
never@2570 176
duke@435 177 // for writing to strings; buffer will expand automatically
duke@435 178 class stringStream : public outputStream {
duke@435 179 protected:
duke@435 180 char* buffer;
duke@435 181 size_t buffer_pos;
duke@435 182 size_t buffer_length;
duke@435 183 bool buffer_fixed;
fparain@5409 184 DEBUG_ONLY(ResourceMark* rm;)
duke@435 185 public:
duke@435 186 stringStream(size_t initial_bufsize = 256);
duke@435 187 stringStream(char* fixed_buffer, size_t fixed_buffer_size);
duke@435 188 ~stringStream();
duke@435 189 virtual void write(const char* c, size_t len);
duke@435 190 size_t size() { return buffer_pos; }
duke@435 191 const char* base() { return buffer; }
duke@435 192 void reset() { buffer_pos = 0; _precount = 0; _position = 0; }
duke@435 193 char* as_string();
duke@435 194 };
duke@435 195
duke@435 196 class fileStream : public outputStream {
duke@435 197 protected:
duke@435 198 FILE* _file;
duke@435 199 bool _need_close;
duke@435 200 public:
minqi@2964 201 fileStream() { _file = NULL; _need_close = false; }
duke@435 202 fileStream(const char* file_name);
kamg@2515 203 fileStream(const char* file_name, const char* opentype);
vlivanov@5027 204 fileStream(FILE* file, bool need_close = false) { _file = file; _need_close = need_close; }
duke@435 205 ~fileStream();
duke@435 206 bool is_open() const { return _file != NULL; }
kamg@2515 207 void set_need_close(bool b) { _need_close = b;}
duke@435 208 virtual void write(const char* c, size_t len);
kamg@2515 209 size_t read(void *data, size_t size, size_t count) { return ::fread(data, size, count, _file); }
kamg@2515 210 char* readln(char *data, int count);
kamg@2515 211 int eof() { return feof(_file); }
kamg@2515 212 long fileSize();
kamg@2515 213 void rewind() { ::rewind(_file); }
duke@435 214 void flush();
duke@435 215 };
duke@435 216
iklam@7089 217 CDS_ONLY(extern fileStream* classlist_file;)
iklam@7089 218
duke@435 219 // unlike fileStream, fdStream does unbuffered I/O by calling
duke@435 220 // open() and write() directly. It is async-safe, but output
duke@435 221 // from multiple thread may be mixed together. Used by fatal
duke@435 222 // error handler.
duke@435 223 class fdStream : public outputStream {
duke@435 224 protected:
duke@435 225 int _fd;
duke@435 226 bool _need_close;
duke@435 227 public:
duke@435 228 fdStream(const char* file_name);
duke@435 229 fdStream(int fd = -1) { _fd = fd; _need_close = false; }
duke@435 230 ~fdStream();
duke@435 231 bool is_open() const { return _fd != -1; }
duke@435 232 void set_fd(int fd) { _fd = fd; _need_close = false; }
duke@435 233 int fd() const { return _fd; }
duke@435 234 virtual void write(const char* c, size_t len);
duke@435 235 void flush() {};
duke@435 236 };
duke@435 237
phh@9904 238 class Mutex;
minqi@5683 239 class gcLogFileStream : public fileStream {
minqi@2964 240 protected:
minqi@5683 241 const char* _file_name;
jwilhelm@4647 242 jlong _bytes_written;
minqi@5683 243 uintx _cur_file_num; // current logfile rotation number, from 0 to NumberOfGCLogFiles-1
phh@9904 244 private:
phh@9904 245 Mutex* _file_lock;
phh@9904 246 void rotate_log_impl(bool force, outputStream* out);
minqi@2964 247 public:
minqi@5683 248 gcLogFileStream(const char* file_name);
minqi@5683 249 ~gcLogFileStream();
minqi@2964 250 virtual void write(const char* c, size_t len);
minqi@6535 251 virtual void rotate_log(bool force, outputStream* out = NULL);
minqi@5683 252 void dump_loggc_header();
minqi@6535 253
minqi@6535 254 /* If "force" sets true, force log file rotation from outside JVM */
minqi@6535 255 bool should_rotate(bool force) {
minqi@6535 256 return force ||
minqi@6535 257 ((GCLogFileSize != 0) && ((uintx)_bytes_written >= GCLogFileSize));
minqi@6535 258 }
minqi@6535 259
minqi@2964 260 };
minqi@2964 261
minqi@5683 262 #ifndef PRODUCT
minqi@5683 263 // unit test for checking -Xloggc:<filename> parsing result
minqi@5683 264 void test_loggc_filename();
kevinw@9478 265 void test_snprintf();
minqi@5683 266 #endif
minqi@5683 267
duke@435 268 void ostream_init();
duke@435 269 void ostream_init_log();
duke@435 270 void ostream_exit();
duke@435 271 void ostream_abort();
duke@435 272
duke@435 273 // staticBufferStream uses a user-supplied buffer for all formatting.
duke@435 274 // Used for safe formatting during fatal error handling. Not MT safe.
duke@435 275 // Do not share the stream between multiple threads.
duke@435 276 class staticBufferStream : public outputStream {
duke@435 277 private:
duke@435 278 char* _buffer;
duke@435 279 size_t _buflen;
duke@435 280 outputStream* _outer_stream;
duke@435 281 public:
duke@435 282 staticBufferStream(char* buffer, size_t buflen,
duke@435 283 outputStream *outer_stream);
duke@435 284 ~staticBufferStream() {};
duke@435 285 virtual void write(const char* c, size_t len);
duke@435 286 void flush();
drchase@6680 287 void print(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
drchase@6680 288 void print_cr(const char* format, ...) ATTRIBUTE_PRINTF(2, 3);
drchase@6680 289 void vprint(const char *format, va_list argptr) ATTRIBUTE_PRINTF(2, 0);
drchase@6680 290 void vprint_cr(const char* format, va_list argptr) ATTRIBUTE_PRINTF(2, 0);
duke@435 291 };
duke@435 292
duke@435 293 // In the non-fixed buffer case an underlying buffer will be created and
duke@435 294 // managed in C heap. Not MT-safe.
duke@435 295 class bufferedStream : public outputStream {
duke@435 296 protected:
duke@435 297 char* buffer;
duke@435 298 size_t buffer_pos;
never@657 299 size_t buffer_max;
duke@435 300 size_t buffer_length;
duke@435 301 bool buffer_fixed;
duke@435 302 public:
never@657 303 bufferedStream(size_t initial_bufsize = 256, size_t bufmax = 1024*1024*10);
never@657 304 bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax = 1024*1024*10);
duke@435 305 ~bufferedStream();
duke@435 306 virtual void write(const char* c, size_t len);
duke@435 307 size_t size() { return buffer_pos; }
duke@435 308 const char* base() { return buffer; }
duke@435 309 void reset() { buffer_pos = 0; _precount = 0; _position = 0; }
duke@435 310 char* as_string();
duke@435 311 };
duke@435 312
duke@435 313 #define O_BUFLEN 2000 // max size of output of individual print() methods
duke@435 314
duke@435 315 #ifndef PRODUCT
duke@435 316
duke@435 317 class networkStream : public bufferedStream {
duke@435 318
duke@435 319 private:
duke@435 320 int _socket;
duke@435 321
duke@435 322 public:
duke@435 323 networkStream();
duke@435 324 ~networkStream();
duke@435 325
duke@435 326 bool connect(const char *host, short port);
duke@435 327 bool is_open() const { return _socket != -1; }
duke@435 328 int read(char *buf, size_t len);
duke@435 329 void close();
duke@435 330 virtual void flush();
duke@435 331 };
duke@435 332
duke@435 333 #endif
stefank@2314 334
stefank@2314 335 #endif // SHARE_VM_UTILITIES_OSTREAM_HPP

mercurial