001/*
002 *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 *  This file is licensed to You under the Eclipse Public License (EPL);
005 *  You may not use this file except in compliance with the License. You
006 *  may obtain a copy of the License at
007 *
008 *      http://www.opensource.org/licenses/eclipse-1.0.php
009 *
010 *  See the COPYRIGHT.txt file distributed with this work for information
011 *  regarding copyright ownership.
012 */
013package org.jikesrvm.classloader;
014
015import org.jikesrvm.VM;
016import org.jikesrvm.runtime.Entrypoints;
017import org.jikesrvm.runtime.Callbacks.ClassLoadedMonitor;
018import org.jikesrvm.scheduler.Synchronization;
019
020/**
021 * Implements functionality to support JMX classloading beans.
022 * <p>
023 * Pulling the functionality into the core of the VM will hopefully allow us to
024 * support multiple class libraries without having to duplicate a lot of code.
025 * TODO: we need to add OpenJDK support before we actually know whether that is true.
026 */
027public final class JMXSupport implements ClassLoadedMonitor {
028
029  public static final JMXSupport CLASS_LOADING_JMX_SUPPORT = new JMXSupport();
030
031   /** the count of loaded classes */
032  @SuppressWarnings("unused") // accessed via low-level synchronization
033  private int classLoadedCount;
034
035  private JMXSupport() {
036    // disallow instantiation
037  }
038
039  @Override
040  public void notifyClassLoaded(RVMClass klass) {
041    increaseClassLoadedCount();
042  }
043
044  private static void increaseClassLoadedCount() {
045    // Need to use low-level synchronization because this method can be called
046    // very early in the boot process. The class loaded monitor is added directly
047    // after the bootstrap class loader is booted.
048    Synchronization.fetchAndAdd(CLASS_LOADING_JMX_SUPPORT,
049        Entrypoints.classLoadedCountField.getOffset(), 1);
050  }
051
052  public static int getLoadedClassCount() {
053    return Synchronization.fetchAndAdd(CLASS_LOADING_JMX_SUPPORT,
054        Entrypoints.classLoadedCountField.getOffset(), 0);
055  }
056
057  public static long getUnloadedClassCount() {
058    return 0; // class unloading not support yet
059  }
060
061  public static boolean isVerbose() {
062    return VM.verboseClassLoading;
063  }
064
065  public static void setVerbose(boolean verbose) {
066    VM.verboseClassLoading = verbose;
067  }
068
069  /**
070   * Sets the count of loaded classes when writing the bootimage.
071   * @param bootimageTypeCount the number of types contained in the bootimage
072   */
073  public void setClassLoadedCountForBootimage(int bootimageTypeCount) {
074    if (VM.VerifyAssertions && VM.runningVM) {
075      VM._assert(VM.NOT_REACHED, "This method is intended to be called only by the bootimage writer");
076    }
077    this.classLoadedCount = bootimageTypeCount;
078  }
079
080}