Path: blob/master/src/java.base/share/classes/jdk/internal/perf/PerfCounter.java
41159 views
/*1* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.internal.perf;2627import java.nio.ByteBuffer;28import java.nio.ByteOrder;29import java.nio.LongBuffer;30import java.security.AccessController;3132/**33* Performance counter support for internal JRE classes.34* This class defines a fixed list of counters for the platform35* to use as an interim solution until RFE# 6209222 is implemented.36* The perf counters will be created in the jvmstat perf buffer37* that the HotSpot VM creates. The default size is 32K and thus38* the number of counters is bounded. You can alter the size39* with {@code -XX:PerfDataMemorySize=<bytes>} option. If there is40* insufficient memory in the jvmstat perf buffer, the C heap memory41* will be used and thus the application will continue to run if42* the counters added exceeds the buffer size but the counters43* will be missing.44*45* See HotSpot jvmstat implementation for certain circumstances46* that the jvmstat perf buffer is not supported.47*48*/49public class PerfCounter {50@SuppressWarnings("removal")51private static final Perf perf =52AccessController.doPrivileged(new Perf.GetPerfAction());5354// Must match values defined in hotspot/src/share/vm/runtime/perfdata.hpp55private static final int V_Constant = 1;56private static final int V_Monotonic = 2;57private static final int V_Variable = 3;58private static final int U_None = 1;5960private final String name;61private final LongBuffer lb;6263private PerfCounter(String name, int type) {64this.name = name;65ByteBuffer bb = perf.createLong(name, type, U_None, 0L);66bb.order(ByteOrder.nativeOrder());67this.lb = bb.asLongBuffer();68}6970public static PerfCounter newPerfCounter(String name) {71return new PerfCounter(name, V_Variable);72}7374public static PerfCounter newConstantPerfCounter(String name) {75PerfCounter c = new PerfCounter(name, V_Constant);76return c;77}7879/**80* Returns the current value of the perf counter.81*/82public synchronized long get() {83return lb.get(0);84}8586/**87* Sets the value of the perf counter to the given newValue.88*/89public synchronized void set(long newValue) {90lb.put(0, newValue);91}9293/**94* Adds the given value to the perf counter.95*/96public synchronized void add(long value) {97long res = get() + value;98lb.put(0, res);99}100101/**102* Increments the perf counter with 1.103*/104public void increment() {105add(1);106}107108/**109* Adds the given interval to the perf counter.110*/111public void addTime(long interval) {112add(interval);113}114115/**116* Adds the elapsed time from the given start time (ns) to the perf counter.117*/118public void addElapsedTimeFrom(long startTime) {119add(System.nanoTime() - startTime);120}121122@Override123public String toString() {124return name + " = " + get();125}126127static class CoreCounters {128static final PerfCounter pdt = newPerfCounter("sun.classloader.parentDelegationTime");129static final PerfCounter lc = newPerfCounter("sun.classloader.findClasses");130static final PerfCounter lct = newPerfCounter("sun.classloader.findClassTime");131static final PerfCounter rcbt = newPerfCounter("sun.urlClassLoader.readClassBytesTime");132static final PerfCounter zfc = newPerfCounter("sun.zip.zipFiles");133static final PerfCounter zfot = newPerfCounter("sun.zip.zipFile.openTime");134}135136/**137* Number of findClass calls138*/139public static PerfCounter getFindClasses() {140return CoreCounters.lc;141}142143/**144* Time (ns) spent in finding classes that includes145* lookup and read class bytes and defineClass146*/147public static PerfCounter getFindClassTime() {148return CoreCounters.lct;149}150151/**152* Time (ns) spent in finding classes153*/154public static PerfCounter getReadClassBytesTime() {155return CoreCounters.rcbt;156}157158/**159* Time (ns) spent in the parent delegation to160* the parent of the defining class loader161*/162public static PerfCounter getParentDelegationTime() {163return CoreCounters.pdt;164}165166/**167* Number of zip files opened.168*/169public static PerfCounter getZipFileCount() {170return CoreCounters.zfc;171}172173/**174* Time (ns) spent in opening the zip files that175* includes building the entries hash table176*/177public static PerfCounter getZipFileOpenTime() {178return CoreCounters.zfot;179}180181}182183184