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     */
013    package org.jikesrvm.adaptive.measurements.listeners;
014    
015    import org.jikesrvm.VM;
016    import org.jikesrvm.scheduler.RVMThread;
017    import org.vmmagic.pragma.Uninterruptible;
018    
019    /**
020     * A simple listener to accumulate counts of total events
021     * and the fraction of those events that occured at loop backedges.
022     * In effect, this provides a mechanism for estimating the
023     * call density of the program.  If most yieldpoints are being taken at
024     * backedges, then call density is low.
025     */
026    @Uninterruptible
027    public final class CallDensityListener extends NullListener {
028    
029      private double numSamples = 0;
030      private double numBackedgeSamples = 0;
031    
032      /**
033       * This method is called when its time to record that a
034       * yield point has occurred.
035       * @param whereFrom Was this a yieldpoint in a PROLOGUE, BACKEDGE, or
036       *             EPILOGUE?
037       */
038      public void update(int whereFrom) {
039        numSamples++;
040        if (whereFrom == RVMThread.BACKEDGE) numBackedgeSamples++;
041      }
042    
043      public double callDensity() {
044        return 1 - (numBackedgeSamples / numSamples);
045      }
046    
047      public void reset() {
048        numSamples = 0;
049        numBackedgeSamples = 0;
050      }
051    
052      public void report() {
053        VM.sysWriteln("The call density of the program is ", callDensity());
054      }
055    }