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.adaptive.AosEntrypoints;
017import org.jikesrvm.scheduler.Synchronization;
018import org.vmmagic.pragma.Uninterruptible;
019
020/**
021 * A YieldCounterListener samples yield points, and
022 * notifies an Organizer when a threshold is reached.
023 * <p>
024 * In effect, this class provides a way to "wake up" an infrequent
025 * service periodically.
026 */
027@Uninterruptible
028public final class YieldCounterListener extends NullListener {
029
030  /**
031   * Constructor
032   *
033   * @param yieldThreshold  the threshold of when to call organizer
034   */
035  public YieldCounterListener(int yieldThreshold) {
036    this.yieldThreshold = yieldThreshold;
037  }
038
039  @Override
040  public void update(int whereFrom) {
041    if (VM.VerifyAssertions) VM._assert(AosEntrypoints.yieldCountListenerNumYieldsField != null);
042    int yp = Synchronization.fetchAndAdd(this, AosEntrypoints.yieldCountListenerNumYieldsField.getOffset(), 1) + 1;
043    if (yp == yieldThreshold) {
044      totalYields += yp;
045      activateOrganizer();
046    }
047  }
048
049  @Override
050  public void report() {
051    VM.sysWriteln("Yield points counted: ", totalYields);
052  }
053
054  /**
055   * No-op.
056   */
057  @Override
058  public void reset() { }
059
060  private final int yieldThreshold;
061  @SuppressWarnings({"unused", "UnusedDeclaration", "CanBeFinal"})
062// Accessed via EntryPoints
063  private final int numYields = 0;
064  private int totalYields = 0;
065}