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