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