Mercurial > hg > truffle
annotate src/share/vm/utilities/ostream.hpp @ 21244:bf5e055dbc9c
SSAUtils: minor refactoring.
author | Josef Eisl <josef.eisl@jku.at> |
---|---|
date | Thu, 30 Apr 2015 15:36:03 +0200 |
parents | 7848fc12602b |
children |
rev | line source |
---|---|
0 | 1 /* |
17937
78bbf4d43a14
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
17827
diff
changeset
|
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
0 | 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 * | |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
356
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
356
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
356
diff
changeset
|
21 * questions. |
0 | 22 * |
23 */ | |
24 | |
1972 | 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 | |
20190
0982ec23da03
8043607: Add a GC id as a log decoration similar to PrintGCTimeStamps
brutisso
parents:
17937
diff
changeset
|
31 class GCId; |
11136
dbc0b5dc08f5
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
10195
diff
changeset
|
32 DEBUG_ONLY(class ResourceMark;) |
dbc0b5dc08f5
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
10195
diff
changeset
|
33 |
0 | 34 // Output streams for printing |
35 // | |
36 // Printing guidelines: | |
37 // Where possible, please use tty->print() and tty->print_cr(). | |
38 // For product mode VM warnings use warning() which internally uses tty. | |
39 // In places where tty is not initialized yet or too much overhead, | |
40 // we may use jio_printf: | |
41 // jio_fprintf(defaultStream::output_stream(), "Message"); | |
42 // This allows for redirection via -XX:+DisplayVMOutputToStdout and | |
43 // -XX:+DisplayVMOutputToStderr | |
44 class outputStream : public ResourceObj { | |
45 protected: | |
46 int _indentation; // current indentation | |
47 int _width; // width of the page | |
48 int _position; // position on the current line | |
49 int _newlines; // number of '\n' output so far | |
50 julong _precount; // number of chars output, less _position | |
51 TimeStamp _stamp; // for time stamps | |
52 | |
53 void update_position(const char* s, size_t len); | |
54 static const char* do_vsnprintf(char* buffer, size_t buflen, | |
55 const char* format, va_list ap, | |
56 bool add_cr, | |
17937
78bbf4d43a14
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
17827
diff
changeset
|
57 size_t& result_len) ATTRIBUTE_PRINTF(3, 0); |
0 | 58 |
59 public: | |
60 // creation | |
61 outputStream(int width = 80); | |
62 outputStream(int width, bool has_time_stamps); | |
63 | |
64 // indentation | |
6605 | 65 outputStream& indent(); |
0 | 66 void inc() { _indentation++; }; |
67 void dec() { _indentation--; }; | |
6605 | 68 void inc(int n) { _indentation += n; }; |
69 void dec(int n) { _indentation -= n; }; | |
0 | 70 int indentation() const { return _indentation; } |
71 void set_indentation(int i) { _indentation = i; } | |
72 void fill_to(int col); | |
100
c7c777385a15
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
0
diff
changeset
|
73 void move_to(int col, int slop = 6, int min_space = 2); |
0 | 74 |
75 // sizing | |
76 int width() const { return _width; } | |
77 int position() const { return _position; } | |
78 int newlines() const { return _newlines; } | |
79 julong count() const { return _precount + _position; } | |
80 void set_count(julong count) { _precount = count - _position; } | |
81 void set_position(int pos) { _position = pos; } | |
82 | |
83 // printing | |
17937
78bbf4d43a14
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
17827
diff
changeset
|
84 void print(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); |
78bbf4d43a14
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
17827
diff
changeset
|
85 void print_cr(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); |
78bbf4d43a14
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
17827
diff
changeset
|
86 void vprint(const char *format, va_list argptr) ATTRIBUTE_PRINTF(2, 0); |
78bbf4d43a14
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
17827
diff
changeset
|
87 void vprint_cr(const char* format, va_list argptr) ATTRIBUTE_PRINTF(2, 0); |
0 | 88 void print_raw(const char* str) { write(str, strlen(str)); } |
89 void print_raw(const char* str, int len) { write(str, len); } | |
90 void print_raw_cr(const char* str) { write(str, strlen(str)); cr(); } | |
91 void print_raw_cr(const char* str, int len){ write(str, len); cr(); } | |
6605 | 92 void print_data(void* data, size_t len, bool with_ascii); |
0 | 93 void put(char ch); |
100
c7c777385a15
6667042: PrintAssembly option does not work without special plugin
jrose
parents:
0
diff
changeset
|
94 void sp(int count = 1); |
0 | 95 void cr(); |
96 void bol() { if (_position > 0) cr(); } | |
97 | |
98 // Time stamp | |
99 TimeStamp& time_stamp() { return _stamp; } | |
100 void stamp(); | |
342
37f87013dfd8
6711316: Open source the Garbage-First garbage collector
ysr
parents:
100
diff
changeset
|
101 void stamp(bool guard, const char* prefix, const char* suffix); |
37f87013dfd8
6711316: Open source the Garbage-First garbage collector
ysr
parents:
100
diff
changeset
|
102 void stamp(bool guard) { |
37f87013dfd8
6711316: Open source the Garbage-First garbage collector
ysr
parents:
100
diff
changeset
|
103 stamp(guard, "", ": "); |
37f87013dfd8
6711316: Open source the Garbage-First garbage collector
ysr
parents:
100
diff
changeset
|
104 } |
0 | 105 // Date stamp |
106 void date_stamp(bool guard, const char* prefix, const char* suffix); | |
107 // A simplified call that includes a suffix of ": " | |
108 void date_stamp(bool guard) { | |
109 date_stamp(guard, "", ": "); | |
110 } | |
20190
0982ec23da03
8043607: Add a GC id as a log decoration similar to PrintGCTimeStamps
brutisso
parents:
17937
diff
changeset
|
111 void gclog_stamp(const GCId& gc_id); |
0 | 112 |
113 // portable printing of 64 bit integers | |
114 void print_jlong(jlong value); | |
115 void print_julong(julong value); | |
116 | |
117 // flushing | |
118 virtual void flush() {} | |
119 virtual void write(const char* str, size_t len) = 0; | |
17827 | 120 virtual void rotate_log(bool force, outputStream* out = NULL) {} // GC log rotation |
3767
2a241e764894
6941923: RFE: Handling large log files produced by long running Java Applications
minqi
parents:
2426
diff
changeset
|
121 virtual ~outputStream() {} // close properly on deletion |
0 | 122 |
123 void dec_cr() { dec(); cr(); } | |
124 void inc_cr() { inc(); cr(); } | |
125 }; | |
126 | |
127 // standard output | |
3767
2a241e764894
6941923: RFE: Handling large log files produced by long running Java Applications
minqi
parents:
2426
diff
changeset
|
128 // ANSI C++ name collision |
0 | 129 extern outputStream* tty; // tty output |
130 extern outputStream* gclog_or_tty; // stream for gc log if -Xloggc:<f>, or tty | |
131 | |
6605 | 132 class streamIndentor : public StackObj { |
133 private: | |
134 outputStream* _str; | |
135 int _amount; | |
136 | |
137 public: | |
138 streamIndentor(outputStream* str, int amt = 2) : _str(str), _amount(amt) { | |
139 _str->inc(_amount); | |
140 } | |
141 ~streamIndentor() { _str->dec(_amount); } | |
142 }; | |
143 | |
144 | |
0 | 145 // advisory locking for the shared tty stream: |
146 class ttyLocker: StackObj { | |
2263
5841dc1964f0
7021531: lock ordering problems after fix for 6354181
never
parents:
2199
diff
changeset
|
147 friend class ttyUnlocker; |
0 | 148 private: |
149 intx _holder; | |
150 | |
151 public: | |
152 static intx hold_tty(); // returns a "holder" token | |
153 static void release_tty(intx holder); // must witness same token | |
2263
5841dc1964f0
7021531: lock ordering problems after fix for 6354181
never
parents:
2199
diff
changeset
|
154 static bool release_tty_if_locked(); // returns true if lock was released |
0 | 155 static void break_tty_lock_for_safepoint(intx holder); |
156 | |
157 ttyLocker() { _holder = hold_tty(); } | |
158 ~ttyLocker() { release_tty(_holder); } | |
159 }; | |
160 | |
2263
5841dc1964f0
7021531: lock ordering problems after fix for 6354181
never
parents:
2199
diff
changeset
|
161 // Release the tty lock if it's held and reacquire it if it was |
5841dc1964f0
7021531: lock ordering problems after fix for 6354181
never
parents:
2199
diff
changeset
|
162 // locked. Used to avoid lock ordering problems. |
5841dc1964f0
7021531: lock ordering problems after fix for 6354181
never
parents:
2199
diff
changeset
|
163 class ttyUnlocker: StackObj { |
5841dc1964f0
7021531: lock ordering problems after fix for 6354181
never
parents:
2199
diff
changeset
|
164 private: |
5841dc1964f0
7021531: lock ordering problems after fix for 6354181
never
parents:
2199
diff
changeset
|
165 bool _was_locked; |
5841dc1964f0
7021531: lock ordering problems after fix for 6354181
never
parents:
2199
diff
changeset
|
166 public: |
5841dc1964f0
7021531: lock ordering problems after fix for 6354181
never
parents:
2199
diff
changeset
|
167 ttyUnlocker() { |
5841dc1964f0
7021531: lock ordering problems after fix for 6354181
never
parents:
2199
diff
changeset
|
168 _was_locked = ttyLocker::release_tty_if_locked(); |
5841dc1964f0
7021531: lock ordering problems after fix for 6354181
never
parents:
2199
diff
changeset
|
169 } |
5841dc1964f0
7021531: lock ordering problems after fix for 6354181
never
parents:
2199
diff
changeset
|
170 ~ttyUnlocker() { |
5841dc1964f0
7021531: lock ordering problems after fix for 6354181
never
parents:
2199
diff
changeset
|
171 if (_was_locked) { |
5841dc1964f0
7021531: lock ordering problems after fix for 6354181
never
parents:
2199
diff
changeset
|
172 ttyLocker::hold_tty(); |
5841dc1964f0
7021531: lock ordering problems after fix for 6354181
never
parents:
2199
diff
changeset
|
173 } |
5841dc1964f0
7021531: lock ordering problems after fix for 6354181
never
parents:
2199
diff
changeset
|
174 } |
5841dc1964f0
7021531: lock ordering problems after fix for 6354181
never
parents:
2199
diff
changeset
|
175 }; |
5841dc1964f0
7021531: lock ordering problems after fix for 6354181
never
parents:
2199
diff
changeset
|
176 |
0 | 177 // for writing to strings; buffer will expand automatically |
178 class stringStream : public outputStream { | |
179 protected: | |
180 char* buffer; | |
181 size_t buffer_pos; | |
182 size_t buffer_length; | |
183 bool buffer_fixed; | |
11136
dbc0b5dc08f5
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
10195
diff
changeset
|
184 DEBUG_ONLY(ResourceMark* rm;) |
0 | 185 public: |
186 stringStream(size_t initial_bufsize = 256); | |
187 stringStream(char* fixed_buffer, size_t fixed_buffer_size); | |
188 ~stringStream(); | |
189 virtual void write(const char* c, size_t len); | |
190 size_t size() { return buffer_pos; } | |
191 const char* base() { return buffer; } | |
192 void reset() { buffer_pos = 0; _precount = 0; _position = 0; } | |
193 char* as_string(); | |
194 }; | |
195 | |
196 class fileStream : public outputStream { | |
197 protected: | |
198 FILE* _file; | |
199 bool _need_close; | |
200 public: | |
3767
2a241e764894
6941923: RFE: Handling large log files produced by long running Java Applications
minqi
parents:
2426
diff
changeset
|
201 fileStream() { _file = NULL; _need_close = false; } |
0 | 202 fileStream(const char* file_name); |
2199
d8a72fbc4be7
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
1972
diff
changeset
|
203 fileStream(const char* file_name, const char* opentype); |
10195
e12c9b3740db
8012260: ciReplay: Include PID into the name of replay data file
vlivanov
parents:
8108
diff
changeset
|
204 fileStream(FILE* file, bool need_close = false) { _file = file; _need_close = need_close; } |
0 | 205 ~fileStream(); |
206 bool is_open() const { return _file != NULL; } | |
2199
d8a72fbc4be7
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
1972
diff
changeset
|
207 void set_need_close(bool b) { _need_close = b;} |
0 | 208 virtual void write(const char* c, size_t len); |
2199
d8a72fbc4be7
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
1972
diff
changeset
|
209 size_t read(void *data, size_t size, size_t count) { return ::fread(data, size, count, _file); } |
d8a72fbc4be7
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
1972
diff
changeset
|
210 char* readln(char *data, int count); |
d8a72fbc4be7
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
1972
diff
changeset
|
211 int eof() { return feof(_file); } |
d8a72fbc4be7
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
1972
diff
changeset
|
212 long fileSize(); |
d8a72fbc4be7
7003401: Implement VM error-reporting functionality on erroneous termination
kamg
parents:
1972
diff
changeset
|
213 void rewind() { ::rewind(_file); } |
0 | 214 void flush(); |
215 }; | |
216 | |
20375
6e0cb14ce59b
8046070: Class Data Sharing clean up and refactoring
iklam
parents:
20190
diff
changeset
|
217 CDS_ONLY(extern fileStream* classlist_file;) |
6e0cb14ce59b
8046070: Class Data Sharing clean up and refactoring
iklam
parents:
20190
diff
changeset
|
218 |
0 | 219 // unlike fileStream, fdStream does unbuffered I/O by calling |
220 // open() and write() directly. It is async-safe, but output | |
221 // from multiple thread may be mixed together. Used by fatal | |
222 // error handler. | |
223 class fdStream : public outputStream { | |
224 protected: | |
225 int _fd; | |
226 bool _need_close; | |
227 public: | |
228 fdStream(const char* file_name); | |
229 fdStream(int fd = -1) { _fd = fd; _need_close = false; } | |
230 ~fdStream(); | |
231 bool is_open() const { return _fd != -1; } | |
232 void set_fd(int fd) { _fd = fd; _need_close = false; } | |
233 int fd() const { return _fd; } | |
234 virtual void write(const char* c, size_t len); | |
235 void flush() {}; | |
236 }; | |
237 | |
12215 | 238 class gcLogFileStream : public fileStream { |
3767
2a241e764894
6941923: RFE: Handling large log files produced by long running Java Applications
minqi
parents:
2426
diff
changeset
|
239 protected: |
12215 | 240 const char* _file_name; |
8108
0598674c0056
8008314: Unimplemented() Atomic::load breaks the applications
jwilhelm
parents:
6605
diff
changeset
|
241 jlong _bytes_written; |
12215 | 242 uintx _cur_file_num; // current logfile rotation number, from 0 to NumberOfGCLogFiles-1 |
3767
2a241e764894
6941923: RFE: Handling large log files produced by long running Java Applications
minqi
parents:
2426
diff
changeset
|
243 public: |
12215 | 244 gcLogFileStream(const char* file_name); |
245 ~gcLogFileStream(); | |
3767
2a241e764894
6941923: RFE: Handling large log files produced by long running Java Applications
minqi
parents:
2426
diff
changeset
|
246 virtual void write(const char* c, size_t len); |
17827 | 247 virtual void rotate_log(bool force, outputStream* out = NULL); |
12215 | 248 void dump_loggc_header(); |
17827 | 249 |
250 /* If "force" sets true, force log file rotation from outside JVM */ | |
251 bool should_rotate(bool force) { | |
252 return force || | |
253 ((GCLogFileSize != 0) && ((uintx)_bytes_written >= GCLogFileSize)); | |
254 } | |
255 | |
3767
2a241e764894
6941923: RFE: Handling large log files produced by long running Java Applications
minqi
parents:
2426
diff
changeset
|
256 }; |
2a241e764894
6941923: RFE: Handling large log files produced by long running Java Applications
minqi
parents:
2426
diff
changeset
|
257 |
12215 | 258 #ifndef PRODUCT |
259 // unit test for checking -Xloggc:<filename> parsing result | |
260 void test_loggc_filename(); | |
261 #endif | |
262 | |
0 | 263 void ostream_init(); |
264 void ostream_init_log(); | |
265 void ostream_exit(); | |
266 void ostream_abort(); | |
267 | |
268 // staticBufferStream uses a user-supplied buffer for all formatting. | |
269 // Used for safe formatting during fatal error handling. Not MT safe. | |
270 // Do not share the stream between multiple threads. | |
271 class staticBufferStream : public outputStream { | |
272 private: | |
273 char* _buffer; | |
274 size_t _buflen; | |
275 outputStream* _outer_stream; | |
276 public: | |
277 staticBufferStream(char* buffer, size_t buflen, | |
278 outputStream *outer_stream); | |
279 ~staticBufferStream() {}; | |
280 virtual void write(const char* c, size_t len); | |
281 void flush(); | |
17937
78bbf4d43a14
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
17827
diff
changeset
|
282 void print(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); |
78bbf4d43a14
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
17827
diff
changeset
|
283 void print_cr(const char* format, ...) ATTRIBUTE_PRINTF(2, 3); |
78bbf4d43a14
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
17827
diff
changeset
|
284 void vprint(const char *format, va_list argptr) ATTRIBUTE_PRINTF(2, 0); |
78bbf4d43a14
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
17827
diff
changeset
|
285 void vprint_cr(const char* format, va_list argptr) ATTRIBUTE_PRINTF(2, 0); |
0 | 286 }; |
287 | |
288 // In the non-fixed buffer case an underlying buffer will be created and | |
289 // managed in C heap. Not MT-safe. | |
290 class bufferedStream : public outputStream { | |
291 protected: | |
292 char* buffer; | |
293 size_t buffer_pos; | |
222 | 294 size_t buffer_max; |
0 | 295 size_t buffer_length; |
296 bool buffer_fixed; | |
297 public: | |
222 | 298 bufferedStream(size_t initial_bufsize = 256, size_t bufmax = 1024*1024*10); |
299 bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax = 1024*1024*10); | |
0 | 300 ~bufferedStream(); |
301 virtual void write(const char* c, size_t len); | |
302 size_t size() { return buffer_pos; } | |
303 const char* base() { return buffer; } | |
304 void reset() { buffer_pos = 0; _precount = 0; _position = 0; } | |
305 char* as_string(); | |
306 }; | |
307 | |
308 #define O_BUFLEN 2000 // max size of output of individual print() methods | |
309 | |
310 #ifndef PRODUCT | |
311 | |
312 class networkStream : public bufferedStream { | |
313 | |
314 private: | |
315 int _socket; | |
316 | |
317 public: | |
318 networkStream(); | |
319 ~networkStream(); | |
320 | |
321 bool connect(const char *host, short port); | |
322 bool is_open() const { return _socket != -1; } | |
323 int read(char *buf, size_t len); | |
324 void close(); | |
325 virtual void flush(); | |
326 }; | |
327 | |
328 #endif | |
1972 | 329 |
330 #endif // SHARE_VM_UTILITIES_OSTREAM_HPP |