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