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 /*
  26  * PerfData Version Constants
  27  *   - Major Version - change whenever the structure of PerfDataEntry changes
  28  *   - Minor Version - change whenever the data within the PerfDataEntry
  29  *                     structure changes. for example, new unit or variability
  30  *                     values are added or new PerfData subtypes are added.
  31  */
  32 #define PERFDATA_MAJOR_VERSION 2
  33 #define PERFDATA_MINOR_VERSION 0
  34 
  35 /* Byte order of the PerfData memory region. The byte order is exposed in
  36  * the PerfData memory region as the data in the memory region may have
  37  * been generated by a little endian JVM implementation. Tracking the byte
  38  * order in the PerfData memory region allows Java applications to adapt
  39  * to the native byte order for monitoring purposes. This indicator is
  40  * also useful when a snapshot of the PerfData memory region is shipped
  41  * to a machine with a native byte order different from that of the
  42  * originating machine.
  43  */
  44 #define PERFDATA_BIG_ENDIAN     0
  45 #define PERFDATA_LITTLE_ENDIAN  1
  46 
  47 /*
  48  * The PerfDataPrologue structure is known by the PerfDataBuffer Java class
  49  * libraries that read the PerfData memory region. The size and the position
  50  * of the fields must be changed along with their counterparts in the
  51  * PerfDataBuffer Java class. The first four bytes of this structure
  52  * should never change, or compatibility problems between the monitoring
  53  * applications and Hotspot VMs will result. The reserved fields are
  54  * available for future enhancements.
  55  */
  56 typedef struct {
  57   jint   magic;              // magic number - 0xcafec0c0
  58   jbyte  byte_order;         // byte order of the buffer
  59   jbyte  major_version;      // major and minor version numbers
  60   jbyte  minor_version;
  61   jbyte  accessible;         // ready to access
  62   jint   used;               // number of PerfData memory bytes used
  63   jint   overflow;           // number of bytes of overflow
  64   jlong  mod_time_stamp;     // time stamp of last structural modification
  65   jint   entry_offset;       // offset of the first PerfDataEntry
  66   jint   num_entries;        // number of allocated PerfData entries
  67 } PerfDataPrologue;
  68 
  69 /* The PerfDataEntry structure defines the fixed portion of an entry
  70  * in the PerfData memory region. The PerfDataBuffer Java libraries
  71  * are aware of this structure and need to be changed when this
  72  * structure changes.
  73  */
  74 typedef struct {
  75 
  76   jint entry_length;      // entry length in bytes
  77   jint name_offset;       // offset of the data item name
  78   jint vector_length;     // length of the vector. If 0, then scalar
  79   jbyte data_type;        // type of the data item -
  80                           // 'B','Z','J','I','S','C','D','F','V','L','['
  81   jbyte flags;            // flags indicating misc attributes
  82   jbyte data_units;       // unit of measure for the data type
  83   jbyte data_variability; // variability classification of data type
  84   jint  data_offset;      // offset of the data item
  85 
  86 /*
  87   body of PerfData memory entry is variable length
  88 
  89   jbyte[name_length] data_name;        // name of the data item
  90   jbyte[pad_length] data_pad;          // alignment of data item
  91   j<data_type>[data_length] data_item; // array of appropriate types.
  92                                        // data_length is > 1 only when the
  93                                        // data_type is T_ARRAY.
  94 */
  95 } PerfDataEntry;
  96 
  97 // Prefix of performance data file.
  98 extern const char PERFDATA_NAME[];
  99 
 100 // UINT_CHARS contains the number of characters holding a process id
 101 // (i.e. pid). pid is defined as unsigned "int" so the maximum possible pid value
 102 // would be 2^32 - 1 (4294967295) which can be represented as a 10 characters
 103 // string.
 104 static const size_t UINT_CHARS = 10;
 105 
 106 /* the PerfMemory class manages creation, destruction,
 107  * and allocation of the PerfData region.
 108  */
 109 class PerfMemory : AllStatic {
 110     friend class VMStructs;
 111   private:
 112     static char*  _start;
 113     static char*  _end;
 114     static char*  _top;
 115     static size_t _capacity;
 116     static PerfDataPrologue*  _prologue;
 117     static jint   _initialized;
 118 
 119     static void create_memory_region(size_t sizep);
 120     static void delete_memory_region();
 121 
 122   public:
 123     enum PerfMemoryMode {
 124       PERF_MODE_RO = 0,
 125       PERF_MODE_RW = 1
 126     };
 127 
 128     static char* alloc(size_t size);
 129     static char* start() { return _start; }
 130     static char* end() { return _end; }
 131     static size_t used() { return (size_t) (_top - _start); }
 132     static size_t capacity() { return _capacity; }
 133     static bool is_initialized() { return _initialized != 0; }
 134     static bool contains(char* addr) {
 135       return ((_start != NULL) && (addr >= _start) && (addr < _end));
 136     }
 137     static void mark_updated();
 138 
 139     // methods for attaching to and detaching from the PerfData
 140     // memory segment of another JVM process on the same system.
 141     static void attach(const char* user, int vmid, PerfMemoryMode mode,
 142                        char** addrp, size_t* size, TRAPS);
 143     static void detach(char* addr, size_t bytes, TRAPS);
 144 
 145     static void initialize();
 146     static void destroy();
 147     static void set_accessible(bool value) {
 148       if (UsePerfData) {
 149         _prologue->accessible = value;
 150       }
 151     }
 152 
 153     // filename of backing store or NULL if none.
 154     static char* backing_store_filename();
 155 
 156     // returns the complete file path of hsperfdata.
 157     // the caller is expected to free the allocated memory.
 158     static char* get_perfdata_file_path();
 159 };
 160 
 161 void perfMemory_init();
 162 void perfMemory_exit();