src/share/vm/utilities/ostream.hpp

Fri, 10 Jun 2011 15:08:36 -0700

author
minqi
date
Fri, 10 Jun 2011 15:08:36 -0700
changeset 2964
2a241e764894
parent 2708
1d1603768966
child 3992
4ee06e614636
permissions
-rw-r--r--

6941923: RFE: Handling large log files produced by long running Java Applications
Summary: supply optinal flags to realize gc log rotation
Reviewed-by: ysr, jwilhelm

duke@435 1 /*
trims@2708 2 * Copyright (c) 1997, 2011, 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
duke@435 31 // Output streams for printing
duke@435 32 //
duke@435 33 // Printing guidelines:
duke@435 34 // Where possible, please use tty->print() and tty->print_cr().
duke@435 35 // For product mode VM warnings use warning() which internally uses tty.
duke@435 36 // In places where tty is not initialized yet or too much overhead,
duke@435 37 // we may use jio_printf:
duke@435 38 // jio_fprintf(defaultStream::output_stream(), "Message");
duke@435 39 // This allows for redirection via -XX:+DisplayVMOutputToStdout and
duke@435 40 // -XX:+DisplayVMOutputToStderr
duke@435 41 class outputStream : public ResourceObj {
duke@435 42 protected:
duke@435 43 int _indentation; // current indentation
duke@435 44 int _width; // width of the page
duke@435 45 int _position; // position on the current line
duke@435 46 int _newlines; // number of '\n' output so far
duke@435 47 julong _precount; // number of chars output, less _position
duke@435 48 TimeStamp _stamp; // for time stamps
duke@435 49
duke@435 50 void update_position(const char* s, size_t len);
duke@435 51 static const char* do_vsnprintf(char* buffer, size_t buflen,
duke@435 52 const char* format, va_list ap,
duke@435 53 bool add_cr,
duke@435 54 size_t& result_len);
duke@435 55
duke@435 56 public:
duke@435 57 // creation
duke@435 58 outputStream(int width = 80);
duke@435 59 outputStream(int width, bool has_time_stamps);
duke@435 60
duke@435 61 // indentation
duke@435 62 void indent();
duke@435 63 void inc() { _indentation++; };
duke@435 64 void dec() { _indentation--; };
duke@435 65 int indentation() const { return _indentation; }
duke@435 66 void set_indentation(int i) { _indentation = i; }
duke@435 67 void fill_to(int col);
jrose@535 68 void move_to(int col, int slop = 6, int min_space = 2);
duke@435 69
duke@435 70 // sizing
duke@435 71 int width() const { return _width; }
duke@435 72 int position() const { return _position; }
duke@435 73 int newlines() const { return _newlines; }
duke@435 74 julong count() const { return _precount + _position; }
duke@435 75 void set_count(julong count) { _precount = count - _position; }
duke@435 76 void set_position(int pos) { _position = pos; }
duke@435 77
duke@435 78 // printing
duke@435 79 void print(const char* format, ...);
duke@435 80 void print_cr(const char* format, ...);
duke@435 81 void vprint(const char *format, va_list argptr);
duke@435 82 void vprint_cr(const char* format, va_list argptr);
duke@435 83 void print_raw(const char* str) { write(str, strlen(str)); }
duke@435 84 void print_raw(const char* str, int len) { write(str, len); }
duke@435 85 void print_raw_cr(const char* str) { write(str, strlen(str)); cr(); }
duke@435 86 void print_raw_cr(const char* str, int len){ write(str, len); cr(); }
duke@435 87 void put(char ch);
jrose@535 88 void sp(int count = 1);
duke@435 89 void cr();
duke@435 90 void bol() { if (_position > 0) cr(); }
duke@435 91
duke@435 92 // Time stamp
duke@435 93 TimeStamp& time_stamp() { return _stamp; }
duke@435 94 void stamp();
ysr@777 95 void stamp(bool guard, const char* prefix, const char* suffix);
ysr@777 96 void stamp(bool guard) {
ysr@777 97 stamp(guard, "", ": ");
ysr@777 98 }
duke@435 99 // Date stamp
duke@435 100 void date_stamp(bool guard, const char* prefix, const char* suffix);
duke@435 101 // A simplified call that includes a suffix of ": "
duke@435 102 void date_stamp(bool guard) {
duke@435 103 date_stamp(guard, "", ": ");
duke@435 104 }
duke@435 105
duke@435 106 // portable printing of 64 bit integers
duke@435 107 void print_jlong(jlong value);
duke@435 108 void print_julong(julong value);
duke@435 109
duke@435 110 // flushing
duke@435 111 virtual void flush() {}
duke@435 112 virtual void write(const char* str, size_t len) = 0;
minqi@2964 113 virtual void rotate_log() {} // GC log rotation
minqi@2964 114 virtual ~outputStream() {} // close properly on deletion
duke@435 115
duke@435 116 void dec_cr() { dec(); cr(); }
duke@435 117 void inc_cr() { inc(); cr(); }
duke@435 118 };
duke@435 119
duke@435 120 // standard output
minqi@2964 121 // ANSI C++ name collision
duke@435 122 extern outputStream* tty; // tty output
duke@435 123 extern outputStream* gclog_or_tty; // stream for gc log if -Xloggc:<f>, or tty
duke@435 124
duke@435 125 // advisory locking for the shared tty stream:
duke@435 126 class ttyLocker: StackObj {
never@2570 127 friend class ttyUnlocker;
duke@435 128 private:
duke@435 129 intx _holder;
duke@435 130
duke@435 131 public:
duke@435 132 static intx hold_tty(); // returns a "holder" token
duke@435 133 static void release_tty(intx holder); // must witness same token
never@2570 134 static bool release_tty_if_locked(); // returns true if lock was released
duke@435 135 static void break_tty_lock_for_safepoint(intx holder);
duke@435 136
duke@435 137 ttyLocker() { _holder = hold_tty(); }
duke@435 138 ~ttyLocker() { release_tty(_holder); }
duke@435 139 };
duke@435 140
never@2570 141 // Release the tty lock if it's held and reacquire it if it was
never@2570 142 // locked. Used to avoid lock ordering problems.
never@2570 143 class ttyUnlocker: StackObj {
never@2570 144 private:
never@2570 145 bool _was_locked;
never@2570 146 public:
never@2570 147 ttyUnlocker() {
never@2570 148 _was_locked = ttyLocker::release_tty_if_locked();
never@2570 149 }
never@2570 150 ~ttyUnlocker() {
never@2570 151 if (_was_locked) {
never@2570 152 ttyLocker::hold_tty();
never@2570 153 }
never@2570 154 }
never@2570 155 };
never@2570 156
duke@435 157 // for writing to strings; buffer will expand automatically
duke@435 158 class stringStream : public outputStream {
duke@435 159 protected:
duke@435 160 char* buffer;
duke@435 161 size_t buffer_pos;
duke@435 162 size_t buffer_length;
duke@435 163 bool buffer_fixed;
duke@435 164 public:
duke@435 165 stringStream(size_t initial_bufsize = 256);
duke@435 166 stringStream(char* fixed_buffer, size_t fixed_buffer_size);
duke@435 167 ~stringStream();
duke@435 168 virtual void write(const char* c, size_t len);
duke@435 169 size_t size() { return buffer_pos; }
duke@435 170 const char* base() { return buffer; }
duke@435 171 void reset() { buffer_pos = 0; _precount = 0; _position = 0; }
duke@435 172 char* as_string();
duke@435 173 };
duke@435 174
duke@435 175 class fileStream : public outputStream {
duke@435 176 protected:
duke@435 177 FILE* _file;
duke@435 178 bool _need_close;
duke@435 179 public:
minqi@2964 180 fileStream() { _file = NULL; _need_close = false; }
duke@435 181 fileStream(const char* file_name);
kamg@2515 182 fileStream(const char* file_name, const char* opentype);
duke@435 183 fileStream(FILE* file) { _file = file; _need_close = false; }
duke@435 184 ~fileStream();
duke@435 185 bool is_open() const { return _file != NULL; }
kamg@2515 186 void set_need_close(bool b) { _need_close = b;}
duke@435 187 virtual void write(const char* c, size_t len);
kamg@2515 188 size_t read(void *data, size_t size, size_t count) { return ::fread(data, size, count, _file); }
kamg@2515 189 char* readln(char *data, int count);
kamg@2515 190 int eof() { return feof(_file); }
kamg@2515 191 long fileSize();
kamg@2515 192 void rewind() { ::rewind(_file); }
duke@435 193 void flush();
duke@435 194 };
duke@435 195
duke@435 196 // unlike fileStream, fdStream does unbuffered I/O by calling
duke@435 197 // open() and write() directly. It is async-safe, but output
duke@435 198 // from multiple thread may be mixed together. Used by fatal
duke@435 199 // error handler.
duke@435 200 class fdStream : public outputStream {
duke@435 201 protected:
duke@435 202 int _fd;
duke@435 203 bool _need_close;
duke@435 204 public:
duke@435 205 fdStream(const char* file_name);
duke@435 206 fdStream(int fd = -1) { _fd = fd; _need_close = false; }
duke@435 207 ~fdStream();
duke@435 208 bool is_open() const { return _fd != -1; }
duke@435 209 void set_fd(int fd) { _fd = fd; _need_close = false; }
duke@435 210 int fd() const { return _fd; }
duke@435 211 virtual void write(const char* c, size_t len);
duke@435 212 void flush() {};
duke@435 213 };
duke@435 214
minqi@2964 215 class rotatingFileStream : public fileStream {
minqi@2964 216 protected:
minqi@2964 217 char* _file_name;
minqi@2964 218 jlong _bytes_writen;
minqi@2964 219 uintx _cur_file_num; // current logfile rotation number, from 0 to MaxGCLogFileNumbers-1
minqi@2964 220 public:
minqi@2964 221 rotatingFileStream(const char* file_name);
minqi@2964 222 rotatingFileStream(const char* file_name, const char* opentype);
minqi@2964 223 rotatingFileStream(FILE* file) : fileStream(file) {}
minqi@2964 224 ~rotatingFileStream();
minqi@2964 225 virtual void write(const char* c, size_t len);
minqi@2964 226 virtual void rotate_log();
minqi@2964 227 };
minqi@2964 228
duke@435 229 void ostream_init();
duke@435 230 void ostream_init_log();
duke@435 231 void ostream_exit();
duke@435 232 void ostream_abort();
duke@435 233
duke@435 234 // staticBufferStream uses a user-supplied buffer for all formatting.
duke@435 235 // Used for safe formatting during fatal error handling. Not MT safe.
duke@435 236 // Do not share the stream between multiple threads.
duke@435 237 class staticBufferStream : public outputStream {
duke@435 238 private:
duke@435 239 char* _buffer;
duke@435 240 size_t _buflen;
duke@435 241 outputStream* _outer_stream;
duke@435 242 public:
duke@435 243 staticBufferStream(char* buffer, size_t buflen,
duke@435 244 outputStream *outer_stream);
duke@435 245 ~staticBufferStream() {};
duke@435 246 virtual void write(const char* c, size_t len);
duke@435 247 void flush();
duke@435 248 void print(const char* format, ...);
duke@435 249 void print_cr(const char* format, ...);
duke@435 250 void vprint(const char *format, va_list argptr);
duke@435 251 void vprint_cr(const char* format, va_list argptr);
duke@435 252 };
duke@435 253
duke@435 254 // In the non-fixed buffer case an underlying buffer will be created and
duke@435 255 // managed in C heap. Not MT-safe.
duke@435 256 class bufferedStream : public outputStream {
duke@435 257 protected:
duke@435 258 char* buffer;
duke@435 259 size_t buffer_pos;
never@657 260 size_t buffer_max;
duke@435 261 size_t buffer_length;
duke@435 262 bool buffer_fixed;
duke@435 263 public:
never@657 264 bufferedStream(size_t initial_bufsize = 256, size_t bufmax = 1024*1024*10);
never@657 265 bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax = 1024*1024*10);
duke@435 266 ~bufferedStream();
duke@435 267 virtual void write(const char* c, size_t len);
duke@435 268 size_t size() { return buffer_pos; }
duke@435 269 const char* base() { return buffer; }
duke@435 270 void reset() { buffer_pos = 0; _precount = 0; _position = 0; }
duke@435 271 char* as_string();
duke@435 272 };
duke@435 273
duke@435 274 #define O_BUFLEN 2000 // max size of output of individual print() methods
duke@435 275
duke@435 276 #ifndef PRODUCT
duke@435 277
duke@435 278 class networkStream : public bufferedStream {
duke@435 279
duke@435 280 private:
duke@435 281 int _socket;
duke@435 282
duke@435 283 public:
duke@435 284 networkStream();
duke@435 285 ~networkStream();
duke@435 286
duke@435 287 bool connect(const char *host, short port);
duke@435 288 bool is_open() const { return _socket != -1; }
duke@435 289 int read(char *buf, size_t len);
duke@435 290 void close();
duke@435 291 virtual void flush();
duke@435 292 };
duke@435 293
duke@435 294 #endif
stefank@2314 295
stefank@2314 296 #endif // SHARE_VM_UTILITIES_OSTREAM_HPP

mercurial