lttng: Re-enable API tracking
[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;
9de979b2 33import org.eclipse.core.resources.IProject;
828e5592 34import org.eclipse.core.resources.IResource;
faa38350 35import org.eclipse.core.runtime.CoreException;
42459d24 36import org.eclipse.core.runtime.IStatus;
b22a582a 37import org.eclipse.core.runtime.MultiStatus;
032ecd45 38import org.eclipse.core.runtime.Path;
42459d24 39import org.eclipse.core.runtime.Status;
b22a582a 40import org.eclipse.linuxtools.internal.tmf.core.Activator;
e73a4ba5 41import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
c068a752
GB
42import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModule;
43import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModuleHelper;
44import org.eclipse.linuxtools.tmf.core.analysis.TmfAnalysisManager;
6c13869b 45import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider;
72f1e62a 46import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
c068a752 47import org.eclipse.linuxtools.tmf.core.exceptions.TmfAnalysisException;
b4f71e4a 48import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
5419a136 49import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
faa38350 50import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
fec1ac0b 51import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
faa38350
PT
52import org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal;
53import org.eclipse.linuxtools.tmf.core.signal.TmfTraceRangeUpdatedSignal;
032ecd45 54import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
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
0f89d4ba
AM
397 public Class<? extends ITmfEvent> getEventType() {
398 return 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
fd3f1eff 712 public synchronized ITmfContext armRequest(final ITmfEventRequest request) {
faa38350
PT
713 if (executorIsShutdown()) {
714 return null;
715 }
fd3f1eff
AM
716 if (!TmfTimestamp.BIG_BANG.equals(request.getRange().getStartTime())
717 && (request.getIndex() == 0)) {
718 final ITmfContext context = seekEvent(request.getRange().getStartTime());
719 request.setStartIndex((int) context.getRank());
8584dc20 720 return context;
8584dc20 721
5419a136
AM
722 }
723 return seekEvent(request.getIndex());
3791b5df
FC
724 }
725
faa38350
PT
726 // ------------------------------------------------------------------------
727 // Signal handlers
728 // ------------------------------------------------------------------------
729
730 /**
731 * Handler for the Trace Opened signal
732 *
733 * @param signal
734 * The incoming signal
735 * @since 2.0
736 */
737 @TmfSignalHandler
738 public void traceOpened(TmfTraceOpenedSignal signal) {
b9a5bf8f
AM
739 boolean signalIsForUs = false;
740 for (ITmfTrace trace : TmfTraceManager.getTraceSet(signal.getTrace())) {
741 if (trace == this) {
742 signalIsForUs = true;
fe0c44c4 743 break;
faa38350
PT
744 }
745 }
faa38350 746
b9a5bf8f 747 if (!signalIsForUs) {
fe0c44c4
AM
748 return;
749 }
750
751 /*
b9a5bf8f 752 * The signal is either for this trace, or for an experiment containing
fe0c44c4
AM
753 * this trace.
754 */
b22a582a
AM
755 MultiStatus status = new MultiStatus(Activator.PLUGIN_ID, IStatus.OK, null, null);
756 status.add(buildStatistics());
757 status.add(buildStateSystem());
c068a752 758 status.add(executeAnalysis());
b22a582a
AM
759 if (!status.isOK()) {
760 Activator.log(status);
fe0c44c4
AM
761 }
762
9de979b2 763 refreshSupplementaryFiles();
fe0c44c4 764
faa38350 765 if (signal.getTrace() == this) {
f8fc4a3a 766 /* Additionally, the signal is directly for this trace. */
faa38350
PT
767 if (getNbEvents() == 0) {
768 return;
769 }
770
f8fc4a3a
PT
771 /* For a streaming trace, the range updated signal should be sent
772 * by the subclass when a new safe time is determined.
773 */
774 if (getStreamingInterval() > 0) {
775 return;
776 }
777
faa38350
PT
778 final TmfTimeRange timeRange = new TmfTimeRange(getStartTime(), TmfTimestamp.BIG_CRUNCH);
779 final TmfTraceRangeUpdatedSignal rangeUpdatedsignal = new TmfTraceRangeUpdatedSignal(this, this, timeRange);
780
781 // Broadcast in separate thread to prevent deadlock
782 new Thread() {
783 @Override
784 public void run() {
785 broadcast(rangeUpdatedsignal);
786 }
787 }.start();
788 return;
789 }
790 }
791
9de979b2
GB
792 /**
793 * Refresh the supplementary files resources, so it can pick up new files
794 * that got created.
795 */
796 public void refreshSupplementaryFiles() {
797 if (fResource != null) {
798 IProject project = fResource.getProject();
799 IFolder supplFolder = project.getFolder(TmfCommonConstants.TRACE_SUPPLEMENATARY_FOLDER_NAME);
800 if (supplFolder.exists()) {
801 try {
802 supplFolder.refreshLocal(IResource.DEPTH_INFINITE, null);
803 } catch (CoreException e) {
804 Activator.logError("Error refreshing resources", e); //$NON-NLS-1$
805 }
806 }
807 }
808 }
809
faa38350
PT
810 /**
811 * Signal handler for the TmfTraceRangeUpdatedSignal signal
812 *
813 * @param signal The incoming signal
814 * @since 2.0
815 */
816 @TmfSignalHandler
817 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal) {
818 if (signal.getTrace() == this) {
819 getIndexer().buildIndex(getNbEvents(), signal.getRange(), false);
820 }
821 }
822
032ecd45
MAL
823 /**
824 * Signal handler for the TmfTraceUpdatedSignal signal
825 *
826 * @param signal The incoming signal
827 * @since 2.0
828 */
829 @TmfSignalHandler
830 public void traceUpdated(final TmfTraceUpdatedSignal signal) {
831 if (signal.getSource() == getIndexer()) {
832 fNbEvents = signal.getNbEvents();
833 fStartTime = signal.getRange().getStartTime();
834 fEndTime = signal.getRange().getEndTime();
835 }
836 }
837
e73a4ba5
GB
838 /**
839 * Returns the file resource used to store synchronization formula. The file
840 * may not exist.
841 *
842 * @return the synchronization file
843 */
844 private File getSyncFormulaFile() {
845 File file = null;
846 if (fResource instanceof IFolder) {
847 try {
848 String supplDirectory;
849
850 supplDirectory = fResource.getPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER);
851
852 file = new File(supplDirectory + File.separator + SYNCHRONIZATION_FORMULA_FILE);
853
854 } catch (CoreException e) {
855
856 }
857 }
858 return file;
859 }
860
861 // ------------------------------------------------------------------------
862 // Timestamp transformation functions
863 // ------------------------------------------------------------------------
864
865 /**
866 * @since 3.0
867 */
868 @Override
869 public ITmfTimestampTransform getTimestampTransform() {
870 if (fTsTransform == null) {
871 /* Check if a formula is stored somewhere in the resources */
872 File sync_file = getSyncFormulaFile();
873 if (sync_file != null && sync_file.exists()) {
874
875 try {
876 FileInputStream fis = new FileInputStream(sync_file);
877 ObjectInputStream ois = new ObjectInputStream(fis);
878 fTsTransform = (ITmfTimestampTransform) ois.readObject();
879
880 ois.close();
881 fis.close();
882 } catch (ClassNotFoundException e1) {
883 fTsTransform = TmfTimestampTransform.IDENTITY;
884 } catch (FileNotFoundException e1) {
885 fTsTransform = TmfTimestampTransform.IDENTITY;
886 } catch (IOException e1) {
887 fTsTransform = TmfTimestampTransform.IDENTITY;
888 }
889 } else {
890 fTsTransform = TmfTimestampTransform.IDENTITY;
891 }
892 }
893 return fTsTransform;
894 }
895
896 /**
897 * @since 3.0
898 */
899 @Override
900 public void setTimestampTransform(final ITmfTimestampTransform tt) {
901 fTsTransform = tt;
902
903 /* Save the timestamp transform to a file */
904 File sync_file = getSyncFormulaFile();
905 if (sync_file != null) {
906 if (sync_file.exists()) {
907 sync_file.delete();
908 }
909 FileOutputStream fos;
910 ObjectOutputStream oos;
911
912 /* Save the header of the file */
913 try {
914 fos = new FileOutputStream(sync_file, false);
915 oos = new ObjectOutputStream(fos);
916
917 oos.writeObject(fTsTransform);
918 oos.close();
919 fos.close();
920 } catch (IOException e1) {
921 Activator.logError("Error writing timestamp transform for trace", e1); //$NON-NLS-1$
922 }
923 }
924 }
925
926 /**
927 * @since 3.0
928 */
929 @Override
930 public ITmfTimestamp createTimestamp(long ts) {
931 return new TmfTimestamp(getTimestampTransform().transform(ts));
932 }
933
3791b5df 934 // ------------------------------------------------------------------------
09e86496 935 // toString
3791b5df
FC
936 // ------------------------------------------------------------------------
937
12c155f5 938 @Override
09e86496 939 @SuppressWarnings("nls")
5419a136 940 public synchronized String toString() {
20658947
FC
941 return "TmfTrace [fPath=" + fPath + ", fCacheSize=" + fCacheSize
942 + ", fNbEvents=" + fNbEvents + ", fStartTime=" + fStartTime
943 + ", fEndTime=" + fEndTime + ", fStreamingInterval=" + fStreamingInterval + "]";
12c155f5
FC
944 }
945
8c8bf09f 946}
This page took 0.113234 seconds and 5 git commands to generate.