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.adaptive.measurements.listeners;
014
015import org.jikesrvm.VM;
016import org.jikesrvm.scheduler.RVMThread;
017import org.vmmagic.pragma.Uninterruptible;
018
019/**
020 * A simple listener to accumulate counts of total events
021 * and the fraction of those events that occurred 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
027public final class CallDensityListener extends NullListener {
028
029  private double numSamples = 0;
030  private double numBackedgeSamples = 0;
031
032  @Override
033  public void update(int whereFrom) {
034    numSamples++;
035    if (whereFrom == RVMThread.BACKEDGE) numBackedgeSamples++;
036  }
037
038  public double callDensity() {
039    return 1 - (numBackedgeSamples / numSamples);
040  }
041
042  @Override
043  public void reset() {
044    numSamples = 0;
045    numBackedgeSamples = 0;
046  }
047
048  @Override
049  public void report() {
050    VM.sysWriteln("The call density of the program is ", callDensity());
051  }
052}