< prev index next >

src/share/vm/utilities/ostream.hpp

Print this page




  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  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  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 
  31 class GCId;
  32 DEBUG_ONLY(class ResourceMark;)
  33 
  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


 218 
 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 



 238 class gcLogFileStream : public fileStream {
 239  protected:
 240   const char*  _file_name;
 241   jlong  _bytes_written;
 242   uintx  _cur_file_num;             // current logfile rotation number, from 0 to NumberOfGCLogFiles-1





 243  public:
 244   gcLogFileStream(const char* file_name);
 245   ~gcLogFileStream();

 246   virtual void write(const char* c, size_t len);


 247   virtual void rotate_log(bool force, outputStream* out = NULL);

 248   void dump_loggc_header();
 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 
 256 };
 257 
















 258 #ifndef PRODUCT
 259 // unit test for checking -Xloggc:<filename> parsing result
 260 void test_loggc_filename();
 261 void test_snprintf();
 262 #endif
 263 
 264 void ostream_init();
 265 void ostream_init_log();
 266 void ostream_exit();
 267 void ostream_abort();
 268 
 269 // staticBufferStream uses a user-supplied buffer for all formatting.
 270 // Used for safe formatting during fatal error handling.  Not MT safe.
 271 // Do not share the stream between multiple threads.
 272 class staticBufferStream : public outputStream {
 273  private:
 274   char* _buffer;
 275   size_t _buflen;
 276   outputStream* _outer_stream;
 277  public:




  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  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  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 #include "runtime/task.hpp"
  31 
  32 class GCId;
  33 DEBUG_ONLY(class ResourceMark;)
  34 
  35 // Output streams for printing
  36 //
  37 // Printing guidelines:
  38 // Where possible, please use tty->print() and tty->print_cr().
  39 // For product mode VM warnings use warning() which internally uses tty.
  40 // In places where tty is not initialized yet or too much overhead,
  41 // we may use jio_printf:
  42 //     jio_fprintf(defaultStream::output_stream(), "Message");
  43 // This allows for redirection via -XX:+DisplayVMOutputToStdout and
  44 // -XX:+DisplayVMOutputToStderr
  45 class outputStream : public ResourceObj {
  46  protected:
  47    int _indentation; // current indentation
  48    int _width;       // width of the page
  49    int _position;    // position on the current line
  50    int _newlines;    // number of '\n' output so far


 219 
 220 // unlike fileStream, fdStream does unbuffered I/O by calling
 221 // open() and write() directly. It is async-safe, but output
 222 // from multiple thread may be mixed together. Used by fatal
 223 // error handler.
 224 class fdStream : public outputStream {
 225  protected:
 226   int  _fd;
 227   bool _need_close;
 228  public:
 229   fdStream(const char* file_name);
 230   fdStream(int fd = -1) { _fd = fd; _need_close = false; }
 231   ~fdStream();
 232   bool is_open() const { return _fd != -1; }
 233   void set_fd(int fd) { _fd = fd; _need_close = false; }
 234   int fd() const { return _fd; }
 235   virtual void write(const char* c, size_t len);
 236   void flush() {};
 237 };
 238 
 239 class Mutex;
 240 class GCLogEvent;
 241 
 242 class gcLogFileStream : public fileStream {
 243  protected:
 244   const char*  _file_name;
 245   jlong  _bytes_written;
 246   uintx  _cur_file_num;             // current logfile rotation number, from 0 to NumberOfGCLogFiles-1
 247  private:
 248   GCLogEvent* _reader;
 249   GCLogEvent* _writer;
 250   Mutex* _lock;
 251 
 252  public:
 253   gcLogFileStream(const char* file_name);
 254   ~gcLogFileStream();
 255   // write asynchronously
 256   virtual void write(const char* c, size_t len);
 257   // really write to file system.
 258   void write_blocking(const char* c, size_t len);
 259   virtual void rotate_log(bool force, outputStream* out = NULL);
 260   void flush_log();
 261   void dump_loggc_header();
 262 
 263   /* If "force" sets true, force log file rotation from outside JVM */
 264   bool should_rotate(bool force) {
 265     return force ||
 266              ((GCLogFileSize != 0) && ((uintx)_bytes_written >= GCLogFileSize));
 267   }
 268 
 269 };
 270 
 271 class GCLogFlusher: public PeriodicTask {
 272   gcLogFileStream * _ostream;
 273   elapsedTimer _accumulatedTime;
 274 public:
 275   GCLogFlusher(int interval, gcLogFileStream* ostream):PeriodicTask(interval), _ostream(ostream) {}
 276 
 277   virtual void task() {
 278     TraceTime t("GCLogFlusher", &_accumulatedTime, true, PrintGCLogFlushTime);
 279     _ostream->flush_log();
 280   }
 281 
 282   elapsedTimer getAccumulatedTime() {
 283     return _accumulatedTime;
 284   }
 285 };
 286 
 287 #ifndef PRODUCT
 288 // unit test for checking -Xloggc:<filename> parsing result
 289 void test_loggc_filename();
 290 void test_snprintf();
 291 #endif
 292 
 293 void ostream_init();
 294 void ostream_init_log();
 295 void ostream_exit();
 296 void ostream_abort();
 297 
 298 // staticBufferStream uses a user-supplied buffer for all formatting.
 299 // Used for safe formatting during fatal error handling.  Not MT safe.
 300 // Do not share the stream between multiple threads.
 301 class staticBufferStream : public outputStream {
 302  private:
 303   char* _buffer;
 304   size_t _buflen;
 305   outputStream* _outer_stream;
 306  public:


< prev index next >