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