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