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 | ||
fc6ccf6f | 18 | import org.eclipse.linuxtools.tmf.component.TmfEventProvider; |
8c8bf09f ASL |
19 | import org.eclipse.linuxtools.tmf.event.TmfEvent; |
20 | import org.eclipse.linuxtools.tmf.event.TmfTimeRange; | |
21 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; | |
2fb2eb37 | 22 | import org.eclipse.linuxtools.tmf.request.ITmfDataRequest; |
f6b14ce2 | 23 | import org.eclipse.linuxtools.tmf.request.ITmfDataRequest.ExecutionType; |
2fb2eb37 | 24 | import org.eclipse.linuxtools.tmf.request.ITmfEventRequest; |
550d787e FC |
25 | import org.eclipse.linuxtools.tmf.request.TmfDataRequest; |
26 | import org.eclipse.linuxtools.tmf.request.TmfEventRequest; | |
ff4ed569 FC |
27 | import org.eclipse.linuxtools.tmf.signal.TmfExperimentSelectedSignal; |
28 | import org.eclipse.linuxtools.tmf.signal.TmfExperimentUpdatedSignal; | |
8c8bf09f | 29 | import org.eclipse.linuxtools.tmf.signal.TmfSignalHandler; |
550d787e | 30 | import org.eclipse.linuxtools.tmf.signal.TmfSignalManager; |
ff4ed569 | 31 | import org.eclipse.linuxtools.tmf.signal.TmfTraceUpdatedSignal; |
9f584e4c FC |
32 | import org.eclipse.linuxtools.tmf.trace.ITmfContext; |
33 | import org.eclipse.linuxtools.tmf.trace.ITmfLocation; | |
8c8bf09f | 34 | import org.eclipse.linuxtools.tmf.trace.ITmfTrace; |
9f584e4c FC |
35 | import org.eclipse.linuxtools.tmf.trace.TmfCheckpoint; |
36 | import org.eclipse.linuxtools.tmf.trace.TmfContext; | |
8c8bf09f ASL |
37 | |
38 | /** | |
39 | * <b><u>TmfExperiment</u></b> | |
40 | * <p> | |
41 | * TmfExperiment presents a time-ordered, unified view of a set of TmfTraces | |
9b635e61 | 42 | * that are part of a tracing experiment. |
8c8bf09f ASL |
43 | * <p> |
44 | */ | |
fc6ccf6f | 45 | public class TmfExperiment<T extends TmfEvent> extends TmfEventProvider<T> implements ITmfTrace { |
8c8bf09f ASL |
46 | |
47 | // ------------------------------------------------------------------------ | |
48 | // Attributes | |
49 | // ------------------------------------------------------------------------ | |
50 | ||
51 | // The currently selected experiment | |
82e04272 | 52 | protected static TmfExperiment<?> fCurrentExperiment = null; |
e31e01e8 | 53 | |
550d787e | 54 | // The set of traces that constitute the experiment |
82e04272 | 55 | protected ITmfTrace[] fTraces; |
8c8bf09f ASL |
56 | |
57 | // The total number of events | |
82e04272 | 58 | protected long fNbEvents; |
8c8bf09f ASL |
59 | |
60 | // The experiment time range | |
82e04272 | 61 | protected TmfTimeRange fTimeRange; |
8c8bf09f | 62 | |
9b635e61 | 63 | // The experiment reference timestamp (default: Zero) |
82e04272 | 64 | protected TmfTimestamp fEpoch; |
8c8bf09f | 65 | |
9f584e4c | 66 | // The experiment index |
82e04272 | 67 | protected Vector<TmfCheckpoint> fCheckpoints = new Vector<TmfCheckpoint>(); |
9f584e4c | 68 | |
f6b14ce2 FC |
69 | // The current experiment context |
70 | protected TmfExperimentContext fExperimentContext; | |
9b635e61 | 71 | |
8c8bf09f ASL |
72 | // ------------------------------------------------------------------------ |
73 | // Constructors | |
74 | // ------------------------------------------------------------------------ | |
75 | ||
76 | /** | |
77 | * @param type | |
78 | * @param id | |
79 | * @param traces | |
80 | * @param epoch | |
81 | * @param indexPageSize | |
82 | */ | |
83 | public TmfExperiment(Class<T> type, String id, ITmfTrace[] traces, TmfTimestamp epoch, int indexPageSize) { | |
045df77d | 84 | this(type, id, traces, TmfTimestamp.Zero, indexPageSize, false); |
cb866e08 FC |
85 | } |
86 | ||
9b635e61 | 87 | public TmfExperiment(Class<T> type, String id, ITmfTrace[] traces, TmfTimestamp epoch, int indexPageSize, boolean preIndexExperiment) { |
ce785d7d | 88 | super(id, type); |
8c8bf09f | 89 | |
9f584e4c | 90 | fTraces = traces; |
8c8bf09f ASL |
91 | fEpoch = epoch; |
92 | fIndexPageSize = indexPageSize; | |
93 | ||
9b635e61 | 94 | if (preIndexExperiment) indexExperiment(true); |
cb866e08 | 95 | |
8c8bf09f | 96 | updateTimeRange(); |
550d787e | 97 | } |
8c8bf09f | 98 | |
82e04272 FC |
99 | protected TmfExperiment(String id, Class<T> type) { |
100 | super(id, type); | |
101 | } | |
102 | ||
8c8bf09f ASL |
103 | /** |
104 | * @param type | |
105 | * @param id | |
106 | * @param traces | |
107 | */ | |
108 | public TmfExperiment(Class<T> type, String id, ITmfTrace[] traces) { | |
109 | this(type, id, traces, TmfTimestamp.Zero, DEFAULT_INDEX_PAGE_SIZE); | |
110 | } | |
111 | ||
112 | /** | |
113 | * @param type | |
114 | * @param id | |
115 | * @param traces | |
116 | * @param indexPageSize | |
117 | */ | |
118 | public TmfExperiment(Class<T> type, String id, ITmfTrace[] traces, int indexPageSize) { | |
119 | this(type, id, traces, TmfTimestamp.Zero, indexPageSize); | |
120 | } | |
377f1ad8 | 121 | |
f6b14ce2 FC |
122 | /** |
123 | * Copy constructor | |
124 | * @param other | |
125 | */ | |
ce785d7d | 126 | public TmfExperiment(TmfExperiment<T> other) { |
3b38ea61 | 127 | super(other.getName() + "(clone)", other.fType); //$NON-NLS-1$ |
377f1ad8 | 128 | |
ce785d7d FC |
129 | fEpoch = other.fEpoch; |
130 | fIndexPageSize = other.fIndexPageSize; | |
377f1ad8 | 131 | |
ce785d7d FC |
132 | fTraces = new ITmfTrace[other.fTraces.length]; |
133 | for (int trace = 0; trace < other.fTraces.length; trace++) { | |
134 | fTraces[trace] = other.fTraces[trace].createTraceCopy(); | |
377f1ad8 WB |
135 | } |
136 | ||
ce785d7d FC |
137 | fNbEvents = other.fNbEvents; |
138 | fTimeRange = other.fTimeRange; | |
377f1ad8 WB |
139 | } |
140 | ||
d4011df2 | 141 | @Override |
377f1ad8 | 142 | public TmfExperiment<T> createTraceCopy() { |
550d787e FC |
143 | TmfExperiment<T> experiment = new TmfExperiment<T>(this); |
144 | TmfSignalManager.deregister(experiment); | |
145 | return experiment; | |
377f1ad8 WB |
146 | } |
147 | ||
8c8bf09f | 148 | /** |
ff4ed569 | 149 | * Clears the experiment |
8c8bf09f ASL |
150 | */ |
151 | @Override | |
db1ea19b | 152 | public synchronized void dispose() { |
550d787e FC |
153 | if (fTraces != null) { |
154 | for (ITmfTrace trace : fTraces) { | |
155 | trace.dispose(); | |
156 | } | |
157 | fTraces = null; | |
158 | } | |
159 | if (fCheckpoints != null) { | |
160 | fCheckpoints.clear(); | |
ff4ed569 | 161 | } |
2fb2eb37 | 162 | super.dispose(); |
8c8bf09f ASL |
163 | } |
164 | ||
9f584e4c | 165 | // ------------------------------------------------------------------------ |
cbd4ad82 | 166 | // ITmfTrace |
9f584e4c FC |
167 | // ------------------------------------------------------------------------ |
168 | ||
d4011df2 | 169 | @Override |
9f584e4c FC |
170 | public String getPath() { |
171 | return null; | |
172 | } | |
173 | ||
d4011df2 | 174 | @Override |
9f584e4c FC |
175 | public long getNbEvents() { |
176 | return fNbEvents; | |
177 | } | |
178 | ||
d4011df2 FC |
179 | @Override |
180 | public int getCacheSize() { | |
54d55ced FC |
181 | return fIndexPageSize; |
182 | } | |
183 | ||
d4011df2 | 184 | @Override |
9f584e4c FC |
185 | public TmfTimeRange getTimeRange() { |
186 | return fTimeRange; | |
187 | } | |
188 | ||
d4011df2 | 189 | @Override |
9f584e4c FC |
190 | public TmfTimestamp getStartTime() { |
191 | return fTimeRange.getStartTime(); | |
192 | } | |
193 | ||
d4011df2 | 194 | @Override |
9f584e4c FC |
195 | public TmfTimestamp getEndTime() { |
196 | return fTimeRange.getEndTime(); | |
197 | } | |
198 | ||
54d55ced FC |
199 | public Vector<TmfCheckpoint> getCheckpoints() { |
200 | return fCheckpoints; | |
201 | } | |
202 | ||
8c8bf09f | 203 | // ------------------------------------------------------------------------ |
e31e01e8 | 204 | // Accessors |
8c8bf09f ASL |
205 | // ------------------------------------------------------------------------ |
206 | ||
c1c69938 | 207 | public static void setCurrentExperiment(TmfExperiment<?> experiment) { |
f6b14ce2 FC |
208 | fCurrentExperiment = experiment; |
209 | } | |
210 | ||
e31e01e8 FC |
211 | public static TmfExperiment<?> getCurrentExperiment() { |
212 | return fCurrentExperiment; | |
8c8bf09f ASL |
213 | } |
214 | ||
8c8bf09f ASL |
215 | public TmfTimestamp getEpoch() { |
216 | return fEpoch; | |
217 | } | |
218 | ||
9f584e4c FC |
219 | public ITmfTrace[] getTraces() { |
220 | return fTraces; | |
8c8bf09f ASL |
221 | } |
222 | ||
223 | /** | |
224 | * Returns the rank of the first event with the requested timestamp. | |
225 | * If none, returns the index of the next event (if any). | |
226 | * | |
85fb0e54 | 227 | * @param timestamp |
8c8bf09f ASL |
228 | * @return |
229 | */ | |
d4011df2 FC |
230 | @Override |
231 | public long getRank(TmfTimestamp timestamp) { | |
85fb0e54 | 232 | TmfExperimentContext context = seekEvent(timestamp); |
8c8bf09f ASL |
233 | return context.getRank(); |
234 | } | |
235 | ||
236 | /** | |
237 | * Returns the timestamp of the event at the requested index. | |
238 | * If none, returns null. | |
239 | * | |
240 | * @param index | |
241 | * @return | |
242 | */ | |
243 | public TmfTimestamp getTimestamp(int index) { | |
85fb0e54 | 244 | TmfExperimentContext context = seekEvent(index); |
7f407ead | 245 | TmfEvent event = getNextEvent(context); |
85fb0e54 | 246 | return (event != null) ? event.getTimestamp() : null; |
8c8bf09f ASL |
247 | } |
248 | ||
249 | // ------------------------------------------------------------------------ | |
250 | // Operators | |
251 | // ------------------------------------------------------------------------ | |
252 | ||
253 | /** | |
254 | * Update the total number of events | |
255 | */ | |
256 | private void updateNbEvents() { | |
257 | int nbEvents = 0; | |
258 | for (ITmfTrace trace : fTraces) { | |
259 | nbEvents += trace.getNbEvents(); | |
260 | } | |
261 | fNbEvents = nbEvents; | |
262 | } | |
263 | ||
264 | /** | |
265 | * Update the global time range | |
266 | */ | |
267 | private void updateTimeRange() { | |
268 | TmfTimestamp startTime = fTimeRange != null ? fTimeRange.getStartTime() : TmfTimestamp.BigCrunch; | |
269 | TmfTimestamp endTime = fTimeRange != null ? fTimeRange.getEndTime() : TmfTimestamp.BigBang; | |
270 | ||
271 | for (ITmfTrace trace : fTraces) { | |
550d787e FC |
272 | TmfTimestamp traceStartTime = trace.getStartTime(); |
273 | if (traceStartTime.compareTo(startTime, true) < 0) | |
274 | startTime = traceStartTime; | |
275 | TmfTimestamp traceEndTime = trace.getEndTime(); | |
276 | if (traceEndTime.compareTo(endTime, true) > 0) | |
277 | endTime = traceEndTime; | |
8c8bf09f | 278 | } |
36548af3 | 279 | fTimeRange = new TmfTimeRange(startTime, endTime); |
8c8bf09f ASL |
280 | } |
281 | ||
282 | // ------------------------------------------------------------------------ | |
283 | // TmfProvider | |
284 | // ------------------------------------------------------------------------ | |
285 | ||
286 | @Override | |
2fb2eb37 | 287 | public ITmfContext armRequest(ITmfDataRequest<T> request) { |
9b635e61 | 288 | // Tracer.trace("Ctx: Arming request - start"); |
2fb2eb37 | 289 | TmfTimestamp timestamp = (request instanceof ITmfEventRequest<?>) ? |
9b635e61 | 290 | ((ITmfEventRequest<T>) request).getRange().getStartTime() : null; |
9f584e4c FC |
291 | TmfExperimentContext context = (timestamp != null) ? |
292 | seekEvent(timestamp) : seekEvent(request.getIndex()); | |
9b635e61 | 293 | // Tracer.trace("Ctx: Arming request - done"); |
8c8bf09f ASL |
294 | return context; |
295 | } | |
296 | ||
297 | @SuppressWarnings("unchecked") | |
298 | @Override | |
299 | public T getNext(ITmfContext context) { | |
300 | if (context instanceof TmfExperimentContext) { | |
301 | return (T) getNextEvent((TmfExperimentContext) context); | |
302 | } | |
303 | return null; | |
304 | } | |
305 | ||
550d787e | 306 | // ------------------------------------------------------------------------ |
9f584e4c FC |
307 | // ITmfTrace trace positioning |
308 | // ------------------------------------------------------------------------ | |
309 | ||
310 | // Returns a brand new context based on the location provided | |
8f50c396 | 311 | // and initializes the event queues |
d4011df2 | 312 | @Override |
9b635e61 | 313 | public synchronized TmfExperimentContext seekLocation(ITmfLocation<?> location) { |
8f50c396 FC |
314 | |
315 | // Validate the location | |
316 | if (location != null && !(location instanceof TmfExperimentLocation)) { | |
317 | return null; // Throw an exception? | |
9f584e4c | 318 | } |
8f50c396 FC |
319 | |
320 | // Instantiate the location | |
321 | TmfExperimentLocation expLocation = (location == null) | |
322 | ? new TmfExperimentLocation(new ITmfLocation<?>[fTraces.length], new long[fTraces.length]) | |
323 | : (TmfExperimentLocation) location.clone(); | |
324 | ||
325 | // Create and populate the context's traces contexts | |
326 | TmfExperimentContext context = new TmfExperimentContext(fTraces, new TmfContext[fTraces.length]); | |
9b635e61 FC |
327 | // Tracer.trace("Ctx: SeekLocation - start"); |
328 | ||
8f50c396 FC |
329 | long rank = 0; |
330 | for (int i = 0; i < fTraces.length; i++) { | |
331 | // Get the relevant trace attributes | |
332 | ITmfLocation<?> traceLocation = expLocation.getLocation()[i]; | |
333 | long traceRank = expLocation.getRanks()[i]; | |
334 | ||
335 | // Set the corresponding sub-context | |
336 | context.getContexts()[i] = fTraces[i].seekLocation(traceLocation); | |
337 | context.getContexts()[i].setRank(traceRank); | |
338 | rank += traceRank; | |
339 | ||
340 | // Set the trace location and read the corresponding event | |
341 | expLocation.getLocation()[i] = context.getContexts()[i].getLocation(); | |
fa867360 | 342 | context.getEvents()[i] = fTraces[i].getNextEvent(context.getContexts()[i]); |
8f50c396 FC |
343 | } |
344 | ||
9b635e61 FC |
345 | // Tracer.trace("Ctx: SeekLocation - done"); |
346 | ||
8f50c396 FC |
347 | // Finalize context |
348 | context.setLocation(expLocation); | |
8f50c396 | 349 | context.setLastTrace(TmfExperimentContext.NO_TRACE); |
9b635e61 FC |
350 | context.setRank(rank); |
351 | ||
f6b14ce2 | 352 | fExperimentContext = context; |
9b635e61 | 353 | |
8f50c396 | 354 | return context; |
9f584e4c FC |
355 | } |
356 | ||
54d55ced FC |
357 | /* (non-Javadoc) |
358 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.event.TmfTimestamp) | |
359 | */ | |
d4011df2 | 360 | @Override |
9b635e61 FC |
361 | public synchronized TmfExperimentContext seekEvent(TmfTimestamp timestamp) { |
362 | ||
363 | // Tracer.trace("Ctx: seekEvent(TS) - start"); | |
8c8bf09f | 364 | |
9f584e4c FC |
365 | if (timestamp == null) { |
366 | timestamp = TmfTimestamp.BigBang; | |
367 | } | |
368 | ||
369 | // First, find the right checkpoint | |
370 | int index = Collections.binarySearch(fCheckpoints, new TmfCheckpoint(timestamp, null)); | |
371 | ||
372 | // In the very likely case that the checkpoint was not found, bsearch | |
373 | // returns its negated would-be location (not an offset...). From that | |
374 | // index, we can then position the stream and get the event. | |
375 | if (index < 0) { | |
376 | index = Math.max(0, -(index + 2)); | |
377 | } | |
378 | ||
379 | // Position the experiment at the checkpoint | |
452ad365 | 380 | ITmfLocation<?> location; |
9f584e4c FC |
381 | synchronized (fCheckpoints) { |
382 | if (fCheckpoints.size() > 0) { | |
383 | if (index >= fCheckpoints.size()) { | |
384 | index = fCheckpoints.size() - 1; | |
385 | } | |
386 | location = fCheckpoints.elementAt(index).getLocation(); | |
387 | } | |
388 | else { | |
389 | location = null; | |
390 | } | |
391 | } | |
392 | ||
85fb0e54 | 393 | TmfExperimentContext context = seekLocation(location); |
cbd4ad82 | 394 | context.setRank((long) index * fIndexPageSize); |
9f584e4c | 395 | |
9b635e61 | 396 | // And locate the event |
fa867360 | 397 | TmfEvent event = parseEvent(context); |
9f584e4c | 398 | while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) { |
fa867360 FC |
399 | getNextEvent(context); |
400 | event = parseEvent(context); | |
9f584e4c FC |
401 | } |
402 | ||
f6b14ce2 | 403 | if (event == null) { |
fa867360 FC |
404 | context.setLocation(null); |
405 | context.setRank(ITmfContext.UNKNOWN_RANK); | |
9b635e61 | 406 | } |
f6b14ce2 FC |
407 | |
408 | return context; | |
9f584e4c | 409 | } |
8c8bf09f | 410 | |
54d55ced FC |
411 | /* (non-Javadoc) |
412 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(long) | |
413 | */ | |
d4011df2 | 414 | @Override |
9b635e61 FC |
415 | public synchronized TmfExperimentContext seekEvent(long rank) { |
416 | ||
417 | // Tracer.trace("Ctx: seekEvent(rank) - start"); | |
9f584e4c | 418 | |
54d55ced FC |
419 | // Position the stream at the previous checkpoint |
420 | int index = (int) rank / fIndexPageSize; | |
421 | ITmfLocation<?> location; | |
422 | synchronized (fCheckpoints) { | |
423 | if (fCheckpoints.size() == 0) { | |
424 | location = null; | |
425 | } | |
426 | else { | |
427 | if (index >= fCheckpoints.size()) { | |
428 | index = fCheckpoints.size() - 1; | |
429 | } | |
430 | location = fCheckpoints.elementAt(index).getLocation(); | |
431 | } | |
432 | } | |
e31e01e8 | 433 | |
54d55ced | 434 | TmfExperimentContext context = seekLocation(location); |
9b635e61 | 435 | context.setRank((long) index * fIndexPageSize); |
54d55ced | 436 | |
9b635e61 | 437 | // And locate the event |
fa867360 | 438 | TmfEvent event = parseEvent(context); |
9b635e61 | 439 | long pos = context.getRank(); |
fa867360 FC |
440 | while (event != null && pos++ < rank) { |
441 | getNextEvent(context); | |
442 | event = parseEvent(context); | |
54d55ced | 443 | } |
9f584e4c | 444 | |
f6b14ce2 | 445 | if (event == null) { |
fa867360 FC |
446 | context.setLocation(null); |
447 | context.setRank(ITmfContext.UNKNOWN_RANK); | |
9b635e61 | 448 | } |
f6b14ce2 | 449 | |
fa867360 | 450 | return context; |
8c8bf09f ASL |
451 | } |
452 | ||
453 | /** | |
454 | * Scan the next events from all traces and return the next one | |
455 | * in chronological order. | |
456 | * | |
457 | * @param context | |
458 | * @return | |
459 | */ | |
9b635e61 FC |
460 | |
461 | // private void dumpContext(TmfExperimentContext context, boolean isBefore) { | |
462 | ||
463 | // TmfContext context0 = context.getContexts()[0]; | |
464 | // TmfEvent event0 = context.getEvents()[0]; | |
465 | // TmfExperimentLocation location0 = (TmfExperimentLocation) context.getLocation(); | |
466 | // long rank0 = context.getRank(); | |
467 | // int trace = context.getLastTrace(); | |
468 | // | |
469 | // StringBuffer result = new StringBuffer("Ctx: " + (isBefore ? "B " : "A ")); | |
470 | // | |
471 | // result.append("[Ctx: fLoc= " + context0.getLocation().toString() + ", fRnk= " + context0.getRank() + "] "); | |
472 | // result.append("[Evt: " + event0.getTimestamp().toString() + "] "); | |
473 | // result.append("[Loc: fLoc= " + location0.getLocation()[0].toString() + ", fRnk= " + location0.getRanks()[0] + "] "); | |
474 | // result.append("[Rnk: " + rank0 + "], [Trc: " + trace + "]"); | |
475 | // Tracer.trace(result.toString()); | |
476 | // } | |
477 | ||
d4011df2 | 478 | @Override |
9f584e4c | 479 | public synchronized TmfEvent getNextEvent(TmfContext context) { |
54d55ced | 480 | |
8f50c396 FC |
481 | // Validate the context |
482 | if (!(context instanceof TmfExperimentContext)) { | |
483 | return null; // Throw an exception? | |
484 | } | |
54d55ced | 485 | |
f6b14ce2 | 486 | if (!context.equals(fExperimentContext)) { |
9b635e61 FC |
487 | // Tracer.trace("Ctx: Restoring context"); |
488 | seekLocation(context.getLocation()); | |
489 | } | |
490 | ||
8f50c396 FC |
491 | TmfExperimentContext expContext = (TmfExperimentContext) context; |
492 | ||
9b635e61 FC |
493 | // dumpContext(expContext, true); |
494 | ||
8f50c396 FC |
495 | // If an event was consumed previously, get the next one from that trace |
496 | int lastTrace = expContext.getLastTrace(); | |
497 | if (lastTrace != TmfExperimentContext.NO_TRACE) { | |
498 | TmfContext traceContext = expContext.getContexts()[lastTrace]; | |
499 | expContext.getEvents()[lastTrace] = expContext.getTraces()[lastTrace].getNextEvent(traceContext); | |
fa867360 | 500 | expContext.setLastTrace(TmfExperimentContext.NO_TRACE); |
8f50c396 FC |
501 | } |
502 | ||
503 | // Scan the candidate events and identify the "next" trace to read from | |
504 | int trace = TmfExperimentContext.NO_TRACE; | |
505 | TmfTimestamp timestamp = TmfTimestamp.BigCrunch; | |
506 | for (int i = 0; i < expContext.getTraces().length; i++) { | |
507 | TmfEvent event = expContext.getEvents()[i]; | |
508 | if (event != null && event.getTimestamp() != null) { | |
509 | TmfTimestamp otherTS = event.getTimestamp(); | |
510 | if (otherTS.compareTo(timestamp, true) < 0) { | |
511 | trace = i; | |
512 | timestamp = otherTS; | |
8c8bf09f ASL |
513 | } |
514 | } | |
515 | } | |
8f50c396 FC |
516 | |
517 | // Update the experiment context and set the "next" event | |
518 | TmfEvent event = null; | |
fa867360 FC |
519 | if (trace != TmfExperimentContext.NO_TRACE) { |
520 | updateIndex(expContext, timestamp); | |
82e04272 | 521 | |
fa867360 FC |
522 | TmfContext traceContext = expContext.getContexts()[trace]; |
523 | TmfExperimentLocation expLocation = (TmfExperimentLocation) expContext.getLocation(); | |
64fe8e8a FC |
524 | // expLocation.getLocation()[trace] = traceContext.getLocation().clone(); |
525 | expLocation.getLocation()[trace] = traceContext.getLocation(); | |
82e04272 FC |
526 | |
527 | // updateIndex(expContext, timestamp); | |
528 | ||
fa867360 | 529 | expLocation.getRanks()[trace] = traceContext.getRank(); |
8f50c396 FC |
530 | expContext.setLastTrace(trace); |
531 | expContext.updateRank(1); | |
8f50c396 FC |
532 | event = expContext.getEvents()[trace]; |
533 | } | |
534 | ||
9b635e61 FC |
535 | // if (event != null) { |
536 | // Tracer.trace("Exp: " + (expContext.getRank() - 1) + ": " + event.getTimestamp().toString()); | |
537 | // dumpContext(expContext, false); | |
538 | // Tracer.trace("Ctx: Event returned= " + event.getTimestamp().toString()); | |
539 | // } | |
540 | ||
8f50c396 | 541 | return event; |
8c8bf09f ASL |
542 | } |
543 | ||
9b635e61 | 544 | public synchronized void updateIndex(ITmfContext context, TmfTimestamp timestamp) { |
550d787e | 545 | // Build the index as we go along |
9b635e61 | 546 | long rank = context.getRank(); |
550d787e FC |
547 | if (context.isValidRank() && (rank % fIndexPageSize) == 0) { |
548 | // Determine the table position | |
9b635e61 | 549 | long position = rank / fIndexPageSize; |
550d787e FC |
550 | // Add new entry at proper location (if empty) |
551 | if (fCheckpoints.size() == position) { | |
552 | ITmfLocation<?> location = context.getLocation().clone(); | |
1a971e96 | 553 | fCheckpoints.add(new TmfCheckpoint(timestamp.clone(), location)); |
550d787e FC |
554 | // System.out.println(this + "[" + (fCheckpoints.size() - 1) + "] " + timestamp + ", " + location.toString()); |
555 | } | |
556 | } | |
557 | } | |
558 | ||
54d55ced | 559 | /* (non-Javadoc) |
045df77d | 560 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#parseEvent(org.eclipse.linuxtools.tmf.trace.TmfContext) |
54d55ced | 561 | */ |
d4011df2 | 562 | @Override |
9f584e4c | 563 | public TmfEvent parseEvent(TmfContext context) { |
54d55ced | 564 | |
9b635e61 FC |
565 | // Validate the context |
566 | if (!(context instanceof TmfExperimentContext)) { | |
567 | return null; // Throw an exception? | |
568 | } | |
54d55ced | 569 | |
f6b14ce2 | 570 | if (!context.equals(fExperimentContext)) { |
9b635e61 FC |
571 | // Tracer.trace("Ctx: Restoring context"); |
572 | seekLocation(context.getLocation()); | |
573 | } | |
574 | ||
575 | TmfExperimentContext expContext = (TmfExperimentContext) context; | |
576 | ||
577 | // If an event was consumed previously, get the next one from that trace | |
578 | int lastTrace = expContext.getLastTrace(); | |
579 | if (lastTrace != TmfExperimentContext.NO_TRACE) { | |
580 | TmfContext traceContext = expContext.getContexts()[lastTrace]; | |
581 | expContext.getEvents()[lastTrace] = expContext.getTraces()[lastTrace].getNextEvent(traceContext); | |
fa867360 | 582 | expContext.setLastTrace(TmfExperimentContext.NO_TRACE); |
f6b14ce2 | 583 | fExperimentContext = (TmfExperimentContext) context; |
9b635e61 FC |
584 | } |
585 | ||
586 | // Scan the candidate events and identify the "next" trace to read from | |
587 | int trace = TmfExperimentContext.NO_TRACE; | |
588 | TmfTimestamp timestamp = TmfTimestamp.BigCrunch; | |
589 | for (int i = 0; i < expContext.getTraces().length; i++) { | |
590 | TmfEvent event = expContext.getEvents()[i]; | |
591 | if (event != null && event.getTimestamp() != null) { | |
592 | TmfTimestamp otherTS = event.getTimestamp(); | |
593 | if (otherTS.compareTo(timestamp, true) < 0) { | |
594 | trace = i; | |
595 | timestamp = otherTS; | |
85fb0e54 FC |
596 | } |
597 | } | |
85fb0e54 | 598 | } |
54d55ced | 599 | |
9b635e61 | 600 | TmfEvent event = null; |
fa867360 | 601 | if (trace != TmfExperimentContext.NO_TRACE) { |
9b635e61 FC |
602 | event = expContext.getEvents()[trace]; |
603 | } | |
f6b14ce2 | 604 | |
9b635e61 | 605 | return event; |
8c8bf09f ASL |
606 | } |
607 | ||
608 | /* (non-Javadoc) | |
609 | * @see java.lang.Object#toString() | |
610 | */ | |
611 | @Override | |
3b38ea61 | 612 | @SuppressWarnings("nls") |
8c8bf09f | 613 | public String toString() { |
ce785d7d | 614 | return "[TmfExperiment (" + getName() + ")]"; |
8c8bf09f ASL |
615 | } |
616 | ||
617 | // ------------------------------------------------------------------------ | |
618 | // Indexing | |
619 | // ------------------------------------------------------------------------ | |
620 | ||
621 | /* | |
622 | * The experiment holds the globally ordered events of its set of traces. | |
623 | * It is expected to provide access to each individual event by index i.e. | |
9f584e4c | 624 | * it must be possible to request the Nth event of the experiment. |
8c8bf09f ASL |
625 | * |
626 | * The purpose of the index is to keep the information needed to rapidly | |
627 | * restore the traces contexts at regular intervals (every INDEX_PAGE_SIZE | |
628 | * event). | |
629 | */ | |
630 | ||
631 | // The index page size | |
9b635e61 | 632 | private static final int DEFAULT_INDEX_PAGE_SIZE = 5000; |
82e04272 | 633 | protected int fIndexPageSize; |
e31e01e8 | 634 | |
9b635e61 FC |
635 | // private static BufferedWriter fEventLog = null; |
636 | // private static BufferedWriter openLogFile(String filename) { | |
637 | // BufferedWriter outfile = null; | |
638 | // try { | |
639 | // outfile = new BufferedWriter(new FileWriter(filename)); | |
640 | // } catch (IOException e) { | |
641 | // e.printStackTrace(); | |
642 | // } | |
643 | // return outfile; | |
644 | // } | |
645 | ||
550d787e | 646 | @SuppressWarnings("unchecked") |
cb866e08 | 647 | private void indexExperiment(boolean waitForCompletion) { |
550d787e | 648 | |
550d787e | 649 | fCheckpoints.clear(); |
9b635e61 FC |
650 | |
651 | // fEventLog = openLogFile("TraceEvent.log"); | |
82e04272 | 652 | // System.out.println(System.currentTimeMillis() + ": Experiment indexing started"); |
550d787e | 653 | |
f9673903 FC |
654 | ITmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, TmfTimeRange.Eternity, |
655 | TmfDataRequest.ALL_DATA, 1, ITmfDataRequest.ExecutionType.BACKGROUND) { | |
550d787e | 656 | |
cb866e08 | 657 | // long indexingStart = System.nanoTime(); |
550d787e FC |
658 | |
659 | TmfTimestamp startTime = null; | |
660 | TmfTimestamp lastTime = null; | |
550d787e FC |
661 | |
662 | @Override | |
f9673903 FC |
663 | public void handleData(TmfEvent event) { |
664 | super.handleData(event); | |
665 | if (event != null) { | |
666 | TmfTimestamp ts = event.getTimestamp(); | |
550d787e FC |
667 | if (startTime == null) |
668 | startTime = new TmfTimestamp(ts); | |
669 | lastTime = new TmfTimestamp(ts); | |
670 | ||
83e13355 | 671 | if ((getNbRead() % fIndexPageSize) == 0) { |
550d787e FC |
672 | updateExperiment(); |
673 | } | |
674 | } | |
9aae0442 | 675 | } |
e31e01e8 | 676 | |
550d787e FC |
677 | @Override |
678 | public void handleSuccess() { | |
9b635e61 FC |
679 | // long indexingEnd = System.nanoTime(); |
680 | ||
681 | updateExperiment(); | |
682 | // System.out.println(System.currentTimeMillis() + ": Experiment indexing completed"); | |
f9673903 | 683 | |
9b635e61 FC |
684 | // long average = (indexingEnd - indexingStart) / fNbEvents; |
685 | // System.out.println(getName() + ": start=" + startTime + ", end=" + lastTime + ", elapsed=" + (indexingEnd * 1.0 - indexingStart) / 1000000000); | |
686 | // System.out.println(getName() + ": nbEvents=" + fNbEvents + " (" + (average / 1000) + "." + (average % 1000) + " us/evt)"); | |
e31e01e8 FC |
687 | } |
688 | ||
550d787e | 689 | private void updateExperiment() { |
f9673903 FC |
690 | int nbRead = getNbRead(); |
691 | if (nbRead != 0) { | |
0c2a2e08 FC |
692 | // updateTimeRange(); |
693 | // updateNbEvents(); | |
6c042a35 | 694 | fTimeRange = new TmfTimeRange(startTime, new TmfTimestamp(lastTime)); |
f9673903 | 695 | fNbEvents = nbRead; |
6c042a35 FC |
696 | notifyListeners(); |
697 | } | |
550d787e FC |
698 | } |
699 | }; | |
e31e01e8 | 700 | |
550d787e | 701 | sendRequest((ITmfDataRequest<T>) request); |
cb866e08 FC |
702 | if (waitForCompletion) |
703 | try { | |
704 | request.waitForCompletion(); | |
705 | } catch (InterruptedException e) { | |
706 | e.printStackTrace(); | |
707 | } | |
550d787e FC |
708 | } |
709 | ||
710 | protected void notifyListeners() { | |
9b635e61 | 711 | broadcast(new TmfExperimentUpdatedSignal(this, this)); // , null)); |
9f584e4c FC |
712 | } |
713 | ||
8c8bf09f ASL |
714 | // ------------------------------------------------------------------------ |
715 | // Signal handlers | |
716 | // ------------------------------------------------------------------------ | |
717 | ||
718 | @TmfSignalHandler | |
951d134a | 719 | public void experimentSelected(TmfExperimentSelectedSignal<T> signal) { |
ff4ed569 FC |
720 | TmfExperiment<?> experiment = signal.getExperiment(); |
721 | if (experiment == this) { | |
722 | setCurrentExperiment(experiment); | |
cb866e08 | 723 | indexExperiment(false); |
ff4ed569 FC |
724 | } |
725 | else { | |
726 | dispose(); | |
727 | } | |
8c8bf09f ASL |
728 | } |
729 | ||
730 | @TmfSignalHandler | |
731 | public void experimentUpdated(TmfExperimentUpdatedSignal signal) { | |
8c8bf09f ASL |
732 | } |
733 | ||
734 | @TmfSignalHandler | |
735 | public void traceUpdated(TmfTraceUpdatedSignal signal) { | |
736 | // TODO: Incremental index update | |
737 | synchronized(this) { | |
738 | updateNbEvents(); | |
739 | updateTimeRange(); | |
740 | } | |
9b635e61 | 741 | broadcast(new TmfExperimentUpdatedSignal(this, this)); // , signal.getTrace())); |
8c8bf09f ASL |
742 | } |
743 | ||
9b635e61 FC |
744 | // ------------------------------------------------------------------------ |
745 | // TmfDataProvider | |
746 | // ------------------------------------------------------------------------ | |
747 | ||
748 | @Override | |
f6b14ce2 | 749 | protected void queueBackgroundRequest(final ITmfDataRequest<T> request, final int blockSize, final boolean indexing) { |
9b635e61 FC |
750 | |
751 | // TODO: Handle the data requests also... | |
752 | if (!(request instanceof ITmfEventRequest<?>)) { | |
753 | super.queueRequest(request); | |
754 | return; | |
755 | } | |
f6b14ce2 | 756 | final ITmfEventRequest<T> eventRequest = (ITmfEventRequest<T>) request; |
9b635e61 | 757 | |
9b635e61 FC |
758 | Thread thread = new Thread() { |
759 | @Override | |
760 | public void run() { | |
761 | ||
762 | // final long requestStart = System.nanoTime(); | |
763 | ||
764 | final Integer[] CHUNK_SIZE = new Integer[1]; | |
f6b14ce2 | 765 | CHUNK_SIZE[0] = blockSize + ((indexing) ? 1 : 0); |
9b635e61 FC |
766 | |
767 | final Integer[] nbRead = new Integer[1]; | |
768 | nbRead[0] = 0; | |
769 | ||
770 | // final TmfTimestamp[] timestamp = new TmfTimestamp[1]; | |
f6b14ce2 FC |
771 | // timestamp[0] = new TmfTimestamp(eventRequest.getRange().getStartTime()); |
772 | // final TmfTimestamp endTS = eventRequest.getRange().getEndTime(); | |
9b635e61 FC |
773 | |
774 | final Boolean[] isFinished = new Boolean[1]; | |
775 | isFinished[0] = Boolean.FALSE; | |
776 | ||
777 | while (!isFinished[0]) { | |
778 | ||
f6b14ce2 | 779 | // TmfEventRequest<T> subRequest = new TmfEventRequest<T>(eventRequest.getDataType(), new TmfTimeRange(timestamp[0], endTS), CHUNK_SIZE[0], eventRequest.getBlockize(), ExecutionType.BACKGROUND) |
f9673903 FC |
780 | // TmfDataRequest<T> subRequest = new TmfDataRequest<T>(eventRequest.getDataType(), nbRead[0], CHUNK_SIZE[0], eventRequest.getBlockize(), ExecutionType.BACKGROUND) |
781 | TmfDataRequest<T> subRequest = new TmfDataRequest<T>(eventRequest.getDataType(), nbRead[0], CHUNK_SIZE[0], ExecutionType.BACKGROUND) | |
9b635e61 | 782 | { |
9b635e61 | 783 | @Override |
f9673903 FC |
784 | public void handleData(T data) { |
785 | super.handleData(data); | |
786 | eventRequest.handleData(data); | |
787 | if (getNbRead() == CHUNK_SIZE[0]) { | |
788 | nbRead[0] += getNbRead(); | |
9b635e61 | 789 | } |
f9673903 | 790 | if (getNbRead() > CHUNK_SIZE[0]) { |
9b635e61 FC |
791 | System.out.println("ERROR - Read too many events"); |
792 | } | |
793 | } | |
f9673903 | 794 | |
9b635e61 FC |
795 | @Override |
796 | public void handleCompleted() { | |
797 | // System.out.println("Request completed at: " + timestamp[0]); | |
f9673903 | 798 | if (getNbRead() < CHUNK_SIZE[0]) { |
c1c69938 FC |
799 | if (isCancelled()) { |
800 | eventRequest.cancel(); | |
801 | } | |
802 | else { | |
803 | eventRequest.done(); | |
804 | } | |
9b635e61 | 805 | isFinished[0] = Boolean.TRUE; |
f9673903 | 806 | nbRead[0] += getNbRead(); |
c1c69938 | 807 | // System.out.println("fNbRead=" + getNbRead() + " total=" + nbRead[0]); |
9b635e61 FC |
808 | } |
809 | super.handleCompleted(); | |
810 | } | |
811 | }; | |
812 | ||
813 | if (!isFinished[0]) { | |
f6b14ce2 | 814 | queueRequest(subRequest); |
9b635e61 FC |
815 | |
816 | try { | |
817 | subRequest.waitForCompletion(); | |
818 | // System.out.println("Finished at " + timestamp[0]); | |
819 | } catch (InterruptedException e) { | |
820 | e.printStackTrace(); | |
821 | } | |
822 | ||
823 | // TmfTimestamp newTS = new TmfTimestamp(timestamp[0].getValue() + 1, timestamp[0].getScale(), timestamp[0].getPrecision()); | |
824 | // timestamp[0] = newTS; | |
f6b14ce2 | 825 | CHUNK_SIZE[0] = blockSize; |
9b635e61 FC |
826 | // System.out.println("New timestamp: " + timestamp[0]); |
827 | } | |
828 | } | |
829 | // final long requestEnded = System.nanoTime(); | |
830 | // System.out.println("Background request completed. Elapsed= " + (requestEnded * 1.0 - requestStart) / 1000000000); | |
831 | } | |
832 | }; | |
f6b14ce2 | 833 | |
9b635e61 FC |
834 | thread.start(); |
835 | } | |
836 | ||
8c8bf09f | 837 | } |