aoqi@0: /* aoqi@0: * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_RUNTIME_PERFDATA_HPP aoqi@0: #define SHARE_VM_RUNTIME_PERFDATA_HPP aoqi@0: aoqi@0: #include "memory/allocation.inline.hpp" aoqi@0: #include "runtime/perfMemory.hpp" aoqi@0: #include "runtime/timer.hpp" aoqi@0: #include "utilities/growableArray.hpp" aoqi@0: aoqi@0: /* jvmstat global and subsystem counter name space - enumeration value aoqi@0: * serve as an index into the PerfDataManager::_name_space[] array aoqi@0: * containing the corresponding name space string. Only the top level aoqi@0: * subsystem name spaces are represented here. aoqi@0: */ aoqi@0: enum CounterNS { aoqi@0: // top level name spaces aoqi@0: JAVA_NS, aoqi@0: COM_NS, aoqi@0: SUN_NS, aoqi@0: // subsystem name spaces aoqi@0: JAVA_GC, // Garbage Collection name spaces aoqi@0: COM_GC, aoqi@0: SUN_GC, aoqi@0: JAVA_CI, // Compiler name spaces aoqi@0: COM_CI, aoqi@0: SUN_CI, aoqi@0: JAVA_CLS, // Class Loader name spaces aoqi@0: COM_CLS, aoqi@0: SUN_CLS, aoqi@0: JAVA_RT, // Runtime name spaces aoqi@0: COM_RT, aoqi@0: SUN_RT, aoqi@0: JAVA_OS, // Operating System name spaces aoqi@0: COM_OS, aoqi@0: SUN_OS, aoqi@0: JAVA_THREADS, // Threads System name spaces aoqi@0: COM_THREADS, aoqi@0: SUN_THREADS, aoqi@0: JAVA_PROPERTY, // Java Property name spaces aoqi@0: COM_PROPERTY, aoqi@0: SUN_PROPERTY, aoqi@0: NULL_NS, aoqi@0: COUNTERNS_LAST = NULL_NS aoqi@0: }; aoqi@0: aoqi@0: /* aoqi@0: * Classes to support access to production performance data aoqi@0: * aoqi@0: * The PerfData class structure is provided for creation, access, and update aoqi@0: * of performance data (a.k.a. instrumentation) in a specific memory region aoqi@0: * which is possibly accessible as shared memory. Although not explicitly aoqi@0: * prevented from doing so, developers should not use the values returned aoqi@0: * by accessor methods to make algorithmic decisions as they are potentially aoqi@0: * extracted from a shared memory region. Although any shared memory region aoqi@0: * created is with appropriate access restrictions, allowing read-write access aoqi@0: * only to the principal that created the JVM, it is believed that a the aoqi@0: * shared memory region facilitates an easier attack path than attacks aoqi@0: * launched through mechanisms such as /proc. For this reason, it is aoqi@0: * recommended that data returned by PerfData accessor methods be used aoqi@0: * cautiously. aoqi@0: * aoqi@0: * There are three variability classifications of performance data aoqi@0: * Constants - value is written to the PerfData memory once, on creation aoqi@0: * Variables - value is modifiable, with no particular restrictions aoqi@0: * Counters - value is monotonically changing (increasing or decreasing) aoqi@0: * aoqi@0: * The performance data items can also have various types. The class aoqi@0: * hierarchy and the structure of the memory region are designed to aoqi@0: * accommodate new types as they are needed. Types are specified in aoqi@0: * terms of Java basic types, which accommodates client applications aoqi@0: * written in the Java programming language. The class hierarchy is: aoqi@0: * aoqi@0: * - PerfData (Abstract) aoqi@0: * - PerfLong (Abstract) aoqi@0: * - PerfLongConstant (alias: PerfConstant) aoqi@0: * - PerfLongVariant (Abstract) aoqi@0: * - PerfLongVariable (alias: PerfVariable) aoqi@0: * - PerfLongCounter (alias: PerfCounter) aoqi@0: * aoqi@0: * - PerfByteArray (Abstract) aoqi@0: * - PerfString (Abstract) aoqi@0: * - PerfStringVariable aoqi@0: * - PerfStringConstant aoqi@0: * aoqi@0: * aoqi@0: * As seen in the class hierarchy, the initially supported types are: aoqi@0: * aoqi@0: * Long - performance data holds a Java long type aoqi@0: * ByteArray - performance data holds an array of Java bytes aoqi@0: * used for holding C++ char arrays. aoqi@0: * aoqi@0: * The String type is derived from the ByteArray type. aoqi@0: * aoqi@0: * A PerfData subtype is not required to provide an implementation for aoqi@0: * each variability classification. For example, the String type provides aoqi@0: * Variable and Constant variablility classifications in the PerfStringVariable aoqi@0: * and PerfStringConstant classes, but does not provide a counter type. aoqi@0: * aoqi@0: * Performance data are also described by a unit of measure. Units allow aoqi@0: * client applications to make reasonable decisions on how to treat aoqi@0: * performance data generically, preventing the need to hard-code the aoqi@0: * specifics of a particular data item in client applications. The current aoqi@0: * set of units are: aoqi@0: * aoqi@0: * None - the data has no units of measure aoqi@0: * Bytes - data is measured in bytes aoqi@0: * Ticks - data is measured in clock ticks aoqi@0: * Events - data is measured in events. For example, aoqi@0: * the number of garbage collection events or the aoqi@0: * number of methods compiled. aoqi@0: * String - data is not numerical. For example, aoqi@0: * the java command line options aoqi@0: * Hertz - data is a frequency aoqi@0: * aoqi@0: * The performance counters also provide a support attribute, indicating aoqi@0: * the stability of the counter as a programmatic interface. The support aoqi@0: * level is also implied by the name space in which the counter is created. aoqi@0: * The counter name space support conventions follow the Java package, class, aoqi@0: * and property support conventions: aoqi@0: * aoqi@0: * java.* - stable, supported interface aoqi@0: * com.sun.* - unstable, supported interface aoqi@0: * sun.* - unstable, unsupported interface aoqi@0: * aoqi@0: * In the above context, unstable is a measure of the interface support aoqi@0: * level, not the implementation stability level. aoqi@0: * aoqi@0: * Currently, instances of PerfData subtypes are considered to have aoqi@0: * a life time equal to that of the VM and are managed by the aoqi@0: * PerfDataManager class. All constructors for the PerfData class and aoqi@0: * its subtypes have protected constructors. Creation of PerfData aoqi@0: * instances is performed by invoking various create methods on the aoqi@0: * PerfDataManager class. Users should not attempt to delete these aoqi@0: * instances as the PerfDataManager class expects to perform deletion aoqi@0: * operations on exit of the VM. aoqi@0: * aoqi@0: * Examples: aoqi@0: * aoqi@0: * Creating performance counter that holds a monotonically increasing aoqi@0: * long data value with units specified in U_Bytes in the "java.gc.*" aoqi@0: * name space. aoqi@0: * aoqi@0: * PerfLongCounter* foo_counter; aoqi@0: * aoqi@0: * foo_counter = PerfDataManager::create_long_counter(JAVA_GC, "foo", aoqi@0: * PerfData::U_Bytes, aoqi@0: * optionalInitialValue, aoqi@0: * CHECK); aoqi@0: * foo_counter->inc(); aoqi@0: * aoqi@0: * Creating a performance counter that holds a variably change long aoqi@0: * data value with untis specified in U_Bytes in the "com.sun.ci aoqi@0: * name space. aoqi@0: * aoqi@0: * PerfLongVariable* bar_varible; aoqi@0: * bar_variable = PerfDataManager::create_long_variable(COM_CI, "bar", aoqi@0: .* PerfData::U_Bytes, aoqi@0: * optionalInitialValue, aoqi@0: * CHECK); aoqi@0: * aoqi@0: * bar_variable->inc(); aoqi@0: * bar_variable->set_value(0); aoqi@0: * aoqi@0: * Creating a performance counter that holds a constant string value in aoqi@0: * the "sun.cls.*" name space. aoqi@0: * aoqi@0: * PerfDataManager::create_string_constant(SUN_CLS, "foo", string, CHECK); aoqi@0: * aoqi@0: * Although the create_string_constant() factory method returns a pointer aoqi@0: * to the PerfStringConstant object, it can safely be ignored. Developers aoqi@0: * are not encouraged to access the string constant's value via this aoqi@0: * pointer at this time due to security concerns. aoqi@0: * aoqi@0: * Creating a performance counter in an arbitrary name space that holds a aoqi@0: * value that is sampled by the StatSampler periodic task. aoqi@0: * aoqi@0: * PerfDataManager::create_counter("foo.sampled", PerfData::U_Events, aoqi@0: * &my_jlong, CHECK); aoqi@0: * aoqi@0: * In this example, the PerfData pointer can be ignored as the caller aoqi@0: * is relying on the StatSampler PeriodicTask to sample the given aoqi@0: * address at a regular interval. The interval is defined by the aoqi@0: * PerfDataSamplingInterval global variable, and is applyied on aoqi@0: * a system wide basis, not on an per-counter basis. aoqi@0: * aoqi@0: * Creating a performance counter in an arbitrary name space that utilizes aoqi@0: * a helper object to return a value to the StatSampler via the take_sample() aoqi@0: * method. aoqi@0: * aoqi@0: * class MyTimeSampler : public PerfLongSampleHelper { aoqi@0: * public: aoqi@0: * jlong take_sample() { return os::elapsed_counter(); } aoqi@0: * }; aoqi@0: * aoqi@0: * PerfDataManager::create_counter(SUN_RT, "helped", aoqi@0: * PerfData::U_Ticks, aoqi@0: * new MyTimeSampler(), CHECK); aoqi@0: * aoqi@0: * In this example, a subtype of PerfLongSampleHelper is instantiated aoqi@0: * and its take_sample() method is overridden to perform whatever aoqi@0: * operation is necessary to generate the data sample. This method aoqi@0: * will be called by the StatSampler at a regular interval, defined aoqi@0: * by the PerfDataSamplingInterval global variable. aoqi@0: * aoqi@0: * As before, PerfSampleHelper is an alias for PerfLongSampleHelper. aoqi@0: * aoqi@0: * For additional uses of PerfData subtypes, see the utility classes aoqi@0: * PerfTraceTime and PerfTraceTimedEvent below. aoqi@0: * aoqi@0: * Always-on non-sampled counters can be created independent of aoqi@0: * the UsePerfData flag. Counters will be created on the c-heap aoqi@0: * if UsePerfData is false. aoqi@0: * aoqi@0: * Until further noice, all PerfData objects should be created and aoqi@0: * manipulated within a guarded block. The guard variable is aoqi@0: * UsePerfData, a product flag set to true by default. This flag may aoqi@0: * be removed from the product in the future. aoqi@0: * aoqi@0: */ aoqi@0: class PerfData : public CHeapObj { aoqi@0: aoqi@0: friend class StatSampler; // for access to protected void sample() aoqi@0: friend class PerfDataManager; // for access to protected destructor aoqi@0: aoqi@0: public: aoqi@0: aoqi@0: // the Variability enum must be kept in synchronization with the aoqi@0: // the com.sun.hotspot.perfdata.Variability class aoqi@0: enum Variability { aoqi@0: V_Constant = 1, aoqi@0: V_Monotonic = 2, aoqi@0: V_Variable = 3, aoqi@0: V_last = V_Variable aoqi@0: }; aoqi@0: aoqi@0: // the Units enum must be kept in synchronization with the aoqi@0: // the com.sun.hotspot.perfdata.Units class aoqi@0: enum Units { aoqi@0: U_None = 1, aoqi@0: U_Bytes = 2, aoqi@0: U_Ticks = 3, aoqi@0: U_Events = 4, aoqi@0: U_String = 5, aoqi@0: U_Hertz = 6, aoqi@0: U_Last = U_Hertz aoqi@0: }; aoqi@0: aoqi@0: // Miscellaneous flags aoqi@0: enum Flags { aoqi@0: F_None = 0x0, aoqi@0: F_Supported = 0x1 // interface is supported - java.* and com.sun.* aoqi@0: }; aoqi@0: aoqi@0: private: aoqi@0: char* _name; aoqi@0: Variability _v; aoqi@0: Units _u; aoqi@0: bool _on_c_heap; aoqi@0: Flags _flags; aoqi@0: aoqi@0: PerfDataEntry* _pdep; aoqi@0: aoqi@0: protected: aoqi@0: aoqi@0: void *_valuep; aoqi@0: aoqi@0: PerfData(CounterNS ns, const char* name, Units u, Variability v); aoqi@0: ~PerfData(); aoqi@0: aoqi@0: // create the entry for the PerfData item in the PerfData memory region. aoqi@0: // this region is maintained separately from the PerfData objects to aoqi@0: // facilitate its use by external processes. aoqi@0: void create_entry(BasicType dtype, size_t dsize, size_t dlen = 0); aoqi@0: aoqi@0: // sample the data item given at creation time and write its value aoqi@0: // into the its corresponding PerfMemory location. aoqi@0: virtual void sample() = 0; aoqi@0: aoqi@0: public: aoqi@0: aoqi@0: // returns a boolean indicating the validity of this object. aoqi@0: // the object is valid if and only if memory in PerfMemory aoqi@0: // region was successfully allocated. aoqi@0: inline bool is_valid() { return _valuep != NULL; } aoqi@0: aoqi@0: // returns a boolean indicating whether the underlying object aoqi@0: // was allocated in the PerfMemory region or on the C heap. aoqi@0: inline bool is_on_c_heap() { return _on_c_heap; } aoqi@0: aoqi@0: // returns a pointer to a char* containing the name of the item. aoqi@0: // The pointer returned is the pointer to a copy of the name aoqi@0: // passed to the constructor, not the pointer to the name in the aoqi@0: // PerfData memory region. This redundancy is maintained for aoqi@0: // security reasons as the PerfMemory region may be in shared aoqi@0: // memory. aoqi@0: const char* name() { return _name; } aoqi@0: aoqi@0: // returns the variability classification associated with this item aoqi@0: Variability variability() { return _v; } aoqi@0: aoqi@0: // returns the units associated with this item. aoqi@0: Units units() { return _u; } aoqi@0: aoqi@0: // returns the flags associated with this item. aoqi@0: Flags flags() { return _flags; } aoqi@0: aoqi@0: // returns the address of the data portion of the item in the aoqi@0: // PerfData memory region. aoqi@0: inline void* get_address() { return _valuep; } aoqi@0: aoqi@0: // returns the value of the data portion of the item in the aoqi@0: // PerfData memory region formatted as a string. aoqi@0: virtual int format(char* cp, int length) = 0; aoqi@0: }; aoqi@0: aoqi@0: /* aoqi@0: * PerfLongSampleHelper, and its alias PerfSamplerHelper, is a base class aoqi@0: * for helper classes that rely upon the StatSampler periodic task to aoqi@0: * invoke the take_sample() method and write the value returned to its aoqi@0: * appropriate location in the PerfData memory region. aoqi@0: */ aoqi@0: class PerfLongSampleHelper : public CHeapObj { aoqi@0: public: aoqi@0: virtual jlong take_sample() = 0; aoqi@0: }; aoqi@0: aoqi@0: typedef PerfLongSampleHelper PerfSampleHelper; aoqi@0: aoqi@0: aoqi@0: /* aoqi@0: * PerfLong is the base class for the various Long PerfData subtypes. aoqi@0: * it contains implementation details that are common among its derived aoqi@0: * types. aoqi@0: */ aoqi@0: class PerfLong : public PerfData { aoqi@0: aoqi@0: protected: aoqi@0: aoqi@0: PerfLong(CounterNS ns, const char* namep, Units u, Variability v); aoqi@0: aoqi@0: public: aoqi@0: int format(char* buffer, int length); aoqi@0: aoqi@0: // returns the value of the data portion of the item in the aoqi@0: // PerfData memory region. aoqi@0: inline jlong get_value() { return *(jlong*)_valuep; } aoqi@0: }; aoqi@0: aoqi@0: /* aoqi@0: * The PerfLongConstant class, and its alias PerfConstant, implement aoqi@0: * a PerfData subtype that holds a jlong data value that is set upon aoqi@0: * creation of an instance of this class. This class provides no aoqi@0: * methods for changing the data value stored in PerfData memory region. aoqi@0: */ aoqi@0: class PerfLongConstant : public PerfLong { aoqi@0: aoqi@0: friend class PerfDataManager; // for access to protected constructor aoqi@0: aoqi@0: private: aoqi@0: // hide sample() - no need to sample constants aoqi@0: void sample() { } aoqi@0: aoqi@0: protected: aoqi@0: aoqi@0: PerfLongConstant(CounterNS ns, const char* namep, Units u, aoqi@0: jlong initial_value=0) aoqi@0: : PerfLong(ns, namep, u, V_Constant) { aoqi@0: aoqi@0: if (is_valid()) *(jlong*)_valuep = initial_value; aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: typedef PerfLongConstant PerfConstant; aoqi@0: aoqi@0: /* aoqi@0: * The PerfLongVariant class, and its alias PerfVariant, implement aoqi@0: * a PerfData subtype that holds a jlong data value that can be modified aoqi@0: * in an unrestricted manner. This class provides the implementation details aoqi@0: * for common functionality among its derived types. aoqi@0: */ aoqi@0: class PerfLongVariant : public PerfLong { aoqi@0: aoqi@0: protected: aoqi@0: jlong* _sampled; aoqi@0: PerfLongSampleHelper* _sample_helper; aoqi@0: aoqi@0: PerfLongVariant(CounterNS ns, const char* namep, Units u, Variability v, aoqi@0: jlong initial_value=0) aoqi@0: : PerfLong(ns, namep, u, v) { aoqi@0: if (is_valid()) *(jlong*)_valuep = initial_value; aoqi@0: } aoqi@0: aoqi@0: PerfLongVariant(CounterNS ns, const char* namep, Units u, Variability v, aoqi@0: jlong* sampled); aoqi@0: aoqi@0: PerfLongVariant(CounterNS ns, const char* namep, Units u, Variability v, aoqi@0: PerfLongSampleHelper* sample_helper); aoqi@0: aoqi@0: void sample(); aoqi@0: aoqi@0: public: aoqi@0: inline void inc() { (*(jlong*)_valuep)++; } aoqi@0: inline void inc(jlong val) { (*(jlong*)_valuep) += val; } aoqi@0: inline void add(jlong val) { (*(jlong*)_valuep) += val; } aoqi@0: void clear_sample_helper() { _sample_helper = NULL; } aoqi@0: }; aoqi@0: aoqi@0: /* aoqi@0: * The PerfLongCounter class, and its alias PerfCounter, implement aoqi@0: * a PerfData subtype that holds a jlong data value that can (should) aoqi@0: * be modified in a monotonic manner. The inc(jlong) and add(jlong) aoqi@0: * methods can be passed negative values to implement a monotonically aoqi@0: * decreasing value. However, we rely upon the programmer to honor aoqi@0: * the notion that this counter always moves in the same direction - aoqi@0: * either increasing or decreasing. aoqi@0: */ aoqi@0: class PerfLongCounter : public PerfLongVariant { aoqi@0: aoqi@0: friend class PerfDataManager; // for access to protected constructor aoqi@0: aoqi@0: protected: aoqi@0: aoqi@0: PerfLongCounter(CounterNS ns, const char* namep, Units u, aoqi@0: jlong initial_value=0) aoqi@0: : PerfLongVariant(ns, namep, u, V_Monotonic, aoqi@0: initial_value) { } aoqi@0: aoqi@0: PerfLongCounter(CounterNS ns, const char* namep, Units u, jlong* sampled) aoqi@0: : PerfLongVariant(ns, namep, u, V_Monotonic, sampled) { } aoqi@0: aoqi@0: PerfLongCounter(CounterNS ns, const char* namep, Units u, aoqi@0: PerfLongSampleHelper* sample_helper) aoqi@0: : PerfLongVariant(ns, namep, u, V_Monotonic, aoqi@0: sample_helper) { } aoqi@0: }; aoqi@0: aoqi@0: typedef PerfLongCounter PerfCounter; aoqi@0: aoqi@0: /* aoqi@0: * The PerfLongVariable class, and its alias PerfVariable, implement aoqi@0: * a PerfData subtype that holds a jlong data value that can aoqi@0: * be modified in an unrestricted manner. aoqi@0: */ aoqi@0: class PerfLongVariable : public PerfLongVariant { aoqi@0: aoqi@0: friend class PerfDataManager; // for access to protected constructor aoqi@0: aoqi@0: protected: aoqi@0: aoqi@0: PerfLongVariable(CounterNS ns, const char* namep, Units u, aoqi@0: jlong initial_value=0) aoqi@0: : PerfLongVariant(ns, namep, u, V_Variable, aoqi@0: initial_value) { } aoqi@0: aoqi@0: PerfLongVariable(CounterNS ns, const char* namep, Units u, jlong* sampled) aoqi@0: : PerfLongVariant(ns, namep, u, V_Variable, sampled) { } aoqi@0: aoqi@0: PerfLongVariable(CounterNS ns, const char* namep, Units u, aoqi@0: PerfLongSampleHelper* sample_helper) aoqi@0: : PerfLongVariant(ns, namep, u, V_Variable, aoqi@0: sample_helper) { } aoqi@0: aoqi@0: public: aoqi@0: inline void set_value(jlong val) { (*(jlong*)_valuep) = val; } aoqi@0: }; aoqi@0: aoqi@0: typedef PerfLongVariable PerfVariable; aoqi@0: aoqi@0: /* aoqi@0: * The PerfByteArray provides a PerfData subtype that allows the creation aoqi@0: * of a contiguous region of the PerfData memory region for storing a vector aoqi@0: * of bytes. This class is currently intended to be a base class for aoqi@0: * the PerfString class, and cannot be instantiated directly. aoqi@0: */ aoqi@0: class PerfByteArray : public PerfData { aoqi@0: aoqi@0: protected: aoqi@0: jint _length; aoqi@0: aoqi@0: PerfByteArray(CounterNS ns, const char* namep, Units u, Variability v, aoqi@0: jint length); aoqi@0: }; aoqi@0: aoqi@0: class PerfString : public PerfByteArray { aoqi@0: aoqi@0: protected: aoqi@0: aoqi@0: void set_string(const char* s2); aoqi@0: aoqi@0: PerfString(CounterNS ns, const char* namep, Variability v, jint length, aoqi@0: const char* initial_value) aoqi@0: : PerfByteArray(ns, namep, U_String, v, length) { aoqi@0: if (is_valid()) set_string(initial_value); aoqi@0: } aoqi@0: aoqi@0: public: aoqi@0: aoqi@0: int format(char* buffer, int length); aoqi@0: }; aoqi@0: aoqi@0: /* aoqi@0: * The PerfStringConstant class provides a PerfData sub class that aoqi@0: * allows a null terminated string of single byte characters to be aoqi@0: * stored in the PerfData memory region. aoqi@0: */ aoqi@0: class PerfStringConstant : public PerfString { aoqi@0: aoqi@0: friend class PerfDataManager; // for access to protected constructor aoqi@0: aoqi@0: private: aoqi@0: aoqi@0: // hide sample() - no need to sample constants aoqi@0: void sample() { } aoqi@0: aoqi@0: protected: aoqi@0: aoqi@0: // Restrict string constant lengths to be <= PerfMaxStringConstLength. aoqi@0: // This prevents long string constants, as can occur with very aoqi@0: // long classpaths or java command lines, from consuming too much aoqi@0: // PerfData memory. aoqi@0: PerfStringConstant(CounterNS ns, const char* namep, aoqi@0: const char* initial_value); aoqi@0: }; aoqi@0: aoqi@0: /* aoqi@0: * The PerfStringVariable class provides a PerfData sub class that aoqi@0: * allows a null terminated string of single byte character data aoqi@0: * to be stored in PerfData memory region. The string value can be reset aoqi@0: * after initialization. If the string value is >= max_length, then aoqi@0: * it will be truncated to max_length characters. The copied string aoqi@0: * is always null terminated. aoqi@0: */ aoqi@0: class PerfStringVariable : public PerfString { aoqi@0: aoqi@0: friend class PerfDataManager; // for access to protected constructor aoqi@0: aoqi@0: protected: aoqi@0: aoqi@0: // sampling of string variables are not yet supported aoqi@0: void sample() { } aoqi@0: aoqi@0: PerfStringVariable(CounterNS ns, const char* namep, jint max_length, aoqi@0: const char* initial_value) aoqi@0: : PerfString(ns, namep, V_Variable, max_length+1, aoqi@0: initial_value) { } aoqi@0: aoqi@0: public: aoqi@0: inline void set_value(const char* val) { set_string(val); } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: /* aoqi@0: * The PerfDataList class is a container class for managing lists aoqi@0: * of PerfData items. The intention of this class is to allow for aoqi@0: * alternative implementations for management of list of PerfData aoqi@0: * items without impacting the code that uses the lists. aoqi@0: * aoqi@0: * The initial implementation is based upon GrowableArray. Searches aoqi@0: * on GrowableArray types is linear in nature and this may become aoqi@0: * a performance issue for creation of PerfData items, particularly aoqi@0: * from Java code where a test for existence is implemented as a aoqi@0: * search over all existing PerfData items. aoqi@0: * aoqi@0: * The abstraction is not complete. A more general container class aoqi@0: * would provide an Iterator abstraction that could be used to aoqi@0: * traverse the lists. This implementation still relys upon integer aoqi@0: * iterators and the at(int index) method. However, the GrowableArray aoqi@0: * is not directly visible outside this class and can be replaced by aoqi@0: * some other implementation, as long as that implementation provides aoqi@0: * a mechanism to iterate over the container by index. aoqi@0: */ aoqi@0: class PerfDataList : public CHeapObj { aoqi@0: aoqi@0: private: aoqi@0: aoqi@0: // GrowableArray implementation aoqi@0: typedef GrowableArray PerfDataArray; aoqi@0: aoqi@0: PerfDataArray* _set; aoqi@0: aoqi@0: // method to search for a instrumentation object by name aoqi@0: static bool by_name(void* name, PerfData* pd); aoqi@0: aoqi@0: protected: aoqi@0: // we expose the implementation here to facilitate the clone aoqi@0: // method. aoqi@0: PerfDataArray* get_impl() { return _set; } aoqi@0: aoqi@0: public: aoqi@0: aoqi@0: // create a PerfDataList with the given initial length aoqi@0: PerfDataList(int length); aoqi@0: aoqi@0: // create a PerfDataList as a shallow copy of the given PerfDataList aoqi@0: PerfDataList(PerfDataList* p); aoqi@0: aoqi@0: ~PerfDataList(); aoqi@0: aoqi@0: // return the PerfData item indicated by name, aoqi@0: // or NULL if it doesn't exist. aoqi@0: PerfData* find_by_name(const char* name); aoqi@0: aoqi@0: // return true if a PerfData item with the name specified in the aoqi@0: // argument exists, otherwise return false. aoqi@0: bool contains(const char* name) { return find_by_name(name) != NULL; } aoqi@0: aoqi@0: // return the number of PerfData items in this list aoqi@0: int length() { return _set->length(); } aoqi@0: aoqi@0: // add a PerfData item to this list aoqi@0: void append(PerfData *p) { _set->append(p); } aoqi@0: aoqi@0: // remove the given PerfData item from this list. When called aoqi@0: // while iterating over the list, this method will result in a aoqi@0: // change in the length of the container. The at(int index) aoqi@0: // method is also impacted by this method as elements with an aoqi@0: // index greater than the index of the element removed by this aoqi@0: // method will be shifted down by one. aoqi@0: void remove(PerfData *p) { _set->remove(p); } aoqi@0: aoqi@0: // create a new PerfDataList from this list. The new list is aoqi@0: // a shallow copy of the original list and care should be taken aoqi@0: // with respect to delete operations on the elements of the list aoqi@0: // as the are likely in use by another copy of the list. aoqi@0: PerfDataList* clone(); aoqi@0: aoqi@0: // for backward compatibility with GrowableArray - need to implement aoqi@0: // some form of iterator to provide a cleaner abstraction for aoqi@0: // iteration over the container. aoqi@0: PerfData* at(int index) { return _set->at(index); } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: /* aoqi@0: * The PerfDataManager class is responsible for creating PerfData aoqi@0: * subtypes via a set a factory methods and for managing lists aoqi@0: * of the various PerfData types. aoqi@0: */ aoqi@0: class PerfDataManager : AllStatic { aoqi@0: aoqi@0: friend class StatSampler; // for access to protected PerfDataList methods aoqi@0: aoqi@0: private: aoqi@0: static PerfDataList* _all; aoqi@0: static PerfDataList* _sampled; aoqi@0: static PerfDataList* _constants; aoqi@0: static const char* _name_spaces[]; aoqi@0: aoqi@0: // add a PerfData item to the list(s) of know PerfData objects aoqi@0: static void add_item(PerfData* p, bool sampled); aoqi@0: aoqi@0: protected: aoqi@0: // return the list of all known PerfData items aoqi@0: static PerfDataList* all(); aoqi@0: static int count() { return _all->length(); } aoqi@0: aoqi@0: // return the list of all known PerfData items that are to be aoqi@0: // sampled by the StatSampler. aoqi@0: static PerfDataList* sampled(); aoqi@0: static int sampled_count() { return _sampled->length(); } aoqi@0: aoqi@0: // return the list of all known PerfData items that have a aoqi@0: // variability classification of type Constant aoqi@0: static PerfDataList* constants(); aoqi@0: static int constants_count() { return _constants->length(); } aoqi@0: aoqi@0: public: aoqi@0: aoqi@0: // method to check for the existence of a PerfData item with aoqi@0: // the given name. aoqi@0: static bool exists(const char* name) { return _all->contains(name); } aoqi@0: aoqi@0: // method to search for a instrumentation object by name aoqi@0: static PerfData* find_by_name(const char* name); aoqi@0: aoqi@0: // method to map a CounterNS enumeration to a namespace string aoqi@0: static const char* ns_to_string(CounterNS ns) { aoqi@0: return _name_spaces[ns]; aoqi@0: } aoqi@0: aoqi@0: // methods to test the interface stability of a given counter namespace aoqi@0: // aoqi@0: static bool is_stable_supported(CounterNS ns) { aoqi@0: return (ns != NULL_NS) && ((ns % 3) == JAVA_NS); aoqi@0: } aoqi@0: static bool is_unstable_supported(CounterNS ns) { aoqi@0: return (ns != NULL_NS) && ((ns % 3) == COM_NS); aoqi@0: } aoqi@0: static bool is_unstable_unsupported(CounterNS ns) { aoqi@0: return (ns == NULL_NS) || ((ns % 3) == SUN_NS); aoqi@0: } aoqi@0: aoqi@0: // methods to test the interface stability of a given counter name aoqi@0: // aoqi@0: static bool is_stable_supported(const char* name) { aoqi@0: const char* javadot = "java."; aoqi@0: return strncmp(name, javadot, strlen(javadot)) == 0; aoqi@0: } aoqi@0: static bool is_unstable_supported(const char* name) { aoqi@0: const char* comdot = "com.sun."; aoqi@0: return strncmp(name, comdot, strlen(comdot)) == 0; aoqi@0: } aoqi@0: static bool is_unstable_unsupported(const char* name) { aoqi@0: return !(is_stable_supported(name) && is_unstable_supported(name)); aoqi@0: } aoqi@0: aoqi@0: // method to construct counter name strings in a given name space. aoqi@0: // The string object is allocated from the Resource Area and calls aoqi@0: // to this method must be made within a ResourceMark. aoqi@0: // aoqi@0: static char* counter_name(const char* name_space, const char* name); aoqi@0: aoqi@0: // method to construct name space strings in a given name space. aoqi@0: // The string object is allocated from the Resource Area and calls aoqi@0: // to this method must be made within a ResourceMark. aoqi@0: // aoqi@0: static char* name_space(const char* name_space, const char* sub_space) { aoqi@0: return counter_name(name_space, sub_space); aoqi@0: } aoqi@0: aoqi@0: // same as above, but appends the instance number to the name space aoqi@0: // aoqi@0: static char* name_space(const char* name_space, const char* sub_space, aoqi@0: int instance); aoqi@0: static char* name_space(const char* name_space, int instance); aoqi@0: aoqi@0: aoqi@0: // these methods provide the general interface for creating aoqi@0: // performance data resources. The types of performance data aoqi@0: // resources can be extended by adding additional create aoqi@0: // methods. aoqi@0: aoqi@0: // Constant Types aoqi@0: static PerfStringConstant* create_string_constant(CounterNS ns, aoqi@0: const char* name, aoqi@0: const char *s, TRAPS); aoqi@0: aoqi@0: static PerfLongConstant* create_long_constant(CounterNS ns, aoqi@0: const char* name, aoqi@0: PerfData::Units u, aoqi@0: jlong val, TRAPS); aoqi@0: aoqi@0: aoqi@0: // Variable Types aoqi@0: static PerfStringVariable* create_string_variable(CounterNS ns, aoqi@0: const char* name, aoqi@0: int max_length, aoqi@0: const char *s, TRAPS); aoqi@0: aoqi@0: static PerfStringVariable* create_string_variable(CounterNS ns, aoqi@0: const char* name, aoqi@0: const char *s, TRAPS) { aoqi@0: return create_string_variable(ns, name, 0, s, CHECK_NULL); aoqi@0: }; aoqi@0: aoqi@0: static PerfLongVariable* create_long_variable(CounterNS ns, aoqi@0: const char* name, aoqi@0: PerfData::Units u, aoqi@0: jlong ival, TRAPS); aoqi@0: aoqi@0: static PerfLongVariable* create_long_variable(CounterNS ns, aoqi@0: const char* name, aoqi@0: PerfData::Units u, TRAPS) { aoqi@0: return create_long_variable(ns, name, u, (jlong)0, CHECK_NULL); aoqi@0: }; aoqi@0: aoqi@0: static PerfLongVariable* create_long_variable(CounterNS, const char* name, aoqi@0: PerfData::Units u, aoqi@0: jlong* sp, TRAPS); aoqi@0: aoqi@0: static PerfLongVariable* create_long_variable(CounterNS ns, aoqi@0: const char* name, aoqi@0: PerfData::Units u, aoqi@0: PerfLongSampleHelper* sh, aoqi@0: TRAPS); aoqi@0: aoqi@0: aoqi@0: // Counter Types aoqi@0: static PerfLongCounter* create_long_counter(CounterNS ns, const char* name, aoqi@0: PerfData::Units u, aoqi@0: jlong ival, TRAPS); aoqi@0: aoqi@0: static PerfLongCounter* create_long_counter(CounterNS ns, const char* name, aoqi@0: PerfData::Units u, TRAPS) { aoqi@0: return create_long_counter(ns, name, u, (jlong)0, CHECK_NULL); aoqi@0: }; aoqi@0: aoqi@0: static PerfLongCounter* create_long_counter(CounterNS ns, const char* name, aoqi@0: PerfData::Units u, jlong* sp, aoqi@0: TRAPS); aoqi@0: aoqi@0: static PerfLongCounter* create_long_counter(CounterNS ns, const char* name, aoqi@0: PerfData::Units u, aoqi@0: PerfLongSampleHelper* sh, aoqi@0: TRAPS); aoqi@0: aoqi@0: aoqi@0: // these creation methods are provided for ease of use. These allow aoqi@0: // Long performance data types to be created with a shorthand syntax. aoqi@0: aoqi@0: static PerfConstant* create_constant(CounterNS ns, const char* name, aoqi@0: PerfData::Units u, jlong val, TRAPS) { aoqi@0: return create_long_constant(ns, name, u, val, CHECK_NULL); aoqi@0: } aoqi@0: aoqi@0: static PerfVariable* create_variable(CounterNS ns, const char* name, aoqi@0: PerfData::Units u, jlong ival, TRAPS) { aoqi@0: return create_long_variable(ns, name, u, ival, CHECK_NULL); aoqi@0: } aoqi@0: aoqi@0: static PerfVariable* create_variable(CounterNS ns, const char* name, aoqi@0: PerfData::Units u, TRAPS) { aoqi@0: return create_long_variable(ns, name, u, (jlong)0, CHECK_NULL); aoqi@0: } aoqi@0: aoqi@0: static PerfVariable* create_variable(CounterNS ns, const char* name, aoqi@0: PerfData::Units u, jlong* sp, TRAPS) { aoqi@0: return create_long_variable(ns, name, u, sp, CHECK_NULL); aoqi@0: } aoqi@0: aoqi@0: static PerfVariable* create_variable(CounterNS ns, const char* name, aoqi@0: PerfData::Units u, aoqi@0: PerfSampleHelper* sh, TRAPS) { aoqi@0: return create_long_variable(ns, name, u, sh, CHECK_NULL); aoqi@0: } aoqi@0: aoqi@0: static PerfCounter* create_counter(CounterNS ns, const char* name, aoqi@0: PerfData::Units u, jlong ival, TRAPS) { aoqi@0: return create_long_counter(ns, name, u, ival, CHECK_NULL); aoqi@0: } aoqi@0: aoqi@0: static PerfCounter* create_counter(CounterNS ns, const char* name, aoqi@0: PerfData::Units u, TRAPS) { aoqi@0: return create_long_counter(ns, name, u, (jlong)0, CHECK_NULL); aoqi@0: } aoqi@0: aoqi@0: static PerfCounter* create_counter(CounterNS ns, const char* name, aoqi@0: PerfData::Units u, jlong* sp, TRAPS) { aoqi@0: return create_long_counter(ns, name, u, sp, CHECK_NULL); aoqi@0: } aoqi@0: aoqi@0: static PerfCounter* create_counter(CounterNS ns, const char* name, aoqi@0: PerfData::Units u, aoqi@0: PerfSampleHelper* sh, TRAPS) { aoqi@0: return create_long_counter(ns, name, u, sh, CHECK_NULL); aoqi@0: } aoqi@0: aoqi@0: static void destroy(); aoqi@0: }; aoqi@0: aoqi@0: // Useful macros to create the performance counters aoqi@0: #define NEWPERFTICKCOUNTER(counter, counter_ns, counter_name) \ aoqi@0: {counter = PerfDataManager::create_counter(counter_ns, counter_name, \ aoqi@0: PerfData::U_Ticks,CHECK);} aoqi@0: aoqi@0: #define NEWPERFEVENTCOUNTER(counter, counter_ns, counter_name) \ aoqi@0: {counter = PerfDataManager::create_counter(counter_ns, counter_name, \ aoqi@0: PerfData::U_Events,CHECK);} aoqi@0: aoqi@0: #define NEWPERFBYTECOUNTER(counter, counter_ns, counter_name) \ aoqi@0: {counter = PerfDataManager::create_counter(counter_ns, counter_name, \ aoqi@0: PerfData::U_Bytes,CHECK);} aoqi@0: aoqi@0: // Utility Classes aoqi@0: aoqi@0: /* aoqi@0: * this class will administer a PerfCounter used as a time accumulator aoqi@0: * for a basic block much like the TraceTime class. aoqi@0: * aoqi@0: * Example: aoqi@0: * aoqi@0: * static PerfCounter* my_time_counter = PerfDataManager::create_counter("my.time.counter", PerfData::U_Ticks, 0LL, CHECK); aoqi@0: * aoqi@0: * { aoqi@0: * PerfTraceTime ptt(my_time_counter); aoqi@0: * // perform the operation you want to measure aoqi@0: * } aoqi@0: * aoqi@0: * Note: use of this class does not need to occur within a guarded aoqi@0: * block. The UsePerfData guard is used with the implementation aoqi@0: * of this class. aoqi@0: */ aoqi@0: class PerfTraceTime : public StackObj { aoqi@0: aoqi@0: protected: aoqi@0: elapsedTimer _t; aoqi@0: PerfLongCounter* _timerp; aoqi@0: // pointer to thread-local or global recursion counter variable aoqi@0: int* _recursion_counter; aoqi@0: aoqi@0: public: aoqi@0: inline PerfTraceTime(PerfLongCounter* timerp) : _timerp(timerp), _recursion_counter(NULL) { aoqi@0: if (!UsePerfData) return; aoqi@0: _t.start(); aoqi@0: } aoqi@0: aoqi@0: inline PerfTraceTime(PerfLongCounter* timerp, int* recursion_counter) : _timerp(timerp), _recursion_counter(recursion_counter) { aoqi@0: if (!UsePerfData || (_recursion_counter != NULL && aoqi@0: (*_recursion_counter)++ > 0)) return; aoqi@0: _t.start(); aoqi@0: } aoqi@0: aoqi@0: inline void suspend() { if (!UsePerfData) return; _t.stop(); } aoqi@0: inline void resume() { if (!UsePerfData) return; _t.start(); } aoqi@0: aoqi@0: inline ~PerfTraceTime() { aoqi@0: if (!UsePerfData || (_recursion_counter != NULL && aoqi@0: --(*_recursion_counter) > 0)) return; aoqi@0: _t.stop(); aoqi@0: _timerp->inc(_t.ticks()); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: /* The PerfTraceTimedEvent class is responsible for counting the aoqi@0: * occurrence of some event and measuring the the elapsed time of aoqi@0: * the event in two separate PerfCounter instances. aoqi@0: * aoqi@0: * Example: aoqi@0: * aoqi@0: * static PerfCounter* my_time_counter = PerfDataManager::create_counter("my.time.counter", PerfData::U_Ticks, CHECK); aoqi@0: * static PerfCounter* my_event_counter = PerfDataManager::create_counter("my.event.counter", PerfData::U_Events, CHECK); aoqi@0: * aoqi@0: * { aoqi@0: * PerfTraceTimedEvent ptte(my_time_counter, my_event_counter); aoqi@0: * // perform the operation you want to count and measure aoqi@0: * } aoqi@0: * aoqi@0: * Note: use of this class does not need to occur within a guarded aoqi@0: * block. The UsePerfData guard is used with the implementation aoqi@0: * of this class. aoqi@0: * aoqi@0: */ aoqi@0: class PerfTraceTimedEvent : public PerfTraceTime { aoqi@0: aoqi@0: protected: aoqi@0: PerfLongCounter* _eventp; aoqi@0: aoqi@0: public: aoqi@0: inline PerfTraceTimedEvent(PerfLongCounter* timerp, PerfLongCounter* eventp): PerfTraceTime(timerp), _eventp(eventp) { aoqi@0: if (!UsePerfData) return; aoqi@0: _eventp->inc(); aoqi@0: } aoqi@0: aoqi@0: inline PerfTraceTimedEvent(PerfLongCounter* timerp, PerfLongCounter* eventp, int* recursion_counter): PerfTraceTime(timerp, recursion_counter), _eventp(eventp) { aoqi@0: if (!UsePerfData) return; aoqi@0: _eventp->inc(); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_RUNTIME_PERFDATA_HPP