Mon, 03 Jun 2013 08:52:20 +0200
8013329: File leak in hotspot/src/share/vm/compiler/compileBroker.cpp
Summary: Added calling of the destructor of CompileLog so that files are closed. Added/moved memory allocation/deallocation of the string that contains the name of the log file to class CompileLog.
Reviewed-by: kvn, roland
1.1 --- a/src/share/vm/compiler/compileBroker.cpp Wed Apr 24 11:49:38 2013 +0200 1.2 +++ b/src/share/vm/compiler/compileBroker.cpp Mon Jun 03 08:52:20 2013 +0200 1.3 @@ -1642,42 +1642,37 @@ 1.4 // Set up state required by +LogCompilation. 1.5 void CompileBroker::init_compiler_thread_log() { 1.6 CompilerThread* thread = CompilerThread::current(); 1.7 - char fileBuf[4*K]; 1.8 + char file_name[4*K]; 1.9 FILE* fp = NULL; 1.10 - char* file = NULL; 1.11 intx thread_id = os::current_thread_id(); 1.12 for (int try_temp_dir = 1; try_temp_dir >= 0; try_temp_dir--) { 1.13 const char* dir = (try_temp_dir ? os::get_temp_directory() : NULL); 1.14 if (dir == NULL) { 1.15 - jio_snprintf(fileBuf, sizeof(fileBuf), "hs_c" UINTX_FORMAT "_pid%u.log", 1.16 + jio_snprintf(file_name, sizeof(file_name), "hs_c" UINTX_FORMAT "_pid%u.log", 1.17 thread_id, os::current_process_id()); 1.18 } else { 1.19 - jio_snprintf(fileBuf, sizeof(fileBuf), 1.20 + jio_snprintf(file_name, sizeof(file_name), 1.21 "%s%shs_c" UINTX_FORMAT "_pid%u.log", dir, 1.22 os::file_separator(), thread_id, os::current_process_id()); 1.23 } 1.24 - fp = fopen(fileBuf, "at"); 1.25 + 1.26 + fp = fopen(file_name, "at"); 1.27 if (fp != NULL) { 1.28 - file = NEW_C_HEAP_ARRAY(char, strlen(fileBuf)+1, mtCompiler); 1.29 - strcpy(file, fileBuf); 1.30 - break; 1.31 + if (LogCompilation && Verbose) { 1.32 + tty->print_cr("Opening compilation log %s", file_name); 1.33 + } 1.34 + CompileLog* log = new(ResourceObj::C_HEAP, mtCompiler) CompileLog(file_name, fp, thread_id); 1.35 + thread->init_log(log); 1.36 + 1.37 + if (xtty != NULL) { 1.38 + ttyLocker ttyl; 1.39 + // Record any per thread log files 1.40 + xtty->elem("thread_logfile thread='%d' filename='%s'", thread_id, file_name); 1.41 + } 1.42 + return; 1.43 } 1.44 } 1.45 - if (fp == NULL) { 1.46 - warning("Cannot open log file: %s", fileBuf); 1.47 - } else { 1.48 - if (LogCompilation && Verbose) 1.49 - tty->print_cr("Opening compilation log %s", file); 1.50 - CompileLog* log = new(ResourceObj::C_HEAP, mtCompiler) CompileLog(file, fp, thread_id); 1.51 - thread->init_log(log); 1.52 - 1.53 - if (xtty != NULL) { 1.54 - ttyLocker ttyl; 1.55 - 1.56 - // Record any per thread log files 1.57 - xtty->elem("thread_logfile thread='%d' filename='%s'", thread_id, file); 1.58 - } 1.59 - } 1.60 + warning("Cannot open log file: %s", file_name); 1.61 } 1.62 1.63 // ------------------------------------------------------------------
2.1 --- a/src/share/vm/compiler/compileLog.cpp Wed Apr 24 11:49:38 2013 +0200 2.2 +++ b/src/share/vm/compiler/compileLog.cpp Mon Jun 03 08:52:20 2013 +0200 2.3 @@ -34,17 +34,18 @@ 2.4 2.5 // ------------------------------------------------------------------ 2.6 // CompileLog::CompileLog 2.7 -CompileLog::CompileLog(const char* file, FILE* fp, intx thread_id) 2.8 +CompileLog::CompileLog(const char* file_name, FILE* fp, intx thread_id) 2.9 : _context(_context_buffer, sizeof(_context_buffer)) 2.10 { 2.11 - initialize(new(ResourceObj::C_HEAP, mtCompiler) fileStream(fp)); 2.12 - _file = file; 2.13 + initialize(new(ResourceObj::C_HEAP, mtCompiler) fileStream(fp, true)); 2.14 _file_end = 0; 2.15 _thread_id = thread_id; 2.16 2.17 _identities_limit = 0; 2.18 _identities_capacity = 400; 2.19 _identities = NEW_C_HEAP_ARRAY(char, _identities_capacity, mtCompiler); 2.20 + _file = NEW_C_HEAP_ARRAY(char, strlen(file_name)+1, mtCompiler); 2.21 + strcpy((char*)_file, file_name); 2.22 2.23 // link into the global list 2.24 { MutexLocker locker(CompileTaskAlloc_lock); 2.25 @@ -57,6 +58,7 @@ 2.26 delete _out; 2.27 _out = NULL; 2.28 FREE_C_HEAP_ARRAY(char, _identities, mtCompiler); 2.29 + FREE_C_HEAP_ARRAY(char, _file, mtCompiler); 2.30 } 2.31 2.32 2.33 @@ -188,7 +190,8 @@ 2.34 if (called_exit) return; 2.35 called_exit = true; 2.36 2.37 - for (CompileLog* log = _first; log != NULL; log = log->_next) { 2.38 + CompileLog* log = _first; 2.39 + while (log != NULL) { 2.40 log->flush(); 2.41 const char* partial_file = log->file(); 2.42 int partial_fd = open(partial_file, O_RDONLY); 2.43 @@ -267,7 +270,11 @@ 2.44 close(partial_fd); 2.45 unlink(partial_file); 2.46 } 2.47 + CompileLog* next_log = log->_next; 2.48 + delete log; 2.49 + log = next_log; 2.50 } 2.51 + _first = NULL; 2.52 } 2.53 2.54 // ------------------------------------------------------------------
3.1 --- a/src/share/vm/compiler/compileLog.hpp Wed Apr 24 11:49:38 2013 +0200 3.2 +++ b/src/share/vm/compiler/compileLog.hpp Mon Jun 03 08:52:20 2013 +0200 3.3 @@ -57,7 +57,7 @@ 3.4 void va_tag(bool push, const char* format, va_list ap); 3.5 3.6 public: 3.7 - CompileLog(const char* file, FILE* fp, intx thread_id); 3.8 + CompileLog(const char* file_name, FILE* fp, intx thread_id); 3.9 ~CompileLog(); 3.10 3.11 intx thread_id() { return _thread_id; }