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