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