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