src/share/vm/runtime/perfData.hpp

Wed, 09 Apr 2008 15:10:22 -0700

author
rasbold
date
Wed, 09 Apr 2008 15:10:22 -0700
changeset 544
9f4457a14b58
parent 435
a61af66fc99e
child 1310
6a93908f268f
permissions
-rw-r--r--

Merge

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

mercurial