Remove obsolete legacy experiment
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / TmfTrace.java
... / ...
CommitLineData
1/*******************************************************************************
2 * Copyright (c) 2009, 2010, 2012 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 * Francois Chouinard - Updated as per TMF Trace Model 1.0
12 *******************************************************************************/
13
14package org.eclipse.linuxtools.tmf.core.trace;
15
16import java.io.File;
17
18import org.eclipse.core.resources.IResource;
19import org.eclipse.core.runtime.Path;
20import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider;
21import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
22import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
23import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
24import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
25import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
26import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
27import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
28
29/**
30 * Abstract implementation of ITmfTrace.
31 * <p>
32 * Since the concept of 'location' is trace specific, the concrete classes have
33 * to provide the related methods, namely:
34 * <ul>
35 * <li> public ITmfLocation<?> getCurrentLocation()
36 * <li> public double getLocationRatio(ITmfLocation<?> location)
37 * <li> public ITmfContext seekEvent(ITmfLocation<?> location)
38 * <li> public ITmfContext seekEvent(double ratio)
39 * <li> public boolean validate(IProject project, String path)
40 * </ul>
41 * A concrete trace must provide its corresponding parser. A common way to
42 * accomplish this is by making the concrete class extend TmfTrace and
43 * implement ITmfEventParser.
44 * <p>
45 * The concrete class can either specify its own indexer or use the provided
46 * TmfCheckpointIndexer (default). In this case, the trace cache size will be
47 * used as checkpoint interval.
48 *
49 * @version 1.0
50 * @author Francois Chouinard
51 *
52 * @see ITmfEvent
53 * @see ITmfTraceIndexer
54 * @see ITmfEventParser
55 */
56public abstract class TmfTrace<T extends ITmfEvent> extends TmfEventProvider<T> implements ITmfTrace<T> {
57
58 // ------------------------------------------------------------------------
59 // Attributes
60 // ------------------------------------------------------------------------
61
62 // The resource used for persistent properties for this trace
63 private IResource fResource;
64
65 // The trace path
66 private String fPath;
67
68 // The trace cache page size
69 private int fCacheSize = ITmfTrace.DEFAULT_TRACE_CACHE_SIZE;
70
71 // The number of events collected (so far)
72 private long fNbEvents = 0;
73
74 // The time span of the event stream
75 private ITmfTimestamp fStartTime = TmfTimestamp.BIG_CRUNCH;
76 private ITmfTimestamp fEndTime = TmfTimestamp.BIG_BANG;
77
78 // The trace streaming interval (0 = no streaming)
79 private long fStreamingInterval = 0;
80
81 // The trace indexer
82 private ITmfTraceIndexer<ITmfTrace<ITmfEvent>> fIndexer;
83
84 // The trace parser
85 private ITmfEventParser<T> fParser;
86
87 // ------------------------------------------------------------------------
88 // Construction
89 // ------------------------------------------------------------------------
90
91 /**
92 * The default, parameterless, constructor
93 */
94 @SuppressWarnings({ "unchecked", "rawtypes" })
95 public TmfTrace() {
96 super();
97 fIndexer = new TmfCheckpointIndexer(this);
98 }
99
100 /**
101 * The standard constructor (non-live trace). Applicable when the trace
102 * implements its own parser and if at checkpoint-based index is OK.
103 *
104 * @param resource the resource associated to the trace
105 * @param type the trace event type
106 * @param path the trace path
107 * @param cacheSize the trace cache size
108 * @throws TmfTraceException
109 */
110 protected TmfTrace(final IResource resource, final Class<T> type, final String path, final int cacheSize) throws TmfTraceException {
111 this(resource, type, path, cacheSize, 0);
112 }
113
114 /**
115 * The standard constructor (live trace). Applicable when the trace
116 * implements its own parser and if at checkpoint-based index is OK.
117 *
118 * @param resource the resource associated to the trace
119 * @param type the trace event type
120 * @param path the trace path
121 * @param cacheSize the trace cache size
122 * @param interval the trace streaming interval
123 * @throws TmfTraceException
124 */
125 protected TmfTrace(final IResource resource, final Class<T> type, final String path, final int cacheSize, final long interval) throws TmfTraceException {
126 this(resource, type, path, cacheSize, interval, null);
127 }
128
129 /**
130 * The 'non-default indexer' constructor. Allows to provide a trace
131 * specific indexer.
132 *
133 * @param resource the resource associated to the trace
134 * @param type the trace event type
135 * @param path the trace path
136 * @param cacheSize the trace cache size
137 * @param indexer the trace indexer
138 * @throws TmfTraceException
139 */
140 protected TmfTrace(final IResource resource, final Class<T> type, final String path, final int cacheSize,
141 final long interval, final ITmfTraceIndexer<?> indexer) throws TmfTraceException {
142 this(resource, type, path, cacheSize, interval, indexer, null);
143 }
144
145 /**
146 * The full constructor where trace specific indexer/parser are provided.
147 *
148 * @param resource the resource associated to the trace
149 * @param type the trace event type
150 * @param path the trace path
151 * @param cacheSize the trace cache size
152 * @param indexer the trace indexer
153 * @param parser the trace event parser
154 * @throws TmfTraceException
155 */
156 @SuppressWarnings({ "unchecked", "rawtypes" })
157 protected TmfTrace(final IResource resource, final Class<T> type, final String path, final int cacheSize,
158 final long interval, final ITmfTraceIndexer<?> indexer, final ITmfEventParser<T> parser) throws TmfTraceException {
159 super();
160 fCacheSize = (cacheSize > 0) ? cacheSize : ITmfTrace.DEFAULT_TRACE_CACHE_SIZE;
161 fStreamingInterval = interval;
162 fIndexer = (indexer != null) ? indexer : new TmfCheckpointIndexer(this, fCacheSize);
163 fParser = parser;
164 initialize(resource, path, type);
165 }
166
167 /**
168 * Copy constructor
169 *
170 * @param trace the original trace
171 */
172 @SuppressWarnings({ "unchecked", "rawtypes" })
173 public TmfTrace(final TmfTrace<T> trace) throws TmfTraceException {
174 super();
175 if (trace == null) {
176 throw new IllegalArgumentException();
177 }
178 fCacheSize = trace.getCacheSize();
179 fStreamingInterval = trace.getStreamingInterval();
180 fIndexer = new TmfCheckpointIndexer(this);
181 fParser = trace.fParser;
182 initialize(trace.getResource(), trace.getPath(), trace.getEventType());
183 }
184
185 // ------------------------------------------------------------------------
186 // ITmfTrace - Initializers
187 // ------------------------------------------------------------------------
188
189 /* (non-Javadoc)
190 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(org.eclipse.core.resources.IResource, java.lang.String, java.lang.Class)
191 */
192 @Override
193 public void initTrace(final IResource resource, final String path, final Class<T> type) throws TmfTraceException {
194 initialize(resource, path, type);
195 }
196
197 /**
198 * Initialize the trace common attributes and the base component.
199 *
200 * @param resource the Eclipse resource (trace)
201 * @param path the trace path
202 * @param type the trace event type
203 *
204 * @throws TmfTraceException
205 */
206 @SuppressWarnings("unchecked")
207 protected void initialize(final IResource resource, final String path, final Class<T> type) throws TmfTraceException {
208 if (path == null) {
209 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
210 }
211 fPath = path;
212 fResource = resource;
213 String traceName = (resource != null) ? resource.getName() : null;
214 // If no resource was provided, extract the display name the trace path
215 if (traceName == null) {
216 final int sep = path.lastIndexOf(Path.SEPARATOR);
217 traceName = (sep >= 0) ? path.substring(sep + 1) : path;
218 }
219 if (fParser == null) {
220 if (this instanceof ITmfEventParser) {
221 fParser = (ITmfEventParser<T>) this;
222 } else {
223 throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$
224 }
225 }
226 super.init(traceName, type);
227 }
228
229 /**
230 * Indicates if the path points to an existing file/directory
231 *
232 * @param path the path to test
233 * @return true if the file/directory exists
234 */
235 protected boolean fileExists(final String path) {
236 final File file = new File(path);
237 return file.exists();
238 }
239
240 /**
241 * Index the trace
242 *
243 * @param waitForCompletion index synchronously (true) or not (false)
244 */
245 protected void indexTrace(boolean waitForCompletion) {
246 getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, waitForCompletion);
247 }
248
249 // ------------------------------------------------------------------------
250 // ITmfTrace - Basic getters
251 // ------------------------------------------------------------------------
252
253 /* (non-Javadoc)
254 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEventType()
255 */
256 @Override
257 @SuppressWarnings("unchecked")
258 public Class<T> getEventType() {
259 return (Class<T>) super.getType();
260 }
261
262 /* (non-Javadoc)
263 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
264 */
265 @Override
266 public IResource getResource() {
267 return fResource;
268 }
269
270 /* (non-Javadoc)
271 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getPath()
272 */
273 @Override
274 public String getPath() {
275 return fPath;
276 }
277
278 /* (non-Javadoc)
279 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getIndexPageSize()
280 */
281 @Override
282 public int getCacheSize() {
283 return fCacheSize;
284 }
285
286 /* (non-Javadoc)
287 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStreamingInterval()
288 */
289 @Override
290 public long getStreamingInterval() {
291 return fStreamingInterval;
292 }
293
294 /**
295 * @return the trace indexer
296 */
297 protected ITmfTraceIndexer<ITmfTrace<ITmfEvent>> getIndexer() {
298 return fIndexer;
299 }
300
301 /**
302 * @return the trace parser
303 */
304 protected ITmfEventParser<T> getParser() {
305 return fParser;
306 }
307
308 // ------------------------------------------------------------------------
309 // ITmfTrace - Trace characteristics getters
310 // ------------------------------------------------------------------------
311
312 /* (non-Javadoc)
313 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNbEvents()
314 */
315 @Override
316 public synchronized long getNbEvents() {
317 return fNbEvents;
318 }
319
320 /* (non-Javadoc)
321 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getTimeRange()
322 */
323 @Override
324 public TmfTimeRange getTimeRange() {
325 return new TmfTimeRange(fStartTime, fEndTime);
326 }
327
328 /* (non-Javadoc)
329 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStartTime()
330 */
331 @Override
332 public ITmfTimestamp getStartTime() {
333 return fStartTime.clone();
334 }
335
336 /* (non-Javadoc)
337 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEndTime()
338 */
339 @Override
340 public ITmfTimestamp getEndTime() {
341 return fEndTime.clone();
342 }
343
344 // ------------------------------------------------------------------------
345 // Convenience setters/getters
346 // ------------------------------------------------------------------------
347
348 /**
349 * Set the trace cache size. Must be done at initialization time.
350 *
351 * @param cacheSize The trace cache size
352 */
353 protected void setCacheSize(final int cacheSize) {
354 fCacheSize = cacheSize;
355 }
356
357 /**
358 * Set the trace known number of events. This can be quite dynamic
359 * during indexing or for live traces.
360 *
361 * @param nbEvents The number of events
362 */
363 protected synchronized void setNbEvents(final long nbEvents) {
364 fNbEvents = (nbEvents > 0) ? nbEvents : 0;
365 }
366
367 /**
368 * Update the trace events time range
369 *
370 * @param range the new time range
371 */
372 protected void setTimeRange(final TmfTimeRange range) {
373 fStartTime = range.getStartTime().clone();
374 fEndTime = range.getEndTime().clone();
375 }
376
377 /**
378 * Update the trace chronologically first event timestamp
379 *
380 * @param startTime the new first event timestamp
381 */
382 protected void setStartTime(final ITmfTimestamp startTime) {
383 fStartTime = startTime.clone();
384 }
385
386 /**
387 * Update the trace chronologically last event timestamp
388 *
389 * @param endTime the new last event timestamp
390 */
391 protected void setEndTime(final ITmfTimestamp endTime) {
392 fEndTime = endTime.clone();
393 }
394
395 /**
396 * Set the polling interval for live traces (default = 0 = no streaming).
397 *
398 * @param interval the new trace streaming interval
399 */
400 protected void setStreamingInterval(final long interval) {
401 fStreamingInterval = (interval > 0) ? interval : 0;
402 }
403
404 /**
405 * Set the trace indexer. Must be done at initialization time.
406 *
407 * @param indexer the trace indexer
408 */
409 protected void setIndexer(final ITmfTraceIndexer<ITmfTrace<ITmfEvent>> indexer) {
410 fIndexer = indexer;
411 }
412
413 /**
414 * Set the trace parser. Must be done at initialization time.
415 *
416 * @param parser the new trace parser
417 */
418 protected void setParser(final ITmfEventParser<T> parser) {
419 fParser = parser;
420 }
421
422 // ------------------------------------------------------------------------
423 // ITmfTrace - SeekEvent operations (returning a trace context)
424 // ------------------------------------------------------------------------
425
426 /* (non-Javadoc)
427 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(long)
428 */
429 @Override
430 public synchronized ITmfContext seekEvent(final long rank) {
431
432 // A rank <= 0 indicates to seek the first event
433 if (rank <= 0) {
434 ITmfContext context = seekEvent((ITmfLocation<?>) null);
435 context.setRank(0);
436 return context;
437 }
438
439 // Position the trace at the checkpoint
440 final ITmfContext context = fIndexer.seekIndex(rank);
441
442 // And locate the requested event context
443 long pos = context.getRank();
444 if (pos < rank) {
445 ITmfEvent event = getNext(context);
446 while (event != null && ++pos < rank) {
447 event = getNext(context);
448 }
449 }
450 return context;
451 }
452
453 /* (non-Javadoc)
454 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
455 */
456 @Override
457 public synchronized ITmfContext seekEvent(final ITmfTimestamp timestamp) {
458
459 // A null timestamp indicates to seek the first event
460 if (timestamp == null) {
461 ITmfContext context = seekEvent((ITmfLocation<?>) null);
462 context.setRank(0);
463 return context;
464 }
465
466 // Position the trace at the checkpoint
467 final ITmfContext context = fIndexer.seekIndex(timestamp);
468
469 // And locate the requested event context
470 final ITmfContext nextEventContext = context.clone(); // Must use clone() to get the right subtype...
471 ITmfEvent event = getNext(nextEventContext);
472 while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) {
473 context.setLocation(nextEventContext.getLocation().clone());
474 context.increaseRank();
475 event = getNext(nextEventContext);
476 }
477 if (event == null) {
478 context.setLocation(null);
479 context.setRank(ITmfContext.UNKNOWN_RANK);
480 }
481 return context;
482 }
483
484 // ------------------------------------------------------------------------
485 // ITmfTrace - Read operations (returning an actual event)
486 // ------------------------------------------------------------------------
487
488 /* (non-Javadoc)
489 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#readNextEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
490 */
491 @Override
492 public synchronized T getNext(final ITmfContext context) {
493 // parseEvent() does not update the context
494 final T event = fParser.parseEvent(context);
495 if (event != null) {
496 updateAttributes(context, event.getTimestamp());
497 context.setLocation(getCurrentLocation());
498 context.increaseRank();
499 processEvent(event);
500 }
501 return event;
502 }
503
504 /**
505 * Hook for special event processing by the concrete class
506 * (called by TmfTrace.getEvent())
507 *
508 * @param event the event
509 */
510 protected void processEvent(final ITmfEvent event) {
511 // Do nothing
512 }
513
514 /**
515 * Update the trace attributes
516 *
517 * @param context the current trace context
518 * @param timestamp the corresponding timestamp
519 */
520 protected synchronized void updateAttributes(final ITmfContext context, final ITmfTimestamp timestamp) {
521 if (fStartTime.equals(TmfTimestamp.BIG_BANG) || fStartTime.compareTo(timestamp, false) > 0) {
522 fStartTime = timestamp.clone();
523 }
524 if (fEndTime.equals(TmfTimestamp.BIG_CRUNCH) || fEndTime.compareTo(timestamp, false) < 0) {
525 fEndTime = timestamp.clone();
526 }
527 if (context.hasValidRank()) {
528 long rank = context.getRank();
529 if (fNbEvents <= rank) {
530 fNbEvents = rank + 1;
531 }
532 fIndexer.updateIndex(context, timestamp);
533 }
534 }
535
536 // ------------------------------------------------------------------------
537 // TmfDataProvider
538 // ------------------------------------------------------------------------
539
540 /* (non-Javadoc)
541 * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#armRequest(org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest)
542 */
543 @Override
544 protected ITmfContext armRequest(final ITmfDataRequest<T> request) {
545 if (request instanceof ITmfEventRequest<?>
546 && !TmfTimestamp.BIG_BANG.equals(((ITmfEventRequest<T>) request).getRange().getStartTime())
547 && request.getIndex() == 0)
548 {
549 final ITmfContext context = seekEvent(((ITmfEventRequest<T>) request).getRange().getStartTime());
550 ((ITmfEventRequest<T>) request).setStartIndex((int) context.getRank());
551 return context;
552
553 }
554 return seekEvent(request.getIndex());
555 }
556
557 // ------------------------------------------------------------------------
558 // toString
559 // ------------------------------------------------------------------------
560
561 /* (non-Javadoc)
562 * @see java.lang.Object#toString()
563 */
564 @Override
565 @SuppressWarnings("nls")
566 public synchronized String toString() {
567 return "TmfTrace [fPath=" + fPath + ", fCacheSize=" + fCacheSize
568 + ", fNbEvents=" + fNbEvents + ", fStartTime=" + fStartTime
569 + ", fEndTime=" + fEndTime + ", fStreamingInterval=" + fStreamingInterval + "]";
570 }
571
572}
This page took 0.02631 seconds and 5 git commands to generate.