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