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