tmf: Clean up tmf.core.trace package
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / TmfTrace.java
CommitLineData
8c8bf09f 1/*******************************************************************************
61759503 2 * Copyright (c) 2009, 2013 Ericsson
0bfb7d06 3 *
8c8bf09f
ASL
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
0bfb7d06 8 *
8c8bf09f 9 * Contributors:
20658947
FC
10 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Updated as per TMF Trace Model 1.0
ea271da6 12 * Patrick Tasse - Updated for removal of context clone
8c8bf09f
ASL
13 *******************************************************************************/
14
6c13869b 15package org.eclipse.linuxtools.tmf.core.trace;
8c8bf09f 16
6f4a1d2b 17import java.io.File;
35c160d9
AM
18import java.util.Collections;
19import java.util.LinkedHashMap;
a51b2b9f 20import java.util.Map;
8c8bf09f 21
828e5592 22import org.eclipse.core.resources.IResource;
faa38350 23import org.eclipse.core.runtime.CoreException;
9b749023 24import org.eclipse.core.runtime.IPath;
42459d24 25import org.eclipse.core.runtime.IStatus;
b22a582a 26import org.eclipse.core.runtime.MultiStatus;
42459d24 27import org.eclipse.core.runtime.Status;
b22a582a 28import org.eclipse.linuxtools.internal.tmf.core.Activator;
6c13869b 29import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider;
72f1e62a 30import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
b4f71e4a 31import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
5419a136
AM
32import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
33import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
faa38350 34import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
fec1ac0b 35import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
faa38350
PT
36import org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal;
37import org.eclipse.linuxtools.tmf.core.signal.TmfTraceRangeUpdatedSignal;
7898bb21 38import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
200789b3 39import org.eclipse.linuxtools.tmf.core.statistics.ITmfStatistics;
1c0de632 40import org.eclipse.linuxtools.tmf.core.statistics.TmfStateStatistics;
3bd46eef
AM
41import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
42import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
43import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
a3db8436
AM
44import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfTraceIndexer;
45import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer;
46import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
8c8bf09f
ASL
47
48/**
09e86496
FC
49 * Abstract implementation of ITmfTrace.
50 * <p>
13cb5f43
FC
51 * Since the concept of 'location' is trace specific, the concrete classes have
52 * to provide the related methods, namely:
53 * <ul>
54 * <li> public ITmfLocation<?> getCurrentLocation()
55 * <li> public double getLocationRatio(ITmfLocation<?> location)
56 * <li> public ITmfContext seekEvent(ITmfLocation<?> location)
57 * <li> public ITmfContext seekEvent(double ratio)
da1a4b39 58 * <li> public IStatus validate(IProject project, String path)
13cb5f43
FC
59 * </ul>
60 * A concrete trace must provide its corresponding parser. A common way to
61 * accomplish this is by making the concrete class extend TmfTrace and
62 * implement ITmfEventParser.
63 * <p>
64 * The concrete class can either specify its own indexer or use the provided
65 * TmfCheckpointIndexer (default). In this case, the trace cache size will be
66 * used as checkpoint interval.
0bfb7d06 67 *
f7703ed6
FC
68 * @version 1.0
69 * @author Francois Chouinard
70 *
f7703ed6
FC
71 * @see ITmfEvent
72 * @see ITmfTraceIndexer
73 * @see ITmfEventParser
8c8bf09f 74 */
6256d8ad 75public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace {
62d1696a 76
e31e01e8 77 // ------------------------------------------------------------------------
8c8bf09f 78 // Attributes
e31e01e8 79 // ------------------------------------------------------------------------
8c8bf09f 80
09e86496
FC
81 // The resource used for persistent properties for this trace
82 private IResource fResource;
83
b0a282fb 84 // The trace path
12c155f5 85 private String fPath;
b0a282fb 86
0316808c
FC
87 // The trace cache page size
88 private int fCacheSize = ITmfTrace.DEFAULT_TRACE_CACHE_SIZE;
62d1696a 89
0316808c
FC
90 // The number of events collected (so far)
91 private long fNbEvents = 0;
62d1696a
FC
92
93 // The time span of the event stream
9cbe7899 94 private ITmfTimestamp fStartTime = TmfTimestamp.BIG_BANG;
a4115405 95 private ITmfTimestamp fEndTime = TmfTimestamp.BIG_BANG;
62d1696a 96
0316808c
FC
97 // The trace streaming interval (0 = no streaming)
98 private long fStreamingInterval = 0;
085d898f 99
0316808c 100 // The trace indexer
6256d8ad 101 private ITmfTraceIndexer fIndexer;
20658947 102
0316808c 103 // The trace parser
6256d8ad 104 private ITmfEventParser fParser;
7e6347b0 105
200789b3
AM
106 // The trace's statistics
107 private ITmfStatistics fStatistics;
108
a51b2b9f
AM
109 /**
110 * The collection of state systems that are registered with this trace. Each
111 * sub-class can decide to add its (one or many) state system to this map
112 * during their {@link #buildStateSystem()}.
113 *
114 * @since 2.0
115 */
116 protected final Map<String, ITmfStateSystem> fStateSystems =
35c160d9 117 new LinkedHashMap<String, ITmfStateSystem>();
a51b2b9f 118
e31e01e8 119 // ------------------------------------------------------------------------
3791b5df 120 // Construction
e31e01e8 121 // ------------------------------------------------------------------------
8c8bf09f 122
62d1696a 123 /**
3791b5df 124 * The default, parameterless, constructor
62d1696a 125 */
3791b5df
FC
126 public TmfTrace() {
127 super();
05bd3318
FC
128 }
129
130 /**
8cf330ae 131 * Full constructor.
0bfb7d06 132 *
8cf330ae
AM
133 * @param resource
134 * The resource associated to the trace
135 * @param type
136 * The type of events that will be read from this trace
137 * @param path
138 * The path to the trace on the filesystem
139 * @param cacheSize
140 * The trace cache size. Pass '-1' to use the default specified
141 * in {@link ITmfTrace#DEFAULT_TRACE_CACHE_SIZE}
142 * @param interval
143 * The trace streaming interval. You can use '0' for post-mortem
144 * traces.
145 * @param indexer
146 * The trace indexer. You can pass 'null' to use a default
147 * checkpoint indexer.
148 * @param parser
149 * The trace event parser. Use 'null' if (and only if) the trace
150 * object itself is also the ITmfEventParser to be used.
151 * @throws TmfTraceException
152 * If something failed during the opening
20658947 153 */
8cf330ae
AM
154 protected TmfTrace(final IResource resource,
155 final Class<? extends ITmfEvent> type,
156 final String path,
157 final int cacheSize,
158 final long interval,
159 final ITmfTraceIndexer indexer,
160 final ITmfEventParser parser)
161 throws TmfTraceException {
00641a97 162 super();
0316808c 163 fCacheSize = (cacheSize > 0) ? cacheSize : ITmfTrace.DEFAULT_TRACE_CACHE_SIZE;
3791b5df 164 fStreamingInterval = interval;
7e6347b0 165 fIndexer = (indexer != null) ? indexer : new TmfCheckpointIndexer(this, fCacheSize);
13cb5f43 166 fParser = parser;
09e86496 167 initialize(resource, path, type);
8c8bf09f
ASL
168 }
169
3791b5df
FC
170 /**
171 * Copy constructor
0bfb7d06 172 *
3791b5df 173 * @param trace the original trace
063f0d27 174 * @throws TmfTraceException Should not happen usually
3791b5df 175 */
6256d8ad 176 public TmfTrace(final TmfTrace trace) throws TmfTraceException {
3791b5df 177 super();
0316808c 178 if (trace == null) {
3791b5df 179 throw new IllegalArgumentException();
0316808c 180 }
20658947
FC
181 fCacheSize = trace.getCacheSize();
182 fStreamingInterval = trace.getStreamingInterval();
7e6347b0 183 fIndexer = new TmfCheckpointIndexer(this);
13cb5f43
FC
184 fParser = trace.fParser;
185 initialize(trace.getResource(), trace.getPath(), trace.getEventType());
3791b5df
FC
186 }
187
7e6347b0
FC
188 // ------------------------------------------------------------------------
189 // ITmfTrace - Initializers
190 // ------------------------------------------------------------------------
191
7e6347b0 192 @Override
6256d8ad 193 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
9dc3dee2 194 fIndexer = new TmfCheckpointIndexer(this, fCacheSize);
7e6347b0 195 initialize(resource, path, type);
7e6347b0
FC
196 }
197
09e86496 198 /**
1703b536 199 * Initialize the trace common attributes and the base component.
0bfb7d06
MK
200 *
201 * @param resource the Eclipse resource (trace)
1703b536
FC
202 * @param path the trace path
203 * @param type the trace event type
0bfb7d06 204 *
6f4e8ec0 205 * @throws TmfTraceException If something failed during the initialization
3791b5df 206 */
248af329
AM
207 protected void initialize(final IResource resource,
208 final String path,
209 final Class<? extends ITmfEvent> type)
210 throws TmfTraceException {
0316808c 211 if (path == null) {
b4f71e4a 212 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
0316808c 213 }
3791b5df 214 fPath = path;
1703b536 215 fResource = resource;
25e48683 216 String traceName = (resource != null) ? resource.getName() : null;
1703b536
FC
217 // If no resource was provided, extract the display name the trace path
218 if (traceName == null) {
9b749023 219 final int sep = path.lastIndexOf(IPath.SEPARATOR);
1703b536
FC
220 traceName = (sep >= 0) ? path.substring(sep + 1) : path;
221 }
2352aed9
FC
222 if (fParser == null) {
223 if (this instanceof ITmfEventParser) {
6256d8ad 224 fParser = (ITmfEventParser) this;
2352aed9
FC
225 } else {
226 throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$
227 }
228 }
3791b5df 229 super.init(traceName, type);
fec1ac0b
BH
230 // register as VIP after super.init() because TmfComponent registers to signal manager there
231 TmfSignalManager.registerVIP(this);
3791b5df
FC
232 }
233
2352aed9
FC
234 /**
235 * Indicates if the path points to an existing file/directory
0bfb7d06 236 *
2352aed9
FC
237 * @param path the path to test
238 * @return true if the file/directory exists
3791b5df 239 */
2352aed9 240 protected boolean fileExists(final String path) {
085d898f 241 final File file = new File(path);
3791b5df
FC
242 return file.exists();
243 }
244
c7e1020d 245 /**
51e75066 246 * @since 2.0
c7e1020d 247 */
51e75066
AM
248 @Override
249 public void indexTrace(boolean waitForCompletion) {
9e0640dc 250 getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, waitForCompletion);
c7e1020d
FC
251 }
252
200789b3 253 /**
6f4e8ec0 254 * The default implementation of TmfTrace uses a TmfStatistics back-end.
200789b3
AM
255 * Override this if you want to specify another type (or none at all).
256 *
b22a582a
AM
257 * @return An IStatus indicating if the statistics could be built
258 * successfully or not.
259 * @since 3.0
200789b3 260 */
b22a582a 261 protected IStatus buildStatistics() {
200789b3
AM
262 /*
263 * Initialize the statistics provider, but only if a Resource has been
264 * set (so we don't build it for experiments, for unit tests, etc.)
265 */
b22a582a
AM
266 try {
267 fStatistics = (fResource == null ? null : new TmfStateStatistics(this) );
268 } catch (TmfTraceException e) {
269 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e);
270 }
271 return Status.OK_STATUS;
200789b3
AM
272 }
273
faa38350
PT
274 /**
275 * Build the state system(s) associated with this trace type.
276 *
277 * Suppressing the warning, because the 'throws' will usually happen in
278 * sub-classes.
279 *
42459d24
AM
280 * @return An IStatus indicating if the state system could be build
281 * successfully or not.
282 * @since 3.0
faa38350 283 */
42459d24 284 protected IStatus buildStateSystem() {
faa38350
PT
285 /*
286 * Nothing is done in the base implementation, please specify
a51b2b9f 287 * how/if to register a new state system in derived classes.
faa38350 288 */
42459d24 289 return Status.OK_STATUS;
faa38350
PT
290 }
291
b5ee6881
FC
292 /**
293 * Clears the trace
294 */
295 @Override
296 public synchronized void dispose() {
1a4205d9 297 /* Clean up the index if applicable */
77551cc2
FC
298 if (getIndexer() != null) {
299 getIndexer().dispose();
300 }
1a4205d9
AM
301
302 /* Clean up the statistics */
303 if (fStatistics != null) {
304 fStatistics.dispose();
305 }
a51b2b9f
AM
306
307 /* Clean up the state systems */
308 for (ITmfStateSystem ss : fStateSystems.values()) {
309 ss.dispose();
310 }
311
b5ee6881
FC
312 super.dispose();
313 }
314
3791b5df 315 // ------------------------------------------------------------------------
09e86496 316 // ITmfTrace - Basic getters
e31e01e8 317 // ------------------------------------------------------------------------
8c8bf09f 318
25e48683 319 @Override
6256d8ad
AM
320 public Class<ITmfEvent> getEventType() {
321 return (Class<ITmfEvent>) super.getType();
25e48683
FC
322 }
323
d4011df2 324 @Override
09e86496
FC
325 public IResource getResource() {
326 return fResource;
8c8bf09f
ASL
327 }
328
d4011df2 329 @Override
09e86496
FC
330 public String getPath() {
331 return fPath;
8c8bf09f
ASL
332 }
333
20658947
FC
334 @Override
335 public int getCacheSize() {
336 return fCacheSize;
337 }
338
20658947
FC
339 @Override
340 public long getStreamingInterval() {
341 return fStreamingInterval;
342 }
343
0316808c
FC
344 /**
345 * @return the trace indexer
a3db8436 346 * @since 3.0
0316808c 347 */
6256d8ad 348 protected ITmfTraceIndexer getIndexer() {
0316808c
FC
349 return fIndexer;
350 }
351
352 /**
353 * @return the trace parser
354 */
6256d8ad 355 protected ITmfEventParser getParser() {
0316808c
FC
356 return fParser;
357 }
358
200789b3
AM
359 /**
360 * @since 2.0
361 */
362 @Override
363 public ITmfStatistics getStatistics() {
364 return fStatistics;
365 }
366
7898bb21
AM
367 /**
368 * @since 2.0
369 */
370 @Override
35c160d9
AM
371 public final Map<String, ITmfStateSystem> getStateSystems() {
372 return Collections.unmodifiableMap(fStateSystems);
7898bb21
AM
373 }
374
6c5e0863
AM
375 /**
376 * @since 2.0
377 */
378 @Override
379 public final void registerStateSystem(String id, ITmfStateSystem ss) {
380 fStateSystems.put(id, ss);
381 }
382
09e86496
FC
383 // ------------------------------------------------------------------------
384 // ITmfTrace - Trace characteristics getters
385 // ------------------------------------------------------------------------
386
d4011df2 387 @Override
5419a136 388 public synchronized long getNbEvents() {
3791b5df 389 return fNbEvents;
b0a282fb
FC
390 }
391
3bd46eef
AM
392 /**
393 * @since 2.0
62d1696a 394 */
d4011df2 395 @Override
12c155f5 396 public TmfTimeRange getTimeRange() {
cb866e08 397 return new TmfTimeRange(fStartTime, fEndTime);
8c8bf09f
ASL
398 }
399
3bd46eef
AM
400 /**
401 * @since 2.0
e31e01e8 402 */
d4011df2 403 @Override
4df4581d 404 public ITmfTimestamp getStartTime() {
4593bd5b 405 return fStartTime;
146a887c
FC
406 }
407
3bd46eef
AM
408 /**
409 * @since 2.0
e31e01e8 410 */
d4011df2 411 @Override
4df4581d 412 public ITmfTimestamp getEndTime() {
4593bd5b 413 return fEndTime;
20658947
FC
414 }
415
d7ee91bb 416 /**
d7ee91bb
PT
417 * @since 2.0
418 */
66262ad8
BH
419 @Override
420 public ITmfTimestamp getInitialRangeOffset() {
d7ee91bb
PT
421 final long DEFAULT_INITIAL_OFFSET_VALUE = (1L * 100 * 1000 * 1000); // .1sec
422 return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE, ITmfTimestamp.NANOSECOND_SCALE);
423 }
424
bb52f9bc
GB
425 /**
426 * @since 3.0
427 */
428 @Override
429 public String getHostId() {
430 return this.getName();
431 }
432
20658947 433 // ------------------------------------------------------------------------
d7ee91bb 434 // Convenience setters
20658947
FC
435 // ------------------------------------------------------------------------
436
0316808c
FC
437 /**
438 * Set the trace cache size. Must be done at initialization time.
0bfb7d06 439 *
0316808c
FC
440 * @param cacheSize The trace cache size
441 */
442 protected void setCacheSize(final int cacheSize) {
443 fCacheSize = cacheSize;
444 }
445
446 /**
447 * Set the trace known number of events. This can be quite dynamic
448 * during indexing or for live traces.
0bfb7d06 449 *
0316808c
FC
450 * @param nbEvents The number of events
451 */
452 protected synchronized void setNbEvents(final long nbEvents) {
453 fNbEvents = (nbEvents > 0) ? nbEvents : 0;
454 }
455
20658947
FC
456 /**
457 * Update the trace events time range
0bfb7d06 458 *
20658947 459 * @param range the new time range
3bd46eef 460 * @since 2.0
20658947
FC
461 */
462 protected void setTimeRange(final TmfTimeRange range) {
4593bd5b
AM
463 fStartTime = range.getStartTime();
464 fEndTime = range.getEndTime();
20658947
FC
465 }
466
467 /**
468 * Update the trace chronologically first event timestamp
0bfb7d06 469 *
20658947 470 * @param startTime the new first event timestamp
3bd46eef 471 * @since 2.0
20658947
FC
472 */
473 protected void setStartTime(final ITmfTimestamp startTime) {
4593bd5b 474 fStartTime = startTime;
20658947
FC
475 }
476
477 /**
478 * Update the trace chronologically last event timestamp
0bfb7d06 479 *
20658947 480 * @param endTime the new last event timestamp
3bd46eef 481 * @since 2.0
20658947
FC
482 */
483 protected void setEndTime(final ITmfTimestamp endTime) {
4593bd5b 484 fEndTime = endTime;
20658947
FC
485 }
486
487 /**
0316808c 488 * Set the polling interval for live traces (default = 0 = no streaming).
0bfb7d06 489 *
20658947
FC
490 * @param interval the new trace streaming interval
491 */
492 protected void setStreamingInterval(final long interval) {
1703b536 493 fStreamingInterval = (interval > 0) ? interval : 0;
146a887c
FC
494 }
495
0316808c
FC
496 /**
497 * Set the trace indexer. Must be done at initialization time.
0bfb7d06 498 *
0316808c 499 * @param indexer the trace indexer
a3db8436 500 * @since 3.0
0316808c 501 */
6256d8ad 502 protected void setIndexer(final ITmfTraceIndexer indexer) {
0316808c
FC
503 fIndexer = indexer;
504 }
505
506 /**
507 * Set the trace parser. Must be done at initialization time.
0bfb7d06 508 *
0316808c
FC
509 * @param parser the new trace parser
510 */
6256d8ad 511 protected void setParser(final ITmfEventParser parser) {
0316808c
FC
512 fParser = parser;
513 }
514
09e86496 515 // ------------------------------------------------------------------------
7e6347b0 516 // ITmfTrace - SeekEvent operations (returning a trace context)
09e86496
FC
517 // ------------------------------------------------------------------------
518
1b70b6dc 519 @Override
7e6347b0 520 public synchronized ITmfContext seekEvent(final long rank) {
09e86496 521
7e6347b0 522 // A rank <= 0 indicates to seek the first event
2352aed9 523 if (rank <= 0) {
1e1bef82 524 ITmfContext context = seekEvent((ITmfLocation) null);
2352aed9
FC
525 context.setRank(0);
526 return context;
527 }
09e86496 528
09e86496 529 // Position the trace at the checkpoint
7e6347b0 530 final ITmfContext context = fIndexer.seekIndex(rank);
09e86496
FC
531
532 // And locate the requested event context
7e6347b0
FC
533 long pos = context.getRank();
534 if (pos < rank) {
c32744d6 535 ITmfEvent event = getNext(context);
0bfb7d06 536 while ((event != null) && (++pos < rank)) {
c32744d6 537 event = getNext(context);
7e6347b0 538 }
09e86496
FC
539 }
540 return context;
1b70b6dc
PT
541 }
542
3bd46eef
AM
543 /**
544 * @since 2.0
09e86496
FC
545 */
546 @Override
7e6347b0 547 public synchronized ITmfContext seekEvent(final ITmfTimestamp timestamp) {
09e86496 548
7e6347b0 549 // A null timestamp indicates to seek the first event
2352aed9 550 if (timestamp == null) {
1e1bef82 551 ITmfContext context = seekEvent((ITmfLocation) null);
2352aed9
FC
552 context.setRank(0);
553 return context;
554 }
09e86496 555
1703b536 556 // Position the trace at the checkpoint
408e65d2 557 ITmfContext context = fIndexer.seekIndex(timestamp);
09e86496
FC
558
559 // And locate the requested event context
ea271da6
PT
560 ITmfLocation previousLocation = context.getLocation();
561 long previousRank = context.getRank();
562 ITmfEvent event = getNext(context);
7e6347b0 563 while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) {
ea271da6
PT
564 previousLocation = context.getLocation();
565 previousRank = context.getRank();
566 event = getNext(context);
09e86496 567 }
0316808c
FC
568 if (event == null) {
569 context.setLocation(null);
570 context.setRank(ITmfContext.UNKNOWN_RANK);
ea271da6
PT
571 } else {
572 context.dispose();
573 context = seekEvent(previousLocation);
574 context.setRank(previousRank);
0316808c 575 }
09e86496
FC
576 return context;
577 }
0283f7ff 578
09e86496
FC
579 // ------------------------------------------------------------------------
580 // ITmfTrace - Read operations (returning an actual event)
581 // ------------------------------------------------------------------------
582
d4011df2 583 @Override
6256d8ad 584 public synchronized ITmfEvent getNext(final ITmfContext context) {
09e86496 585 // parseEvent() does not update the context
6256d8ad 586 final ITmfEvent event = fParser.parseEvent(context);
09e86496 587 if (event != null) {
d337369a 588 updateAttributes(context, event.getTimestamp());
09e86496
FC
589 context.setLocation(getCurrentLocation());
590 context.increaseRank();
591 processEvent(event);
592 }
593 return event;
594 }
595
596 /**
d337369a 597 * Hook for special event processing by the concrete class
7e6347b0 598 * (called by TmfTrace.getEvent())
0bfb7d06 599 *
d337369a 600 * @param event the event
09e86496
FC
601 */
602 protected void processEvent(final ITmfEvent event) {
d337369a 603 // Do nothing
09e86496
FC
604 }
605
d337369a
FC
606 /**
607 * Update the trace attributes
0bfb7d06 608 *
d337369a 609 * @param context the current trace context
2848c377 610 * @param timestamp the corresponding timestamp
3bd46eef 611 * @since 2.0
d337369a
FC
612 */
613 protected synchronized void updateAttributes(final ITmfContext context, final ITmfTimestamp timestamp) {
0bfb7d06 614 if (fStartTime.equals(TmfTimestamp.BIG_BANG) || (fStartTime.compareTo(timestamp, false) > 0)) {
4593bd5b 615 fStartTime = timestamp;
09e86496 616 }
0bfb7d06 617 if (fEndTime.equals(TmfTimestamp.BIG_CRUNCH) || (fEndTime.compareTo(timestamp, false) < 0)) {
4593bd5b 618 fEndTime = timestamp;
09e86496
FC
619 }
620 if (context.hasValidRank()) {
d337369a 621 long rank = context.getRank();
09e86496
FC
622 if (fNbEvents <= rank) {
623 fNbEvents = rank + 1;
624 }
200789b3
AM
625 if (fIndexer != null) {
626 fIndexer.updateIndex(context, timestamp);
627 }
09e86496 628 }
abfad0aa
FC
629 }
630
3791b5df 631 // ------------------------------------------------------------------------
d337369a 632 // TmfDataProvider
3791b5df
FC
633 // ------------------------------------------------------------------------
634
77c4a6df
AM
635 /**
636 * @since 2.0
d337369a 637 */
3791b5df 638 @Override
5419a136 639 public synchronized ITmfContext armRequest(final ITmfDataRequest request) {
faa38350
PT
640 if (executorIsShutdown()) {
641 return null;
642 }
5419a136
AM
643 if ((request instanceof ITmfEventRequest)
644 && !TmfTimestamp.BIG_BANG.equals(((ITmfEventRequest) request).getRange().getStartTime())
645 && (request.getIndex() == 0))
646 {
647 final ITmfContext context = seekEvent(((ITmfEventRequest) request).getRange().getStartTime());
648 ((ITmfEventRequest) request).setStartIndex((int) context.getRank());
8584dc20 649 return context;
8584dc20 650
5419a136
AM
651 }
652 return seekEvent(request.getIndex());
3791b5df
FC
653 }
654
faa38350
PT
655 // ------------------------------------------------------------------------
656 // Signal handlers
657 // ------------------------------------------------------------------------
658
659 /**
660 * Handler for the Trace Opened signal
661 *
662 * @param signal
663 * The incoming signal
664 * @since 2.0
665 */
666 @TmfSignalHandler
667 public void traceOpened(TmfTraceOpenedSignal signal) {
b9a5bf8f
AM
668 boolean signalIsForUs = false;
669 for (ITmfTrace trace : TmfTraceManager.getTraceSet(signal.getTrace())) {
670 if (trace == this) {
671 signalIsForUs = true;
fe0c44c4 672 break;
faa38350
PT
673 }
674 }
faa38350 675
b9a5bf8f 676 if (!signalIsForUs) {
fe0c44c4
AM
677 return;
678 }
679
680 /*
b9a5bf8f 681 * The signal is either for this trace, or for an experiment containing
fe0c44c4
AM
682 * this trace.
683 */
b22a582a
AM
684 MultiStatus status = new MultiStatus(Activator.PLUGIN_ID, IStatus.OK, null, null);
685 status.add(buildStatistics());
686 status.add(buildStateSystem());
687 if (!status.isOK()) {
688 Activator.log(status);
fe0c44c4
AM
689 }
690
691 /* Refresh the project, so it can pick up new files that got created. */
692 try {
693 if (fResource != null) {
694 fResource.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
faa38350 695 }
fe0c44c4
AM
696 } catch (CoreException e) {
697 e.printStackTrace();
faa38350 698 }
fe0c44c4 699
faa38350 700 if (signal.getTrace() == this) {
f8fc4a3a 701 /* Additionally, the signal is directly for this trace. */
faa38350
PT
702 if (getNbEvents() == 0) {
703 return;
704 }
705
f8fc4a3a
PT
706 /* For a streaming trace, the range updated signal should be sent
707 * by the subclass when a new safe time is determined.
708 */
709 if (getStreamingInterval() > 0) {
710 return;
711 }
712
faa38350
PT
713 final TmfTimeRange timeRange = new TmfTimeRange(getStartTime(), TmfTimestamp.BIG_CRUNCH);
714 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal = new TmfTraceRangeUpdatedSignal(this, this, timeRange);
715
716 // Broadcast in separate thread to prevent deadlock
717 new Thread() {
718 @Override
719 public void run() {
720 broadcast(rangeUpdatedsignal);
721 }
722 }.start();
723 return;
724 }
725 }
726
727 /**
728 * Signal handler for the TmfTraceRangeUpdatedSignal signal
729 *
730 * @param signal The incoming signal
731 * @since 2.0
732 */
733 @TmfSignalHandler
734 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal) {
735 if (signal.getTrace() == this) {
736 getIndexer().buildIndex(getNbEvents(), signal.getRange(), false);
737 }
738 }
739
3791b5df 740 // ------------------------------------------------------------------------
09e86496 741 // toString
3791b5df
FC
742 // ------------------------------------------------------------------------
743
12c155f5 744 @Override
09e86496 745 @SuppressWarnings("nls")
5419a136 746 public synchronized String toString() {
20658947
FC
747 return "TmfTrace [fPath=" + fPath + ", fCacheSize=" + fCacheSize
748 + ", fNbEvents=" + fNbEvents + ", fStartTime=" + fStartTime
749 + ", fEndTime=" + fEndTime + ", fStreamingInterval=" + fStreamingInterval + "]";
12c155f5
FC
750 }
751
8c8bf09f 752}
This page took 0.168179 seconds and 5 git commands to generate.