src/share/vm/runtime/perfMemory.cpp

Wed, 19 Nov 2014 14:21:09 -0800

author
mchung
date
Wed, 19 Nov 2014 14:21:09 -0800
changeset 7368
fa6adc194d48
parent 6911
ce8f6bb717c9
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

8064667: Add -XX:+CheckEndorsedAndExtDirs flag to JDK 8
Reviewed-by: coleenp, ccheung

duke@435 1 /*
hseigel@6227 2 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. 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 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "memory/allocation.inline.hpp"
stefank@2314 27 #include "runtime/arguments.hpp"
stefank@2314 28 #include "runtime/java.hpp"
stefank@2314 29 #include "runtime/mutex.hpp"
stefank@2314 30 #include "runtime/mutexLocker.hpp"
goetz@6911 31 #include "runtime/orderAccess.inline.hpp"
stefank@2314 32 #include "runtime/os.hpp"
stefank@2314 33 #include "runtime/perfData.hpp"
stefank@2314 34 #include "runtime/perfMemory.hpp"
stefank@2314 35 #include "runtime/statSampler.hpp"
stefank@2314 36 #include "utilities/globalDefinitions.hpp"
duke@435 37
drchase@6680 38 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
drchase@6680 39
kvn@869 40 // Prefix of performance data file.
kvn@869 41 const char PERFDATA_NAME[] = "hsperfdata";
kvn@869 42
kvn@869 43 // Add 1 for the '_' character between PERFDATA_NAME and pid. The '\0' terminating
kvn@869 44 // character will be included in the sizeof(PERFDATA_NAME) operation.
kvn@869 45 static const size_t PERFDATA_FILENAME_LEN = sizeof(PERFDATA_NAME) +
kvn@869 46 UINT_CHARS + 1;
kvn@869 47
duke@435 48 char* PerfMemory::_start = NULL;
duke@435 49 char* PerfMemory::_end = NULL;
duke@435 50 char* PerfMemory::_top = NULL;
duke@435 51 size_t PerfMemory::_capacity = 0;
duke@435 52 jint PerfMemory::_initialized = false;
duke@435 53 PerfDataPrologue* PerfMemory::_prologue = NULL;
duke@435 54
duke@435 55 void perfMemory_init() {
duke@435 56
duke@435 57 if (!UsePerfData) return;
duke@435 58
duke@435 59 PerfMemory::initialize();
duke@435 60 }
duke@435 61
duke@435 62 void perfMemory_exit() {
duke@435 63
duke@435 64 if (!UsePerfData) return;
duke@435 65 if (!PerfMemory::is_initialized()) return;
duke@435 66
duke@435 67 // if the StatSampler is active, then we don't want to remove
duke@435 68 // resources it may be dependent on. Typically, the StatSampler
duke@435 69 // is disengaged from the watcher thread when this method is called,
duke@435 70 // but it is not disengaged if this method is invoked during a
duke@435 71 // VM abort.
duke@435 72 //
duke@435 73 if (!StatSampler::is_active())
duke@435 74 PerfDataManager::destroy();
duke@435 75
duke@435 76 // remove the persistent external resources, if any. this method
duke@435 77 // does not unmap or invalidate any virtual memory allocated during
duke@435 78 // initialization.
duke@435 79 //
duke@435 80 PerfMemory::destroy();
duke@435 81 }
duke@435 82
duke@435 83 void PerfMemory::initialize() {
duke@435 84
duke@435 85 if (_prologue != NULL)
duke@435 86 // initialization already performed
duke@435 87 return;
duke@435 88
duke@435 89 size_t capacity = align_size_up(PerfDataMemorySize,
duke@435 90 os::vm_allocation_granularity());
duke@435 91
duke@435 92 if (PerfTraceMemOps) {
duke@435 93 tty->print("PerfDataMemorySize = " SIZE_FORMAT ","
duke@435 94 " os::vm_allocation_granularity = " SIZE_FORMAT ","
duke@435 95 " adjusted size = " SIZE_FORMAT "\n",
duke@435 96 PerfDataMemorySize,
duke@435 97 os::vm_allocation_granularity(),
duke@435 98 capacity);
duke@435 99 }
duke@435 100
duke@435 101 // allocate PerfData memory region
duke@435 102 create_memory_region(capacity);
duke@435 103
duke@435 104 if (_start == NULL) {
duke@435 105
duke@435 106 // the PerfMemory region could not be created as desired. Rather
duke@435 107 // than terminating the JVM, we revert to creating the instrumentation
duke@435 108 // on the C heap. When running in this mode, external monitoring
duke@435 109 // clients cannot attach to and monitor this JVM.
duke@435 110 //
duke@435 111 // the warning is issued only in debug mode in order to avoid
duke@435 112 // additional output to the stdout or stderr output streams.
duke@435 113 //
duke@435 114 if (PrintMiscellaneous && Verbose) {
duke@435 115 warning("Could not create PerfData Memory region, reverting to malloc");
duke@435 116 }
duke@435 117
zgu@3900 118 _prologue = NEW_C_HEAP_OBJ(PerfDataPrologue, mtInternal);
duke@435 119 }
duke@435 120 else {
duke@435 121
duke@435 122 // the PerfMemory region was created as expected.
duke@435 123
duke@435 124 if (PerfTraceMemOps) {
duke@435 125 tty->print("PerfMemory created: address = " INTPTR_FORMAT ","
duke@435 126 " size = " SIZE_FORMAT "\n",
duke@435 127 (void*)_start,
duke@435 128 _capacity);
duke@435 129 }
duke@435 130
duke@435 131 _prologue = (PerfDataPrologue *)_start;
duke@435 132 _end = _start + _capacity;
duke@435 133 _top = _start + sizeof(PerfDataPrologue);
duke@435 134 }
duke@435 135
duke@435 136 assert(_prologue != NULL, "prologue pointer must be initialized");
duke@435 137
duke@435 138 #ifdef VM_LITTLE_ENDIAN
duke@435 139 _prologue->magic = (jint)0xc0c0feca;
duke@435 140 _prologue->byte_order = PERFDATA_LITTLE_ENDIAN;
duke@435 141 #else
duke@435 142 _prologue->magic = (jint)0xcafec0c0;
duke@435 143 _prologue->byte_order = PERFDATA_BIG_ENDIAN;
duke@435 144 #endif
duke@435 145
duke@435 146 _prologue->major_version = PERFDATA_MAJOR_VERSION;
duke@435 147 _prologue->minor_version = PERFDATA_MINOR_VERSION;
duke@435 148 _prologue->accessible = 0;
duke@435 149
duke@435 150 _prologue->entry_offset = sizeof(PerfDataPrologue);
duke@435 151 _prologue->num_entries = 0;
duke@435 152 _prologue->used = 0;
duke@435 153 _prologue->overflow = 0;
duke@435 154 _prologue->mod_time_stamp = 0;
duke@435 155
duke@435 156 OrderAccess::release_store(&_initialized, 1);
duke@435 157 }
duke@435 158
duke@435 159 void PerfMemory::destroy() {
duke@435 160
hseigel@6227 161 if (_prologue == NULL) return;
duke@435 162
duke@435 163 if (_start != NULL && _prologue->overflow != 0) {
duke@435 164
duke@435 165 // This state indicates that the contiguous memory region exists and
duke@435 166 // that it wasn't large enough to hold all the counters. In this case,
duke@435 167 // we output a warning message to the user on exit if the -XX:+Verbose
duke@435 168 // flag is set (a debug only flag). External monitoring tools can detect
duke@435 169 // this condition by monitoring the _prologue->overflow word.
duke@435 170 //
duke@435 171 // There are two tunables that can help resolve this issue:
duke@435 172 // - increase the size of the PerfMemory with -XX:PerfDataMemorySize=<n>
duke@435 173 // - decrease the maximum string constant length with
duke@435 174 // -XX:PerfMaxStringConstLength=<n>
duke@435 175 //
duke@435 176 if (PrintMiscellaneous && Verbose) {
duke@435 177 warning("PerfMemory Overflow Occurred.\n"
duke@435 178 "\tCapacity = " SIZE_FORMAT " bytes"
duke@435 179 " Used = " SIZE_FORMAT " bytes"
duke@435 180 " Overflow = " INT32_FORMAT " bytes"
duke@435 181 "\n\tUse -XX:PerfDataMemorySize=<size> to specify larger size.",
duke@435 182 PerfMemory::capacity(),
duke@435 183 PerfMemory::used(),
duke@435 184 _prologue->overflow);
duke@435 185 }
duke@435 186 }
duke@435 187
duke@435 188 if (_start != NULL) {
duke@435 189
duke@435 190 // this state indicates that the contiguous memory region was successfully
duke@435 191 // and that persistent resources may need to be cleaned up. This is
duke@435 192 // expected to be the typical condition.
duke@435 193 //
duke@435 194 delete_memory_region();
duke@435 195 }
duke@435 196
duke@435 197 _start = NULL;
duke@435 198 _end = NULL;
duke@435 199 _top = NULL;
duke@435 200 _prologue = NULL;
duke@435 201 _capacity = 0;
duke@435 202 }
duke@435 203
duke@435 204 // allocate an aligned block of memory from the PerfData memory
duke@435 205 // region. This method assumes that the PerfData memory region
duke@435 206 // was aligned on a double word boundary when created.
duke@435 207 //
duke@435 208 char* PerfMemory::alloc(size_t size) {
duke@435 209
duke@435 210 if (!UsePerfData) return NULL;
duke@435 211
duke@435 212 MutexLocker ml(PerfDataMemAlloc_lock);
duke@435 213
duke@435 214 assert(_prologue != NULL, "called before initialization");
duke@435 215
duke@435 216 // check that there is enough memory for this request
duke@435 217 if ((_top + size) >= _end) {
duke@435 218
duke@435 219 _prologue->overflow += (jint)size;
duke@435 220
duke@435 221 return NULL;
duke@435 222 }
duke@435 223
duke@435 224 char* result = _top;
duke@435 225
duke@435 226 _top += size;
duke@435 227
duke@435 228 assert(contains(result), "PerfData memory pointer out of range");
duke@435 229
duke@435 230 _prologue->used = (jint)used();
duke@435 231 _prologue->num_entries = _prologue->num_entries + 1;
duke@435 232
duke@435 233 return result;
duke@435 234 }
duke@435 235
duke@435 236 void PerfMemory::mark_updated() {
duke@435 237 if (!UsePerfData) return;
duke@435 238
duke@435 239 _prologue->mod_time_stamp = os::elapsed_counter();
duke@435 240 }
duke@435 241
duke@435 242 // Returns the complete path including the file name of performance data file.
duke@435 243 // Caller is expected to release the allocated memory.
duke@435 244 char* PerfMemory::get_perfdata_file_path() {
duke@435 245 char* dest_file = NULL;
duke@435 246
duke@435 247 if (PerfDataSaveFile != NULL) {
duke@435 248 // dest_file_name stores the validated file name if file_name
duke@435 249 // contains %p which will be replaced by pid.
zgu@3900 250 dest_file = NEW_C_HEAP_ARRAY(char, JVM_MAXPATHLEN, mtInternal);
duke@435 251 if(!Arguments::copy_expand_pid(PerfDataSaveFile, strlen(PerfDataSaveFile),
duke@435 252 dest_file, JVM_MAXPATHLEN)) {
zgu@3900 253 FREE_C_HEAP_ARRAY(char, dest_file, mtInternal);
duke@435 254 if (PrintMiscellaneous && Verbose) {
duke@435 255 warning("Invalid performance data file path name specified, "\
duke@435 256 "fall back to a default name");
duke@435 257 }
duke@435 258 } else {
duke@435 259 return dest_file;
duke@435 260 }
duke@435 261 }
duke@435 262 // create the name of the file for retaining the instrumentation memory.
zgu@3900 263 dest_file = NEW_C_HEAP_ARRAY(char, PERFDATA_FILENAME_LEN, mtInternal);
duke@435 264 jio_snprintf(dest_file, PERFDATA_FILENAME_LEN,
duke@435 265 "%s_%d", PERFDATA_NAME, os::current_process_id());
duke@435 266
duke@435 267 return dest_file;
duke@435 268 }

mercurial