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