TMF: Move synchronization supplementary files to a folder
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / 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
9e0640dc 18package org.eclipse.linuxtools.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;
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
04ba3554
GB
69 * @deprecated This file name shouldn't be used directly anymore. Instead,
70 * all synchronization files have been moved to the
71 * {@link #SYNCHRONIZATION_DIRECTORY} folder.
e73a4ba5 72 */
04ba3554 73 @Deprecated
e73a4ba5
GB
74 public final static String SYNCHRONIZATION_FILE_NAME = "synchronization.bin"; //$NON-NLS-1$
75
04ba3554
GB
76 /**
77 * The name of the directory containing trace synchronization data. This
78 * directory typically will be preserved when traces are synchronized.
79 * Analysis involved in synchronization can put their supplementary files in
80 * there so they are not deleted when synchronized traces are copied.
81 *
82 * @since 3.1
83 */
84 public final static String SYNCHRONIZATION_DIRECTORY = "sync_data"; //$NON-NLS-1$
85
9e0640dc
FC
86 /**
87 * The default index page size
88 */
89 public static final int DEFAULT_INDEX_PAGE_SIZE = 5000;
c32744d6 90
8c8bf09f
ASL
91 // ------------------------------------------------------------------------
92 // Attributes
93 // ------------------------------------------------------------------------
94
9e0640dc
FC
95 /**
96 * The set of traces that constitute the experiment
97 */
6256d8ad 98 protected ITmfTrace[] fTraces;
8c8bf09f 99
9e0640dc
FC
100 /**
101 * The set of traces that constitute the experiment
102 */
103 private boolean fInitialized = false;
a1091415 104
8c8bf09f 105 // ------------------------------------------------------------------------
9e0640dc 106 // Construction
8c8bf09f
ASL
107 // ------------------------------------------------------------------------
108
9e0640dc 109 /**
b3dd2736 110 * Default constructor
04ba3554 111 *
b3dd2736
GB
112 * @since 3.0
113 */
114 public TmfExperiment() {
115 super();
116 }
117
118 /**
119 * Constructor with parameters
120 *
e73a4ba5
GB
121 * @param type
122 * the event type
123 * @param id
124 * the experiment id
125 * @param traces
126 * the experiment set of traces
9e0640dc 127 */
6256d8ad 128 public TmfExperiment(final Class<? extends ITmfEvent> type, final String id, final ITmfTrace[] traces) {
99504bb8 129 this(type, id, traces, DEFAULT_INDEX_PAGE_SIZE, null);
96c6806f
PT
130 }
131
99504bb8
GB
132 /**
133 * Constructor of experiment taking type, path, traces and resource
134 *
135 * @param type
136 * the event type
137 * @param id
138 * the experiment id
139 * @param traces
140 * the experiment set of traces
141 * @param resource
142 * the resource associated to the experiment
143 */
144 public TmfExperiment(final Class<? extends ITmfEvent> type, final String id, final ITmfTrace[] traces, IResource resource) {
145 this(type, id, traces, DEFAULT_INDEX_PAGE_SIZE, resource);
146 }
147
8c8bf09f 148 /**
e73a4ba5
GB
149 * @param type
150 * the event type
151 * @param path
152 * the experiment path
153 * @param traces
154 * the experiment set of traces
155 * @param indexPageSize
156 * the experiment index page size
8c8bf09f 157 */
6256d8ad 158 public TmfExperiment(final Class<? extends ITmfEvent> type, final String path, final ITmfTrace[] traces, final int indexPageSize) {
99504bb8
GB
159 this(type, path, traces, indexPageSize, null);
160 }
161
162 /**
163 * Full constructor of an experiment, taking the type, path, traces,
164 * indexPageSize and resource
165 *
166 * @param type
167 * the event type
168 * @param path
169 * the experiment path
170 * @param traces
171 * the experiment set of traces
172 * @param indexPageSize
173 * the experiment index page size
174 * @param resource
175 * the resource associated to the experiment
176 */
177 public TmfExperiment(final Class<? extends ITmfEvent> type, final String path, final ITmfTrace[] traces, final int indexPageSize, IResource resource) {
b3dd2736 178 initExperiment(type, path, traces, indexPageSize, resource);
8c8bf09f 179 }
a79913eb 180
032ecd45
MAL
181 @Override
182 protected ITmfTraceIndexer createIndexer(int interval) {
183 if (getCheckpointSize() > 0) {
184 return new TmfBTreeTraceIndexer(this, interval);
185 }
186 return super.createIndexer(interval);
187 }
188
8c8bf09f 189 /**
ff4ed569 190 * Clears the experiment
8c8bf09f
ASL
191 */
192 @Override
a79913eb
FC
193 public synchronized void dispose() {
194
77551cc2
FC
195 // Clean up the index if applicable
196 if (getIndexer() != null) {
197 getIndexer().dispose();
198 }
b5ee6881 199
a79913eb 200 if (fTraces != null) {
9b749023 201 for (final ITmfTrace trace : fTraces) {
a79913eb 202 trace.dispose();
9b749023 203 }
a79913eb
FC
204 fTraces = null;
205 }
2fb2eb37 206 super.dispose();
8c8bf09f
ASL
207 }
208
9e0640dc
FC
209 // ------------------------------------------------------------------------
210 // ITmfTrace - Initializers
211 // ------------------------------------------------------------------------
212
9e0640dc 213 @Override
6256d8ad 214 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) {
9e0640dc
FC
215 }
216
b3dd2736
GB
217 /**
218 * Initialization of an experiment, taking the type, path, traces,
219 * indexPageSize and resource
220 *
221 * @param type
222 * the event type
223 * @param path
224 * the experiment path
225 * @param traces
226 * the experiment set of traces
227 * @param indexPageSize
228 * the experiment index page size
229 * @param resource
230 * the resource associated to the experiment
231 * @since 3.0
232 */
233 public void initExperiment(final Class<? extends ITmfEvent> type, final String path, final ITmfTrace[] traces, final int indexPageSize, IResource resource) {
234 setCacheSize(indexPageSize);
235 setStreamingInterval(0);
236 setParser(this);
7976315b
BH
237 // traces have to be set before super.initialize()
238 fTraces = traces;
b3dd2736
GB
239 try {
240 super.initialize(resource, path, type);
241 } catch (TmfTraceException e) {
242 Activator.logError("Error initializing experiment", e); //$NON-NLS-1$
243 }
244
b3dd2736 245 if (resource != null) {
04ba3554 246 this.synchronizeTraces();
b3dd2736
GB
247 }
248 }
249
a94410d9
MK
250 /**
251 * @since 2.0
252 */
9e0640dc 253 @Override
a94410d9
MK
254 public IStatus validate(final IProject project, final String path) {
255 return Status.OK_STATUS;
9e0640dc
FC
256 }
257
8c8bf09f 258 // ------------------------------------------------------------------------
e31e01e8 259 // Accessors
8c8bf09f
ASL
260 // ------------------------------------------------------------------------
261
f0c0d2c2
AM
262 /**
263 * Get the traces contained in this experiment.
264 *
265 * @return The array of contained traces
266 */
6256d8ad 267 public ITmfTrace[] getTraces() {
a79913eb 268 return fTraces;
8c8bf09f
ASL
269 }
270
8c8bf09f 271 /**
cbdacf03
FC
272 * Returns the timestamp of the event at the requested index. If none,
273 * returns null.
9b749023 274 *
e73a4ba5
GB
275 * @param index
276 * the event index (rank)
0d9a6d76 277 * @return the corresponding event timestamp
3bd46eef 278 * @since 2.0
8c8bf09f 279 */
cbdacf03 280 public ITmfTimestamp getTimestamp(final int index) {
0316808c 281 final ITmfContext context = seekEvent(index);
c32744d6 282 final ITmfEvent event = getNext(context);
4c9f2944 283 context.dispose();
a79913eb 284 return (event != null) ? event.getTimestamp() : null;
8c8bf09f
ASL
285 }
286
49e2f79a
FC
287 // ------------------------------------------------------------------------
288 // Request management
289 // ------------------------------------------------------------------------
290
e6809677
PT
291 /**
292 * @since 2.0
3bd44ac8 293 */
49e2f79a 294 @Override
fd3f1eff 295 public synchronized ITmfContext armRequest(final ITmfEventRequest request) {
9b749023 296
6a953367
BH
297 // Make sure we have something to read from
298 if (fTraces == null) {
299 return null;
300 }
9b749023 301
fd3f1eff
AM
302 if (!TmfTimestamp.BIG_BANG.equals(request.getRange().getStartTime())
303 && request.getIndex() == 0) {
304 final ITmfContext context = seekEvent(request.getRange().getStartTime());
305 request.setStartIndex((int) context.getRank());
49e2f79a 306 return context;
5419a136 307
49e2f79a
FC
308 }
309
5419a136 310 return seekEvent(request.getIndex());
49e2f79a
FC
311 }
312
a79913eb 313 // ------------------------------------------------------------------------
9f584e4c
FC
314 // ITmfTrace trace positioning
315 // ------------------------------------------------------------------------
316
a3db8436
AM
317 /**
318 * @since 3.0
319 */
a79913eb 320 @Override
1e1bef82 321 public synchronized ITmfContext seekEvent(final ITmfLocation location) {
a79913eb 322 // Validate the location
9e0640dc 323 if (location != null && !(location instanceof TmfExperimentLocation)) {
a79913eb 324 return null; // Throw an exception?
9e0640dc
FC
325 }
326 // Make sure we have something to read from
327 if (fTraces == null) {
a79913eb 328 return null;
9e0640dc 329 }
8f50c396 330
ea271da6
PT
331 // Initialize the location array if necessary
332 TmfLocationArray locationArray = ((location == null) ?
333 new TmfLocationArray(fTraces.length) :
334 ((TmfExperimentLocation) location).getLocationInfo());
335
336 ITmfLocation[] locations = locationArray.getLocations();
337 long[] ranks = locationArray.getRanks();
338
a79913eb 339 // Create and populate the context's traces contexts
ea271da6 340 final TmfExperimentContext context = new TmfExperimentContext(fTraces.length);
9b635e61 341
d62bb185 342 // Position the traces
ea271da6 343 long rank = 0;
a79913eb
FC
344 for (int i = 0; i < fTraces.length; i++) {
345 // Get the relevant trace attributes
ea271da6 346 final ITmfContext traceContext = fTraces[i].seekEvent(locations[i]);
07ef7847 347 context.setContext(i, traceContext);
ea271da6
PT
348 traceContext.setRank(ranks[i]);
349 locations[i] = traceContext.getLocation(); // update location after seek
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
493 * experiment is synchronized. If the directory does not exist, it will be
494 * created. A return value of <code>null</code> means either the trace
495 * resource does not exist or supplementary resources cannot be kept.
496 *
497 * @return The path to the folder where synchronization-related
498 * supplementary files can be kept or <code>null</code> if not
499 * available.
500 * @since 3.1
501 */
502 public String getSynchronizationFolder() {
503 /* Set up the path to the synchronization file we'll use */
504 IResource resource = this.getResource();
505 String syncDirectory = null;
506
507 try {
508 /* get the directory where the file will be stored. */
509 if (resource != null) {
510 syncDirectory = resource.getPersistentProperty(TmfCommonConstants.TRACE_SUPPLEMENTARY_FOLDER);
511 /* Create the synchronization data directory if not present */
512 if (syncDirectory != null) {
513 syncDirectory = syncDirectory + File.separator + SYNCHRONIZATION_DIRECTORY;
514 File syncDir = new File(syncDirectory);
515 syncDir.mkdirs();
516 }
517 }
518 } catch (CoreException e) {
519 return null;
520 }
521
522 return syncDirectory;
523 }
524
e73a4ba5
GB
525 /**
526 * Synchronizes the traces of an experiment. By default it only tries to
527 * read a synchronization file if it exists
528 *
529 * @return The synchronization object
e73a4ba5
GB
530 * @since 3.0
531 */
04ba3554 532 public synchronized SynchronizationAlgorithm synchronizeTraces() {
e73a4ba5
GB
533 return synchronizeTraces(false);
534 }
535
536 /**
537 * Synchronizes the traces of an experiment.
538 *
539 * @param doSync
540 * Whether to actually synchronize or just try opening a sync
541 * file
542 * @return The synchronization object
e73a4ba5
GB
543 * @since 3.0
544 */
04ba3554 545 public synchronized SynchronizationAlgorithm synchronizeTraces(boolean doSync) {
e73a4ba5 546
04ba3554 547 String syncDirectory = getSynchronizationFolder();
e73a4ba5 548
04ba3554 549 final File syncFile = (syncDirectory != null) ? new File(syncDirectory + File.separator + SYNCHRONIZATION_FILE_NAME) : null;
e73a4ba5 550
27213c57 551 final SynchronizationAlgorithm syncAlgo = SynchronizationManager.synchronizeTraces(syncFile, Arrays.asList(fTraces), doSync);
e73a4ba5
GB
552
553 final TmfTraceSynchronizedSignal signal = new TmfTraceSynchronizedSignal(this, syncAlgo);
554
555 /* Broadcast in separate thread to prevent deadlock */
556 new Thread() {
557 @Override
558 public void run() {
559 broadcast(signal);
560 }
561 }.start();
562
563 return syncAlgo;
564 }
565
a79913eb 566 @Override
3b38ea61 567 @SuppressWarnings("nls")
5419a136 568 public synchronized String toString() {
a79913eb
FC
569 return "[TmfExperiment (" + getName() + ")]";
570 }
8c8bf09f
ASL
571
572 // ------------------------------------------------------------------------
9e0640dc 573 // Streaming support
8c8bf09f
ASL
574 // ------------------------------------------------------------------------
575
1b70b6dc 576 private synchronized void initializeStreamingMonitor() {
9e0640dc
FC
577
578 if (fInitialized) {
828e5592 579 return;
9e0640dc 580 }
828e5592
PT
581 fInitialized = true;
582
1b70b6dc 583 if (getStreamingInterval() == 0) {
0316808c 584 final ITmfContext context = seekEvent(0);
cbdacf03 585 final ITmfEvent event = getNext(context);
4c9f2944 586 context.dispose();
9b749023 587 if (event == null) {
1b70b6dc 588 return;
9b749023 589 }
4593bd5b 590 final TmfTimeRange timeRange = new TmfTimeRange(event.getTimestamp(), TmfTimestamp.BIG_CRUNCH);
faa38350 591 final TmfTraceRangeUpdatedSignal signal = new TmfTraceRangeUpdatedSignal(this, this, timeRange);
828e5592
PT
592
593 // Broadcast in separate thread to prevent deadlock
594 new Thread() {
595 @Override
596 public void run() {
597 broadcast(signal);
598 }
599 }.start();
1b70b6dc
PT
600 return;
601 }
602
9e0640dc 603 final Thread thread = new Thread("Streaming Monitor for experiment " + getName()) { //$NON-NLS-1$
bcbea6a6 604 private ITmfTimestamp safeTimestamp = null;
6be2d5cc 605 private ITmfTimestamp lastSafeTimestamp = null;
bcbea6a6 606 private TmfTimeRange timeRange = null;
1b70b6dc
PT
607
608 @Override
609 public void run() {
fc7cd0be 610 while (!executorIsShutdown()) {
9e0640dc 611 if (!getIndexer().isIndexing()) {
a4115405
FC
612 ITmfTimestamp startTimestamp = TmfTimestamp.BIG_CRUNCH;
613 ITmfTimestamp endTimestamp = TmfTimestamp.BIG_BANG;
6256d8ad 614 for (final ITmfTrace trace : fTraces) {
9b749023 615 if (trace.getStartTime().compareTo(startTimestamp) < 0) {
1b70b6dc 616 startTimestamp = trace.getStartTime();
9b749023
AM
617 }
618 if (trace.getStreamingInterval() != 0 && trace.getEndTime().compareTo(endTimestamp) > 0) {
1b70b6dc 619 endTimestamp = trace.getEndTime();
9b749023 620 }
1b70b6dc 621 }
6be2d5cc 622 if (safeTimestamp != null && (lastSafeTimestamp == null || safeTimestamp.compareTo(lastSafeTimestamp, false) > 0)) {
1b70b6dc 623 timeRange = new TmfTimeRange(startTimestamp, safeTimestamp);
6be2d5cc 624 lastSafeTimestamp = safeTimestamp;
9b749023 625 } else {
1b70b6dc 626 timeRange = null;
9b749023 627 }
1b70b6dc
PT
628 safeTimestamp = endTimestamp;
629 if (timeRange != null) {
faa38350
PT
630 final TmfTraceRangeUpdatedSignal signal =
631 new TmfTraceRangeUpdatedSignal(TmfExperiment.this, TmfExperiment.this, timeRange);
1b70b6dc
PT
632 broadcast(signal);
633 }
634 }
635 try {
636 Thread.sleep(getStreamingInterval());
cbdacf03 637 } catch (final InterruptedException e) {
1b70b6dc
PT
638 e.printStackTrace();
639 }
640 }
641 }
642 };
643 thread.start();
644 }
645
1b70b6dc
PT
646 @Override
647 public long getStreamingInterval() {
648 long interval = 0;
6256d8ad 649 for (final ITmfTrace trace : fTraces) {
1b70b6dc 650 interval = Math.max(interval, trace.getStreamingInterval());
9b749023 651 }
1b70b6dc
PT
652 return interval;
653 }
654
8c8bf09f
ASL
655 // ------------------------------------------------------------------------
656 // Signal handlers
657 // ------------------------------------------------------------------------
658
faa38350 659 @Override
9e0640dc 660 @TmfSignalHandler
faa38350 661 public void traceOpened(TmfTraceOpenedSignal signal) {
9e0640dc 662 if (signal.getTrace() == this) {
faa38350 663 initializeStreamingMonitor();
9928ddeb
GB
664
665 /* Initialize the analysis */
666 MultiStatus status = new MultiStatus(Activator.PLUGIN_ID, IStatus.OK, null, null);
667 status.add(executeAnalysis());
668 if (!status.isOK()) {
669 Activator.log(status);
670 }
b5e8ee95 671 TmfTraceManager.refreshSupplementaryFiles(this);
9e0640dc 672 }
a1091415
PT
673 }
674
c4767854
AM
675 /**
676 * @since 3.0
677 */
032ecd45
MAL
678 @Override
679 public synchronized int getCheckpointSize() {
680 int totalCheckpointSize = 0;
681 try {
682 if (fTraces != null) {
683 for (final ITmfTrace trace : fTraces) {
684 if (!(trace instanceof ITmfPersistentlyIndexable)) {
685 return 0;
686 }
687
688 ITmfPersistentlyIndexable persistableIndexTrace = (ITmfPersistentlyIndexable) trace;
689 int currentTraceCheckpointSize = persistableIndexTrace.getCheckpointSize();
690 if (currentTraceCheckpointSize <= 0) {
691 return 0;
692 }
693 totalCheckpointSize += currentTraceCheckpointSize;
694 totalCheckpointSize += 8; // each entry in the TmfLocationArray has a rank in addition of the location
695 }
696 }
697 } catch (UnsupportedOperationException e) {
698 return 0;
699 }
700
701 return totalCheckpointSize;
702 }
703
c4767854
AM
704 /**
705 * @since 3.0
706 */
032ecd45
MAL
707 @Override
708 public ITmfLocation restoreLocation(ByteBuffer bufferIn) {
709 ITmfLocation[] locations = new ITmfLocation[fTraces.length];
710 long[] ranks = new long[fTraces.length];
711 for (int i = 0; i < fTraces.length; ++i) {
712 final ITmfTrace trace = fTraces[i];
713 locations[i] = ((ITmfPersistentlyIndexable) trace).restoreLocation(bufferIn);
714 ranks[i] = bufferIn.getLong();
715 }
716 TmfLocationArray arr = new TmfLocationArray(locations, ranks);
717 TmfExperimentLocation l = new TmfExperimentLocation(arr);
718 return l;
719 }
720
4dc47e28 721}
This page took 0.152205 seconds and 5 git commands to generate.