tmf: Fix time unit of time stamp in TmfTrace.createTimestamp()
[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 54import org.eclipse.linuxtools.tmf.core.synchronization.ITmfTimestampTransform;
9ffcda7d 55import org.eclipse.linuxtools.tmf.core.synchronization.TimestampTransformFactory;
3bd46eef 56import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
b2c463c5 57import org.eclipse.linuxtools.tmf.core.timestamp.TmfNanoTimestamp;
3bd46eef
AM
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 */
6fd3c6e9 91public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace, ITmfTraceCompleteness {
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
339d539c
PT
210 @Override
211 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type, String name) throws TmfTraceException {
212 setName(name);
213 initTrace(resource, path, type);
214 }
215
7e6347b0 216 @Override
6256d8ad 217 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
7e6347b0 218 initialize(resource, path, type);
7e6347b0
FC
219 }
220
09e86496 221 /**
1703b536 222 * Initialize the trace common attributes and the base component.
0bfb7d06
MK
223 *
224 * @param resource the Eclipse resource (trace)
1703b536
FC
225 * @param path the trace path
226 * @param type the trace event type
0bfb7d06 227 *
6f4e8ec0 228 * @throws TmfTraceException If something failed during the initialization
3791b5df 229 */
248af329
AM
230 protected void initialize(final IResource resource,
231 final String path,
232 final Class<? extends ITmfEvent> type)
233 throws TmfTraceException {
0316808c 234 if (path == null) {
b4f71e4a 235 throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
0316808c 236 }
3791b5df 237 fPath = path;
1703b536 238 fResource = resource;
339d539c
PT
239 String traceName = getName();
240 if (traceName == null || traceName.isEmpty()) {
241 traceName = (resource != null) ? resource.getName() : new Path(path).lastSegment();
1703b536 242 }
2352aed9
FC
243 if (fParser == null) {
244 if (this instanceof ITmfEventParser) {
6256d8ad 245 fParser = (ITmfEventParser) this;
2352aed9
FC
246 } else {
247 throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$
248 }
249 }
3791b5df 250 super.init(traceName, type);
fec1ac0b
BH
251 // register as VIP after super.init() because TmfComponent registers to signal manager there
252 TmfSignalManager.registerVIP(this);
1a3f1ec3
GB
253 if (fIndexer != null) {
254 fIndexer.dispose();
255 }
ab186fbb 256 fIndexer = createIndexer(fCacheSize);
3791b5df
FC
257 }
258
2352aed9
FC
259 /**
260 * Indicates if the path points to an existing file/directory
0bfb7d06 261 *
2352aed9
FC
262 * @param path the path to test
263 * @return true if the file/directory exists
3791b5df 264 */
2352aed9 265 protected boolean fileExists(final String path) {
085d898f 266 final File file = new File(path);
3791b5df
FC
267 return file.exists();
268 }
269
c7e1020d 270 /**
51e75066 271 * @since 2.0
c7e1020d 272 */
51e75066
AM
273 @Override
274 public void indexTrace(boolean waitForCompletion) {
9e0640dc 275 getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, waitForCompletion);
c7e1020d
FC
276 }
277
c068a752
GB
278 /**
279 * Instantiate the applicable analysis modules and executes the analysis
280 * modules that are meant to be automatically executed
281 *
282 * @return An IStatus indicating whether the analysis could be run
283 * successfully or not
284 * @since 3.0
285 */
286 protected IStatus executeAnalysis() {
287 MultiStatus status = new MultiStatus(Activator.PLUGIN_ID, IStatus.OK, null, null);
288 Map<String, IAnalysisModuleHelper> modules = TmfAnalysisManager.getAnalysisModules(this.getClass());
289 for (IAnalysisModuleHelper helper : modules.values()) {
290 try {
291 IAnalysisModule module = helper.newModule(this);
292 fAnalysisModules.put(module.getId(), module);
293 if (module.isAutomatic()) {
294 status.add(module.schedule());
295 }
296 } catch (TmfAnalysisException e) {
26683871 297 status.add(new Status(IStatus.WARNING, Activator.PLUGIN_ID, e.getMessage()));
c068a752
GB
298 }
299 }
300 return status;
301 }
302
c4767854
AM
303 /**
304 * @since 3.0
305 */
c068a752 306 @Override
ff3f02c8 307 public IAnalysisModule getAnalysisModule(String analysisId) {
c068a752
GB
308 return fAnalysisModules.get(analysisId);
309 }
310
ff3f02c8
AM
311
312 /**
313 * @since 3.0
314 */
315 @Override
316 public Iterable<IAnalysisModule> getAnalysisModules() {
317 synchronized (fAnalysisModules) {
318 Set<IAnalysisModule> modules = new HashSet<>(fAnalysisModules.values());
319 return modules;
320 }
321 }
322
c4767854
AM
323 /**
324 * @since 3.0
325 */
c068a752 326 @Override
ff3f02c8
AM
327 public <T extends IAnalysisModule> T getAnalysisModuleOfClass(Class<T> moduleClass, String id) {
328 Iterable<T> modules = getAnalysisModulesOfClass(moduleClass);
329 for (T module : modules) {
330 if (id.equals(module.getId())) {
331 return module;
c068a752
GB
332 }
333 }
ff3f02c8 334 return null;
c068a752
GB
335 }
336
c4767854
AM
337 /**
338 * @since 3.0
339 */
c068a752 340 @Override
ff3f02c8
AM
341 public <T> Iterable<T> getAnalysisModulesOfClass(Class<T> moduleClass) {
342 Set<T> modules = new HashSet<>();
343 synchronized (fAnalysisModules) {
344 for (Entry<String, IAnalysisModule> entry : fAnalysisModules.entrySet()) {
345 if (moduleClass.isAssignableFrom(entry.getValue().getClass())) {
346 modules.add(moduleClass.cast(entry.getValue()));
347 }
348 }
349 }
350 return modules;
c068a752
GB
351 }
352
b5ee6881
FC
353 /**
354 * Clears the trace
355 */
356 @Override
357 public synchronized void dispose() {
1a4205d9 358 /* Clean up the index if applicable */
77551cc2
FC
359 if (getIndexer() != null) {
360 getIndexer().dispose();
361 }
1a4205d9 362
a1529f38 363 /* Clean up the analysis modules */
ff3f02c8
AM
364 synchronized (fAnalysisModules) {
365 for (IAnalysisModule module : fAnalysisModules.values()) {
366 module.dispose();
367 }
6ef5ae35 368 fAnalysisModules.clear();
a1529f38
AM
369 }
370
b5ee6881
FC
371 super.dispose();
372 }
373
3791b5df 374 // ------------------------------------------------------------------------
09e86496 375 // ITmfTrace - Basic getters
e31e01e8 376 // ------------------------------------------------------------------------
8c8bf09f 377
25e48683 378 @Override
0f89d4ba
AM
379 public Class<? extends ITmfEvent> getEventType() {
380 return super.getType();
25e48683
FC
381 }
382
d4011df2 383 @Override
09e86496
FC
384 public IResource getResource() {
385 return fResource;
8c8bf09f
ASL
386 }
387
d4011df2 388 @Override
09e86496
FC
389 public String getPath() {
390 return fPath;
8c8bf09f
ASL
391 }
392
20658947
FC
393 @Override
394 public int getCacheSize() {
395 return fCacheSize;
396 }
397
20658947
FC
398 @Override
399 public long getStreamingInterval() {
400 return fStreamingInterval;
401 }
402
0316808c
FC
403 /**
404 * @return the trace indexer
a3db8436 405 * @since 3.0
0316808c 406 */
6256d8ad 407 protected ITmfTraceIndexer getIndexer() {
0316808c
FC
408 return fIndexer;
409 }
410
411 /**
412 * @return the trace parser
413 */
6256d8ad 414 protected ITmfEventParser getParser() {
0316808c
FC
415 return fParser;
416 }
417
09e86496
FC
418 // ------------------------------------------------------------------------
419 // ITmfTrace - Trace characteristics getters
420 // ------------------------------------------------------------------------
421
d4011df2 422 @Override
e9a6e38e 423 public long getNbEvents() {
3791b5df 424 return fNbEvents;
b0a282fb
FC
425 }
426
3bd46eef
AM
427 /**
428 * @since 2.0
62d1696a 429 */
d4011df2 430 @Override
12c155f5 431 public TmfTimeRange getTimeRange() {
cb866e08 432 return new TmfTimeRange(fStartTime, fEndTime);
8c8bf09f
ASL
433 }
434
3bd46eef
AM
435 /**
436 * @since 2.0
e31e01e8 437 */
d4011df2 438 @Override
4df4581d 439 public ITmfTimestamp getStartTime() {
4593bd5b 440 return fStartTime;
146a887c
FC
441 }
442
3bd46eef
AM
443 /**
444 * @since 2.0
e31e01e8 445 */
d4011df2 446 @Override
4df4581d 447 public ITmfTimestamp getEndTime() {
4593bd5b 448 return fEndTime;
20658947
FC
449 }
450
d7ee91bb 451 /**
d7ee91bb
PT
452 * @since 2.0
453 */
66262ad8
BH
454 @Override
455 public ITmfTimestamp getInitialRangeOffset() {
d7ee91bb
PT
456 final long DEFAULT_INITIAL_OFFSET_VALUE = (1L * 100 * 1000 * 1000); // .1sec
457 return new TmfTimestamp(DEFAULT_INITIAL_OFFSET_VALUE, ITmfTimestamp.NANOSECOND_SCALE);
458 }
459
bb52f9bc
GB
460 /**
461 * @since 3.0
462 */
463 @Override
464 public String getHostId() {
465 return this.getName();
466 }
467
20658947 468 // ------------------------------------------------------------------------
d7ee91bb 469 // Convenience setters
20658947
FC
470 // ------------------------------------------------------------------------
471
0316808c
FC
472 /**
473 * Set the trace cache size. Must be done at initialization time.
0bfb7d06 474 *
0316808c
FC
475 * @param cacheSize The trace cache size
476 */
477 protected void setCacheSize(final int cacheSize) {
478 fCacheSize = cacheSize;
479 }
480
481 /**
482 * Set the trace known number of events. This can be quite dynamic
483 * during indexing or for live traces.
0bfb7d06 484 *
0316808c
FC
485 * @param nbEvents The number of events
486 */
487 protected synchronized void setNbEvents(final long nbEvents) {
488 fNbEvents = (nbEvents > 0) ? nbEvents : 0;
489 }
490
20658947
FC
491 /**
492 * Update the trace events time range
0bfb7d06 493 *
20658947 494 * @param range the new time range
3bd46eef 495 * @since 2.0
20658947
FC
496 */
497 protected void setTimeRange(final TmfTimeRange range) {
4593bd5b
AM
498 fStartTime = range.getStartTime();
499 fEndTime = range.getEndTime();
20658947
FC
500 }
501
502 /**
503 * Update the trace chronologically first event timestamp
0bfb7d06 504 *
20658947 505 * @param startTime the new first event timestamp
3bd46eef 506 * @since 2.0
20658947
FC
507 */
508 protected void setStartTime(final ITmfTimestamp startTime) {
4593bd5b 509 fStartTime = startTime;
20658947
FC
510 }
511
512 /**
513 * Update the trace chronologically last event timestamp
0bfb7d06 514 *
20658947 515 * @param endTime the new last event timestamp
3bd46eef 516 * @since 2.0
20658947
FC
517 */
518 protected void setEndTime(final ITmfTimestamp endTime) {
4593bd5b 519 fEndTime = endTime;
20658947
FC
520 }
521
522 /**
0316808c 523 * Set the polling interval for live traces (default = 0 = no streaming).
0bfb7d06 524 *
20658947
FC
525 * @param interval the new trace streaming interval
526 */
527 protected void setStreamingInterval(final long interval) {
1703b536 528 fStreamingInterval = (interval > 0) ? interval : 0;
146a887c
FC
529 }
530
0316808c
FC
531 /**
532 * Set the trace parser. Must be done at initialization time.
0bfb7d06 533 *
0316808c
FC
534 * @param parser the new trace parser
535 */
6256d8ad 536 protected void setParser(final ITmfEventParser parser) {
0316808c
FC
537 fParser = parser;
538 }
539
09e86496 540 // ------------------------------------------------------------------------
7e6347b0 541 // ITmfTrace - SeekEvent operations (returning a trace context)
09e86496
FC
542 // ------------------------------------------------------------------------
543
1b70b6dc 544 @Override
7e6347b0 545 public synchronized ITmfContext seekEvent(final long rank) {
09e86496 546
7e6347b0 547 // A rank <= 0 indicates to seek the first event
2352aed9 548 if (rank <= 0) {
1e1bef82 549 ITmfContext context = seekEvent((ITmfLocation) null);
2352aed9
FC
550 context.setRank(0);
551 return context;
552 }
09e86496 553
09e86496 554 // Position the trace at the checkpoint
7e6347b0 555 final ITmfContext context = fIndexer.seekIndex(rank);
09e86496
FC
556
557 // And locate the requested event context
7e6347b0
FC
558 long pos = context.getRank();
559 if (pos < rank) {
c32744d6 560 ITmfEvent event = getNext(context);
0bfb7d06 561 while ((event != null) && (++pos < rank)) {
c32744d6 562 event = getNext(context);
7e6347b0 563 }
09e86496
FC
564 }
565 return context;
1b70b6dc
PT
566 }
567
3bd46eef
AM
568 /**
569 * @since 2.0
09e86496
FC
570 */
571 @Override
7e6347b0 572 public synchronized ITmfContext seekEvent(final ITmfTimestamp timestamp) {
09e86496 573
7e6347b0 574 // A null timestamp indicates to seek the first event
2352aed9 575 if (timestamp == null) {
1e1bef82 576 ITmfContext context = seekEvent((ITmfLocation) null);
2352aed9
FC
577 context.setRank(0);
578 return context;
579 }
09e86496 580
1703b536 581 // Position the trace at the checkpoint
408e65d2 582 ITmfContext context = fIndexer.seekIndex(timestamp);
09e86496
FC
583
584 // And locate the requested event context
ea271da6
PT
585 ITmfLocation previousLocation = context.getLocation();
586 long previousRank = context.getRank();
587 ITmfEvent event = getNext(context);
7e6347b0 588 while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) {
ea271da6
PT
589 previousLocation = context.getLocation();
590 previousRank = context.getRank();
591 event = getNext(context);
09e86496 592 }
0316808c
FC
593 if (event == null) {
594 context.setLocation(null);
595 context.setRank(ITmfContext.UNKNOWN_RANK);
ea271da6
PT
596 } else {
597 context.dispose();
598 context = seekEvent(previousLocation);
599 context.setRank(previousRank);
0316808c 600 }
09e86496
FC
601 return context;
602 }
0283f7ff 603
09e86496
FC
604 // ------------------------------------------------------------------------
605 // ITmfTrace - Read operations (returning an actual event)
606 // ------------------------------------------------------------------------
607
d4011df2 608 @Override
6256d8ad 609 public synchronized ITmfEvent getNext(final ITmfContext context) {
09e86496 610 // parseEvent() does not update the context
6256d8ad 611 final ITmfEvent event = fParser.parseEvent(context);
09e86496 612 if (event != null) {
d337369a 613 updateAttributes(context, event.getTimestamp());
09e86496
FC
614 context.setLocation(getCurrentLocation());
615 context.increaseRank();
616 processEvent(event);
617 }
618 return event;
619 }
620
621 /**
d337369a 622 * Hook for special event processing by the concrete class
7e6347b0 623 * (called by TmfTrace.getEvent())
0bfb7d06 624 *
d337369a 625 * @param event the event
09e86496
FC
626 */
627 protected void processEvent(final ITmfEvent event) {
d337369a 628 // Do nothing
09e86496
FC
629 }
630
d337369a
FC
631 /**
632 * Update the trace attributes
0bfb7d06 633 *
d337369a 634 * @param context the current trace context
2848c377 635 * @param timestamp the corresponding timestamp
3bd46eef 636 * @since 2.0
d337369a
FC
637 */
638 protected synchronized void updateAttributes(final ITmfContext context, final ITmfTimestamp timestamp) {
0bfb7d06 639 if (fStartTime.equals(TmfTimestamp.BIG_BANG) || (fStartTime.compareTo(timestamp, false) > 0)) {
4593bd5b 640 fStartTime = timestamp;
09e86496 641 }
0bfb7d06 642 if (fEndTime.equals(TmfTimestamp.BIG_CRUNCH) || (fEndTime.compareTo(timestamp, false) < 0)) {
4593bd5b 643 fEndTime = timestamp;
09e86496
FC
644 }
645 if (context.hasValidRank()) {
d337369a 646 long rank = context.getRank();
09e86496
FC
647 if (fNbEvents <= rank) {
648 fNbEvents = rank + 1;
649 }
200789b3
AM
650 if (fIndexer != null) {
651 fIndexer.updateIndex(context, timestamp);
652 }
09e86496 653 }
abfad0aa
FC
654 }
655
3791b5df 656 // ------------------------------------------------------------------------
d337369a 657 // TmfDataProvider
3791b5df
FC
658 // ------------------------------------------------------------------------
659
77c4a6df
AM
660 /**
661 * @since 2.0
d337369a 662 */
3791b5df 663 @Override
fd3f1eff 664 public synchronized ITmfContext armRequest(final ITmfEventRequest request) {
faa38350
PT
665 if (executorIsShutdown()) {
666 return null;
667 }
fd3f1eff
AM
668 if (!TmfTimestamp.BIG_BANG.equals(request.getRange().getStartTime())
669 && (request.getIndex() == 0)) {
670 final ITmfContext context = seekEvent(request.getRange().getStartTime());
671 request.setStartIndex((int) context.getRank());
8584dc20 672 return context;
8584dc20 673
5419a136
AM
674 }
675 return seekEvent(request.getIndex());
3791b5df
FC
676 }
677
faa38350
PT
678 // ------------------------------------------------------------------------
679 // Signal handlers
680 // ------------------------------------------------------------------------
681
682 /**
683 * Handler for the Trace Opened signal
684 *
685 * @param signal
686 * The incoming signal
687 * @since 2.0
688 */
689 @TmfSignalHandler
690 public void traceOpened(TmfTraceOpenedSignal signal) {
b9a5bf8f
AM
691 boolean signalIsForUs = false;
692 for (ITmfTrace trace : TmfTraceManager.getTraceSet(signal.getTrace())) {
693 if (trace == this) {
694 signalIsForUs = true;
fe0c44c4 695 break;
faa38350
PT
696 }
697 }
faa38350 698
b9a5bf8f 699 if (!signalIsForUs) {
fe0c44c4
AM
700 return;
701 }
702
703 /*
b9a5bf8f 704 * The signal is either for this trace, or for an experiment containing
fe0c44c4
AM
705 * this trace.
706 */
be4a197a 707 IStatus status = executeAnalysis();
b22a582a
AM
708 if (!status.isOK()) {
709 Activator.log(status);
fe0c44c4
AM
710 }
711
b5e8ee95 712 TmfTraceManager.refreshSupplementaryFiles(this);
fe0c44c4 713
faa38350 714 if (signal.getTrace() == this) {
f8fc4a3a 715 /* Additionally, the signal is directly for this trace. */
faa38350
PT
716 if (getNbEvents() == 0) {
717 return;
718 }
719
f8fc4a3a
PT
720 /* For a streaming trace, the range updated signal should be sent
721 * by the subclass when a new safe time is determined.
722 */
723 if (getStreamingInterval() > 0) {
724 return;
725 }
726
6fd3c6e9
MAL
727 if (isComplete()) {
728 final TmfTimeRange timeRange = new TmfTimeRange(getStartTime(), TmfTimestamp.BIG_CRUNCH);
729 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal = new TmfTraceRangeUpdatedSignal(this, this, timeRange);
faa38350 730
6fd3c6e9
MAL
731 // Broadcast in separate thread to prevent deadlock
732 broadcastAsync(rangeUpdatedsignal);
733 }
faa38350
PT
734 return;
735 }
736 }
737
738 /**
739 * Signal handler for the TmfTraceRangeUpdatedSignal signal
740 *
741 * @param signal The incoming signal
742 * @since 2.0
743 */
744 @TmfSignalHandler
745 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal) {
746 if (signal.getTrace() == this) {
747 getIndexer().buildIndex(getNbEvents(), signal.getRange(), false);
748 }
749 }
750
032ecd45
MAL
751 /**
752 * Signal handler for the TmfTraceUpdatedSignal signal
753 *
754 * @param signal The incoming signal
c4767854 755 * @since 3.0
032ecd45
MAL
756 */
757 @TmfSignalHandler
758 public void traceUpdated(final TmfTraceUpdatedSignal signal) {
759 if (signal.getSource() == getIndexer()) {
760 fNbEvents = signal.getNbEvents();
761 fStartTime = signal.getRange().getStartTime();
762 fEndTime = signal.getRange().getEndTime();
763 }
764 }
765
e73a4ba5
GB
766 /**
767 * Returns the file resource used to store synchronization formula. The file
768 * may not exist.
769 *
770 * @return the synchronization file
771 */
772 private File getSyncFormulaFile() {
773 File file = null;
774 if (fResource instanceof IFolder) {
775 try {
776 String supplDirectory;
777
778 supplDirectory = fResource.getPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER);
779
780 file = new File(supplDirectory + File.separator + SYNCHRONIZATION_FORMULA_FILE);
781
782 } catch (CoreException e) {
783
784 }
785 }
786 return file;
787 }
788
789 // ------------------------------------------------------------------------
790 // Timestamp transformation functions
791 // ------------------------------------------------------------------------
792
793 /**
794 * @since 3.0
795 */
796 @Override
797 public ITmfTimestampTransform getTimestampTransform() {
798 if (fTsTransform == null) {
799 /* Check if a formula is stored somewhere in the resources */
800 File sync_file = getSyncFormulaFile();
801 if (sync_file != null && sync_file.exists()) {
802
a4524c1b
AM
803 try (FileInputStream fis = new FileInputStream(sync_file);
804 ObjectInputStream ois = new ObjectInputStream(fis);) {
805
e73a4ba5
GB
806 fTsTransform = (ITmfTimestampTransform) ois.readObject();
807
a4524c1b 808 } catch (ClassNotFoundException | IOException e) {
9ffcda7d 809 fTsTransform = TimestampTransformFactory.getDefaultTransform();
e73a4ba5
GB
810 }
811 } else {
9ffcda7d 812 fTsTransform = TimestampTransformFactory.getDefaultTransform();
e73a4ba5
GB
813 }
814 }
815 return fTsTransform;
816 }
817
818 /**
819 * @since 3.0
820 */
821 @Override
822 public void setTimestampTransform(final ITmfTimestampTransform tt) {
823 fTsTransform = tt;
824
825 /* Save the timestamp transform to a file */
826 File sync_file = getSyncFormulaFile();
827 if (sync_file != null) {
828 if (sync_file.exists()) {
829 sync_file.delete();
830 }
e73a4ba5
GB
831
832 /* Save the header of the file */
7b3eb8c0
AM
833 try (FileOutputStream fos = new FileOutputStream(sync_file, false);
834 ObjectOutputStream oos = new ObjectOutputStream(fos);) {
e73a4ba5
GB
835
836 oos.writeObject(fTsTransform);
e73a4ba5
GB
837 } catch (IOException e1) {
838 Activator.logError("Error writing timestamp transform for trace", e1); //$NON-NLS-1$
839 }
840 }
841 }
842
843 /**
844 * @since 3.0
845 */
846 @Override
847 public ITmfTimestamp createTimestamp(long ts) {
b2c463c5 848 return new TmfNanoTimestamp(getTimestampTransform().transform(ts));
e73a4ba5
GB
849 }
850
3791b5df 851 // ------------------------------------------------------------------------
09e86496 852 // toString
3791b5df
FC
853 // ------------------------------------------------------------------------
854
12c155f5 855 @Override
09e86496 856 @SuppressWarnings("nls")
5419a136 857 public synchronized String toString() {
20658947
FC
858 return "TmfTrace [fPath=" + fPath + ", fCacheSize=" + fCacheSize
859 + ", fNbEvents=" + fNbEvents + ", fStartTime=" + fStartTime
860 + ", fEndTime=" + fEndTime + ", fStreamingInterval=" + fStreamingInterval + "]";
12c155f5
FC
861 }
862
6fd3c6e9
MAL
863 /**
864 * @since 3.1
865 */
866 @Override
867 public boolean isComplete() {
868 /*
869 * Be default, all traces are "complete" which means no more data will
870 * be added later
871 */
872 return true;
873 }
874
875 /**
876 * @since 3.1
877 */
878 @Override
879 public void setComplete(boolean isComplete) {
880 /*
881 * This should be overridden by trace classes that can support live
882 * reading (traces in an incomplete state)
883 */
884 }
8c8bf09f 885}
This page took 0.138072 seconds and 5 git commands to generate.