perfMemory.cpp revision 7462:a0dd995271c4
1219820Sjeff/* 2219820Sjeff * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved. 3219820Sjeff * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4219820Sjeff * 5219820Sjeff * This code is free software; you can redistribute it and/or modify it 6219820Sjeff * under the terms of the GNU General Public License version 2 only, as 7219820Sjeff * published by the Free Software Foundation. 8219820Sjeff * 9219820Sjeff * This code is distributed in the hope that it will be useful, but WITHOUT 10219820Sjeff * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11219820Sjeff * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12219820Sjeff * version 2 for more details (a copy is included in the LICENSE file that 13219820Sjeff * accompanied this code). 14219820Sjeff * 15219820Sjeff * You should have received a copy of the GNU General Public License version 16219820Sjeff * 2 along with this work; if not, write to the Free Software Foundation, 17219820Sjeff * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18219820Sjeff * 19219820Sjeff * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20219820Sjeff * or visit www.oracle.com if you need additional information or have any 21219820Sjeff * questions. 22219820Sjeff * 23219820Sjeff */ 24219820Sjeff 25219820Sjeff#include "precompiled.hpp" 26219820Sjeff#include "memory/allocation.inline.hpp" 27219820Sjeff#include "runtime/arguments.hpp" 28219820Sjeff#include "runtime/java.hpp" 29219820Sjeff#include "runtime/mutex.hpp" 30219820Sjeff#include "runtime/mutexLocker.hpp" 31219820Sjeff#include "runtime/orderAccess.inline.hpp" 32219820Sjeff#include "runtime/os.hpp" 33219820Sjeff#include "runtime/perfData.hpp" 34219820Sjeff#include "runtime/perfMemory.hpp" 35219820Sjeff#include "runtime/statSampler.hpp" 36219820Sjeff#include "utilities/globalDefinitions.hpp" 37219820Sjeff 38219820SjeffPRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 39219820Sjeff 40219820Sjeff// Prefix of performance data file. 41219820Sjeffconst char PERFDATA_NAME[] = "hsperfdata"; 42219820Sjeff 43219820Sjeff// Add 1 for the '_' character between PERFDATA_NAME and pid. The '\0' terminating 44219820Sjeff// character will be included in the sizeof(PERFDATA_NAME) operation. 45219820Sjeffstatic const size_t PERFDATA_FILENAME_LEN = sizeof(PERFDATA_NAME) + 46219820Sjeff UINT_CHARS + 1; 47219820Sjeff 48219820Sjeffchar* PerfMemory::_start = NULL; 49219820Sjeffchar* PerfMemory::_end = NULL; 50219820Sjeffchar* PerfMemory::_top = NULL; 51219820Sjeffsize_t PerfMemory::_capacity = 0; 52219820Sjeffjint PerfMemory::_initialized = false; 53219820SjeffPerfDataPrologue* PerfMemory::_prologue = NULL; 54219820Sjeff 55219820Sjeffvoid perfMemory_init() { 56219820Sjeff 57219820Sjeff if (!UsePerfData) return; 58219820Sjeff 59219820Sjeff PerfMemory::initialize(); 60219820Sjeff} 61219820Sjeff 62219820Sjeffvoid perfMemory_exit() { 63219820Sjeff 64219820Sjeff if (!UsePerfData) return; 65219820Sjeff if (!PerfMemory::is_initialized()) return; 66219820Sjeff 67219820Sjeff // if the StatSampler is active, then we don't want to remove 68219820Sjeff // resources it may be dependent on. Typically, the StatSampler 69219820Sjeff // is disengaged from the watcher thread when this method is called, 70219820Sjeff // but it is not disengaged if this method is invoked during a 71219820Sjeff // VM abort. 72219820Sjeff // 73219820Sjeff if (!StatSampler::is_active()) 74219820Sjeff PerfDataManager::destroy(); 75219820Sjeff 76219820Sjeff // remove the persistent external resources, if any. this method 77219820Sjeff // does not unmap or invalidate any virtual memory allocated during 78219820Sjeff // initialization. 79219820Sjeff // 80219820Sjeff PerfMemory::destroy(); 81219820Sjeff} 82219820Sjeff 83219820Sjeffvoid PerfMemory::initialize() { 84219820Sjeff 85219820Sjeff if (_prologue != NULL) 86219820Sjeff // initialization already performed 87219820Sjeff return; 88219820Sjeff 89219820Sjeff size_t capacity = align_size_up(PerfDataMemorySize, 90219820Sjeff os::vm_allocation_granularity()); 91219820Sjeff 92219820Sjeff if (PerfTraceMemOps) { 93219820Sjeff tty->print("PerfDataMemorySize = " SIZE_FORMAT "," 94219820Sjeff " os::vm_allocation_granularity = " SIZE_FORMAT "," 95219820Sjeff " adjusted size = " SIZE_FORMAT "\n", 96219820Sjeff PerfDataMemorySize, 97219820Sjeff os::vm_allocation_granularity(), 98219820Sjeff capacity); 99219820Sjeff } 100219820Sjeff 101219820Sjeff // allocate PerfData memory region 102219820Sjeff create_memory_region(capacity); 103219820Sjeff 104219820Sjeff if (_start == NULL) { 105219820Sjeff 106219820Sjeff // the PerfMemory region could not be created as desired. Rather 107219820Sjeff // than terminating the JVM, we revert to creating the instrumentation 108219820Sjeff // on the C heap. When running in this mode, external monitoring 109219820Sjeff // clients cannot attach to and monitor this JVM. 110219820Sjeff // 111219820Sjeff // the warning is issued only in debug mode in order to avoid 112219820Sjeff // additional output to the stdout or stderr output streams. 113219820Sjeff // 114219820Sjeff if (PrintMiscellaneous && Verbose) { 115219820Sjeff warning("Could not create PerfData Memory region, reverting to malloc"); 116219820Sjeff } 117219820Sjeff 118219820Sjeff _prologue = NEW_C_HEAP_OBJ(PerfDataPrologue, mtInternal); 119219820Sjeff } 120219820Sjeff else { 121219820Sjeff 122219820Sjeff // the PerfMemory region was created as expected. 123219820Sjeff 124219820Sjeff if (PerfTraceMemOps) { 125219820Sjeff tty->print("PerfMemory created: address = " INTPTR_FORMAT "," 126219820Sjeff " size = " SIZE_FORMAT "\n", 127219820Sjeff (void*)_start, 128219820Sjeff _capacity); 129219820Sjeff } 130219820Sjeff 131219820Sjeff _prologue = (PerfDataPrologue *)_start; 132219820Sjeff _end = _start + _capacity; 133219820Sjeff _top = _start + sizeof(PerfDataPrologue); 134219820Sjeff } 135219820Sjeff 136219820Sjeff assert(_prologue != NULL, "prologue pointer must be initialized"); 137219820Sjeff 138219820Sjeff#ifdef VM_LITTLE_ENDIAN 139219820Sjeff _prologue->magic = (jint)0xc0c0feca; 140219820Sjeff _prologue->byte_order = PERFDATA_LITTLE_ENDIAN; 141219820Sjeff#else 142219820Sjeff _prologue->magic = (jint)0xcafec0c0; 143219820Sjeff _prologue->byte_order = PERFDATA_BIG_ENDIAN; 144219820Sjeff#endif 145219820Sjeff 146219820Sjeff _prologue->major_version = PERFDATA_MAJOR_VERSION; 147219820Sjeff _prologue->minor_version = PERFDATA_MINOR_VERSION; 148219820Sjeff _prologue->accessible = 0; 149219820Sjeff 150219820Sjeff _prologue->entry_offset = sizeof(PerfDataPrologue); 151219820Sjeff _prologue->num_entries = 0; 152219820Sjeff _prologue->used = 0; 153219820Sjeff _prologue->overflow = 0; 154219820Sjeff _prologue->mod_time_stamp = 0; 155219820Sjeff 156219820Sjeff OrderAccess::release_store(&_initialized, 1); 157219820Sjeff} 158219820Sjeff 159219820Sjeffvoid PerfMemory::destroy() { 160219820Sjeff 161219820Sjeff if (_prologue == NULL) return; 162219820Sjeff 163219820Sjeff if (_start != NULL && _prologue->overflow != 0) { 164219820Sjeff 165219820Sjeff // This state indicates that the contiguous memory region exists and 166219820Sjeff // that it wasn't large enough to hold all the counters. In this case, 167219820Sjeff // we output a warning message to the user on exit if the -XX:+Verbose 168219820Sjeff // flag is set (a debug only flag). External monitoring tools can detect 169219820Sjeff // this condition by monitoring the _prologue->overflow word. 170219820Sjeff // 171219820Sjeff // There are two tunables that can help resolve this issue: 172219820Sjeff // - increase the size of the PerfMemory with -XX:PerfDataMemorySize=<n> 173219820Sjeff // - decrease the maximum string constant length with 174219820Sjeff // -XX:PerfMaxStringConstLength=<n> 175219820Sjeff // 176219820Sjeff if (PrintMiscellaneous && Verbose) { 177219820Sjeff warning("PerfMemory Overflow Occurred.\n" 178219820Sjeff "\tCapacity = " SIZE_FORMAT " bytes" 179219820Sjeff " Used = " SIZE_FORMAT " bytes" 180219820Sjeff " Overflow = " INT32_FORMAT " bytes" 181219820Sjeff "\n\tUse -XX:PerfDataMemorySize=<size> to specify larger size.", 182219820Sjeff PerfMemory::capacity(), 183219820Sjeff PerfMemory::used(), 184219820Sjeff _prologue->overflow); 185219820Sjeff } 186219820Sjeff } 187219820Sjeff 188219820Sjeff if (_start != NULL) { 189219820Sjeff 190219820Sjeff // this state indicates that the contiguous memory region was successfully 191219820Sjeff // and that persistent resources may need to be cleaned up. This is 192219820Sjeff // expected to be the typical condition. 193219820Sjeff // 194219820Sjeff delete_memory_region(); 195219820Sjeff } 196219820Sjeff 197219820Sjeff _start = NULL; 198219820Sjeff _end = NULL; 199219820Sjeff _top = NULL; 200219820Sjeff _prologue = NULL; 201219820Sjeff _capacity = 0; 202219820Sjeff} 203219820Sjeff 204219820Sjeff// allocate an aligned block of memory from the PerfData memory 205219820Sjeff// region. This method assumes that the PerfData memory region 206219820Sjeff// was aligned on a double word boundary when created. 207219820Sjeff// 208219820Sjeffchar* PerfMemory::alloc(size_t size) { 209219820Sjeff 210219820Sjeff if (!UsePerfData) return NULL; 211219820Sjeff 212219820Sjeff MutexLocker ml(PerfDataMemAlloc_lock); 213219820Sjeff 214219820Sjeff assert(_prologue != NULL, "called before initialization"); 215219820Sjeff 216219820Sjeff // check that there is enough memory for this request 217219820Sjeff if ((_top + size) >= _end) { 218219820Sjeff 219219820Sjeff _prologue->overflow += (jint)size; 220219820Sjeff 221219820Sjeff return NULL; 222219820Sjeff } 223219820Sjeff 224219820Sjeff char* result = _top; 225219820Sjeff 226219820Sjeff _top += size; 227219820Sjeff 228219820Sjeff assert(contains(result), "PerfData memory pointer out of range"); 229219820Sjeff 230219820Sjeff _prologue->used = (jint)used(); 231219820Sjeff _prologue->num_entries = _prologue->num_entries + 1; 232219820Sjeff 233219820Sjeff return result; 234219820Sjeff} 235219820Sjeff 236219820Sjeffvoid PerfMemory::mark_updated() { 237219820Sjeff if (!UsePerfData) return; 238219820Sjeff 239219820Sjeff _prologue->mod_time_stamp = os::elapsed_counter(); 240219820Sjeff} 241219820Sjeff 242219820Sjeff// Returns the complete path including the file name of performance data file. 243219820Sjeff// Caller is expected to release the allocated memory. 244219820Sjeffchar* PerfMemory::get_perfdata_file_path() { 245219820Sjeff char* dest_file = NULL; 246219820Sjeff 247219820Sjeff if (PerfDataSaveFile != NULL) { 248219820Sjeff // dest_file_name stores the validated file name if file_name 249219820Sjeff // contains %p which will be replaced by pid. 250219820Sjeff dest_file = NEW_C_HEAP_ARRAY(char, JVM_MAXPATHLEN, mtInternal); 251219820Sjeff if(!Arguments::copy_expand_pid(PerfDataSaveFile, strlen(PerfDataSaveFile), 252219820Sjeff dest_file, JVM_MAXPATHLEN)) { 253219820Sjeff FREE_C_HEAP_ARRAY(char, dest_file); 254219820Sjeff if (PrintMiscellaneous && Verbose) { 255219820Sjeff warning("Invalid performance data file path name specified, "\ 256219820Sjeff "fall back to a default name"); 257219820Sjeff } 258219820Sjeff } else { 259219820Sjeff return dest_file; 260219820Sjeff } 261219820Sjeff } 262219820Sjeff // create the name of the file for retaining the instrumentation memory. 263219820Sjeff dest_file = NEW_C_HEAP_ARRAY(char, PERFDATA_FILENAME_LEN, mtInternal); 264219820Sjeff jio_snprintf(dest_file, PERFDATA_FILENAME_LEN, 265219820Sjeff "%s_%d", PERFDATA_NAME, os::current_process_id()); 266219820Sjeff 267219820Sjeff return dest_file; 268219820Sjeff} 269219820Sjeff