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