src/share/vm/runtime/perfMemory.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 6911
ce8f6bb717c9
parent 6876
710a3c8b516e
permissions
-rw-r--r--

merge

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

mercurial