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