2010-09-17 Francois Chouinard <fchouinard@gmail.com> Contribution for Bug325662
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf / src / org / eclipse / linuxtools / tmf / trace / TmfTrace.java
CommitLineData
8c8bf09f 1/*******************************************************************************
e31e01e8 2 * Copyright (c) 2009, 2010 Ericsson
8c8bf09f
ASL
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
13package org.eclipse.linuxtools.tmf.trace;
14
b0a282fb 15import java.io.File;
62d1696a 16import java.io.FileNotFoundException;
62d1696a 17import java.util.Collections;
8c8bf09f
ASL
18import java.util.Vector;
19
fc6ccf6f 20import org.eclipse.linuxtools.tmf.component.TmfEventProvider;
8c8bf09f
ASL
21import org.eclipse.linuxtools.tmf.event.TmfEvent;
22import org.eclipse.linuxtools.tmf.event.TmfTimeRange;
23import org.eclipse.linuxtools.tmf.event.TmfTimestamp;
2fb2eb37
FC
24import org.eclipse.linuxtools.tmf.request.ITmfDataRequest;
25import org.eclipse.linuxtools.tmf.request.ITmfEventRequest;
8c8bf09f
ASL
26
27/**
146a887c 28 * <b><u>TmfTrace</u></b>
8c8bf09f 29 * <p>
146a887c
FC
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
4e3aa37d 33 * concrete implementation.
ff4ed569 34 * <p>
54d55ced 35 * Note: The notion of event rank is still under heavy discussion. Although
ff4ed569 36 * used by the Events View and probably useful in the general case, there
54d55ced 37 * is no easy way to implement it for LTTng (actually a strong case is being
ff4ed569
FC
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.
54d55ced 44 *
4e3aa37d 45 * TODO: Add support for live streaming (notifications, incremental indexing, ...)
8c8bf09f 46 */
ff4ed569 47public abstract class TmfTrace<T extends TmfEvent> extends TmfEventProvider<T> implements ITmfTrace, Cloneable {
62d1696a 48
e31e01e8 49 // ------------------------------------------------------------------------
62d1696a 50 // Constants
e31e01e8 51 // ------------------------------------------------------------------------
62d1696a
FC
52
53 // The default number of events to cache
e31e01e8 54 // TODO: Make the DEFAULT_CACHE_SIZE a preference
664902f7 55 public static final int DEFAULT_INDEX_PAGE_SIZE = 1000;
8c8bf09f 56
e31e01e8 57 // ------------------------------------------------------------------------
8c8bf09f 58 // Attributes
e31e01e8 59 // ------------------------------------------------------------------------
8c8bf09f 60
b0a282fb
FC
61 // The trace path
62 private final String fPath;
63
8d2e2848 64 // The cache page size AND checkpoints interval
9f584e4c 65 protected int fIndexPageSize;
62d1696a
FC
66
67 // The set of event stream checkpoints (for random access)
9f584e4c 68 protected Vector<TmfCheckpoint> fCheckpoints = new Vector<TmfCheckpoint>();
62d1696a
FC
69
70 // The number of events collected
a3fe52fc 71 protected long fNbEvents = 0;
62d1696a
FC
72
73 // The time span of the event stream
cb866e08
FC
74 private TmfTimestamp fStartTime = TmfTimestamp.BigCrunch;
75 private TmfTimestamp fEndTime = TmfTimestamp.BigBang;
62d1696a 76
e31e01e8 77 // ------------------------------------------------------------------------
50adc88e 78 // Constructors
e31e01e8 79 // ------------------------------------------------------------------------
8c8bf09f 80
ff4ed569
FC
81 /**
82 * @param path
83 * @throws FileNotFoundException
84 */
ce785d7d 85 protected TmfTrace(String name, Class<T> type, String path) throws FileNotFoundException {
664902f7 86 this(name, type, path, DEFAULT_INDEX_PAGE_SIZE);
ff4ed569
FC
87 }
88
62d1696a 89 /**
e31e01e8
FC
90 * @param path
91 * @param cacheSize
62d1696a
FC
92 * @throws FileNotFoundException
93 */
ce785d7d
FC
94 protected TmfTrace(String name, Class<T> type, String path, int cacheSize) throws FileNotFoundException {
95 super(name, type);
b0a282fb 96 int sep = path.lastIndexOf(File.separator);
ce785d7d
FC
97 String simpleName = (sep >= 0) ? path.substring(sep + 1) : path;
98 setName(simpleName);
b0a282fb 99 fPath = path;
664902f7 100 fIndexPageSize = (cacheSize > 0) ? cacheSize : DEFAULT_INDEX_PAGE_SIZE;
8c8bf09f
ASL
101 }
102
ff4ed569
FC
103 /* (non-Javadoc)
104 * @see java.lang.Object#clone()
62d1696a 105 */
ff4ed569
FC
106 @SuppressWarnings("unchecked")
107 @Override
108 public TmfTrace<T> clone() throws CloneNotSupportedException {
109 TmfTrace<T> clone = (TmfTrace<T>) super.clone();
cb866e08
FC
110 clone.fCheckpoints = (Vector<TmfCheckpoint>) fCheckpoints;
111 clone.fStartTime = new TmfTimestamp(fStartTime);
112 clone.fEndTime = new TmfTimestamp(fEndTime);
ff4ed569 113 return clone;
8c8bf09f
ASL
114 }
115
e31e01e8 116 // ------------------------------------------------------------------------
8c8bf09f 117 // Accessors
e31e01e8 118 // ------------------------------------------------------------------------
8c8bf09f 119
62d1696a 120 /**
b0a282fb 121 * @return the trace path
62d1696a 122 */
b0a282fb
FC
123 public String getPath() {
124 return fPath;
8c8bf09f
ASL
125 }
126
62d1696a
FC
127 /* (non-Javadoc)
128 * @see org.eclipse.linuxtools.tmf.stream.ITmfEventStream#getNbEvents()
129 */
4e3aa37d 130 public long getNbEvents() {
62d1696a 131 return fNbEvents;
8c8bf09f
ASL
132 }
133
b0a282fb
FC
134 /**
135 * @return the size of the cache
136 */
8d2e2848 137 public int getCacheSize() {
9f584e4c 138 return fIndexPageSize;
b0a282fb
FC
139 }
140
62d1696a
FC
141 /* (non-Javadoc)
142 * @see org.eclipse.linuxtools.tmf.stream.ITmfEventStream#getTimeRange()
143 */
8c8bf09f 144 public TmfTimeRange getTimeRange() {
cb866e08 145 return new TmfTimeRange(fStartTime, fEndTime);
8c8bf09f
ASL
146 }
147
e31e01e8
FC
148 /* (non-Javadoc)
149 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStartTime()
150 */
146a887c 151 public TmfTimestamp getStartTime() {
cb866e08 152 return fStartTime;
146a887c
FC
153 }
154
e31e01e8
FC
155 /* (non-Javadoc)
156 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getEndTime()
157 */
146a887c 158 public TmfTimestamp getEndTime() {
cb866e08 159 return fEndTime;
146a887c
FC
160 }
161
ff4ed569
FC
162 @SuppressWarnings("unchecked")
163 public Vector<TmfCheckpoint> getCheckpoints() {
164 return (Vector<TmfCheckpoint>) fCheckpoints.clone();
54d55ced
FC
165 }
166
abfad0aa
FC
167 /**
168 * Returns the rank of the first event with the requested timestamp.
169 * If none, returns the index of the next event (if any).
170 *
171 * @param timestamp
172 * @return
173 */
174 public long getRank(TmfTimestamp timestamp) {
175 TmfContext context = seekEvent(timestamp);
176 return context.getRank();
177 }
178
e31e01e8 179 // ------------------------------------------------------------------------
8c8bf09f 180 // Operators
e31e01e8 181 // ------------------------------------------------------------------------
8c8bf09f 182
4e3aa37d 183 protected void setTimeRange(TmfTimeRange range) {
cb866e08
FC
184 fStartTime = range.getStartTime();
185 fEndTime = range.getEndTime();
4e3aa37d
FC
186 }
187
188 protected void setStartTime(TmfTimestamp startTime) {
cb866e08 189 fStartTime = startTime;
4e3aa37d
FC
190 }
191
192 protected void setEndTime(TmfTimestamp endTime) {
cb866e08 193 fEndTime = endTime;
4e3aa37d
FC
194 }
195
e31e01e8
FC
196 // ------------------------------------------------------------------------
197 // TmfProvider
198 // ------------------------------------------------------------------------
199
200 @Override
2fb2eb37
FC
201 public ITmfContext armRequest(ITmfDataRequest<T> request) {
202 if (request instanceof ITmfEventRequest<?>) {
203 return seekEvent(((ITmfEventRequest<T>) request).getRange().getStartTime());
e31e01e8 204 }
ff4ed569 205 return seekEvent(request.getIndex());
e31e01e8
FC
206 }
207
208 /**
209 * Return the next piece of data based on the context supplied. The context
210 * would typically be updated for the subsequent read.
211 *
212 * @param context
213 * @return
214 */
215 @SuppressWarnings("unchecked")
216 @Override
217 public T getNext(ITmfContext context) {
9f584e4c
FC
218 if (context instanceof TmfContext) {
219 return (T) getNextEvent((TmfContext) context);
e31e01e8
FC
220 }
221 return null;
222 }
223
e31e01e8
FC
224 // ------------------------------------------------------------------------
225 // ITmfTrace
226 // ------------------------------------------------------------------------
227
146a887c
FC
228 /* (non-Javadoc)
229 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.event.TmfTimestamp)
230 */
9f584e4c 231 public TmfContext seekEvent(TmfTimestamp timestamp) {
62d1696a 232
4e3aa37d
FC
233 if (timestamp == null) {
234 timestamp = TmfTimestamp.BigBang;
235 }
236
237 // First, find the right checkpoint
9f584e4c 238 int index = Collections.binarySearch(fCheckpoints, new TmfCheckpoint(timestamp, null));
62d1696a 239
8d2e2848 240 // In the very likely case that the checkpoint was not found, bsearch
62d1696a
FC
241 // returns its negated would-be location (not an offset...). From that
242 // index, we can then position the stream and get the event.
243 if (index < 0) {
244 index = Math.max(0, -(index + 2));
245 }
246
247 // Position the stream at the checkpoint
452ad365 248 ITmfLocation<?> location;
e31e01e8
FC
249 synchronized (fCheckpoints) {
250 if (fCheckpoints.size() > 0) {
251 if (index >= fCheckpoints.size()) {
252 index = fCheckpoints.size() - 1;
253 }
254 location = fCheckpoints.elementAt(index).getLocation();
255 }
256 else {
257 location = null;
258 }
8d2e2848 259 }
54d55ced
FC
260 TmfContext context = seekLocation(location);
261 context.setRank(index * fIndexPageSize);
62d1696a 262
54d55ced 263 // And locate the event
ff4ed569 264 TmfContext nextEventContext = context.clone(); // Must use clone() to get the right subtype...
62d1696a
FC
265 TmfEvent event = getNextEvent(nextEventContext);
266 while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) {
54d55ced
FC
267 context.setLocation(nextEventContext.getLocation().clone());
268 context.updateRank(1);
62d1696a
FC
269 event = getNextEvent(nextEventContext);
270 }
271
54d55ced 272 return context;
62d1696a
FC
273 }
274
146a887c
FC
275 /* (non-Javadoc)
276 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(int)
277 */
9f584e4c 278 public TmfContext seekEvent(long rank) {
62d1696a
FC
279
280 // Position the stream at the previous checkpoint
9f584e4c 281 int index = (int) rank / fIndexPageSize;
452ad365 282 ITmfLocation<?> location;
e31e01e8 283 synchronized (fCheckpoints) {
54d55ced
FC
284 if (fCheckpoints.size() == 0) {
285 location = null;
286 }
287 else {
e31e01e8 288 if (index >= fCheckpoints.size()) {
54d55ced 289 index = fCheckpoints.size() - 1;
e31e01e8
FC
290 }
291 location = fCheckpoints.elementAt(index).getLocation();
292 }
8d2e2848 293 }
54d55ced 294
9f584e4c
FC
295 TmfContext context = seekLocation(location);
296 long pos = index * fIndexPageSize;
297 context.setRank(pos);
e31e01e8 298
9f584e4c 299 if (pos < rank) {
e31e01e8 300 TmfEvent event = getNextEvent(context);
9f584e4c 301 while (event != null && ++pos < rank) {
e31e01e8
FC
302 event = getNextEvent(context);
303 }
165c977c 304 }
62d1696a 305
8f50c396 306 return context;
8c8bf09f
ASL
307 }
308
146a887c
FC
309 /* (non-Javadoc)
310 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getNextEvent(org.eclipse.linuxtools.tmf.trace.ITmfTrace.TraceContext)
311 */
9f584e4c 312 public synchronized TmfEvent getNextEvent(TmfContext context) {
e31e01e8 313 // parseEvent() does not update the context
cc6eec3e 314 TmfEvent event = parseEvent(context);
4e3aa37d 315 if (event != null) {
550d787e 316 updateIndex(context, context.getRank(), event.getTimestamp());
cb866e08 317 context.setLocation(getCurrentLocation());
54d55ced 318 context.updateRank(1);
4e3aa37d
FC
319 processEvent(event);
320 }
146a887c
FC
321 return event;
322 }
8c8bf09f 323
cb866e08
FC
324 protected synchronized void updateIndex(ITmfContext context, long rank, TmfTimestamp timestamp) {
325 if (fStartTime.compareTo(timestamp, false) > 0) fStartTime = timestamp;
326 if (fEndTime.compareTo(timestamp, false) < 0) fEndTime = timestamp;
327 if (context.isValidRank()) {
328 if (fNbEvents <= rank)
329 fNbEvents = rank + 1;
330 // Build the index as we go along
331 if ((rank % fIndexPageSize) == 0) {
332 // Determine the table position
333 long position = rank / fIndexPageSize;
334 // Add new entry at proper location (if empty)
335 if (fCheckpoints.size() == position) {
336 ITmfLocation<?> location = context.getLocation().clone();
337 fCheckpoints.add(new TmfCheckpoint(timestamp, location));
338// System.out.println(getName() + "[" + (fCheckpoints.size() - 1) + "] " + timestamp + ", " + location.toString());
339 }
550d787e
FC
340 }
341 }
342 }
343
4e3aa37d 344 /**
e31e01e8
FC
345 * Hook for "special" processing by the concrete class
346 * (called by getNextEvent())
347 *
146a887c
FC
348 * @param event
349 */
ff4ed569 350 protected void processEvent(TmfEvent event) {
146a887c 351 // Do nothing by default
62d1696a 352 }
4e3aa37d 353
e31e01e8
FC
354 /**
355 * To be implemented by the concrete class
4e3aa37d 356 */
452ad365
FC
357 public abstract TmfContext seekLocation(ITmfLocation<?> location);
358 public abstract ITmfLocation<?> getCurrentLocation();
9f584e4c 359 public abstract TmfEvent parseEvent(TmfContext context);
4e3aa37d 360
e31e01e8
FC
361 // ------------------------------------------------------------------------
362 // toString
363 // ------------------------------------------------------------------------
8d2e2848
FC
364
365 /* (non-Javadoc)
366 * @see java.lang.Object#toString()
367 */
368 @Override
369 public String toString() {
ce785d7d 370 return "[TmfTrace (" + getName() + ")]";
8d2e2848 371 }
146a887c 372
664902f7
FC
373 // ------------------------------------------------------------------------
374 // Indexing
375 // ------------------------------------------------------------------------
376
f9673903
FC
377// /*
378// * The purpose of the index is to keep the information needed to rapidly
379// * restore the traces contexts at regular intervals (every INDEX_PAGE_SIZE
380// * event).
381// */
382//
383// @SuppressWarnings({ "unchecked", "unused" })
384// private void indexTrace(boolean waitForCompletion) {
385//
386// fCheckpoints.clear();
387//
388// ITmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, TmfTimeRange.Eternity, TmfDataRequest.ALL_DATA, 1, ITmfDataRequest.ExecutionType.BACKGROUND) {
389//
390// TmfTimestamp startTime = null;
391// TmfTimestamp lastTime = null;
392//
393// @Override
394// public void handleData() {
395// TmfEvent[] events = getData();
396// if (events.length > 0) {
397// TmfTimestamp ts = events[0].getTimestamp();
398// if (startTime == null) {
399// startTime = new TmfTimestamp(ts);
400// fStartTime = startTime;
401// }
402// lastTime = new TmfTimestamp(ts);
403//
404// if ((fNbRead % DEFAULT_INDEX_PAGE_SIZE) == 0) {
405// updateTraceData();
406// }
407// }
408// }
409//
410// @Override
411// public void handleSuccess() {
412// updateTraceData();
413// }
414//
415// private void updateTraceData() {
416// if (fNbRead != 0) {
417// fEndTime = new TmfTimestamp(lastTime);
418// fNbEvents = fNbRead;
419// notifyListeners();
420// }
421// }
422// };
423//
424// sendRequest((ITmfDataRequest<T>) request);
425// if (waitForCompletion)
426// try {
427// request.waitForCompletion();
428// } catch (InterruptedException e) {
429// e.printStackTrace();
430// }
431// }
664902f7 432
f9673903
FC
433// protected void notifyListeners() {
434// broadcast(new TmfTraceUpdatedSignal(this, this, new TmfTimeRange(fStartTime, fEndTime)));
435// }
664902f7 436
abfad0aa
FC
437 // ------------------------------------------------------------------------
438 // TmfDataProvider
439 // ------------------------------------------------------------------------
440
f9673903
FC
441// @Override
442// protected void queueBackgroundRequest(final ITmfDataRequest<T> request, final int blockSize, final boolean adjust) {
443// super.queueBackgroundRequest(request, fIndexPageSize, true);
444// }
f6b14ce2 445
8c8bf09f 446}
This page took 0.058113 seconds and 5 git commands to generate.