Change ITmfTrace<T extends TmfEvent> to ITmfTrace<T extends ITmfEvent>
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / 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.core.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.core.resources.IProject;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.Path;
25 import org.eclipse.core.runtime.Status;
26 import org.eclipse.core.runtime.jobs.Job;
27 import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider;
28 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
29 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
30 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
31 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
32 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
33 import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
34 import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
35 import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
36 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
37
38 /**
39 * <b><u>TmfTrace</u></b>
40 * <p>
41 * Abstract implementation of ITmfTrace. It should be sufficient to extend this class and provide implementation for
42 * <code>getCurrentLocation()</code> and <code>seekLocation()</code>, as well as a proper parser, to have a working
43 * concrete implementation.
44 * <p>
45 * Note: The notion of event rank is still under heavy discussion. Although used by the Events View and probably useful
46 * in the general case, there is no easy way to implement it for LTTng (actually a strong case is being made that this
47 * is useless).
48 * <p>
49 * That it is not supported by LTTng does by no mean indicate that it is not useful for (just about) every other tracing
50 * tool. Therefore, this class provides a minimal (and partial) implementation of rank. However, the current
51 * implementation should not be relied on in the general case.
52 *
53 * TODO: Add support for live streaming (notifications, incremental indexing, ...)
54 */
55 public abstract class TmfTrace<T extends ITmfEvent> extends TmfEventProvider<T> implements ITmfTrace<T>, Cloneable {
56
57 // ------------------------------------------------------------------------
58 // Constants
59 // ------------------------------------------------------------------------
60
61 // The default number of events to cache
62 // TODO: Make the DEFAULT_CACHE_SIZE a preference
63 public static final int DEFAULT_INDEX_PAGE_SIZE = 50000;
64
65 // ------------------------------------------------------------------------
66 // Attributes
67 // ------------------------------------------------------------------------
68
69 // The trace path
70 private String fPath;
71
72 // The trace name
73 private String fTraceName;
74
75 // The cache page size AND checkpoints interval
76 protected int fIndexPageSize = DEFAULT_INDEX_PAGE_SIZE;
77
78 // The set of event stream checkpoints (for random access)
79 protected Vector<TmfCheckpoint> fCheckpoints = new Vector<TmfCheckpoint>();
80
81 // The number of events collected
82 protected long fNbEvents = 0;
83
84 // The time span of the event stream
85 private ITmfTimestamp fStartTime = TmfTimestamp.BigCrunch;
86 private ITmfTimestamp fEndTime = TmfTimestamp.BigBang;
87
88 // The trace resource
89 private IResource fResource;
90
91 // ------------------------------------------------------------------------
92 // Constructors
93 // ------------------------------------------------------------------------
94
95 public TmfTrace() {
96 super();
97 }
98
99 @Override
100 public void initTrace(String name, String path, Class<T> eventType) throws FileNotFoundException {
101 initTmfTrace(name, path, eventType, DEFAULT_INDEX_PAGE_SIZE, false);
102 }
103
104 @Override
105 public void initTrace(String name, String path, Class<T> eventType, int cacheSize) throws FileNotFoundException {
106 initTmfTrace(name, path, eventType, cacheSize, false);
107 }
108
109 @Override
110 public void initTrace(String name, String path, Class<T> eventType, boolean indexTrace) throws FileNotFoundException {
111 initTmfTrace(name, path, eventType, DEFAULT_INDEX_PAGE_SIZE, indexTrace);
112 }
113
114 @Override
115 public void initTrace(String name, String path, Class<T> eventType, int cacheSize, boolean indexTrace) throws FileNotFoundException {
116 initTmfTrace(name, path, eventType, cacheSize, indexTrace);
117 }
118
119 private void initTmfTrace(String name, String path, Class<T> eventType, int cacheSize, boolean indexTrace) throws FileNotFoundException {
120 fPath = path;
121 if (name != null) {
122 fTraceName = name;
123 }
124 if (fTraceName == null) {
125 fTraceName = ""; //$NON-NLS-1$
126 if (path != null) {
127 int sep = path.lastIndexOf(Path.SEPARATOR);
128 fTraceName = (sep >= 0) ? path.substring(sep + 1) : path;
129 }
130 }
131 super.init(fTraceName, eventType);
132 fIndexPageSize = (cacheSize > 0) ? cacheSize : DEFAULT_INDEX_PAGE_SIZE;
133 if (indexTrace)
134 indexTrace(false);
135 }
136
137 @Override
138 public boolean validate(IProject project, String path) {
139 File file = new File(path);
140 return file.exists();
141 }
142
143 /**
144 * @param path
145 * @throws FileNotFoundException
146 */
147 protected TmfTrace(String name, Class<T> type, String path) throws FileNotFoundException {
148 this(name, type, path, DEFAULT_INDEX_PAGE_SIZE, true);
149 }
150
151 /**
152 * @param path
153 * @param cacheSize
154 * @throws FileNotFoundException
155 */
156 protected TmfTrace(String name, Class<T> type, String path, int cacheSize) throws FileNotFoundException {
157 this(name, type, path, cacheSize, true);
158 }
159
160 /**
161 * @param path
162 * @param indexTrace
163 * @throws FileNotFoundException
164 */
165 protected TmfTrace(String name, Class<T> type, String path, boolean indexTrace) throws FileNotFoundException {
166 this(name, type, path, DEFAULT_INDEX_PAGE_SIZE, indexTrace);
167 }
168
169 /**
170 * @param path
171 * @param cacheSize
172 * @param indexTrace
173 * @throws FileNotFoundException
174 */
175 protected TmfTrace(String name, Class<T> type, String path, int cacheSize, boolean indexTrace) throws FileNotFoundException {
176 super();
177 fTraceName = name;
178 initTrace(name, path, type, cacheSize, indexTrace);
179 }
180
181 @SuppressWarnings("unchecked")
182 @Override
183 public TmfTrace<T> clone() throws CloneNotSupportedException {
184 TmfTrace<T> clone = (TmfTrace<T>) super.clone();
185 clone.fCheckpoints = fCheckpoints;
186 clone.fStartTime = fStartTime.clone();
187 clone.fEndTime = fEndTime.clone();
188 return clone;
189 }
190
191 // ------------------------------------------------------------------------
192 // Accessors
193 // ------------------------------------------------------------------------
194
195 /**
196 * @return the trace path
197 */
198 @Override
199 public String getPath() {
200 return fPath;
201 }
202
203 /* (non-Javadoc)
204 * @see org.eclipse.linuxtools.tmf.stream.ITmfEventStream#getNbEvents()
205 */
206 @Override
207 public long getNbEvents() {
208 return fNbEvents;
209 }
210
211 /**
212 * @return the size of the cache
213 */
214 @Override
215 public int getCacheSize() {
216 return fIndexPageSize;
217 }
218
219 /* (non-Javadoc)
220 * @see org.eclipse.linuxtools.tmf.stream.ITmfEventStream#getTimeRange()
221 */
222 @Override
223 public TmfTimeRange getTimeRange() {
224 return new TmfTimeRange(fStartTime, fEndTime);
225 }
226
227 /* (non-Javadoc)
228 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStartTime()
229 */
230 @Override
231 public ITmfTimestamp getStartTime() {
232 return fStartTime;
233 }
234
235 /* (non-Javadoc)
236 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getEndTime()
237 */
238 @Override
239 public ITmfTimestamp getEndTime() {
240 return fEndTime;
241 }
242
243 /* (non-Javadoc)
244 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStreamingInterval()
245 */
246 @Override
247 public long getStreamingInterval() {
248 return 0;
249 }
250
251 @SuppressWarnings("unchecked")
252 public Vector<TmfCheckpoint> getCheckpoints() {
253 return (Vector<TmfCheckpoint>) fCheckpoints.clone();
254 }
255
256 /**
257 * Returns the rank of the first event with the requested timestamp. If none, returns the index of the next event
258 * (if any).
259 *
260 * @param timestamp
261 * @return
262 */
263 @Override
264 public long getRank(ITmfTimestamp timestamp) {
265 TmfContext context = seekEvent(timestamp);
266 return context.getRank();
267 }
268
269 // ------------------------------------------------------------------------
270 // Operators
271 // ------------------------------------------------------------------------
272
273 protected void setTimeRange(TmfTimeRange range) {
274 fStartTime = range.getStartTime();
275 fEndTime = range.getEndTime();
276 }
277
278 protected void setStartTime(ITmfTimestamp startTime) {
279 fStartTime = startTime;
280 }
281
282 protected void setEndTime(ITmfTimestamp endTime) {
283 fEndTime = endTime;
284 }
285
286 // ------------------------------------------------------------------------
287 // TmfProvider
288 // ------------------------------------------------------------------------
289
290 @Override
291 public ITmfContext armRequest(ITmfDataRequest<T> request) {
292 if (request instanceof ITmfEventRequest<?>
293 && !TmfTimestamp.BigBang.equals(((ITmfEventRequest<T>) request).getRange().getStartTime()) && request.getIndex() == 0) {
294 ITmfContext context = seekEvent(((ITmfEventRequest<T>) request).getRange().getStartTime());
295 ((ITmfEventRequest<T>) request).setStartIndex((int) context.getRank());
296 return context;
297
298 }
299 return seekEvent(request.getIndex());
300 }
301
302 /**
303 * Return the next piece of data based on the context supplied. The context would typically be updated for the
304 * subsequent read.
305 *
306 * @param context
307 * @return
308 */
309 @SuppressWarnings("unchecked")
310 @Override
311 public T getNext(ITmfContext context) {
312 if (context instanceof TmfContext) {
313 return (T) getNextEvent((TmfContext) context);
314 }
315 return null;
316 }
317
318 // ------------------------------------------------------------------------
319 // ITmfTrace
320 // ------------------------------------------------------------------------
321
322 /* (non-Javadoc)
323 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.event.TmfTimestamp)
324 */
325 @Override
326 public TmfContext seekEvent(ITmfTimestamp timestamp) {
327
328 if (timestamp == null) {
329 timestamp = TmfTimestamp.BigBang;
330 }
331
332 // First, find the right checkpoint
333 int index = Collections.binarySearch(fCheckpoints, new TmfCheckpoint(timestamp, null));
334
335 // In the very likely case that the checkpoint was not found, bsearch
336 // returns its negated would-be location (not an offset...). From that
337 // index, we can then position the stream and get the event.
338 if (index < 0) {
339 index = Math.max(0, -(index + 2));
340 }
341
342 // Position the stream at the checkpoint
343 ITmfLocation<?> location;
344 synchronized (fCheckpoints) {
345 if (fCheckpoints.size() > 0) {
346 if (index >= fCheckpoints.size()) {
347 index = fCheckpoints.size() - 1;
348 }
349 location = fCheckpoints.elementAt(index).getLocation();
350 } else {
351 location = null;
352 }
353 }
354 TmfContext context = seekLocation(location);
355 context.setRank(index * fIndexPageSize);
356
357 // And locate the event
358 TmfContext nextEventContext = context.clone(); // Must use clone() to get the right subtype...
359 ITmfEvent event = getNextEvent(nextEventContext);
360 while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) {
361 context.setLocation(nextEventContext.getLocation().clone());
362 context.updateRank(1);
363 event = getNextEvent(nextEventContext);
364 }
365
366 return context;
367 }
368
369 /* (non-Javadoc)
370 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(int)
371 */
372 @Override
373 public TmfContext seekEvent(long rank) {
374
375 // Position the stream at the previous checkpoint
376 int index = (int) rank / fIndexPageSize;
377 ITmfLocation<?> location;
378 synchronized (fCheckpoints) {
379 if (fCheckpoints.size() == 0) {
380 location = null;
381 } else {
382 if (index >= fCheckpoints.size()) {
383 index = fCheckpoints.size() - 1;
384 }
385 location = fCheckpoints.elementAt(index).getLocation();
386 }
387 }
388
389 TmfContext context = seekLocation(location);
390 long pos = index * fIndexPageSize;
391 context.setRank(pos);
392
393 if (pos < rank) {
394 ITmfEvent event = getNextEvent(context);
395 while (event != null && ++pos < rank) {
396 event = getNextEvent(context);
397 }
398 }
399
400 return context;
401 }
402
403 /*
404 * (non-Javadoc)
405 *
406 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getNextEvent(org.eclipse.
407 * linuxtools.tmf.trace.ITmfTrace.TraceContext)
408 */
409 @Override
410 public synchronized ITmfEvent getNextEvent(TmfContext context) {
411 // parseEvent() does not update the context
412 ITmfEvent event = parseEvent(context);
413 if (event != null) {
414 updateIndex(context, context.getRank(), event.getTimestamp());
415 context.setLocation(getCurrentLocation());
416 context.updateRank(1);
417 processEvent(event);
418 }
419 return event;
420 }
421
422 protected synchronized void updateIndex(ITmfContext context, long rank, ITmfTimestamp timestamp) {
423 if (fStartTime.compareTo(timestamp, false) > 0)
424 fStartTime = timestamp;
425 if (fEndTime.compareTo(timestamp, false) < 0)
426 fEndTime = timestamp;
427 if (context.isValidRank()) {
428 if (fNbEvents <= rank)
429 fNbEvents = rank + 1;
430 // Build the index as we go along
431 if ((rank % fIndexPageSize) == 0) {
432 // Determine the table position
433 long position = rank / fIndexPageSize;
434 // Add new entry at proper location (if empty)
435 if (fCheckpoints.size() == position) {
436 ITmfLocation<?> location = context.getLocation().clone();
437 fCheckpoints.add(new TmfCheckpoint(timestamp.clone(), location));
438 // System.out.println(getName() + "[" + (fCheckpoints.size()
439 // - 1) + "] " + timestamp + ", " + location.toString());
440 }
441 }
442 }
443 }
444
445 /**
446 * Hook for special processing by the concrete class (called by getNextEvent())
447 *
448 * @param event
449 */
450 protected void processEvent(ITmfEvent event) {
451 // Do nothing by default
452 }
453
454 // ------------------------------------------------------------------------
455 // toString
456 // ------------------------------------------------------------------------
457
458 /* (non-Javadoc)
459 * @see java.lang.Object#toString()
460 */
461 @Override
462 @SuppressWarnings("nls")
463 public String toString() {
464 return "[TmfTrace (" + getName() + ")]";
465 }
466
467 // ------------------------------------------------------------------------
468 // Indexing
469 // ------------------------------------------------------------------------
470
471 /*
472 * The purpose of the index is to keep the information needed to rapidly
473 * restore the traces contexts at regular intervals (every INDEX_PAGE_SIZE
474 * event).
475 */
476
477 @SuppressWarnings({ "unchecked" })
478 protected void indexTrace(boolean waitForCompletion) {
479
480 final Job job = new Job("Indexing " + getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
481 @Override
482 protected IStatus run(IProgressMonitor monitor) {
483 while (!monitor.isCanceled()) {
484 try {
485 Thread.sleep(100);
486 } catch (InterruptedException e) {
487 return Status.OK_STATUS;
488 }
489 }
490 monitor.done();
491 return Status.OK_STATUS;
492 }
493 };
494 job.schedule();
495
496 fCheckpoints.clear();
497 ITmfEventRequest<ITmfEvent> request = new TmfEventRequest<ITmfEvent>(ITmfEvent.class, TmfTimeRange.Eternity, TmfDataRequest.ALL_DATA,
498 fIndexPageSize, ITmfDataRequest.ExecutionType.BACKGROUND) {
499
500 ITmfTimestamp startTime = null;
501 ITmfTimestamp lastTime = null;
502
503 @Override
504 public void handleData(ITmfEvent event) {
505 super.handleData(event);
506 if (event != null) {
507 ITmfTimestamp ts = event.getTimestamp();
508 if (startTime == null)
509 startTime = ts.clone();
510 lastTime = ts.clone();
511
512 if ((getNbRead() % fIndexPageSize) == 0) {
513 updateTrace();
514 }
515 }
516 }
517
518 @Override
519 public void handleSuccess() {
520 updateTrace();
521 }
522
523 @Override
524 public void handleCompleted() {
525 job.cancel();
526 super.handleCompleted();
527 }
528
529 private void updateTrace() {
530 int nbRead = getNbRead();
531 if (nbRead != 0) {
532 fStartTime = startTime;
533 fEndTime = lastTime;
534 fNbEvents = nbRead;
535 notifyListeners();
536 }
537 }
538 };
539
540 sendRequest((ITmfDataRequest<T>) request);
541 if (waitForCompletion)
542 try {
543 request.waitForCompletion();
544 } catch (InterruptedException e) {
545 e.printStackTrace();
546 }
547 }
548
549 protected void notifyListeners() {
550 broadcast(new TmfTraceUpdatedSignal(this, this, new TmfTimeRange(fStartTime, fEndTime)));
551 }
552
553 /**
554 * Set the resource to be used for bookmarks on this trace
555 * @param resource the bookmarks resource
556 */
557 public void setResource(IResource resource) {
558 fResource = resource;
559 }
560
561 /**
562 * Get the resource used for bookmarks on this trace
563 * @return the bookmarks resource or null if none is set
564 */
565 public IResource getResource() {
566 return fResource;
567 }
568 }
This page took 0.044213 seconds and 6 git commands to generate.