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