Monster merge from the integration branch. Still some problems left and JUnits failing.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf / src / org / eclipse / linuxtools / tmf / trace / TmfTrace.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.trace;
14
15 import java.io.File;
16 import java.io.FileNotFoundException;
17 import java.util.Collections;
18 import java.util.Vector;
19
20 import org.eclipse.linuxtools.tmf.component.TmfEventProvider;
21 import org.eclipse.linuxtools.tmf.event.TmfEvent;
22 import org.eclipse.linuxtools.tmf.event.TmfTimeRange;
23 import org.eclipse.linuxtools.tmf.event.TmfTimestamp;
24 import org.eclipse.linuxtools.tmf.request.ITmfDataRequest;
25 import org.eclipse.linuxtools.tmf.request.ITmfEventRequest;
26
27 /**
28 * <b><u>TmfTrace</u></b>
29 * <p>
30 * Abstract implementation of ITmfTrace. It should be sufficient to extend this
31 * class and provide implementation for <code>getCurrentLocation()</code> and
32 * <code>seekLocation()</code>, as well as a proper parser, to have a working
33 * concrete implementation.
34 * <p>
35 * Note: The notion of event rank is still under heavy discussion. Although
36 * used by the Events View and probably useful in the general case, there
37 * is no easy way to implement it for LTTng (actually a strong case is being
38 * made that this is useless).
39 * <p>
40 * That it is not supported by LTTng does by no mean indicate that it is not
41 * useful for (just about) every other tracing tool. Therefore, this class
42 * provides a minimal (and partial) implementation of rank. However, the current
43 * implementation should not be relied on in the general case.
44 *
45 * TODO: Add support for live streaming (notifications, incremental indexing, ...)
46 */
47 public abstract class TmfTrace<T extends TmfEvent> extends TmfEventProvider<T> implements ITmfTrace, Cloneable {
48
49 // ------------------------------------------------------------------------
50 // Constants
51 // ------------------------------------------------------------------------
52
53 // The default number of events to cache
54 // TODO: Make the DEFAULT_CACHE_SIZE a preference
55 public static final int DEFAULT_CACHE_SIZE = 1000;
56
57 // ------------------------------------------------------------------------
58 // Attributes
59 // ------------------------------------------------------------------------
60
61 // The trace path
62 private final String fPath;
63
64 // The cache page size AND checkpoints interval
65 protected int fIndexPageSize;
66
67 // The set of event stream checkpoints (for random access)
68 protected Vector<TmfCheckpoint> fCheckpoints = new Vector<TmfCheckpoint>();
69
70 // The number of events collected
71 protected long fNbEvents = 0;
72
73 // The time span of the event stream
74 private TmfTimeRange fTimeRange = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigBang);
75
76 // ------------------------------------------------------------------------
77 // Constructors
78 // ------------------------------------------------------------------------
79
80 /**
81 * @param path
82 * @throws FileNotFoundException
83 */
84 protected TmfTrace(String name, Class<T> type, String path) throws FileNotFoundException {
85 this(name, type, path, DEFAULT_CACHE_SIZE);
86 }
87
88 /**
89 * @param path
90 * @param cacheSize
91 * @throws FileNotFoundException
92 */
93 protected TmfTrace(String name, Class<T> type, String path, int cacheSize) throws FileNotFoundException {
94 super(name, type);
95 int sep = path.lastIndexOf(File.separator);
96 String simpleName = (sep >= 0) ? path.substring(sep + 1) : path;
97 setName(simpleName);
98 fPath = path;
99 fIndexPageSize = (cacheSize > 0) ? cacheSize : DEFAULT_CACHE_SIZE;
100
101 try {
102 fClone = clone();
103 } catch (CloneNotSupportedException e) {
104 e.printStackTrace();
105 }
106 }
107
108 /* (non-Javadoc)
109 * @see java.lang.Object#clone()
110 */
111 @SuppressWarnings("unchecked")
112 @Override
113 public TmfTrace<T> clone() throws CloneNotSupportedException {
114 TmfTrace<T> clone = (TmfTrace<T>) super.clone();
115 clone.fCheckpoints = (Vector<TmfCheckpoint>) fCheckpoints.clone();
116 clone.fTimeRange = new TmfTimeRange(fTimeRange);
117 return clone;
118 }
119
120 // ------------------------------------------------------------------------
121 // Accessors
122 // ------------------------------------------------------------------------
123
124 /**
125 * @return the trace path
126 */
127 public String getPath() {
128 return fPath;
129 }
130
131 /* (non-Javadoc)
132 * @see org.eclipse.linuxtools.tmf.stream.ITmfEventStream#getNbEvents()
133 */
134 public long getNbEvents() {
135 return fNbEvents;
136 }
137
138 /**
139 * @return the size of the cache
140 */
141 public int getCacheSize() {
142 return fIndexPageSize;
143 }
144
145 /* (non-Javadoc)
146 * @see org.eclipse.linuxtools.tmf.stream.ITmfEventStream#getTimeRange()
147 */
148 public TmfTimeRange getTimeRange() {
149 return fTimeRange;
150 }
151
152 /* (non-Javadoc)
153 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStartTime()
154 */
155 public TmfTimestamp getStartTime() {
156 return fTimeRange.getStartTime();
157 }
158
159 /* (non-Javadoc)
160 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getEndTime()
161 */
162 public TmfTimestamp getEndTime() {
163 return fTimeRange.getEndTime();
164 }
165
166 @SuppressWarnings("unchecked")
167 public Vector<TmfCheckpoint> getCheckpoints() {
168 return (Vector<TmfCheckpoint>) fCheckpoints.clone();
169 }
170
171 // ------------------------------------------------------------------------
172 // Operators
173 // ------------------------------------------------------------------------
174
175 protected void setTimeRange(TmfTimeRange range) {
176 fTimeRange = range;
177 }
178
179 protected void setStartTime(TmfTimestamp startTime) {
180 fTimeRange = new TmfTimeRange(startTime, fTimeRange.getEndTime());
181 }
182
183 protected void setEndTime(TmfTimestamp endTime) {
184 fTimeRange = new TmfTimeRange(fTimeRange.getStartTime(), endTime);
185 }
186
187 // ------------------------------------------------------------------------
188 // TmfProvider
189 // ------------------------------------------------------------------------
190
191 @Override
192 public ITmfContext armRequest(ITmfDataRequest<T> request) {
193 if (request instanceof ITmfEventRequest<?>) {
194 return seekEvent(((ITmfEventRequest<T>) request).getRange().getStartTime());
195 }
196 return seekEvent(request.getIndex());
197 }
198
199 /**
200 * Return the next piece of data based on the context supplied. The context
201 * would typically be updated for the subsequent read.
202 *
203 * @param context
204 * @return
205 */
206 @SuppressWarnings("unchecked")
207 @Override
208 public T getNext(ITmfContext context) {
209 if (context instanceof TmfContext) {
210 return (T) getNextEvent((TmfContext) context);
211 }
212 return null;
213 }
214
215 // ------------------------------------------------------------------------
216 // ITmfTrace
217 // ------------------------------------------------------------------------
218
219 /* (non-Javadoc)
220 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.event.TmfTimestamp)
221 */
222 public TmfContext seekEvent(TmfTimestamp timestamp) {
223
224 if (timestamp == null) {
225 timestamp = TmfTimestamp.BigBang;
226 }
227
228 // First, find the right checkpoint
229 int index = Collections.binarySearch(fCheckpoints, new TmfCheckpoint(timestamp, null));
230
231 // In the very likely case that the checkpoint was not found, bsearch
232 // returns its negated would-be location (not an offset...). From that
233 // index, we can then position the stream and get the event.
234 if (index < 0) {
235 index = Math.max(0, -(index + 2));
236 }
237
238 // Position the stream at the checkpoint
239 ITmfLocation<?> location;
240 synchronized (fCheckpoints) {
241 if (fCheckpoints.size() > 0) {
242 if (index >= fCheckpoints.size()) {
243 index = fCheckpoints.size() - 1;
244 }
245 location = fCheckpoints.elementAt(index).getLocation();
246 }
247 else {
248 location = null;
249 }
250 }
251 TmfContext context = seekLocation(location);
252 context.setRank(index * fIndexPageSize);
253
254 // And locate the event
255 TmfContext nextEventContext = context.clone(); // Must use clone() to get the right subtype...
256 TmfEvent event = getNextEvent(nextEventContext);
257 while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) {
258 context.setLocation(nextEventContext.getLocation().clone());
259 context.updateRank(1);
260 event = getNextEvent(nextEventContext);
261 }
262
263 return context;
264 }
265
266 /* (non-Javadoc)
267 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(int)
268 */
269 public TmfContext seekEvent(long rank) {
270
271 // Position the stream at the previous checkpoint
272 int index = (int) rank / fIndexPageSize;
273 ITmfLocation<?> location;
274 synchronized (fCheckpoints) {
275 if (fCheckpoints.size() == 0) {
276 location = null;
277 }
278 else {
279 if (index >= fCheckpoints.size()) {
280 index = fCheckpoints.size() - 1;
281 }
282 location = fCheckpoints.elementAt(index).getLocation();
283 }
284 }
285
286 TmfContext context = seekLocation(location);
287 long pos = index * fIndexPageSize;
288 context.setRank(pos);
289
290 if (pos < rank) {
291 TmfEvent event = getNextEvent(context);
292 while (event != null && ++pos < rank) {
293 event = getNextEvent(context);
294 }
295 }
296
297 return context;
298 }
299
300 /* (non-Javadoc)
301 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getNextEvent(org.eclipse.linuxtools.tmf.trace.ITmfTrace.TraceContext)
302 */
303 public synchronized TmfEvent getNextEvent(TmfContext context) {
304 // parseEvent() does not update the context
305 TmfEvent event = parseEvent(context);
306 if (event != null) {
307 context.setLocation(getCurrentLocation());
308 updateIndex(context, context.getRank(), event.getTimestamp());
309 context.updateRank(1);
310 processEvent(event);
311 }
312 return event;
313 }
314
315 public synchronized void updateIndex(ITmfContext context, long rank, TmfTimestamp timestamp) {
316 // Build the index as we go along
317 if (context.isValidRank() && (rank % fIndexPageSize) == 0) {
318 // Determine the table position
319 long position = rank / fIndexPageSize;
320 // Add new entry at proper location (if empty)
321 if (fCheckpoints.size() == position) {
322 ITmfLocation<?> location = getCurrentLocation().clone();
323 fCheckpoints.add(new TmfCheckpoint(timestamp, location));
324 // System.out.println(getName() + "[" + (fCheckpoints.size() - 1) + "] " + timestamp + ", " + location.toString());
325 }
326 }
327 }
328
329 /**
330 * Hook for "special" processing by the concrete class
331 * (called by getNextEvent())
332 *
333 * @param event
334 */
335 protected void processEvent(TmfEvent event) {
336 // Do nothing by default
337 }
338
339 /**
340 * To be implemented by the concrete class
341 */
342 public abstract TmfContext seekLocation(ITmfLocation<?> location);
343 public abstract ITmfLocation<?> getCurrentLocation();
344 public abstract TmfEvent parseEvent(TmfContext context);
345
346 // ------------------------------------------------------------------------
347 // toString
348 // ------------------------------------------------------------------------
349
350 /* (non-Javadoc)
351 * @see java.lang.Object#toString()
352 */
353 @Override
354 public String toString() {
355 return "[TmfTrace (" + getName() + ")]";
356 }
357
358 }
This page took 0.040624 seconds and 5 git commands to generate.