tmf: Fix Select Traces command does not refresh trace type
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / TmfTrace.java
CommitLineData
8c8bf09f 1/*******************************************************************************
089a4872 2 * Copyright (c) 2009, 2014 Ericsson, École Polytechnique de Montréal
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
e73a4ba5
GB
13 * Geneviève Bastien - Added timestamp transforms, its saving to file and
14 * timestamp creation functions
8c8bf09f
ASL
15 *******************************************************************************/
16
6c13869b 17package org.eclipse.linuxtools.tmf.core.trace;
8c8bf09f 18
6f4a1d2b 19import java.io.File;
e73a4ba5 20import java.io.FileInputStream;
e73a4ba5
GB
21import java.io.FileOutputStream;
22import java.io.IOException;
23import java.io.ObjectInputStream;
24import java.io.ObjectOutputStream;
35c160d9 25import java.util.Collections;
ff3f02c8 26import java.util.HashSet;
35c160d9 27import java.util.LinkedHashMap;
a51b2b9f 28import java.util.Map;
c068a752 29import java.util.Map.Entry;
ff3f02c8 30import java.util.Set;
8c8bf09f 31
e73a4ba5 32import org.eclipse.core.resources.IFolder;
9de979b2 33import org.eclipse.core.resources.IProject;
828e5592 34import org.eclipse.core.resources.IResource;
faa38350 35import org.eclipse.core.runtime.CoreException;
42459d24 36import org.eclipse.core.runtime.IStatus;
b22a582a 37import org.eclipse.core.runtime.MultiStatus;
032ecd45 38import org.eclipse.core.runtime.Path;
42459d24 39import org.eclipse.core.runtime.Status;
b22a582a 40import org.eclipse.linuxtools.internal.tmf.core.Activator;
e73a4ba5 41import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
c068a752
GB
42import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModule;
43import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModuleHelper;
44import org.eclipse.linuxtools.tmf.core.analysis.TmfAnalysisManager;
6c13869b 45import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider;
72f1e62a 46import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
c068a752 47import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException;
b4f71e4a 48import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
5419a136 49import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
faa38350 50import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
fec1ac0b 51import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
faa38350
PT
52import org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal;
53import org.eclipse.linuxtools.tmf.core.signal.TmfTraceRangeUpdatedSignal;
032ecd45 54import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
e73a4ba5
GB
55import org.eclipse.linuxtools.tmf.core.synchronization.ITmfTimestampTransform;
56import org.eclipse.linuxtools.tmf.core.synchronization.TmfTimestampTransform;
3bd46eef
AM
57import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
58import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
59import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
a3db8436
AM
60import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfTraceIndexer;
61import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.TmfCheckpointIndexer;
62import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
8c8bf09f
ASL
63
64/**
09e86496
FC
65 * Abstract implementation of ITmfTrace.
66 * <p>
13cb5f43
FC
67 * Since the concept of 'location' is trace specific, the concrete classes have
68 * to provide the related methods, namely:
69 * <ul>
70 * <li> public ITmfLocation<?> getCurrentLocation()
71 * <li> public double getLocationRatio(ITmfLocation<?> location)
72 * <li> public ITmfContext seekEvent(ITmfLocation<?> location)
73 * <li> public ITmfContext seekEvent(double ratio)
da1a4b39 74 * <li> public IStatus validate(IProject project, String path)
13cb5f43
FC
75 * </ul>
76 * A concrete trace must provide its corresponding parser. A common way to
77 * accomplish this is by making the concrete class extend TmfTrace and
78 * implement ITmfEventParser.
79 * <p>
80 * The concrete class can either specify its own indexer or use the provided
81 * TmfCheckpointIndexer (default). In this case, the trace cache size will be
82 * used as checkpoint interval.
0bfb7d06 83 *
f7703ed6
FC
84 * @version 1.0
85 * @author Francois Chouinard
86 *
f7703ed6
FC
87 * @see ITmfEvent
88 * @see ITmfTraceIndexer
89 * @see ITmfEventParser
8c8bf09f 90 */
6256d8ad 91public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace {
62d1696a 92
e31e01e8 93 // ------------------------------------------------------------------------
8c8bf09f 94 // Attributes
e31e01e8 95 // ------------------------------------------------------------------------
8c8bf09f 96
09e86496
FC
97 // The resource used for persistent properties for this trace
98 private IResource fResource;
99
b0a282fb 100 // The trace path
12c155f5 101 private String fPath;
b0a282fb 102
0316808c
FC
103 // The trace cache page size
104 private int fCacheSize = ITmfTrace.DEFAULT_TRACE_CACHE_SIZE;
62d1696a 105
0316808c 106 // The number of events collected (so far)
e9a6e38e 107 private volatile long fNbEvents = 0;
62d1696a
FC
108
109 // The time span of the event stream
9cbe7899 110 private ITmfTimestamp fStartTime = TmfTimestamp.BIG_BANG;
a4115405 111 private ITmfTimestamp fEndTime = TmfTimestamp.BIG_BANG;
62d1696a 112
0316808c
FC
113 // The trace streaming interval (0 = no streaming)
114 private long fStreamingInterval = 0;
085d898f 115
0316808c 116 // The trace indexer
6256d8ad 117 private ITmfTraceIndexer fIndexer;
20658947 118
0316808c 119 // The trace parser
6256d8ad 120 private ITmfEventParser fParser;
7e6347b0 121
e73a4ba5
GB
122 private ITmfTimestampTransform fTsTransform;
123
ff3f02c8
AM
124 private final Map<String, IAnalysisModule> fAnalysisModules =
125 Collections.synchronizedMap(new LinkedHashMap<String, IAnalysisModule>());
c068a752 126
e73a4ba5
GB
127 private static final String SYNCHRONIZATION_FORMULA_FILE = "sync_formula"; //$NON-NLS-1$
128
e31e01e8 129 // ------------------------------------------------------------------------
3791b5df 130 // Construction
e31e01e8 131 // ------------------------------------------------------------------------
8c8bf09f 132
62d1696a 133 /**
3791b5df 134 * The default, parameterless, constructor
62d1696a 135 */
3791b5df
FC
136 public TmfTrace() {
137 super();
ab186fbb 138 fIndexer = createIndexer(DEFAULT_BLOCK_SIZE);
05bd3318
FC
139 }
140
141 /**
8cf330ae 142 * Full constructor.
0bfb7d06 143 *
8cf330ae
AM
144 * @param resource
145 * The resource associated to the trace
146 * @param type
147 * The type of events that will be read from this trace
148 * @param path
149 * The path to the trace on the filesystem
150 * @param cacheSize
151 * The trace cache size. Pass '-1' to use the default specified
152 * in {@link ITmfTrace#DEFAULT_TRACE_CACHE_SIZE}
153 * @param interval
154 * The trace streaming interval. You can use '0' for post-mortem
155 * traces.
8cf330ae
AM
156 * @param parser
157 * The trace event parser. Use 'null' if (and only if) the trace
158 * object itself is also the ITmfEventParser to be used.
159 * @throws TmfTraceException
160 * If something failed during the opening
20658947 161 */
8cf330ae
AM
162 protected TmfTrace(final IResource resource,
163 final Class<? extends ITmfEvent> type,
164 final String path,
165 final int cacheSize,
166 final long interval,
8cf330ae
AM
167 final ITmfEventParser parser)
168 throws TmfTraceException {
00641a97 169 super();
0316808c 170 fCacheSize = (cacheSize > 0) ? cacheSize : ITmfTrace.DEFAULT_TRACE_CACHE_SIZE;
3791b5df 171 fStreamingInterval = interval;
13cb5f43 172 fParser = parser;
09e86496 173 initialize(resource, path, type);
8c8bf09f
ASL
174 }
175
3791b5df
FC
176 /**
177 * Copy constructor
0bfb7d06 178 *
3791b5df 179 * @param trace the original trace
063f0d27 180 * @throws TmfTraceException Should not happen usually
3791b5df 181 */
6256d8ad 182 public TmfTrace(final TmfTrace trace) throws TmfTraceException {
3791b5df 183 super();
0316808c 184 if (trace == null) {
3791b5df 185 throw new IllegalArgumentException();
0316808c 186 }
20658947
FC
187 fCacheSize = trace.getCacheSize();
188 fStreamingInterval = trace.getStreamingInterval();
13cb5f43
FC
189 fParser = trace.fParser;
190 initialize(trace.getResource(), trace.getPath(), trace.getEventType());
3791b5df
FC
191 }
192
032ecd45
MAL
193 /**
194 * Creates the indexer instance. Classes extending this class can override
195 * this to provide a different indexer implementation.
196 *
197 * @param interval the checkpoints interval
198 *
199 * @return the indexer
200 * @since 3.0
201 */
202 protected ITmfTraceIndexer createIndexer(int interval) {
203 return new TmfCheckpointIndexer(this, interval);
204 }
205
7e6347b0
FC
206 // ------------------------------------------------------------------------
207 // ITmfTrace - Initializers
208 // ------------------------------------------------------------------------
209
7e6347b0 210 @Override
6256d8ad 211 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
7e6347b0 212 initialize(resource, path, type);
7e6347b0
FC
213 }
214
09e86496 215 /**
1703b536 216 * Initialize the trace common attributes and the base component.
0bfb7d06
MK
217 *
218 * @param resource the Eclipse resource (trace)
1703b536
FC
219 * @param path the trace path
220 * @param type the trace event type
0bfb7d06 221 *
6f4e8ec0 222 * @throws TmfTraceException If something failed during the initialization
3791b5df 223 */
248af329
AM
224 protected void initialize(final IResource resource,
225 final String path,
226 final Class<? extends ITmfEvent> type)
227 throws TmfTraceException {
0316808c 228 if (path == null) {
b4f71e4a 229 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
0316808c 230 }
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) {
032ecd45 236 traceName = new Path(path).lastSegment();
1703b536 237 }
2352aed9
FC
238 if (fParser == null) {
239 if (this instanceof ITmfEventParser) {
6256d8ad 240 fParser = (ITmfEventParser) this;
2352aed9
FC
241 } else {
242 throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$
243 }
244 }
3791b5df 245 super.init(traceName, type);
fec1ac0b
BH
246 // register as VIP after super.init() because TmfComponent registers to signal manager there
247 TmfSignalManager.registerVIP(this);
ab186fbb 248 fIndexer = createIndexer(fCacheSize);
3791b5df
FC
249 }
250
2352aed9
FC
251 /**
252 * Indicates if the path points to an existing file/directory
0bfb7d06 253 *
2352aed9
FC
254 * @param path the path to test
255 * @return true if the file/directory exists
3791b5df 256 */
2352aed9 257 protected boolean fileExists(final String path) {
085d898f 258 final File file = new File(path);
3791b5df
FC
259 return file.exists();
260 }
261
c7e1020d 262 /**
51e75066 263 * @since 2.0
c7e1020d 264 */
51e75066
AM
265 @Override
266 public void indexTrace(boolean waitForCompletion) {
9e0640dc 267 getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, waitForCompletion);
c7e1020d
FC
268 }
269
c068a752
GB
270 /**
271 * Instantiate the applicable analysis modules and executes the analysis
272 * modules that are meant to be automatically executed
273 *
274 * @return An IStatus indicating whether the analysis could be run
275 * successfully or not
276 * @since 3.0
277 */
278 protected IStatus executeAnalysis() {
279 MultiStatus status = new MultiStatus(Activator.PLUGIN_ID, IStatus.OK, null, null);
280 Map<String, IAnalysisModuleHelper> modules = TmfAnalysisManager.getAnalysisModules(this.getClass());
281 for (IAnalysisModuleHelper helper : modules.values()) {
282 try {
283 IAnalysisModule module = helper.newModule(this);
284 fAnalysisModules.put(module.getId(), module);
285 if (module.isAutomatic()) {
286 status.add(module.schedule());
287 }
288 } catch (TmfAnalysisException e) {
289 status.add(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
290 }
291 }
292 return status;
293 }
294
c4767854
AM
295 /**
296 * @since 3.0
297 */
c068a752 298 @Override
ff3f02c8 299 public IAnalysisModule getAnalysisModule(String analysisId) {
c068a752
GB
300 return fAnalysisModules.get(analysisId);
301 }
302
ff3f02c8
AM
303
304 /**
305 * @since 3.0
306 */
307 @Override
308 public Iterable<IAnalysisModule> getAnalysisModules() {
309 synchronized (fAnalysisModules) {
310 Set<IAnalysisModule> modules = new HashSet<>(fAnalysisModules.values());
311 return modules;
312 }
313 }
314
c4767854
AM
315 /**
316 * @since 3.0
317 */
c068a752 318 @Override
ff3f02c8
AM
319 public <T extends IAnalysisModule> T getAnalysisModuleOfClass(Class<T> moduleClass, String id) {
320 Iterable<T> modules = getAnalysisModulesOfClass(moduleClass);
321 for (T module : modules) {
322 if (id.equals(module.getId())) {
323 return module;
c068a752
GB
324 }
325 }
ff3f02c8 326 return null;
c068a752
GB
327 }
328
c4767854
AM
329 /**
330 * @since 3.0
331 */
c068a752 332 @Override
ff3f02c8
AM
333 public <T> Iterable<T> getAnalysisModulesOfClass(Class<T> moduleClass) {
334 Set<T> modules = new HashSet<>();
335 synchronized (fAnalysisModules) {
336 for (Entry<String, IAnalysisModule> entry : fAnalysisModules.entrySet()) {
337 if (moduleClass.isAssignableFrom(entry.getValue().getClass())) {
338 modules.add(moduleClass.cast(entry.getValue()));
339 }
340 }
341 }
342 return modules;
c068a752
GB
343 }
344
b5ee6881
FC
345 /**
346 * Clears the trace
347 */
348 @Override
349 public synchronized void dispose() {
1a4205d9 350 /* Clean up the index if applicable */
77551cc2
FC
351 if (getIndexer() != null) {
352 getIndexer().dispose();
353 }
1a4205d9 354
a1529f38 355 /* Clean up the analysis modules */
ff3f02c8
AM
356 synchronized (fAnalysisModules) {
357 for (IAnalysisModule module : fAnalysisModules.values()) {
358 module.dispose();
359 }
6ef5ae35 360 fAnalysisModules.clear();
a1529f38
AM
361 }
362
b5ee6881
FC
363 super.dispose();
364 }
365
3791b5df 366 // ------------------------------------------------------------------------
09e86496 367 // ITmfTrace - Basic getters
e31e01e8 368 // ------------------------------------------------------------------------
8c8bf09f 369
25e48683 370 @Override
0f89d4ba
AM
371 public Class<? extends ITmfEvent> getEventType() {
372 return super.getType();
25e48683
FC
373 }
374
d4011df2 375 @Override
09e86496
FC
376 public IResource getResource() {
377 return fResource;
8c8bf09f
ASL
378 }
379
d4011df2 380 @Override
09e86496
FC
381 public String getPath() {
382 return fPath;
8c8bf09f
ASL
383 }
384
20658947
FC
385 @Override
386 public int getCacheSize() {
387 return fCacheSize;
388 }
389
20658947
FC
390 @Override
391 public long getStreamingInterval() {
392 return fStreamingInterval;
393 }
394
0316808c
FC
395 /**
396 * @return the trace indexer
a3db8436 397 * @since 3.0
0316808c 398 */
6256d8ad 399 protected ITmfTraceIndexer getIndexer() {
0316808c
FC
400 return fIndexer;
401 }
402
403 /**
404 * @return the trace parser
405 */
6256d8ad 406 protected ITmfEventParser getParser() {
0316808c
FC
407 return fParser;
408 }
409
09e86496
FC
410 // ------------------------------------------------------------------------
411 // ITmfTrace - Trace characteristics getters
412 // ------------------------------------------------------------------------
413
d4011df2 414 @Override
e9a6e38e 415 public long getNbEvents() {
3791b5df 416 return fNbEvents;
b0a282fb
FC
417 }
418
3bd46eef
AM
419 /**
420 * @since 2.0
62d1696a 421 */
d4011df2 422 @Override
12c155f5 423 public TmfTimeRange getTimeRange() {
cb866e08 424 return new TmfTimeRange(fStartTime, fEndTime);
8c8bf09f
ASL
425 }
426
3bd46eef
AM
427 /**
428 * @since 2.0
e31e01e8 429 */
d4011df2 430 @Override
4df4581d 431 public ITmfTimestamp getStartTime() {
4593bd5b 432 return fStartTime;
146a887c
FC
433 }
434
3bd46eef
AM
435 /**
436 * @since 2.0
e31e01e8 437 */
d4011df2 438 @Override
4df4581d 439 public ITmfTimestamp getEndTime() {
4593bd5b 440 return fEndTime;
20658947
FC
441 }
442
d7ee91bb 443 /**
d7ee91bb
PT
444 * @since 2.0
445 */
66262ad8
BH
446 @Override
447 public ITmfTimestamp getInitialRangeOffset() {
d7ee91bb
PT
448 final long DEFAULT_INITIAL_OFFSET_VALUE = (1L * 100 * 1000 * 1000); // .1sec
449 return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE, ITmfTimestamp.NANOSECOND_SCALE);
450 }
451
bb52f9bc
GB
452 /**
453 * @since 3.0
454 */
455 @Override
456 public String getHostId() {
457 return this.getName();
458 }
459
20658947 460 // ------------------------------------------------------------------------
d7ee91bb 461 // Convenience setters
20658947
FC
462 // ------------------------------------------------------------------------
463
0316808c
FC
464 /**
465 * Set the trace cache size. Must be done at initialization time.
0bfb7d06 466 *
0316808c
FC
467 * @param cacheSize The trace cache size
468 */
469 protected void setCacheSize(final int cacheSize) {
470 fCacheSize = cacheSize;
471 }
472
473 /**
474 * Set the trace known number of events. This can be quite dynamic
475 * during indexing or for live traces.
0bfb7d06 476 *
0316808c
FC
477 * @param nbEvents The number of events
478 */
479 protected synchronized void setNbEvents(final long nbEvents) {
480 fNbEvents = (nbEvents > 0) ? nbEvents : 0;
481 }
482
20658947
FC
483 /**
484 * Update the trace events time range
0bfb7d06 485 *
20658947 486 * @param range the new time range
3bd46eef 487 * @since 2.0
20658947
FC
488 */
489 protected void setTimeRange(final TmfTimeRange range) {
4593bd5b
AM
490 fStartTime = range.getStartTime();
491 fEndTime = range.getEndTime();
20658947
FC
492 }
493
494 /**
495 * Update the trace chronologically first event timestamp
0bfb7d06 496 *
20658947 497 * @param startTime the new first event timestamp
3bd46eef 498 * @since 2.0
20658947
FC
499 */
500 protected void setStartTime(final ITmfTimestamp startTime) {
4593bd5b 501 fStartTime = startTime;
20658947
FC
502 }
503
504 /**
505 * Update the trace chronologically last event timestamp
0bfb7d06 506 *
20658947 507 * @param endTime the new last event timestamp
3bd46eef 508 * @since 2.0
20658947
FC
509 */
510 protected void setEndTime(final ITmfTimestamp endTime) {
4593bd5b 511 fEndTime = endTime;
20658947
FC
512 }
513
514 /**
0316808c 515 * Set the polling interval for live traces (default = 0 = no streaming).
0bfb7d06 516 *
20658947
FC
517 * @param interval the new trace streaming interval
518 */
519 protected void setStreamingInterval(final long interval) {
1703b536 520 fStreamingInterval = (interval > 0) ? interval : 0;
146a887c
FC
521 }
522
0316808c
FC
523 /**
524 * Set the trace parser. Must be done at initialization time.
0bfb7d06 525 *
0316808c
FC
526 * @param parser the new trace parser
527 */
6256d8ad 528 protected void setParser(final ITmfEventParser parser) {
0316808c
FC
529 fParser = parser;
530 }
531
09e86496 532 // ------------------------------------------------------------------------
7e6347b0 533 // ITmfTrace - SeekEvent operations (returning a trace context)
09e86496
FC
534 // ------------------------------------------------------------------------
535
1b70b6dc 536 @Override
7e6347b0 537 public synchronized ITmfContext seekEvent(final long rank) {
09e86496 538
7e6347b0 539 // A rank <= 0 indicates to seek the first event
2352aed9 540 if (rank <= 0) {
1e1bef82 541 ITmfContext context = seekEvent((ITmfLocation) null);
2352aed9
FC
542 context.setRank(0);
543 return context;
544 }
09e86496 545
09e86496 546 // Position the trace at the checkpoint
7e6347b0 547 final ITmfContext context = fIndexer.seekIndex(rank);
09e86496
FC
548
549 // And locate the requested event context
7e6347b0
FC
550 long pos = context.getRank();
551 if (pos < rank) {
c32744d6 552 ITmfEvent event = getNext(context);
0bfb7d06 553 while ((event != null) && (++pos < rank)) {
c32744d6 554 event = getNext(context);
7e6347b0 555 }
09e86496
FC
556 }
557 return context;
1b70b6dc
PT
558 }
559
3bd46eef
AM
560 /**
561 * @since 2.0
09e86496
FC
562 */
563 @Override
7e6347b0 564 public synchronized ITmfContext seekEvent(final ITmfTimestamp timestamp) {
09e86496 565
7e6347b0 566 // A null timestamp indicates to seek the first event
2352aed9 567 if (timestamp == null) {
1e1bef82 568 ITmfContext context = seekEvent((ITmfLocation) null);
2352aed9
FC
569 context.setRank(0);
570 return context;
571 }
09e86496 572
1703b536 573 // Position the trace at the checkpoint
408e65d2 574 ITmfContext context = fIndexer.seekIndex(timestamp);
09e86496
FC
575
576 // And locate the requested event context
ea271da6
PT
577 ITmfLocation previousLocation = context.getLocation();
578 long previousRank = context.getRank();
579 ITmfEvent event = getNext(context);
7e6347b0 580 while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) {
ea271da6
PT
581 previousLocation = context.getLocation();
582 previousRank = context.getRank();
583 event = getNext(context);
09e86496 584 }
0316808c
FC
585 if (event == null) {
586 context.setLocation(null);
587 context.setRank(ITmfContext.UNKNOWN_RANK);
ea271da6
PT
588 } else {
589 context.dispose();
590 context = seekEvent(previousLocation);
591 context.setRank(previousRank);
0316808c 592 }
09e86496
FC
593 return context;
594 }
0283f7ff 595
09e86496
FC
596 // ------------------------------------------------------------------------
597 // ITmfTrace - Read operations (returning an actual event)
598 // ------------------------------------------------------------------------
599
d4011df2 600 @Override
6256d8ad 601 public synchronized ITmfEvent getNext(final ITmfContext context) {
09e86496 602 // parseEvent() does not update the context
6256d8ad 603 final ITmfEvent event = fParser.parseEvent(context);
09e86496 604 if (event != null) {
d337369a 605 updateAttributes(context, event.getTimestamp());
09e86496
FC
606 context.setLocation(getCurrentLocation());
607 context.increaseRank();
608 processEvent(event);
609 }
610 return event;
611 }
612
613 /**
d337369a 614 * Hook for special event processing by the concrete class
7e6347b0 615 * (called by TmfTrace.getEvent())
0bfb7d06 616 *
d337369a 617 * @param event the event
09e86496
FC
618 */
619 protected void processEvent(final ITmfEvent event) {
d337369a 620 // Do nothing
09e86496
FC
621 }
622
d337369a
FC
623 /**
624 * Update the trace attributes
0bfb7d06 625 *
d337369a 626 * @param context the current trace context
2848c377 627 * @param timestamp the corresponding timestamp
3bd46eef 628 * @since 2.0
d337369a
FC
629 */
630 protected synchronized void updateAttributes(final ITmfContext context, final ITmfTimestamp timestamp) {
0bfb7d06 631 if (fStartTime.equals(TmfTimestamp.BIG_BANG) || (fStartTime.compareTo(timestamp, false) > 0)) {
4593bd5b 632 fStartTime = timestamp;
09e86496 633 }
0bfb7d06 634 if (fEndTime.equals(TmfTimestamp.BIG_CRUNCH) || (fEndTime.compareTo(timestamp, false) < 0)) {
4593bd5b 635 fEndTime = timestamp;
09e86496
FC
636 }
637 if (context.hasValidRank()) {
d337369a 638 long rank = context.getRank();
09e86496
FC
639 if (fNbEvents <= rank) {
640 fNbEvents = rank + 1;
641 }
200789b3
AM
642 if (fIndexer != null) {
643 fIndexer.updateIndex(context, timestamp);
644 }
09e86496 645 }
abfad0aa
FC
646 }
647
3791b5df 648 // ------------------------------------------------------------------------
d337369a 649 // TmfDataProvider
3791b5df
FC
650 // ------------------------------------------------------------------------
651
77c4a6df
AM
652 /**
653 * @since 2.0
d337369a 654 */
3791b5df 655 @Override
fd3f1eff 656 public synchronized ITmfContext armRequest(final ITmfEventRequest request) {
faa38350
PT
657 if (executorIsShutdown()) {
658 return null;
659 }
fd3f1eff
AM
660 if (!TmfTimestamp.BIG_BANG.equals(request.getRange().getStartTime())
661 && (request.getIndex() == 0)) {
662 final ITmfContext context = seekEvent(request.getRange().getStartTime());
663 request.setStartIndex((int) context.getRank());
8584dc20 664 return context;
8584dc20 665
5419a136
AM
666 }
667 return seekEvent(request.getIndex());
3791b5df
FC
668 }
669
faa38350
PT
670 // ------------------------------------------------------------------------
671 // Signal handlers
672 // ------------------------------------------------------------------------
673
674 /**
675 * Handler for the Trace Opened signal
676 *
677 * @param signal
678 * The incoming signal
679 * @since 2.0
680 */
681 @TmfSignalHandler
682 public void traceOpened(TmfTraceOpenedSignal signal) {
b9a5bf8f
AM
683 boolean signalIsForUs = false;
684 for (ITmfTrace trace : TmfTraceManager.getTraceSet(signal.getTrace())) {
685 if (trace == this) {
686 signalIsForUs = true;
fe0c44c4 687 break;
faa38350
PT
688 }
689 }
faa38350 690
b9a5bf8f 691 if (!signalIsForUs) {
fe0c44c4
AM
692 return;
693 }
694
695 /*
b9a5bf8f 696 * The signal is either for this trace, or for an experiment containing
fe0c44c4
AM
697 * this trace.
698 */
be4a197a 699 IStatus status = executeAnalysis();
b22a582a
AM
700 if (!status.isOK()) {
701 Activator.log(status);
fe0c44c4
AM
702 }
703
9de979b2 704 refreshSupplementaryFiles();
fe0c44c4 705
faa38350 706 if (signal.getTrace() == this) {
f8fc4a3a 707 /* Additionally, the signal is directly for this trace. */
faa38350
PT
708 if (getNbEvents() == 0) {
709 return;
710 }
711
f8fc4a3a
PT
712 /* For a streaming trace, the range updated signal should be sent
713 * by the subclass when a new safe time is determined.
714 */
715 if (getStreamingInterval() > 0) {
716 return;
717 }
718
faa38350
PT
719 final TmfTimeRange timeRange = new TmfTimeRange(getStartTime(), TmfTimestamp.BIG_CRUNCH);
720 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal = new TmfTraceRangeUpdatedSignal(this, this, timeRange);
721
722 // Broadcast in separate thread to prevent deadlock
089a4872 723 broadcastAsync(rangeUpdatedsignal);
faa38350
PT
724 return;
725 }
726 }
727
9de979b2
GB
728 /**
729 * Refresh the supplementary files resources, so it can pick up new files
730 * that got created.
c4767854 731 * @since 3.0
9de979b2
GB
732 */
733 public void refreshSupplementaryFiles() {
734 if (fResource != null) {
735 IProject project = fResource.getProject();
736 IFolder supplFolder = project.getFolder(TmfCommonConstants.TRACE_SUPPLEMENATARY_FOLDER_NAME);
737 if (supplFolder.exists()) {
738 try {
739 supplFolder.refreshLocal(IResource.DEPTH_INFINITE, null);
740 } catch (CoreException e) {
741 Activator.logError("Error refreshing resources", e); //$NON-NLS-1$
742 }
743 }
744 }
745 }
746
faa38350
PT
747 /**
748 * Signal handler for the TmfTraceRangeUpdatedSignal signal
749 *
750 * @param signal The incoming signal
751 * @since 2.0
752 */
753 @TmfSignalHandler
754 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal) {
755 if (signal.getTrace() == this) {
756 getIndexer().buildIndex(getNbEvents(), signal.getRange(), false);
757 }
758 }
759
032ecd45
MAL
760 /**
761 * Signal handler for the TmfTraceUpdatedSignal signal
762 *
763 * @param signal The incoming signal
c4767854 764 * @since 3.0
032ecd45
MAL
765 */
766 @TmfSignalHandler
767 public void traceUpdated(final TmfTraceUpdatedSignal signal) {
768 if (signal.getSource() == getIndexer()) {
769 fNbEvents = signal.getNbEvents();
770 fStartTime = signal.getRange().getStartTime();
771 fEndTime = signal.getRange().getEndTime();
772 }
773 }
774
e73a4ba5
GB
775 /**
776 * Returns the file resource used to store synchronization formula. The file
777 * may not exist.
778 *
779 * @return the synchronization file
780 */
781 private File getSyncFormulaFile() {
782 File file = null;
783 if (fResource instanceof IFolder) {
784 try {
785 String supplDirectory;
786
787 supplDirectory = fResource.getPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER);
788
789 file = new File(supplDirectory + File.separator + SYNCHRONIZATION_FORMULA_FILE);
790
791 } catch (CoreException e) {
792
793 }
794 }
795 return file;
796 }
797
798 // ------------------------------------------------------------------------
799 // Timestamp transformation functions
800 // ------------------------------------------------------------------------
801
802 /**
803 * @since 3.0
804 */
805 @Override
806 public ITmfTimestampTransform getTimestampTransform() {
807 if (fTsTransform == null) {
808 /* Check if a formula is stored somewhere in the resources */
809 File sync_file = getSyncFormulaFile();
810 if (sync_file != null && sync_file.exists()) {
811
a4524c1b
AM
812 try (FileInputStream fis = new FileInputStream(sync_file);
813 ObjectInputStream ois = new ObjectInputStream(fis);) {
814
e73a4ba5
GB
815 fTsTransform = (ITmfTimestampTransform) ois.readObject();
816
a4524c1b 817 } catch (ClassNotFoundException | IOException e) {
e73a4ba5
GB
818 fTsTransform = TmfTimestampTransform.IDENTITY;
819 }
820 } else {
821 fTsTransform = TmfTimestampTransform.IDENTITY;
822 }
823 }
824 return fTsTransform;
825 }
826
827 /**
828 * @since 3.0
829 */
830 @Override
831 public void setTimestampTransform(final ITmfTimestampTransform tt) {
832 fTsTransform = tt;
833
834 /* Save the timestamp transform to a file */
835 File sync_file = getSyncFormulaFile();
836 if (sync_file != null) {
837 if (sync_file.exists()) {
838 sync_file.delete();
839 }
840 FileOutputStream fos;
841 ObjectOutputStream oos;
842
843 /* Save the header of the file */
844 try {
845 fos = new FileOutputStream(sync_file, false);
846 oos = new ObjectOutputStream(fos);
847
848 oos.writeObject(fTsTransform);
849 oos.close();
850 fos.close();
851 } catch (IOException e1) {
852 Activator.logError("Error writing timestamp transform for trace", e1); //$NON-NLS-1$
853 }
854 }
855 }
856
857 /**
858 * @since 3.0
859 */
860 @Override
861 public ITmfTimestamp createTimestamp(long ts) {
862 return new TmfTimestamp(getTimestampTransform().transform(ts));
863 }
864
3791b5df 865 // ------------------------------------------------------------------------
09e86496 866 // toString
3791b5df
FC
867 // ------------------------------------------------------------------------
868
12c155f5 869 @Override
09e86496 870 @SuppressWarnings("nls")
5419a136 871 public synchronized String toString() {
20658947
FC
872 return "TmfTrace [fPath=" + fPath + ", fCacheSize=" + fCacheSize
873 + ", fNbEvents=" + fNbEvents + ", fStartTime=" + fStartTime
874 + ", fEndTime=" + fEndTime + ", fStreamingInterval=" + fStreamingInterval + "]";
12c155f5
FC
875 }
876
8c8bf09f 877}
This page took 0.171394 seconds and 5 git commands to generate.