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