ss: Remove checkValidTime from the backend interface
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / trace / TmfExperiment.java
CommitLineData
8c8bf09f 1/*******************************************************************************
deaae6e1 2 * Copyright (c) 2009, 2014 Ericsson, École Polytechnique de Montréal
ce2388e0 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
ce2388e0 8 *
8c8bf09f
ASL
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
0316808c 11 * Francois Chouinard - Updated as per TMF Trace Model 1.0
ea271da6
PT
12 * Patrick Tasse - Updated for removal of context clone
13 * Patrick Tasse - Updated for ranks in experiment location
e73a4ba5 14 * Geneviève Bastien - Added support of experiment synchronization
b3dd2736 15 * Added the initExperiment method and default constructor
8c8bf09f
ASL
16 *******************************************************************************/
17
2bdf0193 18package org.eclipse.tracecompass.tmf.core.trace;
8c8bf09f 19
e73a4ba5 20import java.io.File;
032ecd45 21import java.nio.ByteBuffer;
27213c57 22import java.util.Arrays;
e73a4ba5 23
12c155f5 24import org.eclipse.core.resources.IProject;
828e5592 25import org.eclipse.core.resources.IResource;
e73a4ba5 26import org.eclipse.core.runtime.CoreException;
a94410d9 27import org.eclipse.core.runtime.IStatus;
9928ddeb 28import org.eclipse.core.runtime.MultiStatus;
a94410d9 29import org.eclipse.core.runtime.Status;
2bdf0193
AM
30import org.eclipse.tracecompass.internal.tmf.core.Activator;
31import org.eclipse.tracecompass.internal.tmf.core.trace.TmfExperimentContext;
32import org.eclipse.tracecompass.internal.tmf.core.trace.TmfExperimentLocation;
33import org.eclipse.tracecompass.internal.tmf.core.trace.TmfLocationArray;
34import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
35import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
36import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
37import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
38import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
39import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal;
40import org.eclipse.tracecompass.tmf.core.signal.TmfTraceRangeUpdatedSignal;
41import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSynchronizedSignal;
42import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationAlgorithm;
43import org.eclipse.tracecompass.tmf.core.synchronization.SynchronizationManager;
44import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
45import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
46import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
47import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
48import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
49import org.eclipse.tracecompass.tmf.core.trace.indexer.TmfBTreeTraceIndexer;
50import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
8c8bf09f
ASL
51
52/**
9e0640dc 53 * TmfExperiment presents a time-ordered, unified view of a set of ITmfTrace:s
cbdacf03 54 * that are part of a tracing experiment.
4b7b3670
FC
55 *
56 * @version 1.0
57 * @author Francois Chouinard
8c8bf09f 58 */
032ecd45 59public class TmfExperiment extends TmfTrace implements ITmfEventParser, ITmfPersistentlyIndexable {
8c8bf09f 60
c32744d6
FC
61 // ------------------------------------------------------------------------
62 // Constants
63 // ------------------------------------------------------------------------
64
e73a4ba5
GB
65 /**
66 * The file name of the Synchronization
67 *
68 * @since 3.0
7f38b742
GB
69 * @deprecated This file name shouldn't be used directly anymore. All
70 * synchronization files have been moved to a folder and you
71 * should use the {@link #getSynchronizationFolder(boolean)}
72 * method to return the path to this folder.
e73a4ba5 73 */
04ba3554 74 @Deprecated
7f38b742 75 public static final String SYNCHRONIZATION_FILE_NAME = "synchronization.bin"; //$NON-NLS-1$
e73a4ba5 76
04ba3554
GB
77 /**
78 * The name of the directory containing trace synchronization data. This
79 * directory typically will be preserved when traces are synchronized.
80 * Analysis involved in synchronization can put their supplementary files in
81 * there so they are not deleted when synchronized traces are copied.
04ba3554 82 */
7f38b742 83 private static final String SYNCHRONIZATION_DIRECTORY = "sync_data"; //$NON-NLS-1$
04ba3554 84
9e0640dc
FC
85 /**
86 * The default index page size
87 */
88 public static final int DEFAULT_INDEX_PAGE_SIZE = 5000;
c32744d6 89
8c8bf09f
ASL
90 // ------------------------------------------------------------------------
91 // Attributes
92 // ------------------------------------------------------------------------
93
9e0640dc
FC
94 /**
95 * The set of traces that constitute the experiment
96 */
6256d8ad 97 protected ITmfTrace[] fTraces;
8c8bf09f 98
9e0640dc
FC
99 /**
100 * The set of traces that constitute the experiment
101 */
102 private boolean fInitialized = false;
a1091415 103
8c8bf09f 104 // ------------------------------------------------------------------------
9e0640dc 105 // Construction
8c8bf09f
ASL
106 // ------------------------------------------------------------------------
107
9e0640dc 108 /**
b3dd2736 109 * Default constructor
04ba3554 110 *
b3dd2736
GB
111 * @since 3.0
112 */
113 public TmfExperiment() {
114 super();
115 }
116
117 /**
118 * Constructor with parameters
119 *
e73a4ba5
GB
120 * @param type
121 * the event type
122 * @param id
123 * the experiment id
124 * @param traces
125 * the experiment set of traces
9e0640dc 126 */
6256d8ad 127 public TmfExperiment(final Class<? extends ITmfEvent> type, final String id, final ITmfTrace[] traces) {
99504bb8 128 this(type, id, traces, DEFAULT_INDEX_PAGE_SIZE, null);
96c6806f
PT
129 }
130
99504bb8
GB
131 /**
132 * Constructor of experiment taking type, path, traces and resource
133 *
134 * @param type
135 * the event type
136 * @param id
137 * the experiment id
138 * @param traces
139 * the experiment set of traces
140 * @param resource
141 * the resource associated to the experiment
142 */
143 public TmfExperiment(final Class<? extends ITmfEvent> type, final String id, final ITmfTrace[] traces, IResource resource) {
144 this(type, id, traces, DEFAULT_INDEX_PAGE_SIZE, resource);
145 }
146
8c8bf09f 147 /**
e73a4ba5
GB
148 * @param type
149 * the event type
150 * @param path
151 * the experiment path
152 * @param traces
153 * the experiment set of traces
154 * @param indexPageSize
155 * the experiment index page size
8c8bf09f 156 */
6256d8ad 157 public TmfExperiment(final Class<? extends ITmfEvent> type, final String path, final ITmfTrace[] traces, final int indexPageSize) {
99504bb8
GB
158 this(type, path, traces, indexPageSize, null);
159 }
160
161 /**
162 * Full constructor of an experiment, taking the type, path, traces,
163 * indexPageSize and resource
164 *
165 * @param type
166 * the event type
167 * @param path
168 * the experiment path
169 * @param traces
170 * the experiment set of traces
171 * @param indexPageSize
172 * the experiment index page size
173 * @param resource
174 * the resource associated to the experiment
175 */
176 public TmfExperiment(final Class<? extends ITmfEvent> type, final String path, final ITmfTrace[] traces, final int indexPageSize, IResource resource) {
b3dd2736 177 initExperiment(type, path, traces, indexPageSize, resource);
8c8bf09f 178 }
a79913eb 179
032ecd45
MAL
180 @Override
181 protected ITmfTraceIndexer createIndexer(int interval) {
182 if (getCheckpointSize() > 0) {
183 return new TmfBTreeTraceIndexer(this, interval);
184 }
185 return super.createIndexer(interval);
186 }
187
8c8bf09f 188 /**
ff4ed569 189 * Clears the experiment
8c8bf09f
ASL
190 */
191 @Override
a79913eb
FC
192 public synchronized void dispose() {
193
77551cc2
FC
194 // Clean up the index if applicable
195 if (getIndexer() != null) {
196 getIndexer().dispose();
197 }
b5ee6881 198
a79913eb 199 if (fTraces != null) {
9b749023 200 for (final ITmfTrace trace : fTraces) {
a79913eb 201 trace.dispose();
9b749023 202 }
a79913eb
FC
203 fTraces = null;
204 }
2fb2eb37 205 super.dispose();
8c8bf09f
ASL
206 }
207
9e0640dc
FC
208 // ------------------------------------------------------------------------
209 // ITmfTrace - Initializers
210 // ------------------------------------------------------------------------
211
9e0640dc 212 @Override
6256d8ad 213 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) {
9e0640dc
FC
214 }
215
b3dd2736
GB
216 /**
217 * Initialization of an experiment, taking the type, path, traces,
218 * indexPageSize and resource
219 *
220 * @param type
221 * the event type
222 * @param path
223 * the experiment path
224 * @param traces
225 * the experiment set of traces
226 * @param indexPageSize
227 * the experiment index page size
228 * @param resource
229 * the resource associated to the experiment
230 * @since 3.0
231 */
232 public void initExperiment(final Class<? extends ITmfEvent> type, final String path, final ITmfTrace[] traces, final int indexPageSize, IResource resource) {
233 setCacheSize(indexPageSize);
234 setStreamingInterval(0);
235 setParser(this);
7976315b
BH
236 // traces have to be set before super.initialize()
237 fTraces = traces;
b3dd2736
GB
238 try {
239 super.initialize(resource, path, type);
240 } catch (TmfTraceException e) {
241 Activator.logError("Error initializing experiment", e); //$NON-NLS-1$
242 }
243
b3dd2736 244 if (resource != null) {
04ba3554 245 this.synchronizeTraces();
b3dd2736
GB
246 }
247 }
248
a94410d9
MK
249 /**
250 * @since 2.0
251 */
9e0640dc 252 @Override
a94410d9
MK
253 public IStatus validate(final IProject project, final String path) {
254 return Status.OK_STATUS;
9e0640dc
FC
255 }
256
8c8bf09f 257 // ------------------------------------------------------------------------
e31e01e8 258 // Accessors
8c8bf09f
ASL
259 // ------------------------------------------------------------------------
260
f0c0d2c2
AM
261 /**
262 * Get the traces contained in this experiment.
263 *
264 * @return The array of contained traces
265 */
6256d8ad 266 public ITmfTrace[] getTraces() {
a79913eb 267 return fTraces;
8c8bf09f
ASL
268 }
269
8c8bf09f 270 /**
cbdacf03
FC
271 * Returns the timestamp of the event at the requested index. If none,
272 * returns null.
9b749023 273 *
e73a4ba5
GB
274 * @param index
275 * the event index (rank)
0d9a6d76 276 * @return the corresponding event timestamp
3bd46eef 277 * @since 2.0
8c8bf09f 278 */
cbdacf03 279 public ITmfTimestamp getTimestamp(final int index) {
0316808c 280 final ITmfContext context = seekEvent(index);
c32744d6 281 final ITmfEvent event = getNext(context);
4c9f2944 282 context.dispose();
a79913eb 283 return (event != null) ? event.getTimestamp() : null;
8c8bf09f
ASL
284 }
285
49e2f79a
FC
286 // ------------------------------------------------------------------------
287 // Request management
288 // ------------------------------------------------------------------------
289
e6809677
PT
290 /**
291 * @since 2.0
3bd44ac8 292 */
49e2f79a 293 @Override
fd3f1eff 294 public synchronized ITmfContext armRequest(final ITmfEventRequest request) {
9b749023 295
6a953367
BH
296 // Make sure we have something to read from
297 if (fTraces == null) {
298 return null;
299 }
9b749023 300
fd3f1eff
AM
301 if (!TmfTimestamp.BIG_BANG.equals(request.getRange().getStartTime())
302 && request.getIndex() == 0) {
303 final ITmfContext context = seekEvent(request.getRange().getStartTime());
304 request.setStartIndex((int) context.getRank());
49e2f79a 305 return context;
5419a136 306
49e2f79a
FC
307 }
308
5419a136 309 return seekEvent(request.getIndex());
49e2f79a
FC
310 }
311
a79913eb 312 // ------------------------------------------------------------------------
9f584e4c
FC
313 // ITmfTrace trace positioning
314 // ------------------------------------------------------------------------
315
a3db8436
AM
316 /**
317 * @since 3.0
318 */
a79913eb 319 @Override
1e1bef82 320 public synchronized ITmfContext seekEvent(final ITmfLocation location) {
a79913eb 321 // Validate the location
9e0640dc 322 if (location != null && !(location instanceof TmfExperimentLocation)) {
a79913eb 323 return null; // Throw an exception?
9e0640dc
FC
324 }
325 // Make sure we have something to read from
326 if (fTraces == null) {
a79913eb 327 return null;
9e0640dc 328 }
8f50c396 329
ea271da6
PT
330 // Initialize the location array if necessary
331 TmfLocationArray locationArray = ((location == null) ?
332 new TmfLocationArray(fTraces.length) :
333 ((TmfExperimentLocation) location).getLocationInfo());
334
335 ITmfLocation[] locations = locationArray.getLocations();
336 long[] ranks = locationArray.getRanks();
337
a79913eb 338 // Create and populate the context's traces contexts
ea271da6 339 final TmfExperimentContext context = new TmfExperimentContext(fTraces.length);
9b635e61 340
d62bb185 341 // Position the traces
ea271da6 342 long rank = 0;
a79913eb
FC
343 for (int i = 0; i < fTraces.length; i++) {
344 // Get the relevant trace attributes
ea271da6 345 final ITmfContext traceContext = fTraces[i].seekEvent(locations[i]);
07ef7847 346 context.setContext(i, traceContext);
ea271da6 347 traceContext.setRank(ranks[i]);
7f38b742
GB
348 // update location after seek
349 locations[i] = traceContext.getLocation();
07ef7847 350 context.setEvent(i, fTraces[i].getNext(traceContext));
ea271da6 351 rank += ranks[i];
a79913eb 352 }
8f50c396 353
a79913eb 354 // Finalize context
ea271da6 355 context.setLocation(new TmfExperimentLocation(new TmfLocationArray(locations, ranks)));
a79913eb 356 context.setLastTrace(TmfExperimentContext.NO_TRACE);
ea271da6 357 context.setRank(rank);
49e2f79a 358
9b749023 359 return context;
a79913eb 360 }
9f584e4c 361
3bd44ac8
FC
362 // ------------------------------------------------------------------------
363 // ITmfTrace - SeekEvent operations (returning a trace context)
364 // ------------------------------------------------------------------------
365
c76c54bb 366 @Override
0316808c 367 public ITmfContext seekEvent(final double ratio) {
91f6e587 368 final ITmfContext context = seekEvent(Math.round(ratio * getNbEvents()));
c76c54bb
FC
369 return context;
370 }
371
a3db8436
AM
372 /**
373 * @since 3.0
374 */
a79913eb 375 @Override
1e1bef82 376 public double getLocationRatio(final ITmfLocation location) {
9e0640dc 377 if (location instanceof TmfExperimentLocation) {
ea271da6
PT
378 long rank = 0;
379 TmfLocationArray locationArray = ((TmfExperimentLocation) location).getLocationInfo();
380 for (int i = 0; i < locationArray.size(); i++) {
381 rank += locationArray.getRank(i);
382 }
383 return (double) rank / getNbEvents();
9e0640dc
FC
384 }
385 return 0.0;
c76c54bb
FC
386 }
387
a3db8436
AM
388 /**
389 * @since 3.0
390 */
a79913eb 391 @Override
1e1bef82 392 public ITmfLocation getCurrentLocation() {
ea271da6
PT
393 // never used
394 return null;
a79913eb 395 }
c76c54bb 396
9e0640dc
FC
397 // ------------------------------------------------------------------------
398 // ITmfTrace trace positioning
399 // ------------------------------------------------------------------------
400
07671572 401 @Override
6256d8ad 402 public synchronized ITmfEvent parseEvent(final ITmfContext context) {
ea271da6
PT
403 final ITmfContext tmpContext = seekEvent(context.getLocation());
404 final ITmfEvent event = getNext(tmpContext);
07671572
FC
405 return event;
406 }
a79913eb 407
0316808c 408 @Override
6256d8ad 409 public synchronized ITmfEvent getNext(ITmfContext context) {
a79913eb
FC
410
411 // Validate the context
9e0640dc 412 if (!(context instanceof TmfExperimentContext)) {
a79913eb 413 return null; // Throw an exception?
9e0640dc 414 }
0e8c76f8
BH
415
416 // Make sure that we have something to read from
417 if (fTraces == null) {
418 return null;
419 }
420
a87cc4ef 421 TmfExperimentContext expContext = (TmfExperimentContext) context;
a79913eb 422
e73a4ba5
GB
423 // If an event was consumed previously, first get the next one from that
424 // trace
cbdacf03 425 final int lastTrace = expContext.getLastTrace();
a79913eb 426 if (lastTrace != TmfExperimentContext.NO_TRACE) {
07ef7847
AM
427 final ITmfContext traceContext = expContext.getContext(lastTrace);
428 expContext.setEvent(lastTrace, fTraces[lastTrace].getNext(traceContext));
a79913eb 429 expContext.setLastTrace(TmfExperimentContext.NO_TRACE);
a79913eb
FC
430 }
431
432 // Scan the candidate events and identify the "next" trace to read from
433 int trace = TmfExperimentContext.NO_TRACE;
a4115405 434 ITmfTimestamp timestamp = TmfTimestamp.BIG_CRUNCH;
0316808c 435 for (int i = 0; i < fTraces.length; i++) {
07ef7847 436 final ITmfEvent event = expContext.getEvent(i);
a79913eb 437 if (event != null && event.getTimestamp() != null) {
cbdacf03 438 final ITmfTimestamp otherTS = event.getTimestamp();
a79913eb
FC
439 if (otherTS.compareTo(timestamp, true) < 0) {
440 trace = i;
441 timestamp = otherTS;
442 }
443 }
444 }
a87cc4ef 445
6256d8ad 446 ITmfEvent event = null;
07671572 447 if (trace != TmfExperimentContext.NO_TRACE) {
07ef7847 448 event = expContext.getEvent(trace);
408e65d2
FC
449 if (event != null) {
450 updateAttributes(expContext, event.getTimestamp());
408e65d2
FC
451 expContext.increaseRank();
452 expContext.setLastTrace(trace);
07ef7847
AM
453 final ITmfContext traceContext = expContext.getContext(trace);
454 if (traceContext == null) {
455 throw new IllegalStateException();
456 }
17324c9a 457
ea271da6
PT
458 // Update the experiment location
459 TmfLocationArray locationArray = new TmfLocationArray(
460 ((TmfExperimentLocation) expContext.getLocation()).getLocationInfo(),
461 trace, traceContext.getLocation(), traceContext.getRank());
462 expContext.setLocation(new TmfExperimentLocation(locationArray));
17324c9a 463
408e65d2
FC
464 processEvent(event);
465 }
07671572 466 }
a87cc4ef 467
a87cc4ef 468 return event;
a79913eb
FC
469 }
470
66262ad8
BH
471 /**
472 * @since 2.0
473 */
474 @Override
475 public ITmfTimestamp getInitialRangeOffset() {
476 if ((fTraces == null) || (fTraces.length == 0)) {
477 return super.getInitialRangeOffset();
478 }
479
480 ITmfTimestamp initTs = TmfTimestamp.BIG_CRUNCH;
481 for (int i = 0; i < fTraces.length; i++) {
482 ITmfTimestamp ts = fTraces[i].getInitialRangeOffset();
483 if (ts.compareTo(initTs) < 0) {
484 initTs = ts;
485 }
486 }
487 return initTs;
488 }
489
04ba3554
GB
490 /**
491 * Get the path to the folder in the supplementary file where
492 * synchronization-related data can be kept so they are not deleted when the
7f38b742
GB
493 * experiment is synchronized. Analysis involved in synchronization can put
494 * their supplementary files in there so they are preserved after
495 * synchronization.
04ba3554 496 *
7f38b742
GB
497 * If the directory does not exist, it will be created. A return value of
498 * <code>null</code> means either the trace resource does not exist or
499 * supplementary resources cannot be kept.
500 *
501 * @param absolute
502 * If <code>true</code>, it returns the absolute path in the file
503 * system, including the supplementary file path. Otherwise, it
504 * returns only the directory name.
04ba3554
GB
505 * @return The path to the folder where synchronization-related
506 * supplementary files can be kept or <code>null</code> if not
507 * available.
a465519a 508 * @since 3.2
04ba3554 509 */
7f38b742 510 public String getSynchronizationFolder(boolean absolute) {
04ba3554
GB
511 /* Set up the path to the synchronization file we'll use */
512 IResource resource = this.getResource();
513 String syncDirectory = null;
514
515 try {
516 /* get the directory where the file will be stored. */
517 if (resource != null) {
7f38b742 518 String fullDirectory = resource.getPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER);
04ba3554 519 /* Create the synchronization data directory if not present */
7f38b742
GB
520 if (fullDirectory != null) {
521 fullDirectory = fullDirectory + File.separator + SYNCHRONIZATION_DIRECTORY;
522 File syncDir = new File(fullDirectory);
04ba3554
GB
523 syncDir.mkdirs();
524 }
7f38b742
GB
525 if (absolute) {
526 syncDirectory = fullDirectory;
527 } else {
528 syncDirectory = SYNCHRONIZATION_DIRECTORY;
529 }
04ba3554
GB
530 }
531 } catch (CoreException e) {
532 return null;
533 }
534
535 return syncDirectory;
536 }
537
e73a4ba5
GB
538 /**
539 * Synchronizes the traces of an experiment. By default it only tries to
540 * read a synchronization file if it exists
541 *
542 * @return The synchronization object
e73a4ba5
GB
543 * @since 3.0
544 */
04ba3554 545 public synchronized SynchronizationAlgorithm synchronizeTraces() {
e73a4ba5
GB
546 return synchronizeTraces(false);
547 }
548
549 /**
550 * Synchronizes the traces of an experiment.
551 *
552 * @param doSync
553 * Whether to actually synchronize or just try opening a sync
554 * file
555 * @return The synchronization object
e73a4ba5
GB
556 * @since 3.0
557 */
04ba3554 558 public synchronized SynchronizationAlgorithm synchronizeTraces(boolean doSync) {
e73a4ba5 559
7f38b742 560 String syncDirectory = getSynchronizationFolder(true);
e73a4ba5 561
04ba3554 562 final File syncFile = (syncDirectory != null) ? new File(syncDirectory + File.separator + SYNCHRONIZATION_FILE_NAME) : null;
e73a4ba5 563
27213c57 564 final SynchronizationAlgorithm syncAlgo = SynchronizationManager.synchronizeTraces(syncFile, Arrays.asList(fTraces), doSync);
e73a4ba5
GB
565
566 final TmfTraceSynchronizedSignal signal = new TmfTraceSynchronizedSignal(this, syncAlgo);
567
568 /* Broadcast in separate thread to prevent deadlock */
569 new Thread() {
570 @Override
571 public void run() {
572 broadcast(signal);
573 }
574 }.start();
575
576 return syncAlgo;
577 }
578
a79913eb 579 @Override
3b38ea61 580 @SuppressWarnings("nls")
5419a136 581 public synchronized String toString() {
a79913eb
FC
582 return "[TmfExperiment (" + getName() + ")]";
583 }
8c8bf09f
ASL
584
585 // ------------------------------------------------------------------------
9e0640dc 586 // Streaming support
8c8bf09f
ASL
587 // ------------------------------------------------------------------------
588
1b70b6dc 589 private synchronized void initializeStreamingMonitor() {
9e0640dc
FC
590
591 if (fInitialized) {
828e5592 592 return;
9e0640dc 593 }
828e5592
PT
594 fInitialized = true;
595
1b70b6dc 596 if (getStreamingInterval() == 0) {
0316808c 597 final ITmfContext context = seekEvent(0);
cbdacf03 598 final ITmfEvent event = getNext(context);
4c9f2944 599 context.dispose();
9b749023 600 if (event == null) {
1b70b6dc 601 return;
9b749023 602 }
4593bd5b 603 final TmfTimeRange timeRange = new TmfTimeRange(event.getTimestamp(), TmfTimestamp.BIG_CRUNCH);
faa38350 604 final TmfTraceRangeUpdatedSignal signal = new TmfTraceRangeUpdatedSignal(this, this, timeRange);
828e5592
PT
605
606 // Broadcast in separate thread to prevent deadlock
607 new Thread() {
608 @Override
609 public void run() {
610 broadcast(signal);
611 }
612 }.start();
1b70b6dc
PT
613 return;
614 }
615
9e0640dc 616 final Thread thread = new Thread("Streaming Monitor for experiment " + getName()) { //$NON-NLS-1$
bcbea6a6 617 private ITmfTimestamp safeTimestamp = null;
6be2d5cc 618 private ITmfTimestamp lastSafeTimestamp = null;
bcbea6a6 619 private TmfTimeRange timeRange = null;
1b70b6dc
PT
620
621 @Override
622 public void run() {
fc7cd0be 623 while (!executorIsShutdown()) {
9e0640dc 624 if (!getIndexer().isIndexing()) {
a4115405
FC
625 ITmfTimestamp startTimestamp = TmfTimestamp.BIG_CRUNCH;
626 ITmfTimestamp endTimestamp = TmfTimestamp.BIG_BANG;
6256d8ad 627 for (final ITmfTrace trace : fTraces) {
9b749023 628 if (trace.getStartTime().compareTo(startTimestamp) < 0) {
1b70b6dc 629 startTimestamp = trace.getStartTime();
9b749023
AM
630 }
631 if (trace.getStreamingInterval() != 0 && trace.getEndTime().compareTo(endTimestamp) > 0) {
1b70b6dc 632 endTimestamp = trace.getEndTime();
9b749023 633 }
1b70b6dc 634 }
6be2d5cc 635 if (safeTimestamp != null && (lastSafeTimestamp == null || safeTimestamp.compareTo(lastSafeTimestamp, false) > 0)) {
1b70b6dc 636 timeRange = new TmfTimeRange(startTimestamp, safeTimestamp);
6be2d5cc 637 lastSafeTimestamp = safeTimestamp;
9b749023 638 } else {
1b70b6dc 639 timeRange = null;
9b749023 640 }
1b70b6dc
PT
641 safeTimestamp = endTimestamp;
642 if (timeRange != null) {
faa38350
PT
643 final TmfTraceRangeUpdatedSignal signal =
644 new TmfTraceRangeUpdatedSignal(TmfExperiment.this, TmfExperiment.this, timeRange);
1b70b6dc
PT
645 broadcast(signal);
646 }
647 }
648 try {
649 Thread.sleep(getStreamingInterval());
cbdacf03 650 } catch (final InterruptedException e) {
1b70b6dc
PT
651 e.printStackTrace();
652 }
653 }
654 }
655 };
656 thread.start();
657 }
658
1b70b6dc
PT
659 @Override
660 public long getStreamingInterval() {
661 long interval = 0;
6256d8ad 662 for (final ITmfTrace trace : fTraces) {
1b70b6dc 663 interval = Math.max(interval, trace.getStreamingInterval());
9b749023 664 }
1b70b6dc
PT
665 return interval;
666 }
667
8c8bf09f
ASL
668 // ------------------------------------------------------------------------
669 // Signal handlers
670 // ------------------------------------------------------------------------
671
faa38350 672 @Override
9e0640dc 673 @TmfSignalHandler
faa38350 674 public void traceOpened(TmfTraceOpenedSignal signal) {
9e0640dc 675 if (signal.getTrace() == this) {
faa38350 676 initializeStreamingMonitor();
9928ddeb
GB
677
678 /* Initialize the analysis */
679 MultiStatus status = new MultiStatus(Activator.PLUGIN_ID, IStatus.OK, null, null);
680 status.add(executeAnalysis());
681 if (!status.isOK()) {
682 Activator.log(status);
683 }
b5e8ee95 684 TmfTraceManager.refreshSupplementaryFiles(this);
9e0640dc 685 }
a1091415
PT
686 }
687
c4767854
AM
688 /**
689 * @since 3.0
690 */
032ecd45
MAL
691 @Override
692 public synchronized int getCheckpointSize() {
693 int totalCheckpointSize = 0;
694 try {
695 if (fTraces != null) {
696 for (final ITmfTrace trace : fTraces) {
697 if (!(trace instanceof ITmfPersistentlyIndexable)) {
698 return 0;
699 }
700
701 ITmfPersistentlyIndexable persistableIndexTrace = (ITmfPersistentlyIndexable) trace;
702 int currentTraceCheckpointSize = persistableIndexTrace.getCheckpointSize();
703 if (currentTraceCheckpointSize <= 0) {
704 return 0;
705 }
706 totalCheckpointSize += currentTraceCheckpointSize;
7f38b742
GB
707 // each entry in the TmfLocationArray has a rank in addition
708 // of the location
709 totalCheckpointSize += 8;
032ecd45
MAL
710 }
711 }
712 } catch (UnsupportedOperationException e) {
713 return 0;
714 }
715
716 return totalCheckpointSize;
717 }
718
c4767854
AM
719 /**
720 * @since 3.0
721 */
032ecd45
MAL
722 @Override
723 public ITmfLocation restoreLocation(ByteBuffer bufferIn) {
724 ITmfLocation[] locations = new ITmfLocation[fTraces.length];
725 long[] ranks = new long[fTraces.length];
726 for (int i = 0; i < fTraces.length; ++i) {
727 final ITmfTrace trace = fTraces[i];
728 locations[i] = ((ITmfPersistentlyIndexable) trace).restoreLocation(bufferIn);
729 ranks[i] = bufferIn.getLong();
730 }
731 TmfLocationArray arr = new TmfLocationArray(locations, ranks);
732 TmfExperimentLocation l = new TmfExperimentLocation(arr);
733 return l;
734 }
735
4dc47e28 736}
This page took 0.134511 seconds and 5 git commands to generate.