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