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