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