1 /*
   2  * Copyright (c) 2001, 2019, 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 #ifndef SHARE_RUNTIME_PERFDATA_HPP
  26 #define SHARE_RUNTIME_PERFDATA_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "runtime/perfMemory.hpp"
  30 #include "runtime/atomic.hpp"
  31 #include "runtime/timer.hpp"
  32 
  33 template <typename T> class GrowableArray;
  34 
  35 /* jvmstat global and subsystem counter name space - enumeration value
  36  * serve as an index into the PerfDataManager::_name_space[] array
  37  * containing the corresponding name space string. Only the top level
  38  * subsystem name spaces are represented here.
  39  */
  40 enum CounterNS {
  41   // top level name spaces
  42   JAVA_NS,
  43   COM_NS,
  44   SUN_NS,
  45   // subsystem name spaces
  46   JAVA_GC,              // Garbage Collection name spaces
  47   COM_GC,
  48   SUN_GC,
  49   JAVA_CI,              // Compiler name spaces
  50   COM_CI,
  51   SUN_CI,
  52   JAVA_CLS,             // Class Loader name spaces
  53   COM_CLS,
  54   SUN_CLS,
  55   JAVA_RT,              // Runtime name spaces
  56   COM_RT,
  57   SUN_RT,
  58   JAVA_OS,              // Operating System name spaces
  59   COM_OS,
  60   SUN_OS,
  61   JAVA_THREADS,         // Threads System name spaces
  62   COM_THREADS,
  63   SUN_THREADS,
  64   JAVA_PROPERTY,        // Java Property name spaces
  65   COM_PROPERTY,
  66   SUN_PROPERTY,
  67   NULL_NS,
  68   COUNTERNS_LAST = NULL_NS
  69 };
  70 
  71 /*
  72  * Classes to support access to production performance data
  73  *
  74  * The PerfData class structure is provided for creation, access, and update
  75  * of performance data (a.k.a. instrumentation) in a specific memory region
  76  * which is possibly accessible as shared memory. Although not explicitly
  77  * prevented from doing so, developers should not use the values returned
  78  * by accessor methods to make algorithmic decisions as they are potentially
  79  * extracted from a shared memory region. Although any shared memory region
  80  * created is with appropriate access restrictions, allowing read-write access
  81  * only to the principal that created the JVM, it is believed that a the
  82  * shared memory region facilitates an easier attack path than attacks
  83  * launched through mechanisms such as /proc. For this reason, it is
  84  * recommended that data returned by PerfData accessor methods be used
  85  * cautiously.
  86  *
  87  * There are three variability classifications of performance data
  88  *   Constants  -  value is written to the PerfData memory once, on creation
  89  *   Variables  -  value is modifiable, with no particular restrictions
  90  *   Counters   -  value is monotonically changing (increasing or decreasing)
  91  *
  92  * The performance data items can also have various types. The class
  93  * hierarchy and the structure of the memory region are designed to
  94  * accommodate new types as they are needed. Types are specified in
  95  * terms of Java basic types, which accommodates client applications
  96  * written in the Java programming language. The class hierarchy is:
  97  *
  98  * - PerfData (Abstract)
  99  *     - PerfLong (Abstract)
 100  *         - PerfLongConstant        (alias: PerfConstant)
 101  *         - PerfLongVariant (Abstract)
 102  *             - PerfLongVariable    (alias: PerfVariable)
 103  *             - PerfLongCounter     (alias: PerfCounter)
 104  *
 105  *     - PerfByteArray (Abstract)
 106  *         - PerfString (Abstract)
 107  *             - PerfStringVariable
 108  *             - PerfStringConstant
 109  *
 110  *
 111  * As seen in the class hierarchy, the initially supported types are:
 112  *
 113  *    Long      - performance data holds a Java long type
 114  *    ByteArray - performance data holds an array of Java bytes
 115  *                used for holding C++ char arrays.
 116  *
 117  * The String type is derived from the ByteArray type.
 118  *
 119  * A PerfData subtype is not required to provide an implementation for
 120  * each variability classification. For example, the String type provides
 121  * Variable and Constant variability classifications in the PerfStringVariable
 122  * and PerfStringConstant classes, but does not provide a counter type.
 123  *
 124  * Performance data are also described by a unit of measure. Units allow
 125  * client applications to make reasonable decisions on how to treat
 126  * performance data generically, preventing the need to hard-code the
 127  * specifics of a particular data item in client applications. The current
 128  * set of units are:
 129  *
 130  *   None        - the data has no units of measure
 131  *   Bytes       - data is measured in bytes
 132  *   Ticks       - data is measured in clock ticks
 133  *   Events      - data is measured in events. For example,
 134  *                 the number of garbage collection events or the
 135  *                 number of methods compiled.
 136  *   String      - data is not numerical. For example,
 137  *                 the java command line options
 138  *   Hertz       - data is a frequency
 139  *
 140  * The performance counters also provide a support attribute, indicating
 141  * the stability of the counter as a programmatic interface. The support
 142  * level is also implied by the name space in which the counter is created.
 143  * The counter name space support conventions follow the Java package, class,
 144  * and property support conventions:
 145  *
 146  *    java.*          - stable, supported interface
 147  *    com.sun.*       - unstable, supported interface
 148  *    sun.*           - unstable, unsupported interface
 149  *
 150  * In the above context, unstable is a measure of the interface support
 151  * level, not the implementation stability level.
 152  *
 153  * Currently, instances of PerfData subtypes are considered to have
 154  * a life time equal to that of the VM and are managed by the
 155  * PerfDataManager class. All constructors for the PerfData class and
 156  * its subtypes have protected constructors. Creation of PerfData
 157  * instances is performed by invoking various create methods on the
 158  * PerfDataManager class. Users should not attempt to delete these
 159  * instances as the PerfDataManager class expects to perform deletion
 160  * operations on exit of the VM.
 161  *
 162  * Examples:
 163  *
 164  * Creating performance counter that holds a monotonically increasing
 165  * long data value with units specified in U_Bytes in the "java.gc.*"
 166  * name space.
 167  *
 168  *   PerfLongCounter* foo_counter;
 169  *
 170  *   foo_counter = PerfDataManager::create_long_counter(JAVA_GC, "foo",
 171  *                                                       PerfData::U_Bytes,
 172  *                                                       optionalInitialValue,
 173  *                                                       CHECK);
 174  *   foo_counter->inc();
 175  *
 176  * Creating a performance counter that holds a variably change long
 177  * data value with units specified in U_Bytes in the "com.sun.ci
 178  * name space.
 179  *
 180  *   PerfLongVariable* bar_variable;
 181  *   bar_variable = PerfDataManager::create_long_variable(COM_CI, "bar",
 182 .*                                                        PerfData::U_Bytes,
 183  *                                                        optionalInitialValue,
 184  *                                                        CHECK);
 185  *
 186  *   bar_variable->inc();
 187  *   bar_variable->set_value(0);
 188  *
 189  * Creating a performance counter that holds a constant string value in
 190  * the "sun.cls.*" name space.
 191  *
 192  *   PerfDataManager::create_string_constant(SUN_CLS, "foo", string, CHECK);
 193  *
 194  *   Although the create_string_constant() factory method returns a pointer
 195  *   to the PerfStringConstant object, it can safely be ignored. Developers
 196  *   are not encouraged to access the string constant's value via this
 197  *   pointer at this time due to security concerns.
 198  *
 199  * Creating a performance counter in an arbitrary name space that holds a
 200  * value that is sampled by the StatSampler periodic task.
 201  *
 202  *    PerfDataManager::create_counter("foo.sampled", PerfData::U_Events,
 203  *                                    &my_jlong, CHECK);
 204  *
 205  *    In this example, the PerfData pointer can be ignored as the caller
 206  *    is relying on the StatSampler PeriodicTask to sample the given
 207  *    address at a regular interval. The interval is defined by the
 208  *    PerfDataSamplingInterval global variable, and is applied on
 209  *    a system wide basis, not on an per-counter basis.
 210  *
 211  * Creating a performance counter in an arbitrary name space that utilizes
 212  * a helper object to return a value to the StatSampler via the take_sample()
 213  * method.
 214  *
 215  *     class MyTimeSampler : public PerfLongSampleHelper {
 216  *       public:
 217  *         jlong take_sample() { return os::elapsed_counter(); }
 218  *     };
 219  *
 220  *     PerfDataManager::create_counter(SUN_RT, "helped",
 221  *                                     PerfData::U_Ticks,
 222  *                                     new MyTimeSampler(), CHECK);
 223  *
 224  *     In this example, a subtype of PerfLongSampleHelper is instantiated
 225  *     and its take_sample() method is overridden to perform whatever
 226  *     operation is necessary to generate the data sample. This method
 227  *     will be called by the StatSampler at a regular interval, defined
 228  *     by the PerfDataSamplingInterval global variable.
 229  *
 230  *     As before, PerfSampleHelper is an alias for PerfLongSampleHelper.
 231  *
 232  * For additional uses of PerfData subtypes, see the utility classes
 233  * PerfTraceTime and PerfTraceTimedEvent below.
 234  *
 235  * Always-on non-sampled counters can be created independent of
 236  * the UsePerfData flag. Counters will be created on the c-heap
 237  * if UsePerfData is false.
 238  *
 239  * Until further notice, all PerfData objects should be created and
 240  * manipulated within a guarded block. The guard variable is
 241  * UsePerfData, a product flag set to true by default. This flag may
 242  * be removed from the product in the future.
 243  *
 244  */
 245 class PerfData : public CHeapObj<mtInternal> {
 246 
 247   friend class StatSampler;      // for access to protected void sample()
 248   friend class PerfDataManager;  // for access to protected destructor
 249   friend class VMStructs;
 250 
 251   public:
 252 
 253     // the Variability enum must be kept in synchronization with the
 254     // the com.sun.hotspot.perfdata.Variability class
 255     enum Variability {
 256       V_Constant = 1,
 257       V_Monotonic = 2,
 258       V_Variable = 3,
 259       V_last = V_Variable
 260     };
 261 
 262     // the Units enum must be kept in synchronization with the
 263     // the com.sun.hotspot.perfdata.Units class
 264     enum Units {
 265       U_None = 1,
 266       U_Bytes = 2,
 267       U_Ticks = 3,
 268       U_Events = 4,
 269       U_String = 5,
 270       U_Hertz = 6,
 271       U_Last = U_Hertz
 272     };
 273 
 274     // Miscellaneous flags
 275     enum Flags {
 276       F_None = 0x0,
 277       F_Supported = 0x1    // interface is supported - java.* and com.sun.*
 278     };
 279 
 280   private:
 281     char* _name;
 282     Variability _v;
 283     Units _u;
 284     bool _on_c_heap;
 285     Flags _flags;
 286 
 287     PerfDataEntry* _pdep;
 288 
 289   protected:
 290 
 291     void *_valuep;
 292 
 293     PerfData(CounterNS ns, const char* name, Units u, Variability v);
 294     virtual ~PerfData();
 295 
 296     // create the entry for the PerfData item in the PerfData memory region.
 297     // this region is maintained separately from the PerfData objects to
 298     // facilitate its use by external processes.
 299     void create_entry(BasicType dtype, size_t dsize, size_t dlen = 0);
 300 
 301     // sample the data item given at creation time and write its value
 302     // into the its corresponding PerfMemory location.
 303     virtual void sample() = 0;
 304 
 305   public:
 306 
 307     // returns a boolean indicating the validity of this object.
 308     // the object is valid if and only if memory in PerfMemory
 309     // region was successfully allocated.
 310     inline bool is_valid() { return _valuep != NULL; }
 311 
 312     // returns a boolean indicating whether the underlying object
 313     // was allocated in the PerfMemory region or on the C heap.
 314     inline bool is_on_c_heap() { return _on_c_heap; }
 315 
 316     // returns a pointer to a char* containing the name of the item.
 317     // The pointer returned is the pointer to a copy of the name
 318     // passed to the constructor, not the pointer to the name in the
 319     // PerfData memory region. This redundancy is maintained for
 320     // security reasons as the PerfMemory region may be in shared
 321     // memory.
 322     const char* name() { return _name; }
 323 
 324     // returns the variability classification associated with this item
 325     Variability variability() { return _v; }
 326 
 327     // returns the units associated with this item.
 328     Units units() { return _u; }
 329 
 330     // returns the flags associated with this item.
 331     Flags flags() { return _flags; }
 332 
 333     // returns the address of the data portion of the item in the
 334     // PerfData memory region.
 335     inline void* get_address() { return _valuep; }
 336 
 337     // returns the value of the data portion of the item in the
 338     // PerfData memory region formatted as a string.
 339     virtual int format(char* cp, int length) = 0;
 340 };
 341 
 342 /*
 343  * PerfLongSampleHelper, and its alias PerfSamplerHelper, is a base class
 344  * for helper classes that rely upon the StatSampler periodic task to
 345  * invoke the take_sample() method and write the value returned to its
 346  * appropriate location in the PerfData memory region.
 347  */
 348 class PerfLongSampleHelper : public CHeapObj<mtInternal> {
 349   public:
 350     virtual jlong take_sample() = 0;
 351 };
 352 
 353 typedef PerfLongSampleHelper PerfSampleHelper;
 354 
 355 
 356 /*
 357  * PerfLong is the base class for the various Long PerfData subtypes.
 358  * it contains implementation details that are common among its derived
 359  * types.
 360  */
 361 class PerfLong : public PerfData {
 362 
 363   protected:
 364 
 365     PerfLong(CounterNS ns, const char* namep, Units u, Variability v);
 366 
 367   public:
 368     int format(char* buffer, int length);
 369 
 370     // returns the value of the data portion of the item in the
 371     // PerfData memory region.
 372     inline jlong get_value() { return *(jlong*)_valuep; }
 373 };
 374 
 375 /*
 376  * The PerfLongConstant class, and its alias PerfConstant, implement
 377  * a PerfData subtype that holds a jlong data value that is set upon
 378  * creation of an instance of this class. This class provides no
 379  * methods for changing the data value stored in PerfData memory region.
 380  */
 381 class PerfLongConstant : public PerfLong {
 382 
 383   friend class PerfDataManager; // for access to protected constructor
 384 
 385   private:
 386     // hide sample() - no need to sample constants
 387     void sample() { }
 388 
 389   protected:
 390 
 391     PerfLongConstant(CounterNS ns, const char* namep, Units u,
 392                      jlong initial_value=0)
 393                     : PerfLong(ns, namep, u, V_Constant) {
 394 
 395        if (is_valid()) *(jlong*)_valuep = initial_value;
 396     }
 397 };
 398 
 399 typedef PerfLongConstant PerfConstant;
 400 
 401 /*
 402  * The PerfLongVariant class, and its alias PerfVariant, implement
 403  * a PerfData subtype that holds a jlong data value that can be modified
 404  * in an unrestricted manner. This class provides the implementation details
 405  * for common functionality among its derived types.
 406  */
 407 class PerfLongVariant : public PerfLong {
 408 
 409   protected:
 410     jlong* _sampled;
 411     PerfLongSampleHelper* _sample_helper;
 412 
 413     PerfLongVariant(CounterNS ns, const char* namep, Units u, Variability v,
 414                     jlong initial_value=0)
 415                    : PerfLong(ns, namep, u, v) {
 416       if (is_valid()) *(jlong*)_valuep = initial_value;
 417     }
 418 
 419     PerfLongVariant(CounterNS ns, const char* namep, Units u, Variability v,
 420                     jlong* sampled);
 421 
 422     PerfLongVariant(CounterNS ns, const char* namep, Units u, Variability v,
 423                     PerfLongSampleHelper* sample_helper);
 424 
 425     void sample();
 426 
 427   public:
 428     inline void inc() { (*(jlong*)_valuep)++; }
 429     inline void inc(jlong val) { (*(jlong*)_valuep) += val; }
 430     inline void atomic_inc() { Atomic::inc((jlong*)_valuep); }
 431     inline void dec(jlong val) { inc(-val); }
 432     inline void add(jlong val) { (*(jlong*)_valuep) += val; }
 433     inline void atomic_add(jlong val) { Atomic::add(val, (jlong*)_valuep); }
 434     void clear_sample_helper() { _sample_helper = NULL; }
 435 };
 436 
 437 /*
 438  * The PerfLongCounter class, and its alias PerfCounter, implement
 439  * a PerfData subtype that holds a jlong data value that can (should)
 440  * be modified in a monotonic manner. The inc(jlong) and add(jlong)
 441  * methods can be passed negative values to implement a monotonically
 442  * decreasing value. However, we rely upon the programmer to honor
 443  * the notion that this counter always moves in the same direction -
 444  * either increasing or decreasing.
 445  */
 446 class PerfLongCounter : public PerfLongVariant {
 447 
 448   friend class PerfDataManager; // for access to protected constructor
 449 
 450   protected:
 451 
 452     PerfLongCounter(CounterNS ns, const char* namep, Units u,
 453                     jlong initial_value=0)
 454                    : PerfLongVariant(ns, namep, u, V_Monotonic,
 455                                      initial_value) { }
 456 
 457     PerfLongCounter(CounterNS ns, const char* namep, Units u, jlong* sampled)
 458                   : PerfLongVariant(ns, namep, u, V_Monotonic, sampled) { }
 459 
 460     PerfLongCounter(CounterNS ns, const char* namep, Units u,
 461                     PerfLongSampleHelper* sample_helper)
 462                    : PerfLongVariant(ns, namep, u, V_Monotonic,
 463                                      sample_helper) { }
 464 };
 465 
 466 typedef PerfLongCounter PerfCounter;
 467 
 468 /*
 469  * The PerfLongVariable class, and its alias PerfVariable, implement
 470  * a PerfData subtype that holds a jlong data value that can
 471  * be modified in an unrestricted manner.
 472  */
 473 class PerfLongVariable : public PerfLongVariant {
 474 
 475   friend class PerfDataManager; // for access to protected constructor
 476 
 477   protected:
 478 
 479     PerfLongVariable(CounterNS ns, const char* namep, Units u,
 480                      jlong initial_value=0)
 481                     : PerfLongVariant(ns, namep, u, V_Variable,
 482                                       initial_value) { }
 483 
 484     PerfLongVariable(CounterNS ns, const char* namep, Units u, jlong* sampled)
 485                     : PerfLongVariant(ns, namep, u, V_Variable, sampled) { }
 486 
 487     PerfLongVariable(CounterNS ns, const char* namep, Units u,
 488                      PerfLongSampleHelper* sample_helper)
 489                     : PerfLongVariant(ns, namep, u, V_Variable,
 490                                       sample_helper) { }
 491 
 492   public:
 493     inline void set_value(jlong val) { (*(jlong*)_valuep) = val; }
 494 };
 495 
 496 typedef PerfLongVariable PerfVariable;
 497 
 498 /*
 499  * The PerfByteArray provides a PerfData subtype that allows the creation
 500  * of a contiguous region of the PerfData memory region for storing a vector
 501  * of bytes. This class is currently intended to be a base class for
 502  * the PerfString class, and cannot be instantiated directly.
 503  */
 504 class PerfByteArray : public PerfData {
 505 
 506   protected:
 507     jint _length;
 508 
 509     PerfByteArray(CounterNS ns, const char* namep, Units u, Variability v,
 510                   jint length);
 511 };
 512 
 513 class PerfString : public PerfByteArray {
 514 
 515   protected:
 516 
 517     void set_string(const char* s2);
 518 
 519     PerfString(CounterNS ns, const char* namep, Variability v, jint length,
 520                const char* initial_value)
 521               : PerfByteArray(ns, namep, U_String, v, length) {
 522        if (is_valid()) set_string(initial_value);
 523     }
 524 
 525   public:
 526 
 527     int format(char* buffer, int length);
 528 };
 529 
 530 /*
 531  * The PerfStringConstant class provides a PerfData sub class that
 532  * allows a null terminated string of single byte characters to be
 533  * stored in the PerfData memory region.
 534  */
 535 class PerfStringConstant : public PerfString {
 536 
 537   friend class PerfDataManager; // for access to protected constructor
 538 
 539   private:
 540 
 541     // hide sample() - no need to sample constants
 542     void sample() { }
 543 
 544   protected:
 545 
 546     // Restrict string constant lengths to be <= PerfMaxStringConstLength.
 547     // This prevents long string constants, as can occur with very
 548     // long classpaths or java command lines, from consuming too much
 549     // PerfData memory.
 550     PerfStringConstant(CounterNS ns, const char* namep,
 551                        const char* initial_value);
 552 };
 553 
 554 /*
 555  * The PerfStringVariable class provides a PerfData sub class that
 556  * allows a null terminated string of single byte character data
 557  * to be stored in PerfData memory region. The string value can be reset
 558  * after initialization. If the string value is >= max_length, then
 559  * it will be truncated to max_length characters. The copied string
 560  * is always null terminated.
 561  */
 562 class PerfStringVariable : public PerfString {
 563 
 564   friend class PerfDataManager; // for access to protected constructor
 565 
 566   protected:
 567 
 568     // sampling of string variables are not yet supported
 569     void sample() { }
 570 
 571     PerfStringVariable(CounterNS ns, const char* namep, jint max_length,
 572                        const char* initial_value)
 573                       : PerfString(ns, namep, V_Variable, max_length+1,
 574                                    initial_value) { }
 575 
 576   public:
 577     inline void set_value(const char* val) { set_string(val); }
 578 };
 579 
 580 
 581 /*
 582  * The PerfDataList class is a container class for managing lists
 583  * of PerfData items. The intention of this class is to allow for
 584  * alternative implementations for management of list of PerfData
 585  * items without impacting the code that uses the lists.
 586  *
 587  * The initial implementation is based upon GrowableArray. Searches
 588  * on GrowableArray types is linear in nature and this may become
 589  * a performance issue for creation of PerfData items, particularly
 590  * from Java code where a test for existence is implemented as a
 591  * search over all existing PerfData items.
 592  *
 593  * The abstraction is not complete. A more general container class
 594  * would provide an Iterator abstraction that could be used to
 595  * traverse the lists. This implementation still relies upon integer
 596  * iterators and the at(int index) method. However, the GrowableArray
 597  * is not directly visible outside this class and can be replaced by
 598  * some other implementation, as long as that implementation provides
 599  * a mechanism to iterate over the container by index.
 600  */
 601 class PerfDataList : public CHeapObj<mtInternal> {
 602 
 603   private:
 604 
 605     // GrowableArray implementation
 606     typedef GrowableArray<PerfData*> PerfDataArray;
 607 
 608     PerfDataArray* _set;
 609 
 610     // method to search for a instrumentation object by name
 611     static bool by_name(void* name, PerfData* pd);
 612 
 613   protected:
 614     // we expose the implementation here to facilitate the clone
 615     // method.
 616     PerfDataArray* get_impl() { return _set; }
 617 
 618   public:
 619 
 620     // create a PerfDataList with the given initial length
 621     PerfDataList(int length);
 622 
 623     // create a PerfDataList as a shallow copy of the given PerfDataList
 624     PerfDataList(PerfDataList* p);
 625 
 626     ~PerfDataList();
 627 
 628     // return the PerfData item indicated by name,
 629     // or NULL if it doesn't exist.
 630     PerfData* find_by_name(const char* name);
 631 
 632     // return true if a PerfData item with the name specified in the
 633     // argument exists, otherwise return false.
 634     bool contains(const char* name) { return find_by_name(name) != NULL; }
 635 
 636     // return the number of PerfData items in this list
 637     inline int length();
 638 
 639     // add a PerfData item to this list
 640     inline void append(PerfData *p);
 641 
 642     // remove the given PerfData item from this list. When called
 643     // while iterating over the list, this method will result in a
 644     // change in the length of the container. The at(int index)
 645     // method is also impacted by this method as elements with an
 646     // index greater than the index of the element removed by this
 647     // method will be shifted down by one.
 648     inline void remove(PerfData *p);
 649 
 650     // create a new PerfDataList from this list. The new list is
 651     // a shallow copy of the original list and care should be taken
 652     // with respect to delete operations on the elements of the list
 653     // as the are likely in use by another copy of the list.
 654     PerfDataList* clone();
 655 
 656     // for backward compatibility with GrowableArray - need to implement
 657     // some form of iterator to provide a cleaner abstraction for
 658     // iteration over the container.
 659     inline PerfData* at(int index);
 660 };
 661 
 662 
 663 /*
 664  * The PerfDataManager class is responsible for creating PerfData
 665  * subtypes via a set a factory methods and for managing lists
 666  * of the various PerfData types.
 667  */
 668 class PerfDataManager : AllStatic {
 669 
 670   friend class StatSampler;   // for access to protected PerfDataList methods
 671 
 672   private:
 673     static PerfDataList* _all;
 674     static PerfDataList* _sampled;
 675     static PerfDataList* _constants;
 676     static const char* _name_spaces[];
 677     static volatile bool _has_PerfData;
 678 
 679     // add a PerfData item to the list(s) of know PerfData objects
 680     static void add_item(PerfData* p, bool sampled);
 681 
 682   protected:
 683     // return the list of all known PerfData items
 684     static PerfDataList* all();
 685     static inline int count();
 686 
 687     // return the list of all known PerfData items that are to be
 688     // sampled by the StatSampler.
 689     static PerfDataList* sampled();
 690     static inline int sampled_count();
 691 
 692     // return the list of all known PerfData items that have a
 693     // variability classification of type Constant
 694     static PerfDataList* constants();
 695     static inline int constants_count();
 696 
 697   public:
 698 
 699     // method to check for the existence of a PerfData item with
 700     // the given name.
 701     static inline bool exists(const char* name);
 702 
 703     // method to search for a instrumentation object by name
 704     static PerfData* find_by_name(const char* name);
 705 
 706     // method to map a CounterNS enumeration to a namespace string
 707     static const char* ns_to_string(CounterNS ns) {
 708       return _name_spaces[ns];
 709     }
 710 
 711     // methods to test the interface stability of a given counter namespace
 712     //
 713     static bool is_stable_supported(CounterNS ns) {
 714       return (ns != NULL_NS) && ((ns % 3) == JAVA_NS);
 715     }
 716     static bool is_unstable_supported(CounterNS ns) {
 717       return (ns != NULL_NS) && ((ns % 3) == COM_NS);
 718     }
 719     static bool is_unstable_unsupported(CounterNS ns) {
 720       return (ns == NULL_NS) || ((ns % 3) == SUN_NS);
 721     }
 722 
 723     // methods to test the interface stability of a given counter name
 724     //
 725     static bool is_stable_supported(const char* name) {
 726       const char* javadot = "java.";
 727       return strncmp(name, javadot, strlen(javadot)) == 0;
 728     }
 729     static bool is_unstable_supported(const char* name) {
 730       const char* comdot = "com.sun.";
 731       return strncmp(name, comdot, strlen(comdot)) == 0;
 732     }
 733     static bool is_unstable_unsupported(const char* name) {
 734       return !(is_stable_supported(name) && is_unstable_supported(name));
 735     }
 736 
 737     // method to construct counter name strings in a given name space.
 738     // The string object is allocated from the Resource Area and calls
 739     // to this method must be made within a ResourceMark.
 740     //
 741     static char* counter_name(const char* name_space, const char* name);
 742 
 743     // method to construct name space strings in a given name space.
 744     // The string object is allocated from the Resource Area and calls
 745     // to this method must be made within a ResourceMark.
 746     //
 747     static char* name_space(const char* name_space, const char* sub_space) {
 748       return counter_name(name_space, sub_space);
 749     }
 750 
 751     // same as above, but appends the instance number to the name space
 752     //
 753     static char* name_space(const char* name_space, const char* sub_space,
 754                             int instance);
 755     static char* name_space(const char* name_space, int instance);
 756 
 757 
 758     // these methods provide the general interface for creating
 759     // performance data resources. The types of performance data
 760     // resources can be extended by adding additional create<type>
 761     // methods.
 762 
 763     // Constant Types
 764     static PerfStringConstant* create_string_constant(CounterNS ns,
 765                                                       const char* name,
 766                                                       const char *s, TRAPS);
 767 
 768     static PerfLongConstant* create_long_constant(CounterNS ns,
 769                                                   const char* name,
 770                                                   PerfData::Units u,
 771                                                   jlong val, TRAPS);
 772 
 773 
 774     // Variable Types
 775     static PerfStringVariable* create_string_variable(CounterNS ns,
 776                                                       const char* name,
 777                                                       int max_length,
 778                                                       const char *s, TRAPS);
 779 
 780     static PerfStringVariable* create_string_variable(CounterNS ns,
 781                                                       const char* name,
 782                                                       const char *s, TRAPS) {
 783       return create_string_variable(ns, name, 0, s, THREAD);
 784     };
 785 
 786     static PerfLongVariable* create_long_variable(CounterNS ns,
 787                                                   const char* name,
 788                                                   PerfData::Units u,
 789                                                   jlong ival, TRAPS);
 790 
 791     static PerfLongVariable* create_long_variable(CounterNS ns,
 792                                                   const char* name,
 793                                                   PerfData::Units u, TRAPS) {
 794       return create_long_variable(ns, name, u, (jlong)0, THREAD);
 795     };
 796 
 797     static PerfLongVariable* create_long_variable(CounterNS, const char* name,
 798                                                   PerfData::Units u,
 799                                                   jlong* sp, TRAPS);
 800 
 801     static PerfLongVariable* create_long_variable(CounterNS ns,
 802                                                   const char* name,
 803                                                   PerfData::Units u,
 804                                                   PerfLongSampleHelper* sh,
 805                                                   TRAPS);
 806 
 807 
 808     // Counter Types
 809     static PerfLongCounter* create_long_counter(CounterNS ns, const char* name,
 810                                                 PerfData::Units u,
 811                                                 jlong ival, TRAPS);
 812 
 813     static PerfLongCounter* create_long_counter(CounterNS ns, const char* name,
 814                                                 PerfData::Units u, TRAPS) {
 815       return create_long_counter(ns, name, u, (jlong)0, THREAD);
 816     };
 817 
 818     static PerfLongCounter* create_long_counter(CounterNS ns, const char* name,
 819                                                 PerfData::Units u, jlong* sp,
 820                                                 TRAPS);
 821 
 822     static PerfLongCounter* create_long_counter(CounterNS ns, const char* name,
 823                                                 PerfData::Units u,
 824                                                 PerfLongSampleHelper* sh,
 825                                                 TRAPS);
 826 
 827 
 828     // these creation methods are provided for ease of use. These allow
 829     // Long performance data types to be created with a shorthand syntax.
 830 
 831     static PerfConstant* create_constant(CounterNS ns, const char* name,
 832                                          PerfData::Units u, jlong val, TRAPS) {
 833       return create_long_constant(ns, name, u, val, THREAD);
 834     }
 835 
 836     static PerfVariable* create_variable(CounterNS ns, const char* name,
 837                                          PerfData::Units u, jlong ival, TRAPS) {
 838       return create_long_variable(ns, name, u, ival, THREAD);
 839     }
 840 
 841     static PerfVariable* create_variable(CounterNS ns, const char* name,
 842                                          PerfData::Units u, TRAPS) {
 843       return create_long_variable(ns, name, u, (jlong)0, THREAD);
 844     }
 845 
 846     static PerfVariable* create_variable(CounterNS ns, const char* name,
 847                                          PerfData::Units u, jlong* sp, TRAPS) {
 848       return create_long_variable(ns, name, u, sp, THREAD);
 849     }
 850 
 851     static PerfVariable* create_variable(CounterNS ns, const char* name,
 852                                          PerfData::Units u,
 853                                          PerfSampleHelper* sh, TRAPS) {
 854       return create_long_variable(ns, name, u, sh, THREAD);
 855     }
 856 
 857     static PerfCounter* create_counter(CounterNS ns, const char* name,
 858                                        PerfData::Units u, jlong ival, TRAPS) {
 859       return create_long_counter(ns, name, u, ival, THREAD);
 860     }
 861 
 862     static PerfCounter* create_counter(CounterNS ns, const char* name,
 863                                        PerfData::Units u, TRAPS) {
 864       return create_long_counter(ns, name, u, (jlong)0, THREAD);
 865     }
 866 
 867     static PerfCounter* create_counter(CounterNS ns, const char* name,
 868                                        PerfData::Units u, jlong* sp, TRAPS) {
 869       return create_long_counter(ns, name, u, sp, THREAD);
 870     }
 871 
 872     static PerfCounter* create_counter(CounterNS ns, const char* name,
 873                                        PerfData::Units u,
 874                                        PerfSampleHelper* sh, TRAPS) {
 875       return create_long_counter(ns, name, u, sh, THREAD);
 876     }
 877 
 878     static void destroy();
 879     static bool has_PerfData() { return _has_PerfData; }
 880 };
 881 
 882 // Useful macros to create the performance counters
 883 #define NEWPERFTICKCOUNTER(counter, counter_ns, counter_name)  \
 884   {counter = PerfDataManager::create_counter(counter_ns, counter_name, \
 885                                              PerfData::U_Ticks,CHECK);}
 886 
 887 #define NEWPERFEVENTCOUNTER(counter, counter_ns, counter_name)  \
 888   {counter = PerfDataManager::create_counter(counter_ns, counter_name, \
 889                                              PerfData::U_Events,CHECK);}
 890 
 891 #define NEWPERFBYTECOUNTER(counter, counter_ns, counter_name)  \
 892   {counter = PerfDataManager::create_counter(counter_ns, counter_name, \
 893                                              PerfData::U_Bytes,CHECK);}
 894 
 895 // Utility Classes
 896 
 897 /*
 898  * this class will administer a PerfCounter used as a time accumulator
 899  * for a basic block much like the TraceTime class.
 900  *
 901  * Example:
 902  *
 903  *    static PerfCounter* my_time_counter = PerfDataManager::create_counter("my.time.counter", PerfData::U_Ticks, 0LL, CHECK);
 904  *
 905  *    {
 906  *      PerfTraceTime ptt(my_time_counter);
 907  *      // perform the operation you want to measure
 908  *    }
 909  *
 910  * Note: use of this class does not need to occur within a guarded
 911  * block. The UsePerfData guard is used with the implementation
 912  * of this class.
 913  */
 914 class PerfTraceTime : public StackObj {
 915 
 916   protected:
 917     elapsedTimer _t;
 918     PerfLongCounter* _timerp;
 919     // pointer to thread-local or global recursion counter variable
 920     int* _recursion_counter;
 921 
 922   public:
 923     inline PerfTraceTime(PerfLongCounter* timerp) : _timerp(timerp), _recursion_counter(NULL) {
 924       if (!UsePerfData) return;
 925       _t.start();
 926     }
 927 
 928     inline PerfTraceTime(PerfLongCounter* timerp, int* recursion_counter) : _timerp(timerp), _recursion_counter(recursion_counter) {
 929       if (!UsePerfData || (_recursion_counter != NULL &&
 930                            (*_recursion_counter)++ > 0)) return;
 931       _t.start();
 932     }
 933 
 934     inline void suspend() { if (!UsePerfData) return; _t.stop(); }
 935     inline void resume() { if (!UsePerfData) return; _t.start(); }
 936 
 937     ~PerfTraceTime();
 938 };
 939 
 940 /* The PerfTraceTimedEvent class is responsible for counting the
 941  * occurrence of some event and measuring the the elapsed time of
 942  * the event in two separate PerfCounter instances.
 943  *
 944  * Example:
 945  *
 946  *    static PerfCounter* my_time_counter = PerfDataManager::create_counter("my.time.counter", PerfData::U_Ticks, CHECK);
 947  *    static PerfCounter* my_event_counter = PerfDataManager::create_counter("my.event.counter", PerfData::U_Events, CHECK);
 948  *
 949  *    {
 950  *      PerfTraceTimedEvent ptte(my_time_counter, my_event_counter);
 951  *      // perform the operation you want to count and measure
 952  *    }
 953  *
 954  * Note: use of this class does not need to occur within a guarded
 955  * block. The UsePerfData guard is used with the implementation
 956  * of this class.
 957  *
 958  */
 959 class PerfTraceTimedEvent : public PerfTraceTime {
 960 
 961   protected:
 962     PerfLongCounter* _eventp;
 963 
 964   public:
 965     inline PerfTraceTimedEvent(PerfLongCounter* timerp, PerfLongCounter* eventp): PerfTraceTime(timerp), _eventp(eventp) {
 966       if (!UsePerfData) return;
 967       _eventp->inc();
 968     }
 969 
 970     inline PerfTraceTimedEvent(PerfLongCounter* timerp, PerfLongCounter* eventp, int* recursion_counter): PerfTraceTime(timerp, recursion_counter), _eventp(eventp) {
 971       if (!UsePerfData) return;
 972       _eventp->inc();
 973     }
 974 };
 975 
 976 #endif // SHARE_RUNTIME_PERFDATA_HPP