duke@435: /* duke@435: * Copyright 2001-2006 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: # include "incls/_precompiled.incl" duke@435: # include "incls/_perfMemory.cpp.incl" duke@435: duke@435: char* PerfMemory::_start = NULL; duke@435: char* PerfMemory::_end = NULL; duke@435: char* PerfMemory::_top = NULL; duke@435: size_t PerfMemory::_capacity = 0; duke@435: jint PerfMemory::_initialized = false; duke@435: PerfDataPrologue* PerfMemory::_prologue = NULL; duke@435: duke@435: void perfMemory_init() { duke@435: duke@435: if (!UsePerfData) return; duke@435: duke@435: PerfMemory::initialize(); duke@435: } duke@435: duke@435: void perfMemory_exit() { duke@435: duke@435: if (!UsePerfData) return; duke@435: if (!PerfMemory::is_initialized()) return; duke@435: duke@435: // if the StatSampler is active, then we don't want to remove duke@435: // resources it may be dependent on. Typically, the StatSampler duke@435: // is disengaged from the watcher thread when this method is called, duke@435: // but it is not disengaged if this method is invoked during a duke@435: // VM abort. duke@435: // duke@435: if (!StatSampler::is_active()) duke@435: PerfDataManager::destroy(); duke@435: duke@435: // remove the persistent external resources, if any. this method duke@435: // does not unmap or invalidate any virtual memory allocated during duke@435: // initialization. duke@435: // duke@435: PerfMemory::destroy(); duke@435: } duke@435: duke@435: void PerfMemory::initialize() { duke@435: duke@435: if (_prologue != NULL) duke@435: // initialization already performed duke@435: return; duke@435: duke@435: size_t capacity = align_size_up(PerfDataMemorySize, duke@435: os::vm_allocation_granularity()); duke@435: duke@435: if (PerfTraceMemOps) { duke@435: tty->print("PerfDataMemorySize = " SIZE_FORMAT "," duke@435: " os::vm_allocation_granularity = " SIZE_FORMAT "," duke@435: " adjusted size = " SIZE_FORMAT "\n", duke@435: PerfDataMemorySize, duke@435: os::vm_allocation_granularity(), duke@435: capacity); duke@435: } duke@435: duke@435: // allocate PerfData memory region duke@435: create_memory_region(capacity); duke@435: duke@435: if (_start == NULL) { duke@435: duke@435: // the PerfMemory region could not be created as desired. Rather duke@435: // than terminating the JVM, we revert to creating the instrumentation duke@435: // on the C heap. When running in this mode, external monitoring duke@435: // clients cannot attach to and monitor this JVM. duke@435: // duke@435: // the warning is issued only in debug mode in order to avoid duke@435: // additional output to the stdout or stderr output streams. duke@435: // duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("Could not create PerfData Memory region, reverting to malloc"); duke@435: } duke@435: duke@435: _prologue = NEW_C_HEAP_OBJ(PerfDataPrologue); duke@435: } duke@435: else { duke@435: duke@435: // the PerfMemory region was created as expected. duke@435: duke@435: if (PerfTraceMemOps) { duke@435: tty->print("PerfMemory created: address = " INTPTR_FORMAT "," duke@435: " size = " SIZE_FORMAT "\n", duke@435: (void*)_start, duke@435: _capacity); duke@435: } duke@435: duke@435: _prologue = (PerfDataPrologue *)_start; duke@435: _end = _start + _capacity; duke@435: _top = _start + sizeof(PerfDataPrologue); duke@435: } duke@435: duke@435: assert(_prologue != NULL, "prologue pointer must be initialized"); duke@435: duke@435: #ifdef VM_LITTLE_ENDIAN duke@435: _prologue->magic = (jint)0xc0c0feca; duke@435: _prologue->byte_order = PERFDATA_LITTLE_ENDIAN; duke@435: #else duke@435: _prologue->magic = (jint)0xcafec0c0; duke@435: _prologue->byte_order = PERFDATA_BIG_ENDIAN; duke@435: #endif duke@435: duke@435: _prologue->major_version = PERFDATA_MAJOR_VERSION; duke@435: _prologue->minor_version = PERFDATA_MINOR_VERSION; duke@435: _prologue->accessible = 0; duke@435: duke@435: _prologue->entry_offset = sizeof(PerfDataPrologue); duke@435: _prologue->num_entries = 0; duke@435: _prologue->used = 0; duke@435: _prologue->overflow = 0; duke@435: _prologue->mod_time_stamp = 0; duke@435: duke@435: OrderAccess::release_store(&_initialized, 1); duke@435: } duke@435: duke@435: void PerfMemory::destroy() { duke@435: duke@435: assert(_prologue != NULL, "prologue pointer must be initialized"); duke@435: duke@435: if (_start != NULL && _prologue->overflow != 0) { duke@435: duke@435: // This state indicates that the contiguous memory region exists and duke@435: // that it wasn't large enough to hold all the counters. In this case, duke@435: // we output a warning message to the user on exit if the -XX:+Verbose duke@435: // flag is set (a debug only flag). External monitoring tools can detect duke@435: // this condition by monitoring the _prologue->overflow word. duke@435: // duke@435: // There are two tunables that can help resolve this issue: duke@435: // - increase the size of the PerfMemory with -XX:PerfDataMemorySize= duke@435: // - decrease the maximum string constant length with duke@435: // -XX:PerfMaxStringConstLength= duke@435: // duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("PerfMemory Overflow Occurred.\n" duke@435: "\tCapacity = " SIZE_FORMAT " bytes" duke@435: " Used = " SIZE_FORMAT " bytes" duke@435: " Overflow = " INT32_FORMAT " bytes" duke@435: "\n\tUse -XX:PerfDataMemorySize= to specify larger size.", duke@435: PerfMemory::capacity(), duke@435: PerfMemory::used(), duke@435: _prologue->overflow); duke@435: } duke@435: } duke@435: duke@435: if (_start != NULL) { duke@435: duke@435: // this state indicates that the contiguous memory region was successfully duke@435: // and that persistent resources may need to be cleaned up. This is duke@435: // expected to be the typical condition. duke@435: // duke@435: delete_memory_region(); duke@435: } duke@435: duke@435: _start = NULL; duke@435: _end = NULL; duke@435: _top = NULL; duke@435: _prologue = NULL; duke@435: _capacity = 0; duke@435: } duke@435: duke@435: // allocate an aligned block of memory from the PerfData memory duke@435: // region. This method assumes that the PerfData memory region duke@435: // was aligned on a double word boundary when created. duke@435: // duke@435: char* PerfMemory::alloc(size_t size) { duke@435: duke@435: if (!UsePerfData) return NULL; duke@435: duke@435: MutexLocker ml(PerfDataMemAlloc_lock); duke@435: duke@435: assert(_prologue != NULL, "called before initialization"); duke@435: duke@435: // check that there is enough memory for this request duke@435: if ((_top + size) >= _end) { duke@435: duke@435: _prologue->overflow += (jint)size; duke@435: duke@435: return NULL; duke@435: } duke@435: duke@435: char* result = _top; duke@435: duke@435: _top += size; duke@435: duke@435: assert(contains(result), "PerfData memory pointer out of range"); duke@435: duke@435: _prologue->used = (jint)used(); duke@435: _prologue->num_entries = _prologue->num_entries + 1; duke@435: duke@435: return result; duke@435: } duke@435: duke@435: void PerfMemory::mark_updated() { duke@435: if (!UsePerfData) return; duke@435: duke@435: _prologue->mod_time_stamp = os::elapsed_counter(); duke@435: } duke@435: duke@435: // Returns the complete path including the file name of performance data file. duke@435: // Caller is expected to release the allocated memory. duke@435: char* PerfMemory::get_perfdata_file_path() { duke@435: char* dest_file = NULL; duke@435: duke@435: if (PerfDataSaveFile != NULL) { duke@435: // dest_file_name stores the validated file name if file_name duke@435: // contains %p which will be replaced by pid. duke@435: dest_file = NEW_C_HEAP_ARRAY(char, JVM_MAXPATHLEN); duke@435: if(!Arguments::copy_expand_pid(PerfDataSaveFile, strlen(PerfDataSaveFile), duke@435: dest_file, JVM_MAXPATHLEN)) { duke@435: FREE_C_HEAP_ARRAY(char, dest_file); duke@435: if (PrintMiscellaneous && Verbose) { duke@435: warning("Invalid performance data file path name specified, "\ duke@435: "fall back to a default name"); duke@435: } duke@435: } else { duke@435: return dest_file; duke@435: } duke@435: } duke@435: // create the name of the file for retaining the instrumentation memory. duke@435: dest_file = NEW_C_HEAP_ARRAY(char, PERFDATA_FILENAME_LEN); duke@435: jio_snprintf(dest_file, PERFDATA_FILENAME_LEN, duke@435: "%s_%d", PERFDATA_NAME, os::current_process_id()); duke@435: duke@435: return dest_file; duke@435: }