001/*
002 * This file is part of the Tuning Fork Visualization Platform
003 *  (http://sourceforge.net/projects/tuningforkvp)
004 *
005 * Copyright (c) 2005 - 2008 IBM Corporation.
006 * All rights reserved. This program and the accompanying materials
007 * are made available under the terms of the Eclipse Public License v1.0
008 * which accompanies this distribution, and is available at
009 * http://www.eclipse.org/legal/epl-v10.html
010 *
011 * Contributors:
012 *     IBM Corporation - initial API and implementation
013 */
014
015package com.ibm.tuningfork.tracegen.chunk;
016
017import org.vmmagic.pragma.Uninterruptible;
018import org.vmmagic.pragma.Untraced;
019
020import com.ibm.tuningfork.tracegen.types.EventType;
021
022@Uninterruptible
023public final class EventChunk extends Chunk {
024
025  public static final int EVENT_TYPE_ID = 5;
026  public static final int FEEDLET_ID_OFFSET = Chunk.DATA_OFFSET;
027  public static final int SEQUENCE_NUMBER_OFFSET = Chunk.DATA_OFFSET + 4;
028  public static final int EVENT_DATA_OFFSET = Chunk.DATA_OFFSET + 8;
029  protected final static int DEFAULT_EVENT_CHUNK_SIZE = 16 * 1024;
030
031  /* Only for use by EventChunkQueue */
032  @Untraced
033  public EventChunk next = null;
034
035  public EventChunk() {
036    super(EVENT_TYPE_ID, DEFAULT_EVENT_CHUNK_SIZE);
037    seek(EVENT_DATA_OFFSET);
038  }
039
040  public void reset(int feedletIndex, int sequenceNumber) {
041    super.resetImpl();
042    seek(EVENT_DATA_OFFSET);
043    putIntAt(FEEDLET_ID_OFFSET, feedletIndex);
044    putIntAt(SEQUENCE_NUMBER_OFFSET, sequenceNumber);
045  }
046
047  public boolean addEvent(long timeStamp, EventType et) {
048    int required = ENCODING_SPACE_LONG + ENCODING_SPACE_INT;
049    if (!hasRoom(required)) {
050      return false;
051    }
052    addLongUnchecked(timeStamp);
053    addIntUnchecked(et.getIndex());
054    return true;
055  }
056
057  public boolean addEvent(long timeStamp, EventType et, int v) {
058    int required = ENCODING_SPACE_LONG + ENCODING_SPACE_INT
059    + ENCODING_SPACE_INT;
060    if (!hasRoom(required)) {
061      return false;
062    }
063    addLongUnchecked(timeStamp);
064    addIntUnchecked(et.getIndex());
065    addIntUnchecked(v);
066    return true;
067  }
068
069  public boolean addEvent(long timeStamp, EventType et, int v1, int v2) {
070    int required = ENCODING_SPACE_LONG + ENCODING_SPACE_INT + 2*ENCODING_SPACE_INT;
071    if (!hasRoom(required)) {
072      return false;
073    }
074    addLongUnchecked(timeStamp);
075    addIntUnchecked(et.getIndex());
076    addIntUnchecked(v1);
077    addIntUnchecked(v2);
078    return true;
079  }
080
081  public boolean addEvent(long timeStamp, EventType et, int v1, int v2, int v3) {
082    int required = ENCODING_SPACE_LONG + ENCODING_SPACE_INT + 3*ENCODING_SPACE_INT;
083    if (!hasRoom(required)) {
084      return false;
085    }
086    addLongUnchecked(timeStamp);
087    addIntUnchecked(et.getIndex());
088    addIntUnchecked(v1);
089    addIntUnchecked(v2);
090    addIntUnchecked(v3);
091    return true;
092  }
093
094  public boolean addEvent(long timeStamp, EventType et, int v1, int v2, int v3, int v4) {
095    int required = ENCODING_SPACE_LONG + ENCODING_SPACE_INT + 4*ENCODING_SPACE_INT;
096    if (!hasRoom(required)) {
097      return false;
098    }
099    addLongUnchecked(timeStamp);
100    addIntUnchecked(et.getIndex());
101    addIntUnchecked(v1);
102    addIntUnchecked(v2);
103    addIntUnchecked(v3);
104    addIntUnchecked(v4);
105    return true;
106  }
107
108  public boolean addEvent(long timeStamp, EventType et, long v) {
109    int required = ENCODING_SPACE_LONG + ENCODING_SPACE_INT + ENCODING_SPACE_LONG;
110    if (!hasRoom(required)) {
111      return false;
112    }
113    addLongUnchecked(timeStamp);
114    addIntUnchecked(et.getIndex());
115    addLongUnchecked(v);
116    return true;
117  }
118
119  public boolean addEvent(long timeStamp, EventType et, double v) {
120    int required = ENCODING_SPACE_LONG + ENCODING_SPACE_INT + ENCODING_SPACE_DOUBLE;
121    if (!hasRoom(required)) {
122      return false;
123    }
124    addLongUnchecked(timeStamp);
125    addIntUnchecked(et.getIndex());
126    addDoubleUnchecked(v);
127    return true;
128  }
129
130  public boolean addEvent(long timeStamp, EventType et, String v) {
131    int guess = ENCODING_SPACE_LONG + ENCODING_SPACE_INT + JikesRVMSupport.getStringLength(v);
132    if (!hasRoom(guess)) {
133      return false;
134    }
135    int savedCursor = getPosition();
136    addLongUnchecked(timeStamp);
137    addIntUnchecked(et.getIndex());
138    if (!addString(v)) {
139      seek(savedCursor);
140      return false;
141    }
142    return true;
143  }
144
145  public boolean addEvent(long timeStamp, EventType et, int iv, double dv) {
146    int required = ENCODING_SPACE_LONG + 2 * ENCODING_SPACE_INT + ENCODING_SPACE_DOUBLE;
147    if (!hasRoom(required)) {
148      return false;
149    }
150    addLongUnchecked(timeStamp);
151    addIntUnchecked(et.getIndex());
152    addIntUnchecked(iv);
153    addDoubleUnchecked(dv);
154    return true;
155  }
156
157  public boolean addEvent(long timeStamp, EventType et, int iv1, int iv2, double dv) {
158    int required = ENCODING_SPACE_LONG + 3 * ENCODING_SPACE_INT + ENCODING_SPACE_DOUBLE;
159    if (!hasRoom(required)) {
160      return false;
161    }
162    addLongUnchecked(timeStamp);
163    addIntUnchecked(et.getIndex());
164    addIntUnchecked(iv1);
165    addIntUnchecked(iv2);
166    addDoubleUnchecked(dv);
167    return true;
168  }
169
170  public boolean addEvent(long timeStamp, EventType et, double dv, String sv) {
171    int guess = ENCODING_SPACE_LONG + ENCODING_SPACE_DOUBLE + ENCODING_SPACE_INT + JikesRVMSupport.getStringLength(sv);
172    if (!hasRoom(guess)) {
173      return false;
174    }
175    int savedCursor = getPosition();
176    addLongUnchecked(timeStamp);
177    addIntUnchecked(et.getIndex());
178    addDoubleUnchecked(dv);
179    if (!addString(sv)) {
180      seek(savedCursor);
181      return false;
182    }
183    return true;
184  }
185
186  public boolean addEvent(long timeStamp, EventType et, int[] idata,
187                          long[] ldata, double[] ddata, String[] sdata) {
188    int ilen = (idata == null) ? 0 : idata.length;
189    int llen = (ldata == null) ? 0 : ldata.length;
190    int dlen = (ddata == null) ? 0 : ddata.length;
191    int slen = (sdata == null) ? 0 : sdata.length;
192    int guess = ENCODING_SPACE_LONG + ENCODING_SPACE_INT + ilen * ENCODING_SPACE_INT +
193                llen * ENCODING_SPACE_LONG + dlen * ENCODING_SPACE_DOUBLE;
194    if (!hasRoom(guess)) {
195      return false;
196    }
197    int savedPosition = getPosition();
198    addLongUnchecked(timeStamp);
199    addIntUnchecked(et.getIndex());
200    for (int i = 0; i < ilen; i++) {
201      addIntUnchecked(idata[i]);
202    }
203    for (int i = 0; i < llen; i++) {
204      addLongUnchecked(ldata[i]);
205    }
206    for (int i = 0; i < dlen; i++) {
207      addDoubleUnchecked(ddata[i]);
208    }
209    for (int i = 0; i < slen; i++) {
210      if (!addString(sdata[i])) {
211        seek(savedPosition);
212        return false;
213      }
214    }
215    return true;
216  }
217
218}