1 /* 2 * Copyright (c) 2001, 2017, 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 25 #include "precompiled.hpp" 26 #include "logging/log.hpp" 27 #include "memory/allocation.inline.hpp" 28 #include "runtime/arguments.hpp" 29 #include "runtime/java.hpp" 30 #include "runtime/mutex.hpp" 31 #include "runtime/mutexLocker.hpp" 32 #include "runtime/orderAccess.inline.hpp" 33 #include "runtime/os.hpp" 34 #include "runtime/perfData.hpp" 35 #include "runtime/perfMemory.hpp" 36 #include "runtime/safepoint.hpp" 37 #include "runtime/statSampler.hpp" 38 #include "utilities/globalDefinitions.hpp" 39 40 // Prefix of performance data file. 41 const char PERFDATA_NAME[] = "hsperfdata"; 42 43 // Add 1 for the '_' character between PERFDATA_NAME and pid. The '\0' terminating 44 // character will be included in the sizeof(PERFDATA_NAME) operation. 45 static const size_t PERFDATA_FILENAME_LEN = sizeof(PERFDATA_NAME) + 46 UINT_CHARS + 1; 47 48 char* PerfMemory::_start = NULL; 49 char* PerfMemory::_end = NULL; 50 char* PerfMemory::_top = NULL; 51 size_t PerfMemory::_capacity = 0; 52 jint PerfMemory::_initialized = false; 53 PerfDataPrologue* PerfMemory::_prologue = NULL; 54 bool PerfMemory::_destroyed = false; 55 56 void perfMemory_init() { 57 58 if (!UsePerfData) return; 59 60 PerfMemory::initialize(); 61 } 62 63 void perfMemory_exit() { 64 65 if (!UsePerfData) return; 66 if (!PerfMemory::is_initialized()) return; 67 if (PerfMemory::is_destroyed()) 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_size_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 112 // on the C heap. When running in this mode, external monitoring 113 // clients cannot attach to and monitor this JVM. 114 // 115 // the warning is issued only in debug mode in order to avoid 116 // additional output to the stdout or stderr output streams. 117 // 118 if (PrintMiscellaneous && Verbose) { 119 warning("Could not create PerfData Memory region, reverting to malloc"); 120 } 121 122 _prologue = NEW_C_HEAP_OBJ(PerfDataPrologue, mtInternal); 123 } 124 else { 125 126 // the PerfMemory region was created as expected. 127 128 log_debug(perf, memops)("PerfMemory created: address = " INTPTR_FORMAT "," 129 " size = " SIZE_FORMAT "\n", 130 p2i(_start), 131 _capacity); 132 133 _prologue = (PerfDataPrologue *)_start; 134 _end = _start + _capacity; 135 _top = _start + sizeof(PerfDataPrologue); 136 } 137 138 assert(_prologue != NULL, "prologue pointer must be initialized"); 139 140 #ifdef VM_LITTLE_ENDIAN 141 _prologue->magic = (jint)0xc0c0feca; 142 _prologue->byte_order = PERFDATA_LITTLE_ENDIAN; 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 _destroyed = true; 200 } 201 202 // allocate an aligned block of memory from the PerfData memory 203 // region. This method assumes that the PerfData memory region 204 // was aligned on a double word boundary when created. 205 // 206 char* PerfMemory::alloc(size_t size) { 207 208 if (!UsePerfData) return NULL; 209 210 MutexLocker ml(PerfDataMemAlloc_lock); 211 212 assert(_prologue != NULL, "called before initialization"); 213 214 // check that there is enough memory for this request 215 if ((_top + size) >= _end) { 216 217 _prologue->overflow += (jint)size; 218 219 return NULL; 220 } 221 222 char* result = _top; 223 224 _top += size; 225 226 assert(contains(result), "PerfData memory pointer out of range"); 227 228 _prologue->used = (jint)used(); 229 _prologue->num_entries = _prologue->num_entries + 1; 230 231 return result; 232 } 233 234 void PerfMemory::mark_updated() { 235 if (!UsePerfData) return; 236 237 _prologue->mod_time_stamp = os::elapsed_counter(); 238 } 239 240 // Returns the complete path including the file name of performance data file. 241 // Caller is expected to release the allocated memory. 242 char* PerfMemory::get_perfdata_file_path() { 243 char* dest_file = NULL; 244 245 if (PerfDataSaveFile != NULL) { 246 // dest_file_name stores the validated file name if file_name 247 // contains %p which will be replaced by pid. 248 dest_file = NEW_C_HEAP_ARRAY(char, JVM_MAXPATHLEN, mtInternal); 249 if(!Arguments::copy_expand_pid(PerfDataSaveFile, strlen(PerfDataSaveFile), 250 dest_file, JVM_MAXPATHLEN)) { 251 FREE_C_HEAP_ARRAY(char, dest_file); 252 if (PrintMiscellaneous && Verbose) { 253 warning("Invalid performance data file path name specified, "\ 254 "fall back to a default name"); 255 } 256 } else { 257 return dest_file; 258 } 259 } 260 // create the name of the file for retaining the instrumentation memory. 261 dest_file = NEW_C_HEAP_ARRAY(char, PERFDATA_FILENAME_LEN, mtInternal); 262 jio_snprintf(dest_file, PERFDATA_FILENAME_LEN, 263 "%s_%d", PERFDATA_NAME, os::current_process_id()); 264 265 return dest_file; 266 }