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