src/share/vm/utilities/ostream.cpp

Print this page




 645     _need_close = false;
 646   }
 647 }
 648 
 649 void gcLogFileStream::write(const char* s, size_t len) {
 650   if (_file != NULL) {
 651     size_t count = fwrite(s, 1, len, _file);
 652     _bytes_written += count;
 653   }
 654   update_position(s, len);
 655 }
 656 
 657 // rotate_log must be called from VMThread at safepoint. In case need change parameters
 658 // for gc log rotation from thread other than VMThread, a sub type of VM_Operation
 659 // should be created and be submitted to VMThread's operation queue. DO NOT call this
 660 // function directly. Currently, it is safe to rotate log at safepoint through VMThread.
 661 // That is, no mutator threads and concurrent GC threads run parallel with VMThread to
 662 // write to gc log file at safepoint. If in future, changes made for mutator threads or
 663 // concurrent GC threads to run parallel with VMThread at safepoint, write and rotate_log
 664 // must be synchronized.
 665 void gcLogFileStream::rotate_log() {
 666   char time_msg[FILENAMEBUFLEN];
 667   char time_str[EXTRACHARLEN];
 668   char current_file_name[FILENAMEBUFLEN];
 669   char renamed_file_name[FILENAMEBUFLEN];
 670 
 671   if (_bytes_written < (jlong)GCLogFileSize) {
 672     return;
 673   }
 674 
 675 #ifdef ASSERT
 676   Thread *thread = Thread::current();
 677   assert(thread == NULL ||
 678          (thread->is_VM_thread() && SafepointSynchronize::is_at_safepoint()),
 679          "Must be VMThread at safepoint");
 680 #endif
 681   if (NumberOfGCLogFiles == 1) {
 682     // rotate in same file
 683     rewind();
 684     _bytes_written = 0L;
 685     jio_snprintf(time_msg, sizeof(time_msg), "File  %s rotated at %s\n",
 686                  _file_name, os::local_time_string((char *)time_str, sizeof(time_str)));
 687     write(time_msg, strlen(time_msg));





 688     dump_loggc_header();
 689     return;
 690   }
 691 
 692 #if defined(_WINDOWS)
 693 #ifndef F_OK
 694 #define F_OK 0
 695 #endif
 696 #endif // _WINDOWS
 697 
 698   // rotate file in names extended_filename.0, extended_filename.1, ...,
 699   // extended_filename.<NumberOfGCLogFiles - 1>. Current rotation file name will
 700   // have a form of extended_filename.<i>.current where i is the current rotation
 701   // file number. After it reaches max file size, the file will be saved and renamed
 702   // with .current removed from its tail.
 703   size_t filename_len = strlen(_file_name);
 704   if (_file != NULL) {
 705     jio_snprintf(renamed_file_name, filename_len + EXTRACHARLEN, "%s.%d",
 706                  _file_name, _cur_file_num);
 707     jio_snprintf(current_file_name, filename_len + EXTRACHARLEN, "%s.%d" CURRENTAPPX,
 708                  _file_name, _cur_file_num);
 709     jio_snprintf(time_msg, sizeof(time_msg), "%s GC log file has reached the"
 710                            " maximum size. Saved as %s\n",


 711                            os::local_time_string((char *)time_str, sizeof(time_str)),
 712                            renamed_file_name);
 713     write(time_msg, strlen(time_msg));
 714 




 715     fclose(_file);
 716     _file = NULL;
 717 
 718     bool can_rename = true;
 719     if (access(current_file_name, F_OK) != 0) {
 720       // current file does not exist?
 721       warning("No source file exists, cannot rename\n");
 722       can_rename = false;
 723     }
 724     if (can_rename) {
 725       if (access(renamed_file_name, F_OK) == 0) {
 726         if (remove(renamed_file_name) != 0) {
 727           warning("Could not delete existing file %s\n", renamed_file_name);
 728           can_rename = false;
 729         }
 730       } else {
 731         // file does not exist, ok to rename
 732       }
 733     }
 734     if (can_rename && rename(current_file_name, renamed_file_name) != 0) {
 735       warning("Could not rename %s to %s\n", _file_name, renamed_file_name);
 736     }
 737   }
 738 
 739   _cur_file_num++;
 740   if (_cur_file_num > NumberOfGCLogFiles - 1) _cur_file_num = 0;
 741   jio_snprintf(current_file_name,  filename_len + EXTRACHARLEN, "%s.%d" CURRENTAPPX,
 742                _file_name, _cur_file_num);
 743   _file = fopen(current_file_name, "w");
 744 
 745   if (_file != NULL) {
 746     _bytes_written = 0L;
 747     _need_close = true;
 748     // reuse current_file_name for time_msg
 749     jio_snprintf(current_file_name, filename_len + EXTRACHARLEN,
 750                  "%s.%d", _file_name, _cur_file_num);
 751     jio_snprintf(time_msg, sizeof(time_msg), "%s GC log file created %s\n",
 752                            os::local_time_string((char *)time_str, sizeof(time_str)),
 753                            current_file_name);
 754     write(time_msg, strlen(time_msg));





 755     dump_loggc_header();
 756     // remove the existing file
 757     if (access(current_file_name, F_OK) == 0) {
 758       if (remove(current_file_name) != 0) {
 759         warning("Could not delete existing file %s\n", current_file_name);
 760       }
 761     }
 762   } else {
 763     warning("failed to open rotation log file %s due to %s\n"
 764             "Turned off GC log file rotation\n",
 765                   _file_name, strerror(errno));
 766     _need_close = false;
 767     FLAG_SET_DEFAULT(UseGCLogFileRotation, false);
 768   }
 769 }
 770 
 771 defaultStream* defaultStream::instance = NULL;
 772 int defaultStream::_output_fd = 1;
 773 int defaultStream::_error_fd  = 2;
 774 FILE* defaultStream::_output_stream = stdout;




 645     _need_close = false;
 646   }
 647 }
 648 
 649 void gcLogFileStream::write(const char* s, size_t len) {
 650   if (_file != NULL) {
 651     size_t count = fwrite(s, 1, len, _file);
 652     _bytes_written += count;
 653   }
 654   update_position(s, len);
 655 }
 656 
 657 // rotate_log must be called from VMThread at safepoint. In case need change parameters
 658 // for gc log rotation from thread other than VMThread, a sub type of VM_Operation
 659 // should be created and be submitted to VMThread's operation queue. DO NOT call this
 660 // function directly. Currently, it is safe to rotate log at safepoint through VMThread.
 661 // That is, no mutator threads and concurrent GC threads run parallel with VMThread to
 662 // write to gc log file at safepoint. If in future, changes made for mutator threads or
 663 // concurrent GC threads to run parallel with VMThread at safepoint, write and rotate_log
 664 // must be synchronized.
 665 void gcLogFileStream::rotate_log(bool force, outputStream* out) {
 666   char time_msg[FILENAMEBUFLEN];
 667   char time_str[EXTRACHARLEN];
 668   char current_file_name[FILENAMEBUFLEN];
 669   char renamed_file_name[FILENAMEBUFLEN];
 670 
 671   if (!should_rotate(force)) {
 672     return;
 673   }
 674 
 675 #ifdef ASSERT
 676   Thread *thread = Thread::current();
 677   assert(thread == NULL ||
 678          (thread->is_VM_thread() && SafepointSynchronize::is_at_safepoint()),
 679          "Must be VMThread at safepoint");
 680 #endif
 681   if (NumberOfGCLogFiles == 1) {
 682     // rotate in same file
 683     rewind();
 684     _bytes_written = 0L;
 685     jio_snprintf(time_msg, sizeof(time_msg), "File  %s rotated at %s\n",
 686                  _file_name, os::local_time_string((char *)time_str, sizeof(time_str)));
 687     write(time_msg, strlen(time_msg));
 688 
 689     if (out != NULL) {
 690       out->print(time_msg);
 691     }
 692 
 693     dump_loggc_header();
 694     return;
 695   }
 696 
 697 #if defined(_WINDOWS)
 698 #ifndef F_OK
 699 #define F_OK 0
 700 #endif
 701 #endif // _WINDOWS
 702 
 703   // rotate file in names extended_filename.0, extended_filename.1, ...,
 704   // extended_filename.<NumberOfGCLogFiles - 1>. Current rotation file name will
 705   // have a form of extended_filename.<i>.current where i is the current rotation
 706   // file number. After it reaches max file size, the file will be saved and renamed
 707   // with .current removed from its tail.
 708   size_t filename_len = strlen(_file_name);
 709   if (_file != NULL) {
 710     jio_snprintf(renamed_file_name, filename_len + EXTRACHARLEN, "%s.%d",
 711                  _file_name, _cur_file_num);
 712     jio_snprintf(current_file_name, filename_len + EXTRACHARLEN, "%s.%d" CURRENTAPPX,
 713                  _file_name, _cur_file_num);
 714 
 715     const char* msg = force ? "GC log rotation request has been received."
 716                             : "GC log file has reached the maximum size.";
 717     jio_snprintf(time_msg, sizeof(time_msg), "%s %s Saved as %s\n",
 718                      os::local_time_string((char *)time_str, sizeof(time_str)),
 719                                                          msg, renamed_file_name);
 720     write(time_msg, strlen(time_msg));
 721 
 722     if (out != NULL) {
 723       out->print(time_msg);
 724     }
 725 
 726     fclose(_file);
 727     _file = NULL;
 728 
 729     bool can_rename = true;
 730     if (access(current_file_name, F_OK) != 0) {
 731       // current file does not exist?
 732       warning("No source file exists, cannot rename\n");
 733       can_rename = false;
 734     }
 735     if (can_rename) {
 736       if (access(renamed_file_name, F_OK) == 0) {
 737         if (remove(renamed_file_name) != 0) {
 738           warning("Could not delete existing file %s\n", renamed_file_name);
 739           can_rename = false;
 740         }
 741       } else {
 742         // file does not exist, ok to rename
 743       }
 744     }
 745     if (can_rename && rename(current_file_name, renamed_file_name) != 0) {
 746       warning("Could not rename %s to %s\n", _file_name, renamed_file_name);
 747     }
 748   }
 749 
 750   _cur_file_num++;
 751   if (_cur_file_num > NumberOfGCLogFiles - 1) _cur_file_num = 0;
 752   jio_snprintf(current_file_name,  filename_len + EXTRACHARLEN, "%s.%d" CURRENTAPPX,
 753                _file_name, _cur_file_num);
 754   _file = fopen(current_file_name, "w");
 755 
 756   if (_file != NULL) {
 757     _bytes_written = 0L;
 758     _need_close = true;
 759     // reuse current_file_name for time_msg
 760     jio_snprintf(current_file_name, filename_len + EXTRACHARLEN,
 761                  "%s.%d", _file_name, _cur_file_num);
 762     jio_snprintf(time_msg, sizeof(time_msg), "%s GC log file created %s\n",
 763                            os::local_time_string((char *)time_str, sizeof(time_str)),
 764                            current_file_name);
 765     write(time_msg, strlen(time_msg));
 766 
 767     if (out != NULL) {
 768       out->print(time_msg);
 769     }
 770 
 771     dump_loggc_header();
 772     // remove the existing file
 773     if (access(current_file_name, F_OK) == 0) {
 774       if (remove(current_file_name) != 0) {
 775         warning("Could not delete existing file %s\n", current_file_name);
 776       }
 777     }
 778   } else {
 779     warning("failed to open rotation log file %s due to %s\n"
 780             "Turned off GC log file rotation\n",
 781                   _file_name, strerror(errno));
 782     _need_close = false;
 783     FLAG_SET_DEFAULT(UseGCLogFileRotation, false);
 784   }
 785 }
 786 
 787 defaultStream* defaultStream::instance = NULL;
 788 int defaultStream::_output_fd = 1;
 789 int defaultStream::_error_fd  = 2;
 790 FILE* defaultStream::_output_stream = stdout;