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