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) { | |
e31e01e8 FC |
229 | TmfTimestamp traceStartTime = trace.getStartTime(); |
230 | if (traceStartTime.compareTo(startTime, true) < 0) | |
231 | startTime = traceStartTime; | |
232 | ||
233 | TmfTimestamp traceEndTime = trace.getEndTime(); | |
234 | if (traceEndTime.compareTo(endTime, true) > 0) | |
235 | endTime = traceEndTime; | |
8c8bf09f ASL |
236 | } |
237 | fTimeRange = new TmfTimeRange(startTime, endTime); | |
238 | } | |
239 | ||
240 | // ------------------------------------------------------------------------ | |
241 | // TmfProvider | |
242 | // ------------------------------------------------------------------------ | |
243 | ||
244 | @Override | |
fc6ccf6f | 245 | public ITmfContext armRequest(TmfDataRequest<T> request) { |
9f584e4c FC |
246 | TmfTimestamp timestamp = (request instanceof TmfEventRequest<?>) ? |
247 | ((TmfEventRequest<T>) request).getRange().getStartTime() : null; | |
248 | ||
249 | TmfExperimentContext context = (timestamp != null) ? | |
250 | seekEvent(timestamp) : seekEvent(request.getIndex()); | |
251 | ||
8c8bf09f ASL |
252 | return context; |
253 | } | |
254 | ||
255 | @SuppressWarnings("unchecked") | |
256 | @Override | |
257 | public T getNext(ITmfContext context) { | |
258 | if (context instanceof TmfExperimentContext) { | |
259 | return (T) getNextEvent((TmfExperimentContext) context); | |
260 | } | |
261 | return null; | |
262 | } | |
263 | ||
9f584e4c FC |
264 | // ------------------------------------------------------------------------ |
265 | // ITmfTrace trace positioning | |
266 | // ------------------------------------------------------------------------ | |
267 | ||
268 | // Returns a brand new context based on the location provided | |
269 | // Arms the event queues | |
270 | // NOTE: This is a fine example of pathological coupling... | |
452ad365 | 271 | public TmfExperimentContext seekLocation(ITmfLocation<?> location) { |
54d55ced | 272 | |
9f584e4c | 273 | if (location instanceof TmfExperimentLocation || location == null) { |
85fb0e54 | 274 | ITmfLocation<?>[] prvloc = (location != null) ? ((TmfExperimentLocation) location).getLocation() : new TmfExperimentLocation[fTraces.length]; |
452ad365 | 275 | ITmfLocation<?>[] newloc = new ITmfLocation[fTraces.length]; |
9f584e4c FC |
276 | TmfContext[] contexts = new TmfContext[fTraces.length]; |
277 | ||
278 | TmfExperimentContext context = new TmfExperimentContext(fTraces, contexts); | |
279 | TmfEvent[] events = context.getEvents(); | |
280 | ||
281 | long rank = 0; | |
282 | for (int i = 0; i < fTraces.length; i++) { | |
85fb0e54 | 283 | contexts[i] = fTraces[i].seekLocation(prvloc[i]); |
9f584e4c FC |
284 | newloc[i] = contexts[i].getLocation(); // No clone here |
285 | events[i] = fTraces[i].parseEvent(contexts[i]); | |
286 | rank += contexts[i].getRank(); | |
287 | } | |
288 | context.setLocation(new TmfExperimentLocation(newloc)); | |
289 | context.setRank(rank); | |
7f407ead | 290 | context.setLastTrace(-1); |
9f584e4c FC |
291 | return context; |
292 | } | |
293 | return null; | |
294 | } | |
295 | ||
54d55ced FC |
296 | /* (non-Javadoc) |
297 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.event.TmfTimestamp) | |
298 | */ | |
9f584e4c | 299 | public TmfExperimentContext seekEvent(TmfTimestamp timestamp) { |
8c8bf09f | 300 | |
9f584e4c FC |
301 | if (timestamp == null) { |
302 | timestamp = TmfTimestamp.BigBang; | |
303 | } | |
304 | ||
305 | // First, find the right checkpoint | |
306 | int index = Collections.binarySearch(fCheckpoints, new TmfCheckpoint(timestamp, null)); | |
307 | ||
308 | // In the very likely case that the checkpoint was not found, bsearch | |
309 | // returns its negated would-be location (not an offset...). From that | |
310 | // index, we can then position the stream and get the event. | |
311 | if (index < 0) { | |
312 | index = Math.max(0, -(index + 2)); | |
313 | } | |
314 | ||
315 | // Position the experiment at the checkpoint | |
452ad365 | 316 | ITmfLocation<?> location; |
9f584e4c FC |
317 | synchronized (fCheckpoints) { |
318 | if (fCheckpoints.size() > 0) { | |
319 | if (index >= fCheckpoints.size()) { | |
320 | index = fCheckpoints.size() - 1; | |
321 | } | |
322 | location = fCheckpoints.elementAt(index).getLocation(); | |
323 | } | |
324 | else { | |
325 | location = null; | |
326 | } | |
327 | } | |
328 | ||
85fb0e54 | 329 | TmfExperimentContext context = seekLocation(location); |
cbd4ad82 | 330 | context.setRank((long) index * fIndexPageSize); |
9f584e4c | 331 | |
54d55ced FC |
332 | // And locate the event |
333 | TmfExperimentContext nextEventContext = new TmfExperimentContext(context); | |
334 | TmfEvent event = getNextEvent(nextEventContext); | |
9f584e4c | 335 | while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) { |
54d55ced FC |
336 | event = getNextEvent(nextEventContext); |
337 | context = new TmfExperimentContext(nextEventContext); | |
338 | if (event != null) context.updateRank(-1); | |
9f584e4c | 339 | } |
7f407ead | 340 | context.setLastTrace(-1); |
9f584e4c | 341 | |
85fb0e54 | 342 | return context; |
9f584e4c | 343 | } |
8c8bf09f | 344 | |
54d55ced FC |
345 | /* (non-Javadoc) |
346 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(long) | |
347 | */ | |
9f584e4c FC |
348 | public TmfExperimentContext seekEvent(long rank) { |
349 | ||
54d55ced FC |
350 | // Position the stream at the previous checkpoint |
351 | int index = (int) rank / fIndexPageSize; | |
352 | ITmfLocation<?> location; | |
353 | synchronized (fCheckpoints) { | |
354 | if (fCheckpoints.size() == 0) { | |
355 | location = null; | |
356 | } | |
357 | else { | |
358 | if (index >= fCheckpoints.size()) { | |
359 | index = fCheckpoints.size() - 1; | |
360 | } | |
361 | location = fCheckpoints.elementAt(index).getLocation(); | |
362 | } | |
363 | } | |
e31e01e8 | 364 | |
54d55ced | 365 | TmfExperimentContext context = seekLocation(location); |
cbd4ad82 | 366 | long pos = (long) index * fIndexPageSize; |
54d55ced FC |
367 | context.setRank(pos); |
368 | ||
369 | // And locate the event | |
370 | TmfExperimentContext nextEventContext = new TmfExperimentContext(context); | |
371 | TmfEvent event = getNextEvent(nextEventContext); | |
372 | while (event != null && pos++ < rank) { | |
373 | event = getNextEvent(nextEventContext); | |
374 | context = new TmfExperimentContext(nextEventContext); | |
375 | if (event != null) context.updateRank(-1); | |
376 | } | |
377 | context.setLastTrace(-1); | |
9f584e4c | 378 | |
54d55ced | 379 | return context; |
8c8bf09f ASL |
380 | } |
381 | ||
382 | /** | |
383 | * Scan the next events from all traces and return the next one | |
384 | * in chronological order. | |
385 | * | |
386 | * @param context | |
387 | * @return | |
388 | */ | |
9f584e4c | 389 | public synchronized TmfEvent getNextEvent(TmfContext context) { |
54d55ced | 390 | |
9f584e4c FC |
391 | if (context instanceof TmfExperimentContext) { |
392 | TmfExperimentContext expContext = (TmfExperimentContext) context; | |
7f407ead FC |
393 | int lastTrace = expContext.getLastTrace(); |
394 | if (lastTrace != -1) { | |
395 | TmfContext traceContext = expContext.getContexts()[lastTrace]; | |
396 | expContext.getTraces()[lastTrace].getNextEvent(traceContext); | |
397 | expContext.getEvents()[lastTrace] = expContext.getTraces()[lastTrace].parseEvent(traceContext); | |
398 | TmfExperimentLocation expLocation = (TmfExperimentLocation) expContext.getLocation(); | |
399 | expLocation.getLocation()[lastTrace] = traceContext.getLocation().clone(); | |
7f407ead | 400 | } |
54d55ced | 401 | |
85fb0e54 | 402 | int trace = -1; |
9f584e4c | 403 | TmfTimestamp timestamp = TmfTimestamp.BigCrunch; |
85fb0e54 FC |
404 | for (int i = 0; i < expContext.getTraces().length; i++) { |
405 | if (expContext.getEvents()[i] != null) { | |
406 | if (expContext.getEvents()[i].getTimestamp() != null) { | |
407 | TmfTimestamp otherTS = expContext.getEvents()[i].getTimestamp(); | |
408 | if (otherTS.compareTo(timestamp, true) < 0) { | |
409 | trace = i; | |
410 | timestamp = otherTS; | |
411 | } | |
9f584e4c | 412 | } |
8c8bf09f ASL |
413 | } |
414 | } | |
85fb0e54 | 415 | if (trace >= 0) { |
54d55ced FC |
416 | TmfContext traceContext = expContext.getContexts()[trace]; |
417 | expContext.getEvents()[trace] = expContext.getTraces()[trace].parseEvent(traceContext); | |
7f407ead | 418 | expContext.setLastTrace(trace); |
54d55ced FC |
419 | expContext.updateRank(1); |
420 | return expContext.getEvents()[trace]; | |
85fb0e54 | 421 | } |
8c8bf09f | 422 | } |
54d55ced FC |
423 | |
424 | return null; | |
8c8bf09f ASL |
425 | } |
426 | ||
54d55ced FC |
427 | /* (non-Javadoc) |
428 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#parseEvent(org.eclipse.linuxtools.tmf.trace.TmfContext) | |
429 | */ | |
9f584e4c | 430 | public TmfEvent parseEvent(TmfContext context) { |
54d55ced | 431 | |
85fb0e54 FC |
432 | if (context instanceof TmfExperimentContext) { |
433 | TmfExperimentContext expContext = (TmfExperimentContext) context; | |
54d55ced FC |
434 | int lastTrace = expContext.getLastTrace(); |
435 | if (lastTrace != -1) { | |
436 | TmfContext traceContext = expContext.getContexts()[lastTrace]; | |
437 | expContext.getTraces()[lastTrace].getNextEvent(traceContext); | |
438 | expContext.getEvents()[lastTrace] = expContext.getTraces()[lastTrace].parseEvent(traceContext); | |
439 | expContext.updateRank(1); | |
440 | TmfExperimentLocation expLocation = (TmfExperimentLocation) expContext.getLocation(); | |
441 | expLocation.getLocation()[lastTrace] = traceContext.getLocation().clone(); | |
442 | } | |
443 | ||
85fb0e54 FC |
444 | int trace = -1; |
445 | TmfTimestamp timestamp = TmfTimestamp.BigCrunch; | |
446 | for (int i = 0; i < expContext.getTraces().length; i++) { | |
447 | if (expContext.getEvents()[i] != null) { | |
448 | if (expContext.getEvents()[i].getTimestamp() != null) { | |
449 | TmfTimestamp otherTS = expContext.getEvents()[i].getTimestamp(); | |
450 | if (otherTS.compareTo(timestamp, true) < 0) { | |
451 | trace = i; | |
452 | timestamp = otherTS; | |
453 | } | |
454 | } | |
455 | } | |
456 | } | |
457 | if (trace >= 0) { | |
54d55ced FC |
458 | TmfContext traceLocation = expContext.getContexts()[trace]; |
459 | expContext.getEvents()[trace] = expContext.getTraces()[trace].parseEvent(traceLocation); | |
460 | expContext.setLastTrace(-1); | |
85fb0e54 FC |
461 | return expContext.getEvents()[trace]; |
462 | } | |
463 | } | |
54d55ced FC |
464 | |
465 | return null; | |
8c8bf09f ASL |
466 | } |
467 | ||
468 | /* (non-Javadoc) | |
469 | * @see java.lang.Object#toString() | |
470 | */ | |
471 | @Override | |
472 | public String toString() { | |
e31e01e8 | 473 | return "[TmfExperiment (" + fExperimentId + ")]"; |
8c8bf09f ASL |
474 | } |
475 | ||
476 | // ------------------------------------------------------------------------ | |
477 | // Indexing | |
478 | // ------------------------------------------------------------------------ | |
479 | ||
480 | /* | |
481 | * The experiment holds the globally ordered events of its set of traces. | |
482 | * It is expected to provide access to each individual event by index i.e. | |
9f584e4c | 483 | * it must be possible to request the Nth event of the experiment. |
8c8bf09f ASL |
484 | * |
485 | * The purpose of the index is to keep the information needed to rapidly | |
486 | * restore the traces contexts at regular intervals (every INDEX_PAGE_SIZE | |
487 | * event). | |
488 | */ | |
489 | ||
490 | // The index page size | |
491 | private static final int DEFAULT_INDEX_PAGE_SIZE = 1000; | |
e31e01e8 | 492 | private final int fIndexPageSize; |
8c8bf09f | 493 | |
e31e01e8 FC |
494 | // Indicates that an indexing job is already running |
495 | private Boolean fIndexing = false; | |
496 | private Boolean fIndexed = false; | |
8c8bf09f | 497 | |
e31e01e8 FC |
498 | // The indexing job |
499 | private IndexingJob job; | |
500 | ||
501 | /** | |
502 | * indexExperiment | |
503 | * | |
504 | * Creates the experiment index. | |
505 | */ | |
506 | public void indexExperiment(boolean waitForCompletion) { | |
507 | ||
cbd4ad82 | 508 | synchronized(this) { |
e31e01e8 FC |
509 | if (fIndexed || fIndexing) { |
510 | // An indexing job is already running but a new request came | |
511 | // in (probably due to a change in the trace set). The index | |
512 | // being currently built is therefore already invalid. | |
513 | // TODO: Cancel and restart the job | |
514 | // TODO: Add support for dynamically adding/removing traces | |
515 | return; | |
9aae0442 | 516 | } |
e31e01e8 FC |
517 | fIndexing = true; |
518 | } | |
8c8bf09f | 519 | |
e31e01e8 FC |
520 | job = new IndexingJob(fExperimentId); |
521 | job.schedule(); | |
522 | ||
523 | if (waitForCompletion) { | |
524 | try { | |
525 | job.join(); | |
526 | } catch (InterruptedException e) { | |
527 | e.printStackTrace(); | |
528 | } | |
529 | } | |
8c8bf09f | 530 | } |
e31e01e8 FC |
531 | |
532 | private class IndexingJob extends Job { | |
533 | ||
534 | public IndexingJob(String name) { | |
535 | super(name); | |
536 | } | |
537 | ||
538 | /* (non-Javadoc) | |
539 | * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor) | |
540 | */ | |
541 | @Override | |
542 | protected IStatus run(IProgressMonitor monitor) { | |
543 | ||
544 | // Minimal check | |
9f584e4c | 545 | if (fTraces.length == 0) { |
e31e01e8 FC |
546 | fIndexing = false; |
547 | return Status.OK_STATUS; | |
548 | } | |
549 | ||
550 | monitor.beginTask("Indexing " + fExperimentId, IProgressMonitor.UNKNOWN); | |
551 | ||
552 | int nbEvents = 0; | |
553 | TmfTimestamp startTime = null; | |
554 | TmfTimestamp lastTime = null; | |
555 | ||
9f584e4c FC |
556 | // Reset the index |
557 | fCheckpoints = new Vector<TmfCheckpoint>(); | |
e31e01e8 FC |
558 | |
559 | try { | |
9f584e4c FC |
560 | // Position the trace at the beginning |
561 | TmfExperimentContext context = seekLocation(null); | |
54d55ced | 562 | TmfExperimentLocation location = (TmfExperimentLocation) context.getLocation().clone(); |
9f584e4c FC |
563 | |
564 | // Get the first event | |
7f407ead | 565 | TmfEvent event = getNextEvent(context); |
9f584e4c FC |
566 | if (event != null) { |
567 | startTime = new TmfTimestamp(event.getTimestamp()); | |
568 | } | |
569 | ||
570 | // Index the experiment | |
571 | while (event != null) { | |
572 | lastTime = event.getTimestamp(); | |
e31e01e8 | 573 | if ((nbEvents++ % fIndexPageSize) == 0) { |
9f584e4c | 574 | fCheckpoints.add(new TmfCheckpoint(lastTime, location.clone())); |
e31e01e8 FC |
575 | fNbEvents = nbEvents; |
576 | fTimeRange = new TmfTimeRange(startTime, lastTime); | |
9f584e4c | 577 | notifyListeners(new TmfTimeRange(startTime, lastTime)); |
e31e01e8 FC |
578 | |
579 | monitor.worked(1); | |
580 | ||
581 | // Check monitor *after* fCheckpoints has been updated | |
582 | if (monitor.isCanceled()) { | |
583 | monitor.done(); | |
584 | return Status.CANCEL_STATUS; | |
585 | } | |
586 | } | |
587 | ||
588 | // We will need the contexts at the next iteration | |
589 | if ((nbEvents % fIndexPageSize) == 0) { | |
9f584e4c | 590 | location = (TmfExperimentLocation) context.getLocation(); |
e31e01e8 FC |
591 | } |
592 | ||
54d55ced | 593 | event = getNextEvent(context); |
e31e01e8 FC |
594 | } |
595 | ||
596 | } | |
597 | finally { | |
598 | synchronized(this) { | |
599 | fNbEvents = nbEvents; | |
600 | fTimeRange = new TmfTimeRange(startTime, lastTime); | |
601 | fIndexing = false; | |
602 | fIndexed = true; | |
603 | } | |
604 | monitor.done(); | |
605 | } | |
606 | ||
e31e01e8 FC |
607 | return Status.OK_STATUS; |
608 | } | |
609 | } | |
610 | ||
9f584e4c FC |
611 | protected void notifyListeners(TmfTimeRange range) { |
612 | broadcast(new TmfRangeSynchSignal(this, range, null)); | |
613 | } | |
614 | ||
8c8bf09f ASL |
615 | // ------------------------------------------------------------------------ |
616 | // Signal handlers | |
617 | // ------------------------------------------------------------------------ | |
618 | ||
619 | @TmfSignalHandler | |
951d134a | 620 | public void experimentSelected(TmfExperimentSelectedSignal<T> signal) { |
cbd4ad82 | 621 | setCurrentExperiment(signal.getExperiment()); |
e31e01e8 FC |
622 | // if (signal.getExperiment() == this) { |
623 | // indexExperiment(true); | |
624 | // } | |
8c8bf09f ASL |
625 | } |
626 | ||
627 | @TmfSignalHandler | |
628 | public void experimentUpdated(TmfExperimentUpdatedSignal signal) { | |
e31e01e8 | 629 | // indexExperiment(true); |
8c8bf09f ASL |
630 | } |
631 | ||
632 | @TmfSignalHandler | |
633 | public void traceUpdated(TmfTraceUpdatedSignal signal) { | |
634 | // TODO: Incremental index update | |
635 | synchronized(this) { | |
636 | updateNbEvents(); | |
637 | updateTimeRange(); | |
638 | } | |
639 | broadcast(new TmfExperimentUpdatedSignal(this, this, signal.getTrace())); | |
640 | } | |
641 | ||
642 | } |