src/share/vm/runtime/perfMemory.cpp

Wed, 09 Apr 2008 15:10:22 -0700

author
rasbold
date
Wed, 09 Apr 2008 15:10:22 -0700
changeset 544
9f4457a14b58
parent 435
a61af66fc99e
child 869
de78b80cedec
permissions
-rw-r--r--

Merge

duke@435 1 /*
duke@435 2 * Copyright 2001-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 # include "incls/_precompiled.incl"
duke@435 26 # include "incls/_perfMemory.cpp.incl"
duke@435 27
duke@435 28 char* PerfMemory::_start = NULL;
duke@435 29 char* PerfMemory::_end = NULL;
duke@435 30 char* PerfMemory::_top = NULL;
duke@435 31 size_t PerfMemory::_capacity = 0;
duke@435 32 jint PerfMemory::_initialized = false;
duke@435 33 PerfDataPrologue* PerfMemory::_prologue = NULL;
duke@435 34
duke@435 35 void perfMemory_init() {
duke@435 36
duke@435 37 if (!UsePerfData) return;
duke@435 38
duke@435 39 PerfMemory::initialize();
duke@435 40 }
duke@435 41
duke@435 42 void perfMemory_exit() {
duke@435 43
duke@435 44 if (!UsePerfData) return;
duke@435 45 if (!PerfMemory::is_initialized()) return;
duke@435 46
duke@435 47 // if the StatSampler is active, then we don't want to remove
duke@435 48 // resources it may be dependent on. Typically, the StatSampler
duke@435 49 // is disengaged from the watcher thread when this method is called,
duke@435 50 // but it is not disengaged if this method is invoked during a
duke@435 51 // VM abort.
duke@435 52 //
duke@435 53 if (!StatSampler::is_active())
duke@435 54 PerfDataManager::destroy();
duke@435 55
duke@435 56 // remove the persistent external resources, if any. this method
duke@435 57 // does not unmap or invalidate any virtual memory allocated during
duke@435 58 // initialization.
duke@435 59 //
duke@435 60 PerfMemory::destroy();
duke@435 61 }
duke@435 62
duke@435 63 void PerfMemory::initialize() {
duke@435 64
duke@435 65 if (_prologue != NULL)
duke@435 66 // initialization already performed
duke@435 67 return;
duke@435 68
duke@435 69 size_t capacity = align_size_up(PerfDataMemorySize,
duke@435 70 os::vm_allocation_granularity());
duke@435 71
duke@435 72 if (PerfTraceMemOps) {
duke@435 73 tty->print("PerfDataMemorySize = " SIZE_FORMAT ","
duke@435 74 " os::vm_allocation_granularity = " SIZE_FORMAT ","
duke@435 75 " adjusted size = " SIZE_FORMAT "\n",
duke@435 76 PerfDataMemorySize,
duke@435 77 os::vm_allocation_granularity(),
duke@435 78 capacity);
duke@435 79 }
duke@435 80
duke@435 81 // allocate PerfData memory region
duke@435 82 create_memory_region(capacity);
duke@435 83
duke@435 84 if (_start == NULL) {
duke@435 85
duke@435 86 // the PerfMemory region could not be created as desired. Rather
duke@435 87 // than terminating the JVM, we revert to creating the instrumentation
duke@435 88 // on the C heap. When running in this mode, external monitoring
duke@435 89 // clients cannot attach to and monitor this JVM.
duke@435 90 //
duke@435 91 // the warning is issued only in debug mode in order to avoid
duke@435 92 // additional output to the stdout or stderr output streams.
duke@435 93 //
duke@435 94 if (PrintMiscellaneous && Verbose) {
duke@435 95 warning("Could not create PerfData Memory region, reverting to malloc");
duke@435 96 }
duke@435 97
duke@435 98 _prologue = NEW_C_HEAP_OBJ(PerfDataPrologue);
duke@435 99 }
duke@435 100 else {
duke@435 101
duke@435 102 // the PerfMemory region was created as expected.
duke@435 103
duke@435 104 if (PerfTraceMemOps) {
duke@435 105 tty->print("PerfMemory created: address = " INTPTR_FORMAT ","
duke@435 106 " size = " SIZE_FORMAT "\n",
duke@435 107 (void*)_start,
duke@435 108 _capacity);
duke@435 109 }
duke@435 110
duke@435 111 _prologue = (PerfDataPrologue *)_start;
duke@435 112 _end = _start + _capacity;
duke@435 113 _top = _start + sizeof(PerfDataPrologue);
duke@435 114 }
duke@435 115
duke@435 116 assert(_prologue != NULL, "prologue pointer must be initialized");
duke@435 117
duke@435 118 #ifdef VM_LITTLE_ENDIAN
duke@435 119 _prologue->magic = (jint)0xc0c0feca;
duke@435 120 _prologue->byte_order = PERFDATA_LITTLE_ENDIAN;
duke@435 121 #else
duke@435 122 _prologue->magic = (jint)0xcafec0c0;
duke@435 123 _prologue->byte_order = PERFDATA_BIG_ENDIAN;
duke@435 124 #endif
duke@435 125
duke@435 126 _prologue->major_version = PERFDATA_MAJOR_VERSION;
duke@435 127 _prologue->minor_version = PERFDATA_MINOR_VERSION;
duke@435 128 _prologue->accessible = 0;
duke@435 129
duke@435 130 _prologue->entry_offset = sizeof(PerfDataPrologue);
duke@435 131 _prologue->num_entries = 0;
duke@435 132 _prologue->used = 0;
duke@435 133 _prologue->overflow = 0;
duke@435 134 _prologue->mod_time_stamp = 0;
duke@435 135
duke@435 136 OrderAccess::release_store(&_initialized, 1);
duke@435 137 }
duke@435 138
duke@435 139 void PerfMemory::destroy() {
duke@435 140
duke@435 141 assert(_prologue != NULL, "prologue pointer must be initialized");
duke@435 142
duke@435 143 if (_start != NULL && _prologue->overflow != 0) {
duke@435 144
duke@435 145 // This state indicates that the contiguous memory region exists and
duke@435 146 // that it wasn't large enough to hold all the counters. In this case,
duke@435 147 // we output a warning message to the user on exit if the -XX:+Verbose
duke@435 148 // flag is set (a debug only flag). External monitoring tools can detect
duke@435 149 // this condition by monitoring the _prologue->overflow word.
duke@435 150 //
duke@435 151 // There are two tunables that can help resolve this issue:
duke@435 152 // - increase the size of the PerfMemory with -XX:PerfDataMemorySize=<n>
duke@435 153 // - decrease the maximum string constant length with
duke@435 154 // -XX:PerfMaxStringConstLength=<n>
duke@435 155 //
duke@435 156 if (PrintMiscellaneous && Verbose) {
duke@435 157 warning("PerfMemory Overflow Occurred.\n"
duke@435 158 "\tCapacity = " SIZE_FORMAT " bytes"
duke@435 159 " Used = " SIZE_FORMAT " bytes"
duke@435 160 " Overflow = " INT32_FORMAT " bytes"
duke@435 161 "\n\tUse -XX:PerfDataMemorySize=<size> to specify larger size.",
duke@435 162 PerfMemory::capacity(),
duke@435 163 PerfMemory::used(),
duke@435 164 _prologue->overflow);
duke@435 165 }
duke@435 166 }
duke@435 167
duke@435 168 if (_start != NULL) {
duke@435 169
duke@435 170 // this state indicates that the contiguous memory region was successfully
duke@435 171 // and that persistent resources may need to be cleaned up. This is
duke@435 172 // expected to be the typical condition.
duke@435 173 //
duke@435 174 delete_memory_region();
duke@435 175 }
duke@435 176
duke@435 177 _start = NULL;
duke@435 178 _end = NULL;
duke@435 179 _top = NULL;
duke@435 180 _prologue = NULL;
duke@435 181 _capacity = 0;
duke@435 182 }
duke@435 183
duke@435 184 // allocate an aligned block of memory from the PerfData memory
duke@435 185 // region. This method assumes that the PerfData memory region
duke@435 186 // was aligned on a double word boundary when created.
duke@435 187 //
duke@435 188 char* PerfMemory::alloc(size_t size) {
duke@435 189
duke@435 190 if (!UsePerfData) return NULL;
duke@435 191
duke@435 192 MutexLocker ml(PerfDataMemAlloc_lock);
duke@435 193
duke@435 194 assert(_prologue != NULL, "called before initialization");
duke@435 195
duke@435 196 // check that there is enough memory for this request
duke@435 197 if ((_top + size) >= _end) {
duke@435 198
duke@435 199 _prologue->overflow += (jint)size;
duke@435 200
duke@435 201 return NULL;
duke@435 202 }
duke@435 203
duke@435 204 char* result = _top;
duke@435 205
duke@435 206 _top += size;
duke@435 207
duke@435 208 assert(contains(result), "PerfData memory pointer out of range");
duke@435 209
duke@435 210 _prologue->used = (jint)used();
duke@435 211 _prologue->num_entries = _prologue->num_entries + 1;
duke@435 212
duke@435 213 return result;
duke@435 214 }
duke@435 215
duke@435 216 void PerfMemory::mark_updated() {
duke@435 217 if (!UsePerfData) return;
duke@435 218
duke@435 219 _prologue->mod_time_stamp = os::elapsed_counter();
duke@435 220 }
duke@435 221
duke@435 222 // Returns the complete path including the file name of performance data file.
duke@435 223 // Caller is expected to release the allocated memory.
duke@435 224 char* PerfMemory::get_perfdata_file_path() {
duke@435 225 char* dest_file = NULL;
duke@435 226
duke@435 227 if (PerfDataSaveFile != NULL) {
duke@435 228 // dest_file_name stores the validated file name if file_name
duke@435 229 // contains %p which will be replaced by pid.
duke@435 230 dest_file = NEW_C_HEAP_ARRAY(char, JVM_MAXPATHLEN);
duke@435 231 if(!Arguments::copy_expand_pid(PerfDataSaveFile, strlen(PerfDataSaveFile),
duke@435 232 dest_file, JVM_MAXPATHLEN)) {
duke@435 233 FREE_C_HEAP_ARRAY(char, dest_file);
duke@435 234 if (PrintMiscellaneous && Verbose) {
duke@435 235 warning("Invalid performance data file path name specified, "\
duke@435 236 "fall back to a default name");
duke@435 237 }
duke@435 238 } else {
duke@435 239 return dest_file;
duke@435 240 }
duke@435 241 }
duke@435 242 // create the name of the file for retaining the instrumentation memory.
duke@435 243 dest_file = NEW_C_HEAP_ARRAY(char, PERFDATA_FILENAME_LEN);
duke@435 244 jio_snprintf(dest_file, PERFDATA_FILENAME_LEN,
duke@435 245 "%s_%d", PERFDATA_NAME, os::current_process_id());
duke@435 246
duke@435 247 return dest_file;
duke@435 248 }

mercurial