5788325e48d93b96159cab851d5c1a0b6572f792
[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_INDEX_PAGE_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 TmfTimestamp fStartTime = TmfTimestamp.BigCrunch;
75 private TmfTimestamp fEndTime = TmfTimestamp.BigBang;
76
77 // ------------------------------------------------------------------------
78 // Constructors
79 // ------------------------------------------------------------------------
80
81 /**
82 * @param path
83 * @throws FileNotFoundException
84 */
85 protected TmfTrace(String name, Class<T> type, String path) throws FileNotFoundException {
86 this(name, type, path, DEFAULT_INDEX_PAGE_SIZE);
87 }
88
89 /**
90 * @param path
91 * @param cacheSize
92 * @throws FileNotFoundException
93 */
94 protected TmfTrace(String name, Class<T> type, String path, int cacheSize) throws FileNotFoundException {
95 super(name, type);
96 int sep = path.lastIndexOf(File.separator);
97 String simpleName = (sep >= 0) ? path.substring(sep + 1) : path;
98 setName(simpleName);
99 fPath = path;
100 fIndexPageSize = (cacheSize > 0) ? cacheSize : DEFAULT_INDEX_PAGE_SIZE;
101 }
102
103 /* (non-Javadoc)
104 * @see java.lang.Object#clone()
105 */
106 @SuppressWarnings("unchecked")
107 @Override
108 public TmfTrace<T> clone() throws CloneNotSupportedException {
109 TmfTrace<T> clone = (TmfTrace<T>) super.clone();
110 clone.fCheckpoints = (Vector<TmfCheckpoint>) fCheckpoints;
111 clone.fStartTime = new TmfTimestamp(fStartTime);
112 clone.fEndTime = new TmfTimestamp(fEndTime);
113 return clone;
114 }
115
116 // ------------------------------------------------------------------------
117 // Accessors
118 // ------------------------------------------------------------------------
119
120 /**
121 * @return the trace path
122 */
123 public String getPath() {
124 return fPath;
125 }
126
127 /* (non-Javadoc)
128 * @see org.eclipse.linuxtools.tmf.stream.ITmfEventStream#getNbEvents()
129 */
130 public long getNbEvents() {
131 return fNbEvents;
132 }
133
134 /**
135 * @return the size of the cache
136 */
137 public int getCacheSize() {
138 return fIndexPageSize;
139 }
140
141 /* (non-Javadoc)
142 * @see org.eclipse.linuxtools.tmf.stream.ITmfEventStream#getTimeRange()
143 */
144 public TmfTimeRange getTimeRange() {
145 return new TmfTimeRange(fStartTime, fEndTime);
146 }
147
148 /* (non-Javadoc)
149 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStartTime()
150 */
151 public TmfTimestamp getStartTime() {
152 return fStartTime;
153 }
154
155 /* (non-Javadoc)
156 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getEndTime()
157 */
158 public TmfTimestamp getEndTime() {
159 return fEndTime;
160 }
161
162 @SuppressWarnings("unchecked")
163 public Vector<TmfCheckpoint> getCheckpoints() {
164 return (Vector<TmfCheckpoint>) fCheckpoints.clone();
165 }
166
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
179 // ------------------------------------------------------------------------
180 // Operators
181 // ------------------------------------------------------------------------
182
183 protected void setTimeRange(TmfTimeRange range) {
184 fStartTime = range.getStartTime();
185 fEndTime = range.getEndTime();
186 }
187
188 protected void setStartTime(TmfTimestamp startTime) {
189 fStartTime = startTime;
190 }
191
192 protected void setEndTime(TmfTimestamp endTime) {
193 fEndTime = endTime;
194 }
195
196 // ------------------------------------------------------------------------
197 // TmfProvider
198 // ------------------------------------------------------------------------
199
200 @Override
201 public ITmfContext armRequest(ITmfDataRequest<T> request) {
202 if (request instanceof ITmfEventRequest<?>) {
203 return seekEvent(((ITmfEventRequest<T>) request).getRange().getStartTime());
204 }
205 return seekEvent(request.getIndex());
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) {
218 if (context instanceof TmfContext) {
219 return (T) getNextEvent((TmfContext) context);
220 }
221 return null;
222 }
223
224 // ------------------------------------------------------------------------
225 // ITmfTrace
226 // ------------------------------------------------------------------------
227
228 /* (non-Javadoc)
229 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.event.TmfTimestamp)
230 */
231 public TmfContext seekEvent(TmfTimestamp timestamp) {
232
233 if (timestamp == null) {
234 timestamp = TmfTimestamp.BigBang;
235 }
236
237 // First, find the right checkpoint
238 int index = Collections.binarySearch(fCheckpoints, new TmfCheckpoint(timestamp, null));
239
240 // In the very likely case that the checkpoint was not found, bsearch
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
248 ITmfLocation<?> location;
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 }
259 }
260 TmfContext context = seekLocation(location);
261 context.setRank(index * fIndexPageSize);
262
263 // And locate the event
264 TmfContext nextEventContext = context.clone(); // Must use clone() to get the right subtype...
265 TmfEvent event = getNextEvent(nextEventContext);
266 while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) {
267 context.setLocation(nextEventContext.getLocation().clone());
268 context.updateRank(1);
269 event = getNextEvent(nextEventContext);
270 }
271
272 return context;
273 }
274
275 /* (non-Javadoc)
276 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(int)
277 */
278 public TmfContext seekEvent(long rank) {
279
280 // Position the stream at the previous checkpoint
281 int index = (int) rank / fIndexPageSize;
282 ITmfLocation<?> location;
283 synchronized (fCheckpoints) {
284 if (fCheckpoints.size() == 0) {
285 location = null;
286 }
287 else {
288 if (index >= fCheckpoints.size()) {
289 index = fCheckpoints.size() - 1;
290 }
291 location = fCheckpoints.elementAt(index).getLocation();
292 }
293 }
294
295 TmfContext context = seekLocation(location);
296 long pos = index * fIndexPageSize;
297 context.setRank(pos);
298
299 if (pos < rank) {
300 TmfEvent event = getNextEvent(context);
301 while (event != null && ++pos < rank) {
302 event = getNextEvent(context);
303 }
304 }
305
306 return context;
307 }
308
309 /* (non-Javadoc)
310 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getNextEvent(org.eclipse.linuxtools.tmf.trace.ITmfTrace.TraceContext)
311 */
312 public synchronized TmfEvent getNextEvent(TmfContext context) {
313 // parseEvent() does not update the context
314 TmfEvent event = parseEvent(context);
315 if (event != null) {
316 updateIndex(context, context.getRank(), event.getTimestamp());
317 context.setLocation(getCurrentLocation());
318 context.updateRank(1);
319 processEvent(event);
320 }
321 return event;
322 }
323
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 }
340 }
341 }
342 }
343
344 /**
345 * Hook for "special" processing by the concrete class
346 * (called by getNextEvent())
347 *
348 * @param event
349 */
350 protected void processEvent(TmfEvent event) {
351 // Do nothing by default
352 }
353
354 /**
355 * To be implemented by the concrete class
356 */
357 public abstract TmfContext seekLocation(ITmfLocation<?> location);
358 public abstract ITmfLocation<?> getCurrentLocation();
359 public abstract TmfEvent parseEvent(TmfContext context);
360
361 // ------------------------------------------------------------------------
362 // toString
363 // ------------------------------------------------------------------------
364
365 /* (non-Javadoc)
366 * @see java.lang.Object#toString()
367 */
368 @Override
369 public String toString() {
370 return "[TmfTrace (" + getName() + ")]";
371 }
372
373 // ------------------------------------------------------------------------
374 // Indexing
375 // ------------------------------------------------------------------------
376
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 // }
432
433 // protected void notifyListeners() {
434 // broadcast(new TmfTraceUpdatedSignal(this, this, new TmfTimeRange(fStartTime, fEndTime)));
435 // }
436
437 // ------------------------------------------------------------------------
438 // TmfDataProvider
439 // ------------------------------------------------------------------------
440
441 // @Override
442 // protected void queueBackgroundRequest(final ITmfDataRequest<T> request, final int blockSize, final boolean adjust) {
443 // super.queueBackgroundRequest(request, fIndexPageSize, true);
444 // }
445
446 }
This page took 0.047392 seconds and 4 git commands to generate.