Commit | Line | Data |
---|---|---|
8c8bf09f ASL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2010 Ericsson | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.experiment; | |
14 | ||
9f584e4c | 15 | import java.util.Collections; |
8c8bf09f ASL |
16 | import java.util.Vector; |
17 | ||
e31e01e8 FC |
18 | import org.eclipse.core.runtime.IProgressMonitor; |
19 | import org.eclipse.core.runtime.IStatus; | |
20 | import org.eclipse.core.runtime.Status; | |
21 | import org.eclipse.core.runtime.jobs.Job; | |
fc6ccf6f | 22 | import org.eclipse.linuxtools.tmf.component.TmfEventProvider; |
8c8bf09f ASL |
23 | import org.eclipse.linuxtools.tmf.event.TmfEvent; |
24 | import org.eclipse.linuxtools.tmf.event.TmfTimeRange; | |
25 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; | |
2fb2eb37 FC |
26 | import org.eclipse.linuxtools.tmf.request.ITmfDataRequest; |
27 | import org.eclipse.linuxtools.tmf.request.ITmfEventRequest; | |
ff4ed569 FC |
28 | import org.eclipse.linuxtools.tmf.signal.TmfExperimentSelectedSignal; |
29 | import org.eclipse.linuxtools.tmf.signal.TmfExperimentUpdatedSignal; | |
9f584e4c | 30 | import org.eclipse.linuxtools.tmf.signal.TmfRangeSynchSignal; |
8c8bf09f | 31 | import org.eclipse.linuxtools.tmf.signal.TmfSignalHandler; |
ff4ed569 | 32 | import org.eclipse.linuxtools.tmf.signal.TmfTraceUpdatedSignal; |
9f584e4c FC |
33 | import org.eclipse.linuxtools.tmf.trace.ITmfContext; |
34 | import org.eclipse.linuxtools.tmf.trace.ITmfLocation; | |
8c8bf09f | 35 | import org.eclipse.linuxtools.tmf.trace.ITmfTrace; |
9f584e4c FC |
36 | import org.eclipse.linuxtools.tmf.trace.TmfCheckpoint; |
37 | import org.eclipse.linuxtools.tmf.trace.TmfContext; | |
8c8bf09f ASL |
38 | |
39 | /** | |
40 | * <b><u>TmfExperiment</u></b> | |
41 | * <p> | |
42 | * TmfExperiment presents a time-ordered, unified view of a set of TmfTraces | |
43 | * that are part of a tracing experiment. | |
44 | * <p> | |
45 | */ | |
fc6ccf6f | 46 | public class TmfExperiment<T extends TmfEvent> extends TmfEventProvider<T> implements ITmfTrace { |
8c8bf09f ASL |
47 | |
48 | // ------------------------------------------------------------------------ | |
49 | // Attributes | |
50 | // ------------------------------------------------------------------------ | |
51 | ||
52 | // The currently selected experiment | |
cbd4ad82 | 53 | private static TmfExperiment<?> fCurrentExperiment = null; |
e31e01e8 FC |
54 | |
55 | // The experiment ID | |
56 | private String fExperimentId; | |
8c8bf09f | 57 | |
9f584e4c FC |
58 | // The set of traces that constitute the experiment |
59 | private ITmfTrace[] fTraces; | |
8c8bf09f ASL |
60 | |
61 | // The total number of events | |
9f584e4c | 62 | private long fNbEvents; |
8c8bf09f ASL |
63 | |
64 | // The experiment time range | |
65 | private TmfTimeRange fTimeRange; | |
66 | ||
67 | // The experiment reference timestamp (default: BigBang) | |
68 | private TmfTimestamp fEpoch; | |
69 | ||
9f584e4c FC |
70 | // The experiment index |
71 | private Vector<TmfCheckpoint> fCheckpoints = new Vector<TmfCheckpoint>(); | |
72 | ||
8c8bf09f ASL |
73 | // ------------------------------------------------------------------------ |
74 | // Constructors | |
75 | // ------------------------------------------------------------------------ | |
76 | ||
77 | /** | |
78 | * @param type | |
79 | * @param id | |
80 | * @param traces | |
81 | * @param epoch | |
82 | * @param indexPageSize | |
83 | */ | |
84 | public TmfExperiment(Class<T> type, String id, ITmfTrace[] traces, TmfTimestamp epoch, int indexPageSize) { | |
e31e01e8 | 85 | super(type); |
8c8bf09f | 86 | |
e31e01e8 | 87 | fExperimentId = id; |
9f584e4c | 88 | fTraces = traces; |
8c8bf09f ASL |
89 | fEpoch = epoch; |
90 | fIndexPageSize = indexPageSize; | |
91 | ||
92 | updateNbEvents(); | |
93 | updateTimeRange(); | |
e31e01e8 | 94 | } |
8c8bf09f ASL |
95 | |
96 | /** | |
97 | * @param type | |
98 | * @param id | |
99 | * @param traces | |
100 | */ | |
101 | public TmfExperiment(Class<T> type, String id, ITmfTrace[] traces) { | |
102 | this(type, id, traces, TmfTimestamp.Zero, DEFAULT_INDEX_PAGE_SIZE); | |
103 | } | |
104 | ||
105 | /** | |
106 | * @param type | |
107 | * @param id | |
108 | * @param traces | |
109 | * @param indexPageSize | |
110 | */ | |
111 | public TmfExperiment(Class<T> type, String id, ITmfTrace[] traces, int indexPageSize) { | |
112 | this(type, id, traces, TmfTimestamp.Zero, indexPageSize); | |
113 | } | |
377f1ad8 WB |
114 | |
115 | ||
116 | public TmfExperiment(TmfExperiment<T> oldExperiment) { | |
117 | super(oldExperiment.fType); | |
118 | ||
119 | this.fExperimentId = oldExperiment.fExperimentId; | |
120 | this.fEpoch = oldExperiment.fEpoch; | |
121 | ||
122 | this.fIndexPageSize = oldExperiment.fIndexPageSize; | |
123 | ||
124 | this.fTraces = new ITmfTrace[oldExperiment.fTraces.length]; | |
125 | ||
126 | for ( int x=0; x<oldExperiment.fTraces.length; x++) { | |
127 | this.fTraces[x] = oldExperiment.fTraces[x].createTraceCopy(); | |
128 | } | |
129 | ||
130 | // replace updateNbEvents() | |
131 | this.fNbEvents = oldExperiment.fNbEvents; | |
132 | ||
133 | // replace updateTimeRange() | |
134 | this.fTimeRange = oldExperiment.fTimeRange; | |
135 | } | |
136 | ||
137 | public TmfExperiment<T> createTraceCopy() { | |
138 | return new TmfExperiment<T>(this); | |
139 | } | |
140 | ||
141 | ||
8c8bf09f | 142 | /** |
ff4ed569 | 143 | * Clears the experiment |
8c8bf09f ASL |
144 | */ |
145 | @Override | |
2fb2eb37 | 146 | public void dispose() { |
ff4ed569 FC |
147 | for (ITmfTrace trace : fTraces) { |
148 | trace.dispose(); | |
149 | } | |
9f584e4c FC |
150 | fTraces = null; |
151 | fCheckpoints.clear(); | |
2fb2eb37 | 152 | super.dispose(); |
8c8bf09f ASL |
153 | } |
154 | ||
cbd4ad82 FC |
155 | private static void setCurrentExperiment(TmfExperiment<?> experiment) { |
156 | fCurrentExperiment = experiment; | |
157 | } | |
158 | ||
9f584e4c | 159 | // ------------------------------------------------------------------------ |
cbd4ad82 | 160 | // ITmfTrace |
9f584e4c FC |
161 | // ------------------------------------------------------------------------ |
162 | ||
163 | public String getPath() { | |
164 | return null; | |
165 | } | |
166 | ||
fc6ccf6f | 167 | @Override |
9f584e4c FC |
168 | public String getName() { |
169 | return fExperimentId; | |
170 | } | |
171 | ||
172 | public long getNbEvents() { | |
173 | return fNbEvents; | |
174 | } | |
175 | ||
54d55ced FC |
176 | public int getCacheSize() { |
177 | return fIndexPageSize; | |
178 | } | |
179 | ||
9f584e4c FC |
180 | public TmfTimeRange getTimeRange() { |
181 | return fTimeRange; | |
182 | } | |
183 | ||
184 | public TmfTimestamp getStartTime() { | |
185 | return fTimeRange.getStartTime(); | |
186 | } | |
187 | ||
188 | public TmfTimestamp getEndTime() { | |
189 | return fTimeRange.getEndTime(); | |
190 | } | |
191 | ||
54d55ced FC |
192 | public Vector<TmfCheckpoint> getCheckpoints() { |
193 | return fCheckpoints; | |
194 | } | |
195 | ||
8c8bf09f | 196 | // ------------------------------------------------------------------------ |
e31e01e8 | 197 | // Accessors |
8c8bf09f ASL |
198 | // ------------------------------------------------------------------------ |
199 | ||
e31e01e8 FC |
200 | public static TmfExperiment<?> getCurrentExperiment() { |
201 | return fCurrentExperiment; | |
8c8bf09f ASL |
202 | } |
203 | ||
8c8bf09f ASL |
204 | public TmfTimestamp getEpoch() { |
205 | return fEpoch; | |
206 | } | |
207 | ||
9f584e4c FC |
208 | public ITmfTrace[] getTraces() { |
209 | return fTraces; | |
8c8bf09f ASL |
210 | } |
211 | ||
212 | /** | |
213 | * Returns the rank of the first event with the requested timestamp. | |
214 | * If none, returns the index of the next event (if any). | |
215 | * | |
85fb0e54 | 216 | * @param timestamp |
8c8bf09f ASL |
217 | * @return |
218 | */ | |
85fb0e54 FC |
219 | public long getRank(TmfTimestamp timestamp) { |
220 | TmfExperimentContext context = seekEvent(timestamp); | |
8c8bf09f ASL |
221 | return context.getRank(); |
222 | } | |
223 | ||
224 | /** | |
225 | * Returns the timestamp of the event at the requested index. | |
226 | * If none, returns null. | |
227 | * | |
228 | * @param index | |
229 | * @return | |
230 | */ | |
231 | public TmfTimestamp getTimestamp(int index) { | |
85fb0e54 | 232 | TmfExperimentContext context = seekEvent(index); |
7f407ead | 233 | TmfEvent event = getNextEvent(context); |
85fb0e54 | 234 | return (event != null) ? event.getTimestamp() : null; |
8c8bf09f ASL |
235 | } |
236 | ||
237 | // ------------------------------------------------------------------------ | |
238 | // Operators | |
239 | // ------------------------------------------------------------------------ | |
240 | ||
241 | /** | |
242 | * Update the total number of events | |
243 | */ | |
244 | private void updateNbEvents() { | |
245 | int nbEvents = 0; | |
246 | for (ITmfTrace trace : fTraces) { | |
247 | nbEvents += trace.getNbEvents(); | |
248 | } | |
249 | fNbEvents = nbEvents; | |
250 | } | |
251 | ||
252 | /** | |
253 | * Update the global time range | |
254 | */ | |
255 | private void updateTimeRange() { | |
256 | TmfTimestamp startTime = fTimeRange != null ? fTimeRange.getStartTime() : TmfTimestamp.BigCrunch; | |
257 | TmfTimestamp endTime = fTimeRange != null ? fTimeRange.getEndTime() : TmfTimestamp.BigBang; | |
258 | ||
259 | for (ITmfTrace trace : fTraces) { | |
8f50c396 FC |
260 | if (trace.getNbEvents() > 0) { |
261 | TmfTimestamp traceStartTime = trace.getStartTime(); | |
262 | if (traceStartTime.compareTo(startTime, true) < 0) | |
263 | startTime = traceStartTime; | |
264 | ||
265 | TmfTimestamp traceEndTime = trace.getEndTime(); | |
266 | if (traceEndTime.compareTo(endTime, true) > 0) | |
267 | endTime = traceEndTime; | |
268 | } | |
269 | fTimeRange = new TmfTimeRange(startTime, endTime); | |
8c8bf09f | 270 | } |
8c8bf09f ASL |
271 | } |
272 | ||
273 | // ------------------------------------------------------------------------ | |
274 | // TmfProvider | |
275 | // ------------------------------------------------------------------------ | |
276 | ||
277 | @Override | |
2fb2eb37 | 278 | public ITmfContext armRequest(ITmfDataRequest<T> request) { |
2fb2eb37 FC |
279 | TmfTimestamp timestamp = (request instanceof ITmfEventRequest<?>) ? |
280 | ((ITmfEventRequest<T>) request).getRange().getStartTime() : null; | |
9f584e4c FC |
281 | TmfExperimentContext context = (timestamp != null) ? |
282 | seekEvent(timestamp) : seekEvent(request.getIndex()); | |
8c8bf09f ASL |
283 | return context; |
284 | } | |
285 | ||
286 | @SuppressWarnings("unchecked") | |
287 | @Override | |
288 | public T getNext(ITmfContext context) { | |
289 | if (context instanceof TmfExperimentContext) { | |
290 | return (T) getNextEvent((TmfExperimentContext) context); | |
291 | } | |
292 | return null; | |
293 | } | |
294 | ||
9f584e4c FC |
295 | // ------------------------------------------------------------------------ |
296 | // ITmfTrace trace positioning | |
297 | // ------------------------------------------------------------------------ | |
298 | ||
299 | // Returns a brand new context based on the location provided | |
8f50c396 | 300 | // and initializes the event queues |
452ad365 | 301 | public TmfExperimentContext seekLocation(ITmfLocation<?> location) { |
8f50c396 FC |
302 | |
303 | // Validate the location | |
304 | if (location != null && !(location instanceof TmfExperimentLocation)) { | |
305 | return null; // Throw an exception? | |
9f584e4c | 306 | } |
8f50c396 FC |
307 | |
308 | // Instantiate the location | |
309 | TmfExperimentLocation expLocation = (location == null) | |
310 | ? new TmfExperimentLocation(new ITmfLocation<?>[fTraces.length], new long[fTraces.length]) | |
311 | : (TmfExperimentLocation) location.clone(); | |
312 | ||
313 | // Create and populate the context's traces contexts | |
314 | TmfExperimentContext context = new TmfExperimentContext(fTraces, new TmfContext[fTraces.length]); | |
315 | long rank = 0; | |
316 | for (int i = 0; i < fTraces.length; i++) { | |
317 | // Get the relevant trace attributes | |
318 | ITmfLocation<?> traceLocation = expLocation.getLocation()[i]; | |
319 | long traceRank = expLocation.getRanks()[i]; | |
320 | ||
321 | // Set the corresponding sub-context | |
322 | context.getContexts()[i] = fTraces[i].seekLocation(traceLocation); | |
323 | context.getContexts()[i].setRank(traceRank); | |
324 | rank += traceRank; | |
325 | ||
326 | // Set the trace location and read the corresponding event | |
327 | expLocation.getLocation()[i] = context.getContexts()[i].getLocation(); | |
328 | context.getEvents()[i] = fTraces[i].getNextEvent(context.getContexts()[i]); | |
329 | } | |
330 | ||
331 | // Finalize context | |
332 | context.setLocation(expLocation); | |
333 | context.setRank(rank); | |
334 | context.setLastTrace(TmfExperimentContext.NO_TRACE); | |
335 | return context; | |
9f584e4c FC |
336 | } |
337 | ||
54d55ced FC |
338 | /* (non-Javadoc) |
339 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.event.TmfTimestamp) | |
340 | */ | |
9f584e4c | 341 | public TmfExperimentContext seekEvent(TmfTimestamp timestamp) { |
8c8bf09f | 342 | |
9f584e4c FC |
343 | if (timestamp == null) { |
344 | timestamp = TmfTimestamp.BigBang; | |
345 | } | |
346 | ||
347 | // First, find the right checkpoint | |
348 | int index = Collections.binarySearch(fCheckpoints, new TmfCheckpoint(timestamp, null)); | |
349 | ||
350 | // In the very likely case that the checkpoint was not found, bsearch | |
351 | // returns its negated would-be location (not an offset...). From that | |
352 | // index, we can then position the stream and get the event. | |
353 | if (index < 0) { | |
354 | index = Math.max(0, -(index + 2)); | |
355 | } | |
356 | ||
357 | // Position the experiment at the checkpoint | |
452ad365 | 358 | ITmfLocation<?> location; |
9f584e4c FC |
359 | synchronized (fCheckpoints) { |
360 | if (fCheckpoints.size() > 0) { | |
361 | if (index >= fCheckpoints.size()) { | |
362 | index = fCheckpoints.size() - 1; | |
363 | } | |
364 | location = fCheckpoints.elementAt(index).getLocation(); | |
365 | } | |
366 | else { | |
367 | location = null; | |
368 | } | |
369 | } | |
370 | ||
85fb0e54 | 371 | TmfExperimentContext context = seekLocation(location); |
cbd4ad82 | 372 | context.setRank((long) index * fIndexPageSize); |
9f584e4c | 373 | |
54d55ced FC |
374 | // And locate the event |
375 | TmfExperimentContext nextEventContext = new TmfExperimentContext(context); | |
376 | TmfEvent event = getNextEvent(nextEventContext); | |
9f584e4c | 377 | while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) { |
8f50c396 | 378 | context = new TmfExperimentContext(nextEventContext); |
54d55ced | 379 | event = getNextEvent(nextEventContext); |
9f584e4c | 380 | } |
8f50c396 | 381 | context.setLastTrace(TmfExperimentContext.NO_TRACE); |
9f584e4c | 382 | |
85fb0e54 | 383 | return context; |
9f584e4c | 384 | } |
8c8bf09f | 385 | |
54d55ced FC |
386 | /* (non-Javadoc) |
387 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(long) | |
388 | */ | |
9f584e4c FC |
389 | public TmfExperimentContext seekEvent(long rank) { |
390 | ||
54d55ced FC |
391 | // Position the stream at the previous checkpoint |
392 | int index = (int) rank / fIndexPageSize; | |
393 | ITmfLocation<?> location; | |
394 | synchronized (fCheckpoints) { | |
395 | if (fCheckpoints.size() == 0) { | |
396 | location = null; | |
397 | } | |
398 | else { | |
399 | if (index >= fCheckpoints.size()) { | |
400 | index = fCheckpoints.size() - 1; | |
401 | } | |
402 | location = fCheckpoints.elementAt(index).getLocation(); | |
403 | } | |
404 | } | |
e31e01e8 | 405 | |
54d55ced | 406 | TmfExperimentContext context = seekLocation(location); |
cbd4ad82 | 407 | long pos = (long) index * fIndexPageSize; |
54d55ced FC |
408 | context.setRank(pos); |
409 | ||
410 | // And locate the event | |
411 | TmfExperimentContext nextEventContext = new TmfExperimentContext(context); | |
412 | TmfEvent event = getNextEvent(nextEventContext); | |
413 | while (event != null && pos++ < rank) { | |
414 | event = getNextEvent(nextEventContext); | |
415 | context = new TmfExperimentContext(nextEventContext); | |
416 | if (event != null) context.updateRank(-1); | |
417 | } | |
8f50c396 | 418 | context.setLastTrace(TmfExperimentContext.NO_TRACE); |
9f584e4c | 419 | |
54d55ced | 420 | return context; |
8c8bf09f ASL |
421 | } |
422 | ||
423 | /** | |
424 | * Scan the next events from all traces and return the next one | |
425 | * in chronological order. | |
426 | * | |
427 | * @param context | |
428 | * @return | |
429 | */ | |
9f584e4c | 430 | public synchronized TmfEvent getNextEvent(TmfContext context) { |
54d55ced | 431 | |
8f50c396 FC |
432 | // Validate the context |
433 | if (!(context instanceof TmfExperimentContext)) { | |
434 | return null; // Throw an exception? | |
435 | } | |
54d55ced | 436 | |
8f50c396 FC |
437 | TmfExperimentContext expContext = (TmfExperimentContext) context; |
438 | ||
439 | // If an event was consumed previously, get the next one from that trace | |
440 | int lastTrace = expContext.getLastTrace(); | |
441 | if (lastTrace != TmfExperimentContext.NO_TRACE) { | |
442 | TmfContext traceContext = expContext.getContexts()[lastTrace]; | |
443 | expContext.getEvents()[lastTrace] = expContext.getTraces()[lastTrace].getNextEvent(traceContext); | |
444 | } | |
445 | ||
446 | // Scan the candidate events and identify the "next" trace to read from | |
447 | int trace = TmfExperimentContext.NO_TRACE; | |
448 | TmfTimestamp timestamp = TmfTimestamp.BigCrunch; | |
449 | for (int i = 0; i < expContext.getTraces().length; i++) { | |
450 | TmfEvent event = expContext.getEvents()[i]; | |
451 | if (event != null && event.getTimestamp() != null) { | |
452 | TmfTimestamp otherTS = event.getTimestamp(); | |
453 | if (otherTS.compareTo(timestamp, true) < 0) { | |
454 | trace = i; | |
455 | timestamp = otherTS; | |
8c8bf09f ASL |
456 | } |
457 | } | |
458 | } | |
8f50c396 FC |
459 | |
460 | // Update the experiment context and set the "next" event | |
461 | TmfEvent event = null; | |
462 | if (trace >= 0) { | |
463 | expContext.setLastTrace(trace); | |
464 | expContext.updateRank(1); | |
465 | TmfExperimentLocation expLocation = (TmfExperimentLocation) expContext.getLocation(); | |
466 | TmfContext traceContext = expContext.getContexts()[trace]; | |
467 | expLocation.getLocation()[trace] = traceContext.getLocation().clone(); | |
468 | expLocation.getRanks()[trace] = traceContext.getRank(); | |
469 | event = expContext.getEvents()[trace]; | |
470 | } | |
471 | ||
472 | return event; | |
8c8bf09f ASL |
473 | } |
474 | ||
54d55ced FC |
475 | /* (non-Javadoc) |
476 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#parseEvent(org.eclipse.linuxtools.tmf.trace.TmfContext) | |
477 | */ | |
9f584e4c | 478 | public TmfEvent parseEvent(TmfContext context) { |
54d55ced | 479 | |
85fb0e54 FC |
480 | if (context instanceof TmfExperimentContext) { |
481 | TmfExperimentContext expContext = (TmfExperimentContext) context; | |
54d55ced FC |
482 | int lastTrace = expContext.getLastTrace(); |
483 | if (lastTrace != -1) { | |
484 | TmfContext traceContext = expContext.getContexts()[lastTrace]; | |
8f50c396 | 485 | expContext.getEvents()[lastTrace] = expContext.getTraces()[lastTrace].getNextEvent(traceContext); |
54d55ced FC |
486 | expContext.updateRank(1); |
487 | TmfExperimentLocation expLocation = (TmfExperimentLocation) expContext.getLocation(); | |
488 | expLocation.getLocation()[lastTrace] = traceContext.getLocation().clone(); | |
489 | } | |
490 | ||
85fb0e54 FC |
491 | int trace = -1; |
492 | TmfTimestamp timestamp = TmfTimestamp.BigCrunch; | |
493 | for (int i = 0; i < expContext.getTraces().length; i++) { | |
494 | if (expContext.getEvents()[i] != null) { | |
495 | if (expContext.getEvents()[i].getTimestamp() != null) { | |
496 | TmfTimestamp otherTS = expContext.getEvents()[i].getTimestamp(); | |
497 | if (otherTS.compareTo(timestamp, true) < 0) { | |
498 | trace = i; | |
499 | timestamp = otherTS; | |
500 | } | |
501 | } | |
502 | } | |
503 | } | |
504 | if (trace >= 0) { | |
8f50c396 | 505 | expContext.setLastTrace(TmfExperimentContext.NO_TRACE); |
85fb0e54 FC |
506 | return expContext.getEvents()[trace]; |
507 | } | |
508 | } | |
54d55ced FC |
509 | |
510 | return null; | |
8c8bf09f ASL |
511 | } |
512 | ||
513 | /* (non-Javadoc) | |
514 | * @see java.lang.Object#toString() | |
515 | */ | |
516 | @Override | |
517 | public String toString() { | |
e31e01e8 | 518 | return "[TmfExperiment (" + fExperimentId + ")]"; |
8c8bf09f ASL |
519 | } |
520 | ||
521 | // ------------------------------------------------------------------------ | |
522 | // Indexing | |
523 | // ------------------------------------------------------------------------ | |
524 | ||
525 | /* | |
526 | * The experiment holds the globally ordered events of its set of traces. | |
527 | * It is expected to provide access to each individual event by index i.e. | |
9f584e4c | 528 | * it must be possible to request the Nth event of the experiment. |
8c8bf09f ASL |
529 | * |
530 | * The purpose of the index is to keep the information needed to rapidly | |
531 | * restore the traces contexts at regular intervals (every INDEX_PAGE_SIZE | |
532 | * event). | |
533 | */ | |
534 | ||
535 | // The index page size | |
536 | private static final int DEFAULT_INDEX_PAGE_SIZE = 1000; | |
e31e01e8 | 537 | private final int fIndexPageSize; |
8c8bf09f | 538 | |
e31e01e8 FC |
539 | // Indicates that an indexing job is already running |
540 | private Boolean fIndexing = false; | |
541 | private Boolean fIndexed = false; | |
8c8bf09f | 542 | |
e31e01e8 FC |
543 | // The indexing job |
544 | private IndexingJob job; | |
545 | ||
546 | /** | |
547 | * indexExperiment | |
548 | * | |
549 | * Creates the experiment index. | |
550 | */ | |
551 | public void indexExperiment(boolean waitForCompletion) { | |
552 | ||
cbd4ad82 | 553 | synchronized(this) { |
e31e01e8 FC |
554 | if (fIndexed || fIndexing) { |
555 | // An indexing job is already running but a new request came | |
556 | // in (probably due to a change in the trace set). The index | |
557 | // being currently built is therefore already invalid. | |
558 | // TODO: Cancel and restart the job | |
559 | // TODO: Add support for dynamically adding/removing traces | |
560 | return; | |
9aae0442 | 561 | } |
e31e01e8 FC |
562 | fIndexing = true; |
563 | } | |
8c8bf09f | 564 | |
e31e01e8 FC |
565 | job = new IndexingJob(fExperimentId); |
566 | job.schedule(); | |
567 | ||
568 | if (waitForCompletion) { | |
569 | try { | |
570 | job.join(); | |
571 | } catch (InterruptedException e) { | |
572 | e.printStackTrace(); | |
573 | } | |
574 | } | |
8c8bf09f | 575 | } |
e31e01e8 FC |
576 | |
577 | private class IndexingJob extends Job { | |
578 | ||
579 | public IndexingJob(String name) { | |
580 | super(name); | |
581 | } | |
582 | ||
583 | /* (non-Javadoc) | |
584 | * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor) | |
585 | */ | |
586 | @Override | |
587 | protected IStatus run(IProgressMonitor monitor) { | |
588 | ||
589 | // Minimal check | |
9f584e4c | 590 | if (fTraces.length == 0) { |
e31e01e8 FC |
591 | fIndexing = false; |
592 | return Status.OK_STATUS; | |
593 | } | |
594 | ||
595 | monitor.beginTask("Indexing " + fExperimentId, IProgressMonitor.UNKNOWN); | |
596 | ||
597 | int nbEvents = 0; | |
598 | TmfTimestamp startTime = null; | |
599 | TmfTimestamp lastTime = null; | |
600 | ||
9f584e4c FC |
601 | // Reset the index |
602 | fCheckpoints = new Vector<TmfCheckpoint>(); | |
e31e01e8 FC |
603 | |
604 | try { | |
9f584e4c FC |
605 | // Position the trace at the beginning |
606 | TmfExperimentContext context = seekLocation(null); | |
54d55ced | 607 | TmfExperimentLocation location = (TmfExperimentLocation) context.getLocation().clone(); |
9f584e4c FC |
608 | |
609 | // Get the first event | |
7f407ead | 610 | TmfEvent event = getNextEvent(context); |
9f584e4c FC |
611 | if (event != null) { |
612 | startTime = new TmfTimestamp(event.getTimestamp()); | |
613 | } | |
614 | ||
615 | // Index the experiment | |
616 | while (event != null) { | |
617 | lastTime = event.getTimestamp(); | |
e31e01e8 | 618 | if ((nbEvents++ % fIndexPageSize) == 0) { |
8f50c396 | 619 | fCheckpoints.add(new TmfCheckpoint(lastTime, location)); |
e31e01e8 FC |
620 | fNbEvents = nbEvents; |
621 | fTimeRange = new TmfTimeRange(startTime, lastTime); | |
9f584e4c | 622 | notifyListeners(new TmfTimeRange(startTime, lastTime)); |
e31e01e8 FC |
623 | |
624 | monitor.worked(1); | |
625 | ||
626 | // Check monitor *after* fCheckpoints has been updated | |
627 | if (monitor.isCanceled()) { | |
628 | monitor.done(); | |
629 | return Status.CANCEL_STATUS; | |
630 | } | |
631 | } | |
632 | ||
633 | // We will need the contexts at the next iteration | |
634 | if ((nbEvents % fIndexPageSize) == 0) { | |
8f50c396 | 635 | location = (TmfExperimentLocation) context.getLocation().clone(); |
e31e01e8 FC |
636 | } |
637 | ||
54d55ced | 638 | event = getNextEvent(context); |
e31e01e8 FC |
639 | } |
640 | ||
641 | } | |
642 | finally { | |
643 | synchronized(this) { | |
644 | fNbEvents = nbEvents; | |
645 | fTimeRange = new TmfTimeRange(startTime, lastTime); | |
646 | fIndexing = false; | |
647 | fIndexed = true; | |
648 | } | |
649 | monitor.done(); | |
650 | } | |
651 | ||
e31e01e8 FC |
652 | return Status.OK_STATUS; |
653 | } | |
654 | } | |
655 | ||
9f584e4c FC |
656 | protected void notifyListeners(TmfTimeRange range) { |
657 | broadcast(new TmfRangeSynchSignal(this, range, null)); | |
658 | } | |
659 | ||
8c8bf09f ASL |
660 | // ------------------------------------------------------------------------ |
661 | // Signal handlers | |
662 | // ------------------------------------------------------------------------ | |
663 | ||
664 | @TmfSignalHandler | |
951d134a | 665 | public void experimentSelected(TmfExperimentSelectedSignal<T> signal) { |
ff4ed569 FC |
666 | TmfExperiment<?> experiment = signal.getExperiment(); |
667 | if (experiment == this) { | |
668 | setCurrentExperiment(experiment); | |
669 | } | |
670 | else { | |
671 | dispose(); | |
672 | } | |
e31e01e8 FC |
673 | // if (signal.getExperiment() == this) { |
674 | // indexExperiment(true); | |
675 | // } | |
8c8bf09f ASL |
676 | } |
677 | ||
678 | @TmfSignalHandler | |
679 | public void experimentUpdated(TmfExperimentUpdatedSignal signal) { | |
e31e01e8 | 680 | // indexExperiment(true); |
8c8bf09f ASL |
681 | } |
682 | ||
683 | @TmfSignalHandler | |
684 | public void traceUpdated(TmfTraceUpdatedSignal signal) { | |
685 | // TODO: Incremental index update | |
686 | synchronized(this) { | |
687 | updateNbEvents(); | |
688 | updateTimeRange(); | |
689 | } | |
690 | broadcast(new TmfExperimentUpdatedSignal(this, this, signal.getTrace())); | |
691 | } | |
692 | ||
693 | } |