36 #include "runtime/perfMemory.hpp" 37 #include "runtime/safepoint.hpp" 38 #include "runtime/statSampler.hpp" 39 #include "utilities/align.hpp" 40 #include "utilities/globalDefinitions.hpp" 41 42 // Prefix of performance data file. 43 const char PERFDATA_NAME[] = "hsperfdata"; 44 45 // Add 1 for the '_' character between PERFDATA_NAME and pid. The '\0' terminating 46 // character will be included in the sizeof(PERFDATA_NAME) operation. 47 static const size_t PERFDATA_FILENAME_LEN = sizeof(PERFDATA_NAME) + 48 UINT_CHARS + 1; 49 50 char* PerfMemory::_start = NULL; 51 char* PerfMemory::_end = NULL; 52 char* PerfMemory::_top = NULL; 53 size_t PerfMemory::_capacity = 0; 54 jint PerfMemory::_initialized = false; 55 PerfDataPrologue* PerfMemory::_prologue = NULL; 56 57 void perfMemory_init() { 58 59 if (!UsePerfData) return; 60 61 PerfMemory::initialize(); 62 } 63 64 void perfMemory_exit() { 65 66 if (!UsePerfData) return; 67 if (!PerfMemory::is_initialized()) return; 68 69 // Only destroy PerfData objects if we're at a safepoint and the 70 // StatSampler is not active. Otherwise, we risk removing PerfData 71 // objects that are currently being used by running JavaThreads 72 // or the StatSampler. This method is invoked while we are not at 73 // a safepoint during a VM abort so leaving the PerfData objects 74 // around may also help diagnose the failure. In rare cases, 75 // PerfData objects are used in parallel with a safepoint. See 76 // the work around in PerfDataManager::destroy(). 77 // 78 if (SafepointSynchronize::is_at_safepoint() && !StatSampler::is_active()) { 79 PerfDataManager::destroy(); 80 } 81 82 // Remove the persistent external resources, if any. This method 83 // does not unmap or invalidate any virtual memory allocated during 84 // initialization. 85 // 86 PerfMemory::destroy(); 87 } 88 89 void PerfMemory::initialize() { 90 91 if (_prologue != NULL) 92 // initialization already performed 93 return; 94 95 size_t capacity = align_up(PerfDataMemorySize, 96 os::vm_allocation_granularity()); 97 98 log_debug(perf, memops)("PerfDataMemorySize = " SIZE_FORMAT "," 99 " os::vm_allocation_granularity = %d," 100 " adjusted size = " SIZE_FORMAT "\n", 101 PerfDataMemorySize, 102 os::vm_allocation_granularity(), 103 capacity); 104 105 // allocate PerfData memory region 106 create_memory_region(capacity); 107 108 if (_start == NULL) { 109 110 // the PerfMemory region could not be created as desired. Rather 111 // than terminating the JVM, we revert to creating the instrumentation 143 #else 144 _prologue->magic = (jint)0xcafec0c0; 145 _prologue->byte_order = PERFDATA_BIG_ENDIAN; 146 #endif 147 148 _prologue->major_version = PERFDATA_MAJOR_VERSION; 149 _prologue->minor_version = PERFDATA_MINOR_VERSION; 150 _prologue->accessible = 0; 151 152 _prologue->entry_offset = sizeof(PerfDataPrologue); 153 _prologue->num_entries = 0; 154 _prologue->used = 0; 155 _prologue->overflow = 0; 156 _prologue->mod_time_stamp = 0; 157 158 OrderAccess::release_store(&_initialized, 1); 159 } 160 161 void PerfMemory::destroy() { 162 163 if (_prologue == NULL) return; 164 165 if (_start != NULL && _prologue->overflow != 0) { 166 167 // This state indicates that the contiguous memory region exists and 168 // that it wasn't large enough to hold all the counters. In this case, 169 // we output a warning message to the user on exit if the -XX:+Verbose 170 // flag is set (a debug only flag). External monitoring tools can detect 171 // this condition by monitoring the _prologue->overflow word. 172 // 173 // There are two tunables that can help resolve this issue: 174 // - increase the size of the PerfMemory with -XX:PerfDataMemorySize=<n> 175 // - decrease the maximum string constant length with 176 // -XX:PerfMaxStringConstLength=<n> 177 // 178 if (PrintMiscellaneous && Verbose) { 179 warning("PerfMemory Overflow Occurred.\n" 180 "\tCapacity = " SIZE_FORMAT " bytes" 181 " Used = " SIZE_FORMAT " bytes" 182 " Overflow = " INT32_FORMAT " bytes" 183 "\n\tUse -XX:PerfDataMemorySize=<size> to specify larger size.", 184 PerfMemory::capacity(), 185 PerfMemory::used(), 186 _prologue->overflow); 187 } 188 } 189 190 if (_start != NULL) { 191 192 // this state indicates that the contiguous memory region was successfully 193 // and that persistent resources may need to be cleaned up. This is 194 // expected to be the typical condition. 195 // 196 delete_memory_region(); 197 } 198 199 _start = NULL; 200 _end = NULL; 201 _top = NULL; 202 _prologue = NULL; 203 _capacity = 0; 204 } 205 206 // allocate an aligned block of memory from the PerfData memory 207 // region. This method assumes that the PerfData memory region 208 // was aligned on a double word boundary when created. 209 // 210 char* PerfMemory::alloc(size_t size) { 211 212 if (!UsePerfData) return NULL; 213 214 MutexLocker ml(PerfDataMemAlloc_lock); 215 216 assert(_prologue != NULL, "called before initialization"); 217 218 // check that there is enough memory for this request 219 if ((_top + size) >= _end) { 220 221 _prologue->overflow += (jint)size; 222 223 return NULL; 224 } 225 226 char* result = _top; 227 228 _top += size; 229 230 assert(contains(result), "PerfData memory pointer out of range"); 231 232 _prologue->used = (jint)used(); 233 _prologue->num_entries = _prologue->num_entries + 1; 234 235 return result; 236 } 237 238 void PerfMemory::mark_updated() { 239 if (!UsePerfData) return; 240 241 _prologue->mod_time_stamp = os::elapsed_counter(); 242 } 243 244 // Returns the complete path including the file name of performance data file. 245 // Caller is expected to release the allocated memory. 246 char* PerfMemory::get_perfdata_file_path() { 247 char* dest_file = NULL; 248 249 if (PerfDataSaveFile != NULL) { 250 // dest_file_name stores the validated file name if file_name 251 // contains %p which will be replaced by pid. 252 dest_file = NEW_C_HEAP_ARRAY(char, JVM_MAXPATHLEN, mtInternal); 253 if(!Arguments::copy_expand_pid(PerfDataSaveFile, strlen(PerfDataSaveFile), 254 dest_file, JVM_MAXPATHLEN)) { 255 FREE_C_HEAP_ARRAY(char, dest_file); 256 if (PrintMiscellaneous && Verbose) { 257 warning("Invalid performance data file path name specified, "\ 258 "fall back to a default name"); 259 } | 36 #include "runtime/perfMemory.hpp" 37 #include "runtime/safepoint.hpp" 38 #include "runtime/statSampler.hpp" 39 #include "utilities/align.hpp" 40 #include "utilities/globalDefinitions.hpp" 41 42 // Prefix of performance data file. 43 const char PERFDATA_NAME[] = "hsperfdata"; 44 45 // Add 1 for the '_' character between PERFDATA_NAME and pid. The '\0' terminating 46 // character will be included in the sizeof(PERFDATA_NAME) operation. 47 static const size_t PERFDATA_FILENAME_LEN = sizeof(PERFDATA_NAME) + 48 UINT_CHARS + 1; 49 50 char* PerfMemory::_start = NULL; 51 char* PerfMemory::_end = NULL; 52 char* PerfMemory::_top = NULL; 53 size_t PerfMemory::_capacity = 0; 54 jint PerfMemory::_initialized = false; 55 PerfDataPrologue* PerfMemory::_prologue = NULL; 56 bool PerfMemory::_destroyed = false; 57 58 void perfMemory_init() { 59 60 if (!UsePerfData) return; 61 62 PerfMemory::initialize(); 63 } 64 65 void perfMemory_exit() { 66 67 if (!UsePerfData) return; 68 if (!PerfMemory::is_useable()) return; 69 70 // Only destroy PerfData objects if we're at a safepoint and the 71 // StatSampler is not active. Otherwise, we risk removing PerfData 72 // objects that are currently being used by running JavaThreads 73 // or the StatSampler. This method is invoked while we are not at 74 // a safepoint during a VM abort so leaving the PerfData objects 75 // around may also help diagnose the failure. In rare cases, 76 // PerfData objects are used in parallel with a safepoint. See 77 // the work around in PerfDataManager::destroy(). 78 // 79 if (SafepointSynchronize::is_at_safepoint() && !StatSampler::is_active()) { 80 PerfDataManager::destroy(); 81 } 82 83 // Remove the persistent external resources, if any. This method 84 // does not unmap or invalidate any virtual memory allocated during 85 // initialization. 86 // 87 PerfMemory::destroy(); 88 } 89 90 void PerfMemory::initialize() { 91 92 if (is_initialized()) 93 // initialization already performed 94 return; 95 96 size_t capacity = align_up(PerfDataMemorySize, 97 os::vm_allocation_granularity()); 98 99 log_debug(perf, memops)("PerfDataMemorySize = " SIZE_FORMAT "," 100 " os::vm_allocation_granularity = %d," 101 " adjusted size = " SIZE_FORMAT "\n", 102 PerfDataMemorySize, 103 os::vm_allocation_granularity(), 104 capacity); 105 106 // allocate PerfData memory region 107 create_memory_region(capacity); 108 109 if (_start == NULL) { 110 111 // the PerfMemory region could not be created as desired. Rather 112 // than terminating the JVM, we revert to creating the instrumentation 144 #else 145 _prologue->magic = (jint)0xcafec0c0; 146 _prologue->byte_order = PERFDATA_BIG_ENDIAN; 147 #endif 148 149 _prologue->major_version = PERFDATA_MAJOR_VERSION; 150 _prologue->minor_version = PERFDATA_MINOR_VERSION; 151 _prologue->accessible = 0; 152 153 _prologue->entry_offset = sizeof(PerfDataPrologue); 154 _prologue->num_entries = 0; 155 _prologue->used = 0; 156 _prologue->overflow = 0; 157 _prologue->mod_time_stamp = 0; 158 159 OrderAccess::release_store(&_initialized, 1); 160 } 161 162 void PerfMemory::destroy() { 163 164 if (!is_useable()) return; 165 166 if (_start != NULL && _prologue->overflow != 0) { 167 168 // This state indicates that the contiguous memory region exists and 169 // that it wasn't large enough to hold all the counters. In this case, 170 // we output a warning message to the user on exit if the -XX:+Verbose 171 // flag is set (a debug only flag). External monitoring tools can detect 172 // this condition by monitoring the _prologue->overflow word. 173 // 174 // There are two tunables that can help resolve this issue: 175 // - increase the size of the PerfMemory with -XX:PerfDataMemorySize=<n> 176 // - decrease the maximum string constant length with 177 // -XX:PerfMaxStringConstLength=<n> 178 // 179 if (PrintMiscellaneous && Verbose) { 180 warning("PerfMemory Overflow Occurred.\n" 181 "\tCapacity = " SIZE_FORMAT " bytes" 182 " Used = " SIZE_FORMAT " bytes" 183 " Overflow = " INT32_FORMAT " bytes" 184 "\n\tUse -XX:PerfDataMemorySize=<size> to specify larger size.", 185 PerfMemory::capacity(), 186 PerfMemory::used(), 187 _prologue->overflow); 188 } 189 } 190 191 if (_start != NULL) { 192 193 // this state indicates that the contiguous memory region was successfully 194 // and that persistent resources may need to be cleaned up. This is 195 // expected to be the typical condition. 196 // 197 delete_memory_region(); 198 } 199 200 _destroyed = true; 201 } 202 203 // allocate an aligned block of memory from the PerfData memory 204 // region. This method assumes that the PerfData memory region 205 // was aligned on a double word boundary when created. 206 // 207 char* PerfMemory::alloc(size_t size) { 208 209 if (!UsePerfData) return NULL; 210 211 MutexLocker ml(PerfDataMemAlloc_lock); 212 213 assert(is_useable(), "called before init or after destroy"); 214 215 // check that there is enough memory for this request 216 if ((_top + size) >= _end) { 217 218 _prologue->overflow += (jint)size; 219 220 return NULL; 221 } 222 223 char* result = _top; 224 225 _top += size; 226 227 assert(contains(result), "PerfData memory pointer out of range"); 228 229 _prologue->used = (jint)used(); 230 _prologue->num_entries = _prologue->num_entries + 1; 231 232 return result; 233 } 234 235 void PerfMemory::mark_updated() { 236 if (!UsePerfData) return; 237 238 assert(is_useable(), "called before init or after destroy"); 239 240 _prologue->mod_time_stamp = os::elapsed_counter(); 241 } 242 243 // Returns the complete path including the file name of performance data file. 244 // Caller is expected to release the allocated memory. 245 char* PerfMemory::get_perfdata_file_path() { 246 char* dest_file = NULL; 247 248 if (PerfDataSaveFile != NULL) { 249 // dest_file_name stores the validated file name if file_name 250 // contains %p which will be replaced by pid. 251 dest_file = NEW_C_HEAP_ARRAY(char, JVM_MAXPATHLEN, mtInternal); 252 if(!Arguments::copy_expand_pid(PerfDataSaveFile, strlen(PerfDataSaveFile), 253 dest_file, JVM_MAXPATHLEN)) { 254 FREE_C_HEAP_ARRAY(char, dest_file); 255 if (PrintMiscellaneous && Verbose) { 256 warning("Invalid performance data file path name specified, "\ 257 "fall back to a default name"); 258 } |