1660N/A/*
2362N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
1660N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1660N/A *
1660N/A * This code is free software; you can redistribute it and/or modify it
1660N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1660N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1660N/A *
1660N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1660N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1660N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1660N/A * version 2 for more details (a copy is included in the LICENSE file that
1660N/A * accompanied this code).
1660N/A *
1660N/A * You should have received a copy of the GNU General Public License version
1660N/A * 2 along with this work; if not, write to the Free Software Foundation,
1660N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1660N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1660N/A */
1660N/A
1660N/Apackage sun.misc;
1660N/A
1660N/Aimport java.nio.ByteBuffer;
1660N/Aimport java.nio.ByteOrder;
1660N/Aimport java.nio.LongBuffer;
1660N/Aimport java.security.AccessController;
1660N/A
1660N/A/**
1660N/A * Performance counter support for internal JRE classes.
1660N/A * This class defines a fixed list of counters for the platform
1660N/A * to use as an interim solution until RFE# 6209222 is implemented.
1660N/A * The perf counters will be created in the jvmstat perf buffer
1660N/A * that the HotSpot VM creates. The default size is 32K and thus
1660N/A * the number of counters is bounded. You can alter the size
1660N/A * with -XX:PerfDataMemorySize=<bytes> option. If there is
1660N/A * insufficient memory in the jvmstat perf buffer, the C heap memory
1660N/A * will be used and thus the application will continue to run if
1660N/A * the counters added exceeds the buffer size but the counters
1660N/A * will be missing.
1660N/A *
1660N/A * See HotSpot jvmstat implementation for certain circumstances
1660N/A * that the jvmstat perf buffer is not supported.
1660N/A *
1660N/A */
1660N/Apublic class PerfCounter {
1660N/A private static final Perf perf =
1660N/A AccessController.doPrivileged(new Perf.GetPerfAction());
1660N/A
1660N/A // Must match values defined in hotspot/src/share/vm/runtime/perfdata.hpp
1660N/A private final static int V_Constant = 1;
1660N/A private final static int V_Monotonic = 2;
1660N/A private final static int V_Variable = 3;
1660N/A private final static int U_None = 1;
1660N/A
1660N/A private final String name;
1660N/A private final LongBuffer lb;
1660N/A
1660N/A private PerfCounter(String name, int type) {
1660N/A this.name = name;
1660N/A ByteBuffer bb = perf.createLong(name, U_None, type, 0L);
1660N/A bb.order(ByteOrder.nativeOrder());
1660N/A this.lb = bb.asLongBuffer();
1660N/A }
1660N/A
1660N/A static PerfCounter newPerfCounter(String name) {
1660N/A return new PerfCounter(name, V_Variable);
1660N/A }
1660N/A
1660N/A static PerfCounter newConstantPerfCounter(String name) {
1660N/A PerfCounter c = new PerfCounter(name, V_Constant);
1660N/A return c;
1660N/A }
1660N/A
1660N/A /**
1660N/A * Returns the current value of the perf counter.
1660N/A */
1660N/A public synchronized long get() {
1660N/A return lb.get(0);
1660N/A }
1660N/A
1660N/A /**
1660N/A * Sets the value of the perf counter to the given newValue.
1660N/A */
1660N/A public synchronized void set(long newValue) {
1660N/A lb.put(0, newValue);
1660N/A }
1660N/A
1660N/A /**
1660N/A * Adds the given value to the perf counter.
1660N/A */
1660N/A public synchronized void add(long value) {
1660N/A long res = get() + value;
1660N/A lb.put(0, res);
1660N/A }
1660N/A
1660N/A /**
1660N/A * Increments the perf counter with 1.
1660N/A */
1660N/A public void increment() {
1660N/A add(1);
1660N/A }
1660N/A
1660N/A /**
1660N/A * Adds the given interval to the perf counter.
1660N/A */
1660N/A public void addTime(long interval) {
1660N/A add(interval);
1660N/A }
1660N/A
1660N/A /**
1660N/A * Adds the elapsed time from the given start time (ns) to the perf counter.
1660N/A */
1660N/A public void addElapsedTimeFrom(long startTime) {
1660N/A add(System.nanoTime() - startTime);
1660N/A }
1660N/A
1660N/A @Override
1660N/A public String toString() {
1660N/A return name + " = " + get();
1660N/A }
1660N/A
1660N/A static class CoreCounters {
1660N/A static final PerfCounter pdt = newPerfCounter("sun.classloader.parentDelegationTime");
1660N/A static final PerfCounter lc = newPerfCounter("sun.classloader.findClasses");
1660N/A static final PerfCounter lct = newPerfCounter("sun.classloader.findClassTime");
1660N/A static final PerfCounter rcbt = newPerfCounter("sun.urlClassLoader.readClassBytesTime");
1660N/A static final PerfCounter zfc = newPerfCounter("sun.zip.zipFiles");
1660N/A static final PerfCounter zfot = newPerfCounter("sun.zip.zipFile.openTime");
1660N/A }
1660N/A
1660N/A static class WindowsClientCounters {
1660N/A static final PerfCounter d3dAvailable = newConstantPerfCounter("sun.java2d.d3d.available");
1660N/A }
1660N/A
1660N/A /**
1660N/A * Number of findClass calls
1660N/A */
1660N/A public static PerfCounter getFindClasses() {
1660N/A return CoreCounters.lc;
1660N/A }
1660N/A
1660N/A /**
1660N/A * Time (ns) spent in finding classes that includes
1660N/A * lookup and read class bytes and defineClass
1660N/A */
1660N/A public static PerfCounter getFindClassTime() {
1660N/A return CoreCounters.lct;
1660N/A }
1660N/A
1660N/A /**
1660N/A * Time (ns) spent in finding classes
1660N/A */
1660N/A public static PerfCounter getReadClassBytesTime() {
1660N/A return CoreCounters.rcbt;
1660N/A }
1660N/A
1660N/A /**
1660N/A * Time (ns) spent in the parent delegation to
1660N/A * the parent of the defining class loader
1660N/A */
1660N/A public static PerfCounter getParentDelegationTime() {
1660N/A return CoreCounters.pdt;
1660N/A }
1660N/A
1660N/A /**
1660N/A * Number of zip files opened.
1660N/A */
1660N/A public static PerfCounter getZipFileCount() {
1660N/A return CoreCounters.zfc;
1660N/A }
1660N/A
1660N/A /**
1660N/A * Time (ns) spent in opening the zip files that
1660N/A * includes building the entries hash table
1660N/A */
1660N/A public static PerfCounter getZipFileOpenTime() {
1660N/A return CoreCounters.zfot;
1660N/A }
1660N/A
1660N/A /**
1660N/A * D3D graphic pipeline available
1660N/A */
1660N/A public static PerfCounter getD3DAvailable() {
1660N/A return WindowsClientCounters.d3dAvailable;
1660N/A }
1660N/A}