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