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.mmtk.vm.gcspy;
014
015import static org.mmtk.utility.gcspy.StreamConstants.PRESENTATION_ENUM;
016import static org.mmtk.utility.gcspy.StreamConstants.PRESENTATION_MAX_VAR;
017import static org.mmtk.utility.gcspy.StreamConstants.PRESENTATION_PERCENT;
018import static org.mmtk.utility.gcspy.StreamConstants.PRESENTATION_PERCENT_VAR;
019import static org.mmtk.utility.gcspy.StreamConstants.PRESENTATION_PLAIN;
020import static org.mmtk.utility.gcspy.StreamConstants.PRESENTATION_PLUS;
021
022import org.mmtk.utility.Log;
023import org.mmtk.utility.gcspy.Color;
024import org.mmtk.utility.gcspy.drivers.AbstractDriver;
025import org.mmtk.vm.VM;
026import org.vmmagic.pragma.Interruptible;
027import org.vmmagic.pragma.Uninterruptible;
028import org.vmmagic.unboxed.Address;
029
030/**
031 *
032 * Abstract class for a GCspy Stream.
033 * Implementing classes will mostly forward calls
034 * to the gcspy C library
035 */
036
037@Uninterruptible public abstract class Stream {
038
039  /****************************************************************************
040  *
041  * Instance variables
042  */
043
044  /**
045   * The address of the C stream, gcspy_gc_stream_t *stream, used in all calls
046   * to the C library
047   */
048  protected Address stream;
049
050  /** The owning GCspy space */
051  protected ServerSpace serverSpace;
052
053  /** The stream's ID */
054  protected int streamId;
055
056  /**
057   * A summary has 1 or 2 values depending on presentation style
058   * (PERCENT* styles require 2 values).
059   */
060  protected int summaryLen;
061
062  /** The first summary value */
063  protected int summary0;
064
065  /** The second summary value (if any) */
066  protected int summary1;
067
068  /** The minimum value for tiles */
069  private final int min;
070
071  /** The maximum value for tiles */
072  private final int max;
073
074  /** use summaries? */
075  protected boolean summaryEnabled;
076
077  /** the presentation style */
078  protected int presentation;
079
080  protected static final boolean DEBUG = false;
081
082  /**
083   * Construct a new GCspy stream.
084   *
085   * @param driver The AbstractDriver that owns this Stream
086   * @param dataType The stream's data type, one of BYTE_TYPE, SHORT_TYPE or INT_TYPE
087   * @param name The name of the stream (e.g. "Used space")
088   * @param minValue The minimum value for any item in this stream. Values less than
089   *                 this will be represented as "minValue-"
090   * @param maxValue The maximum value for any item in this stream. Values greater than
091   *                 this will be represented as "maxValue+"
092   * @param zeroValue The zero value for this stream
093   * @param defaultValue The default value for this stream
094   * @param stringPre A string to prefix values (e.g. "Used: ")
095   * @param stringPost A string to suffix values (e.g. " bytes.")
096   * @param presentation How a stream value is to be presented.
097   * @param paintStyle How the value is to be painted.
098   * @param indexMaxStream The index for the maximum stream if the presentation is *_VAR.
099   * @param colour The default colour for tiles of this stream
100   * @param summary Is a summary enabled?
101   */
102  protected Stream(
103      AbstractDriver driver,
104      int dataType,
105      String name,
106      int minValue,
107      int maxValue,
108      int zeroValue,
109      int defaultValue,
110      String stringPre,
111      String stringPost,
112      int presentation,
113      int paintStyle,
114      int indexMaxStream,
115      Color colour,
116      boolean summary) {
117
118    serverSpace = driver.getServerSpace();
119    summaryEnabled = summary;
120    this.presentation = presentation;
121    if (summary)
122      setupSummary(presentation);
123    min = minValue;
124    max = maxValue;
125
126    driver.addStream(this);
127    if (DEBUG) {
128      Log.write("Adding stream ");
129      Log.write(name);
130      Log.writeln(" id=", streamId);
131    }
132  }
133
134
135  /**
136   * Set the stream address and id (called by AbstractDriver.addStream).
137   * @param id the id
138   * @param str the address of the gcspy C gcspy_gc_stream_t *stream
139   */
140  public void setStream(int id, Address str) {
141    streamId = id;
142    stream = str;
143  }
144
145  /**
146   * Return the minimum value expected for this stream.
147   * @return the minimum value
148   */
149  public int getMinValue() {
150    return min;
151  }
152
153  /**
154   * Return the maximum value expected for this stream.
155   * @return the maximum value
156   */
157  public int getMaxValue() {
158    return max;
159  }
160
161  /**
162   * Setup the summary array.
163   * @param presentation the presentation style
164   */
165  @Interruptible
166  private void setupSummary(int presentation) {
167    switch (presentation) {
168      case PRESENTATION_PLAIN:
169      case PRESENTATION_PLUS:
170      case PRESENTATION_MAX_VAR:
171      case PRESENTATION_ENUM:
172        summaryLen = 1;
173        break;
174      case PRESENTATION_PERCENT:
175      case PRESENTATION_PERCENT_VAR:
176        summaryLen = 2;
177        break;
178      default:
179        VM.assertions._assert(false);
180    }
181  }
182
183  /**
184   * Set the summary value for presentation styles with just one value
185   * @param value0 the value
186   */
187  public void setSummary(int value0) {
188    if (VM.VERIFY_ASSERTIONS)
189      VM.assertions._assert(presentation != PRESENTATION_PERCENT &&
190                            presentation != PRESENTATION_PERCENT_VAR);
191    summary0 = value0;
192  }
193
194  /**
195   * Set the summary values for presentation styles with two values (i.e.
196   * PRESENTATION_PERCENT and PRESENTATION_PERCENT_VAR).
197   * @param value0 the numerator value
198   * @param value1 the denominator value
199   */
200  public void setSummary(int value0, int value1) {
201    if (VM.VERIFY_ASSERTIONS)
202      VM.assertions._assert(presentation == PRESENTATION_PERCENT ||
203                            presentation == PRESENTATION_PERCENT_VAR);
204    summary0 = value0;
205    summary1 = value1;
206  }
207
208  /**
209   * Send the data for this stream.
210   * @param event the event.
211   * @param numTiles the number of tiles to send (which may be less than maxTileNum)
212
213   */
214  public abstract void send(int event, int numTiles);
215
216  /**
217   * Send the summary data for this stream.
218   */
219  public void sendSummary() {
220    if (summaryEnabled) {
221      serverSpace.summary(streamId, summaryLen);
222      serverSpace.summaryValue(summary0);
223      if (summaryLen == 2)
224        serverSpace.summaryValue(summary1);
225      serverSpace.summaryEnd();
226    }
227  }
228
229}
230
231