rev 11857 : imported patch 8157948
rev 11858 : [mq]: 8157948.01

   1 /*
   2  * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
   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  *
  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 #include "precompiled.hpp"
  25 #include "logging/log.hpp"
  26 #include "logging/logConfiguration.hpp"
  27 #include "logging/logFileOutput.hpp"
  28 #include "memory/allocation.inline.hpp"
  29 #include "runtime/arguments.hpp"
  30 #include "runtime/os.inline.hpp"
  31 #include "utilities/globalDefinitions.hpp"
  32 #include "utilities/defaultStream.hpp"
  33 

  34 const char* LogFileOutput::FileOpenMode = "a";
  35 const char* LogFileOutput::PidFilenamePlaceholder = "%p";
  36 const char* LogFileOutput::TimestampFilenamePlaceholder = "%t";
  37 const char* LogFileOutput::TimestampFormat = "%Y-%m-%d_%H-%M-%S";
  38 const char* LogFileOutput::FileSizeOptionKey = "filesize";
  39 const char* LogFileOutput::FileCountOptionKey = "filecount";
  40 char        LogFileOutput::_pid_str[PidBufferSize];
  41 char        LogFileOutput::_vm_start_time_str[StartTimeBufferSize];
  42 
  43 LogFileOutput::LogFileOutput(const char* name)
  44     : LogFileStreamOutput(NULL), _name(os::strdup_check_oom(name, mtLogging)),
  45       _file_name(NULL), _archive_name(NULL), _archive_name_len(0),
  46       _rotate_size(DefaultFileSize), _file_count(DefaultFileCount),
  47       _current_size(0), _current_file(0), _rotation_semaphore(1) {
  48   _file_name = make_file_name(name, _pid_str, _vm_start_time_str);

  49 }
  50 
  51 void LogFileOutput::set_file_name_parameters(jlong vm_start_time) {
  52   int res = jio_snprintf(_pid_str, sizeof(_pid_str), "%d", os::current_process_id());
  53   assert(res > 0, "PID buffer too small");
  54 
  55   struct tm local_time;
  56   time_t utc_time = vm_start_time / 1000;
  57   os::localtime_pd(&utc_time, &local_time);
  58   res = (int)strftime(_vm_start_time_str, sizeof(_vm_start_time_str), TimestampFormat, &local_time);
  59   assert(res > 0, "VM start time buffer too small.");
  60 }
  61 
  62 LogFileOutput::~LogFileOutput() {
  63   if (_stream != NULL) {
  64     if (fclose(_stream) != 0) {
  65       jio_fprintf(defaultStream::error_stream(), "Could not close log file '%s' (%s).\n",
  66                   _file_name, os::strerror(errno));
  67     }
  68   }
  69   os::free(_archive_name);
  70   os::free(_file_name);
  71   os::free(const_cast<char*>(_name));
  72 }
  73 
  74 static size_t parse_value(const char* value_str) {
  75   char* end;
  76   unsigned long long value = strtoull(value_str, &end, 10);
  77   if (!isdigit(*value_str) || end != value_str + strlen(value_str) || value >= SIZE_MAX) {
  78     return SIZE_MAX;
  79   }
  80   return value;
  81 }
  82 
  83 static bool file_exists(const char* filename) {
  84   struct stat dummy_stat;
  85   return os::stat(filename, &dummy_stat) == 0;
  86 }
  87 
  88 static uint number_of_digits(uint number) {
  89   return number < 10 ? 1 : (number < 100 ? 2 : 3);
  90 }
  91 
  92 static bool is_regular_file(const char* filename) {
  93   struct stat st;
  94   int ret = os::stat(filename, &st);
  95   if (ret != 0) {
  96     return false;
  97   }
  98 #ifdef _WINDOWS
  99   return (st.st_mode & S_IFMT) == _S_IFREG;
 100 #else
 101   return S_ISREG(st.st_mode);
 102 #endif
 103 }
 104 
 105 // Try to find the next number that should be used for file rotation.
 106 // Return UINT_MAX on error.
 107 static uint next_file_number(const char* filename,
 108                              uint number_of_digits,
 109                              uint filecount,
 110                              outputStream* errstream) {
 111   bool found = false;
 112   uint next_num = 0;
 113 
 114   // len is filename + dot + digits + null char
 115   size_t len = strlen(filename) + number_of_digits + 2;
 116   char* archive_name = NEW_C_HEAP_ARRAY(char, len, mtLogging);
 117   char* oldest_name = NEW_C_HEAP_ARRAY(char, len, mtLogging);
 118 
 119   for (uint i = 0; i < filecount; i++) {
 120     int ret = jio_snprintf(archive_name, len, "%s.%0*u",
 121                            filename, number_of_digits, i);
 122     assert(ret > 0 && static_cast<size_t>(ret) == len - 1,
 123            "incorrect buffer length calculation");
 124 
 125     if (file_exists(archive_name) && !is_regular_file(archive_name)) {
 126       // We've encountered something that's not a regular file among the
 127       // possible file rotation targets. Fail immediately to prevent
 128       // problems later.
 129       errstream->print_cr("Possible rotation target file '%s' already exists "
 130                           "but is not a regular file.", archive_name);
 131       next_num = UINT_MAX;
 132       break;
 133     }
 134 
 135     // Stop looking if we find an unused file name
 136     if (!file_exists(archive_name)) {
 137       next_num = i;
 138       found = true;
 139       break;
 140     }
 141 
 142     // Keep track of oldest existing log file
 143     if (!found
 144         || os::compare_file_modified_times(oldest_name, archive_name) > 0) {
 145       strcpy(oldest_name, archive_name);
 146       next_num = i;
 147       found = true;
 148     }
 149   }
 150 
 151   FREE_C_HEAP_ARRAY(char, oldest_name);
 152   FREE_C_HEAP_ARRAY(char, archive_name);
 153   return next_num;
 154 }
 155 
 156 bool LogFileOutput::parse_options(const char* options, outputStream* errstream) {
 157   if (options == NULL || strlen(options) == 0) {
 158     return true;
 159   }
 160   bool success = true;
 161   char* opts = os::strdup_check_oom(options, mtLogging);
 162 
 163   char* comma_pos;
 164   char* pos = opts;
 165   do {
 166     comma_pos = strchr(pos, ',');
 167     if (comma_pos != NULL) {
 168       *comma_pos = '\0';
 169     }
 170 
 171     char* equals_pos = strchr(pos, '=');
 172     if (equals_pos == NULL) {
 173       success = false;
 174       break;
 175     }
 176     char* key = pos;
 177     char* value_str = equals_pos + 1;
 178     *equals_pos = '\0';
 179 
 180     if (strcmp(FileCountOptionKey, key) == 0) {
 181       size_t value = parse_value(value_str);
 182       if (value > MaxRotationFileCount) {
 183         errstream->print_cr("Invalid option: %s must be in range [0, %u]",
 184                             FileCountOptionKey,
 185                             MaxRotationFileCount);
 186         success = false;
 187         break;
 188       }
 189       _file_count = static_cast<uint>(value);
 190     } else if (strcmp(FileSizeOptionKey, key) == 0) {
 191       julong value;
 192       success = Arguments::atojulong(value_str, &value);
 193       if (!success || (value > SIZE_MAX)) {
 194         errstream->print_cr("Invalid option: %s must be in range [0, "
 195                             SIZE_FORMAT "]", FileSizeOptionKey, SIZE_MAX);
 196         success = false;
 197         break;
 198       }
 199       _rotate_size = static_cast<size_t>(value);
 200     } else {
 201       errstream->print_cr("Invalid option '%s' for log file output.", key);
 202       success = false;
 203       break;
 204     }
 205     pos = comma_pos + 1;
 206   } while (comma_pos != NULL);
 207 
 208   os::free(opts);
 209   return success;
 210 }
 211 
 212 bool LogFileOutput::initialize(const char* options, outputStream* errstream) {
 213   if (!parse_options(options, errstream)) {
 214     return false;
 215   }
 216 
 217   if (_file_count > 0) {
 218     // compute digits with filecount - 1 since numbers will start from 0
 219     _file_count_max_digits = number_of_digits(_file_count - 1);
 220     _archive_name_len = 2 + strlen(_file_name) + _file_count_max_digits;
 221     _archive_name = NEW_C_HEAP_ARRAY(char, _archive_name_len, mtLogging);
 222   }
 223 
 224   log_trace(logging)("Initializing logging to file '%s' (filecount: %u"
 225                      ", filesize: " SIZE_FORMAT " KiB).",
 226                      _file_name, _file_count, _rotate_size / K);
 227 
 228   if (_file_count > 0 && file_exists(_file_name)) {
 229     if (!is_regular_file(_file_name)) {
 230       errstream->print_cr("Unable to log to file %s with log file rotation: "
 231                           "%s is not a regular file",
 232                           _file_name, _file_name);
 233       return false;
 234     }
 235     _current_file = next_file_number(_file_name,
 236                                      _file_count_max_digits,
 237                                      _file_count,
 238                                      errstream);
 239     if (_current_file == UINT_MAX) {
 240       return false;
 241     }
 242     log_trace(logging)("Existing log file found, saving it as '%s.%0*u'",
 243                        _file_name, _file_count_max_digits, _current_file);
 244     archive();
 245     increment_file_count();
 246   }
 247 
 248   _stream = fopen(_file_name, FileOpenMode);
 249   if (_stream == NULL) {
 250     errstream->print_cr("Error opening log file '%s': %s",
 251                         _file_name, strerror(errno));
 252     return false;
 253   }
 254 
 255   if (_file_count == 0 && is_regular_file(_file_name)) {
 256     log_trace(logging)("Truncating log file");
 257     os::ftruncate(os::get_fileno(_stream), 0);
 258   }
 259 
 260   return true;
 261 }
 262 
 263 int LogFileOutput::write(const LogDecorations& decorations, const char* msg) {
 264   if (_stream == NULL) {
 265     // An error has occurred with this output, avoid writing to it.
 266     return 0;
 267   }
 268 
 269   _rotation_semaphore.wait();
 270   int written = LogFileStreamOutput::write(decorations, msg);
 271   _current_size += written;
 272 
 273   if (should_rotate()) {
 274     rotate();
 275   }
 276   _rotation_semaphore.signal();
 277 
 278   return written;
 279 }
 280 
 281 int LogFileOutput::write(LogMessageBuffer::Iterator msg_iterator) {
 282   if (_stream == NULL) {
 283     // An error has occurred with this output, avoid writing to it.
 284     return 0;
 285   }
 286 
 287   _rotation_semaphore.wait();
 288   int written = LogFileStreamOutput::write(msg_iterator);
 289   _current_size += written;
 290 
 291   if (should_rotate()) {
 292     rotate();
 293   }
 294   _rotation_semaphore.signal();
 295 
 296   return written;
 297 }
 298 
 299 void LogFileOutput::archive() {
 300   assert(_archive_name != NULL && _archive_name_len > 0, "Rotation must be configured before using this function.");
 301   int ret = jio_snprintf(_archive_name, _archive_name_len, "%s.%0*u",
 302                          _file_name, _file_count_max_digits, _current_file);
 303   assert(ret >= 0, "Buffer should always be large enough");
 304 
 305   // Attempt to remove possibly existing archived log file before we rename.
 306   // Don't care if it fails, we really only care about the rename that follows.
 307   remove(_archive_name);
 308 
 309   // Rename the file from ex hotspot.log to hotspot.log.2
 310   if (rename(_file_name, _archive_name) == -1) {
 311     jio_fprintf(defaultStream::error_stream(), "Could not rename log file '%s' to '%s' (%s).\n",
 312                 _file_name, _archive_name, os::strerror(errno));
 313   }
 314 }
 315 
 316 void LogFileOutput::force_rotate() {
 317   if (_file_count == 0) {
 318     // Rotation not possible
 319     return;
 320   }
 321   _rotation_semaphore.wait();
 322   rotate();
 323   _rotation_semaphore.signal();
 324 }
 325 
 326 void LogFileOutput::rotate() {
 327 
 328   if (fclose(_stream)) {
 329     jio_fprintf(defaultStream::error_stream(), "Error closing file '%s' during log rotation (%s).\n",
 330                 _file_name, os::strerror(errno));
 331   }
 332 
 333   // Archive the current log file
 334   archive();
 335 
 336   // Open the active log file using the same stream as before
 337   _stream = fopen(_file_name, FileOpenMode);
 338   if (_stream == NULL) {
 339     jio_fprintf(defaultStream::error_stream(), "Could not reopen file '%s' during log rotation (%s).\n",
 340                 _file_name, os::strerror(errno));
 341     return;
 342   }
 343 
 344   // Reset accumulated size, increase current file counter, and check for file count wrap-around.
 345   _current_size = 0;
 346   increment_file_count();
 347 }
 348 
 349 char* LogFileOutput::make_file_name(const char* file_name,
 350                                     const char* pid_string,
 351                                     const char* timestamp_string) {
 352   char* result = NULL;
 353 
 354   // Lets start finding out if we have any %d and/or %t in the name.
 355   // We will only replace the first occurrence of any placeholder
 356   const char* pid = strstr(file_name, PidFilenamePlaceholder);
 357   const char* timestamp = strstr(file_name, TimestampFilenamePlaceholder);
 358 
 359   if (pid == NULL && timestamp == NULL) {
 360     // We found no place-holders, return the simple filename
 361     return os::strdup_check_oom(file_name, mtLogging);
 362   }
 363 
 364   // At least one of the place-holders were found in the file_name
 365   const char* first = "";
 366   size_t first_pos = SIZE_MAX;
 367   size_t first_replace_len = 0;
 368 
 369   const char* second = "";
 370   size_t second_pos = SIZE_MAX;
 371   size_t second_replace_len = 0;
 372 
 373   // If we found a %p, then setup our variables accordingly
 374   if (pid != NULL) {
 375     if (timestamp == NULL || pid < timestamp) {
 376       first = pid_string;
 377       first_pos = pid - file_name;
 378       first_replace_len = strlen(PidFilenamePlaceholder);
 379     } else {
 380       second = pid_string;
 381       second_pos = pid - file_name;
 382       second_replace_len = strlen(PidFilenamePlaceholder);
 383     }
 384   }
 385 
 386   if (timestamp != NULL) {
 387     if (pid == NULL || timestamp < pid) {
 388       first = timestamp_string;
 389       first_pos = timestamp - file_name;
 390       first_replace_len = strlen(TimestampFilenamePlaceholder);
 391     } else {
 392       second = timestamp_string;
 393       second_pos = timestamp - file_name;
 394       second_replace_len = strlen(TimestampFilenamePlaceholder);
 395     }
 396   }
 397 
 398   size_t first_len = strlen(first);
 399   size_t second_len = strlen(second);
 400 
 401   // Allocate the new buffer, size it to hold all we want to put in there +1.
 402   size_t result_len =  strlen(file_name) + first_len - first_replace_len + second_len - second_replace_len;
 403   result = NEW_C_HEAP_ARRAY(char, result_len + 1, mtLogging);
 404 
 405   // Assemble the strings
 406   size_t file_name_pos = 0;
 407   size_t i = 0;
 408   while (i < result_len) {
 409     if (file_name_pos == first_pos) {
 410       // We are in the range of the first placeholder
 411       strcpy(result + i, first);
 412       // Bump output buffer position with length of replacing string
 413       i += first_len;
 414       // Bump source buffer position to skip placeholder
 415       file_name_pos += first_replace_len;
 416     } else if (file_name_pos == second_pos) {
 417       // We are in the range of the second placeholder
 418       strcpy(result + i, second);
 419       i += second_len;
 420       file_name_pos += second_replace_len;
 421     } else {
 422       // Else, copy char by char of the original file
 423       result[i] = file_name[file_name_pos++];
 424       i++;
 425     }
 426   }
 427   // Add terminating char
 428   result[result_len] = '\0';
 429   return result;
 430 }
 431 
 432 void LogFileOutput::describe(outputStream *out) {
 433   LogOutput::describe(out);
 434   out->print(" ");
 435 
 436   out->print("filecount=%u,filesize=" SIZE_FORMAT "%s", _file_count,
 437              byte_size_in_proper_unit(_rotate_size),
 438              proper_unit_for_byte_size(_rotate_size));
 439 }
 440 
--- EOF ---