1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/runtime/perfMemory.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,267 @@ 1.4 +/* 1.5 + * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "memory/allocation.inline.hpp" 1.30 +#include "runtime/arguments.hpp" 1.31 +#include "runtime/java.hpp" 1.32 +#include "runtime/mutex.hpp" 1.33 +#include "runtime/mutexLocker.hpp" 1.34 +#include "runtime/os.hpp" 1.35 +#include "runtime/perfData.hpp" 1.36 +#include "runtime/perfMemory.hpp" 1.37 +#include "runtime/statSampler.hpp" 1.38 +#include "utilities/globalDefinitions.hpp" 1.39 + 1.40 +PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 1.41 + 1.42 +// Prefix of performance data file. 1.43 +const char PERFDATA_NAME[] = "hsperfdata"; 1.44 + 1.45 +// Add 1 for the '_' character between PERFDATA_NAME and pid. The '\0' terminating 1.46 +// character will be included in the sizeof(PERFDATA_NAME) operation. 1.47 +static const size_t PERFDATA_FILENAME_LEN = sizeof(PERFDATA_NAME) + 1.48 + UINT_CHARS + 1; 1.49 + 1.50 +char* PerfMemory::_start = NULL; 1.51 +char* PerfMemory::_end = NULL; 1.52 +char* PerfMemory::_top = NULL; 1.53 +size_t PerfMemory::_capacity = 0; 1.54 +jint PerfMemory::_initialized = false; 1.55 +PerfDataPrologue* PerfMemory::_prologue = NULL; 1.56 + 1.57 +void perfMemory_init() { 1.58 + 1.59 + if (!UsePerfData) return; 1.60 + 1.61 + PerfMemory::initialize(); 1.62 +} 1.63 + 1.64 +void perfMemory_exit() { 1.65 + 1.66 + if (!UsePerfData) return; 1.67 + if (!PerfMemory::is_initialized()) return; 1.68 + 1.69 + // if the StatSampler is active, then we don't want to remove 1.70 + // resources it may be dependent on. Typically, the StatSampler 1.71 + // is disengaged from the watcher thread when this method is called, 1.72 + // but it is not disengaged if this method is invoked during a 1.73 + // VM abort. 1.74 + // 1.75 + if (!StatSampler::is_active()) 1.76 + PerfDataManager::destroy(); 1.77 + 1.78 + // remove the persistent external resources, if any. this method 1.79 + // does not unmap or invalidate any virtual memory allocated during 1.80 + // initialization. 1.81 + // 1.82 + PerfMemory::destroy(); 1.83 +} 1.84 + 1.85 +void PerfMemory::initialize() { 1.86 + 1.87 + if (_prologue != NULL) 1.88 + // initialization already performed 1.89 + return; 1.90 + 1.91 + size_t capacity = align_size_up(PerfDataMemorySize, 1.92 + os::vm_allocation_granularity()); 1.93 + 1.94 + if (PerfTraceMemOps) { 1.95 + tty->print("PerfDataMemorySize = " SIZE_FORMAT "," 1.96 + " os::vm_allocation_granularity = " SIZE_FORMAT "," 1.97 + " adjusted size = " SIZE_FORMAT "\n", 1.98 + PerfDataMemorySize, 1.99 + os::vm_allocation_granularity(), 1.100 + capacity); 1.101 + } 1.102 + 1.103 + // allocate PerfData memory region 1.104 + create_memory_region(capacity); 1.105 + 1.106 + if (_start == NULL) { 1.107 + 1.108 + // the PerfMemory region could not be created as desired. Rather 1.109 + // than terminating the JVM, we revert to creating the instrumentation 1.110 + // on the C heap. When running in this mode, external monitoring 1.111 + // clients cannot attach to and monitor this JVM. 1.112 + // 1.113 + // the warning is issued only in debug mode in order to avoid 1.114 + // additional output to the stdout or stderr output streams. 1.115 + // 1.116 + if (PrintMiscellaneous && Verbose) { 1.117 + warning("Could not create PerfData Memory region, reverting to malloc"); 1.118 + } 1.119 + 1.120 + _prologue = NEW_C_HEAP_OBJ(PerfDataPrologue, mtInternal); 1.121 + } 1.122 + else { 1.123 + 1.124 + // the PerfMemory region was created as expected. 1.125 + 1.126 + if (PerfTraceMemOps) { 1.127 + tty->print("PerfMemory created: address = " INTPTR_FORMAT "," 1.128 + " size = " SIZE_FORMAT "\n", 1.129 + (void*)_start, 1.130 + _capacity); 1.131 + } 1.132 + 1.133 + _prologue = (PerfDataPrologue *)_start; 1.134 + _end = _start + _capacity; 1.135 + _top = _start + sizeof(PerfDataPrologue); 1.136 + } 1.137 + 1.138 + assert(_prologue != NULL, "prologue pointer must be initialized"); 1.139 + 1.140 +#ifdef VM_LITTLE_ENDIAN 1.141 + _prologue->magic = (jint)0xc0c0feca; 1.142 + _prologue->byte_order = PERFDATA_LITTLE_ENDIAN; 1.143 +#else 1.144 + _prologue->magic = (jint)0xcafec0c0; 1.145 + _prologue->byte_order = PERFDATA_BIG_ENDIAN; 1.146 +#endif 1.147 + 1.148 + _prologue->major_version = PERFDATA_MAJOR_VERSION; 1.149 + _prologue->minor_version = PERFDATA_MINOR_VERSION; 1.150 + _prologue->accessible = 0; 1.151 + 1.152 + _prologue->entry_offset = sizeof(PerfDataPrologue); 1.153 + _prologue->num_entries = 0; 1.154 + _prologue->used = 0; 1.155 + _prologue->overflow = 0; 1.156 + _prologue->mod_time_stamp = 0; 1.157 + 1.158 + OrderAccess::release_store(&_initialized, 1); 1.159 +} 1.160 + 1.161 +void PerfMemory::destroy() { 1.162 + 1.163 + if (_prologue == NULL) return; 1.164 + 1.165 + if (_start != NULL && _prologue->overflow != 0) { 1.166 + 1.167 + // This state indicates that the contiguous memory region exists and 1.168 + // that it wasn't large enough to hold all the counters. In this case, 1.169 + // we output a warning message to the user on exit if the -XX:+Verbose 1.170 + // flag is set (a debug only flag). External monitoring tools can detect 1.171 + // this condition by monitoring the _prologue->overflow word. 1.172 + // 1.173 + // There are two tunables that can help resolve this issue: 1.174 + // - increase the size of the PerfMemory with -XX:PerfDataMemorySize=<n> 1.175 + // - decrease the maximum string constant length with 1.176 + // -XX:PerfMaxStringConstLength=<n> 1.177 + // 1.178 + if (PrintMiscellaneous && Verbose) { 1.179 + warning("PerfMemory Overflow Occurred.\n" 1.180 + "\tCapacity = " SIZE_FORMAT " bytes" 1.181 + " Used = " SIZE_FORMAT " bytes" 1.182 + " Overflow = " INT32_FORMAT " bytes" 1.183 + "\n\tUse -XX:PerfDataMemorySize=<size> to specify larger size.", 1.184 + PerfMemory::capacity(), 1.185 + PerfMemory::used(), 1.186 + _prologue->overflow); 1.187 + } 1.188 + } 1.189 + 1.190 + if (_start != NULL) { 1.191 + 1.192 + // this state indicates that the contiguous memory region was successfully 1.193 + // and that persistent resources may need to be cleaned up. This is 1.194 + // expected to be the typical condition. 1.195 + // 1.196 + delete_memory_region(); 1.197 + } 1.198 + 1.199 + _start = NULL; 1.200 + _end = NULL; 1.201 + _top = NULL; 1.202 + _prologue = NULL; 1.203 + _capacity = 0; 1.204 +} 1.205 + 1.206 +// allocate an aligned block of memory from the PerfData memory 1.207 +// region. This method assumes that the PerfData memory region 1.208 +// was aligned on a double word boundary when created. 1.209 +// 1.210 +char* PerfMemory::alloc(size_t size) { 1.211 + 1.212 + if (!UsePerfData) return NULL; 1.213 + 1.214 + MutexLocker ml(PerfDataMemAlloc_lock); 1.215 + 1.216 + assert(_prologue != NULL, "called before initialization"); 1.217 + 1.218 + // check that there is enough memory for this request 1.219 + if ((_top + size) >= _end) { 1.220 + 1.221 + _prologue->overflow += (jint)size; 1.222 + 1.223 + return NULL; 1.224 + } 1.225 + 1.226 + char* result = _top; 1.227 + 1.228 + _top += size; 1.229 + 1.230 + assert(contains(result), "PerfData memory pointer out of range"); 1.231 + 1.232 + _prologue->used = (jint)used(); 1.233 + _prologue->num_entries = _prologue->num_entries + 1; 1.234 + 1.235 + return result; 1.236 +} 1.237 + 1.238 +void PerfMemory::mark_updated() { 1.239 + if (!UsePerfData) return; 1.240 + 1.241 + _prologue->mod_time_stamp = os::elapsed_counter(); 1.242 +} 1.243 + 1.244 +// Returns the complete path including the file name of performance data file. 1.245 +// Caller is expected to release the allocated memory. 1.246 +char* PerfMemory::get_perfdata_file_path() { 1.247 + char* dest_file = NULL; 1.248 + 1.249 + if (PerfDataSaveFile != NULL) { 1.250 + // dest_file_name stores the validated file name if file_name 1.251 + // contains %p which will be replaced by pid. 1.252 + dest_file = NEW_C_HEAP_ARRAY(char, JVM_MAXPATHLEN, mtInternal); 1.253 + if(!Arguments::copy_expand_pid(PerfDataSaveFile, strlen(PerfDataSaveFile), 1.254 + dest_file, JVM_MAXPATHLEN)) { 1.255 + FREE_C_HEAP_ARRAY(char, dest_file, mtInternal); 1.256 + if (PrintMiscellaneous && Verbose) { 1.257 + warning("Invalid performance data file path name specified, "\ 1.258 + "fall back to a default name"); 1.259 + } 1.260 + } else { 1.261 + return dest_file; 1.262 + } 1.263 + } 1.264 + // create the name of the file for retaining the instrumentation memory. 1.265 + dest_file = NEW_C_HEAP_ARRAY(char, PERFDATA_FILENAME_LEN, mtInternal); 1.266 + jio_snprintf(dest_file, PERFDATA_FILENAME_LEN, 1.267 + "%s_%d", PERFDATA_NAME, os::current_process_id()); 1.268 + 1.269 + return dest_file; 1.270 +}