1 /*******************************************************************************
2 * Copyright (c) 2012 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.trace
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Collections
;
17 import java
.util
.List
;
19 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
20 import org
.eclipse
.core
.runtime
.IStatus
;
21 import org
.eclipse
.core
.runtime
.Status
;
22 import org
.eclipse
.core
.runtime
.jobs
.Job
;
23 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
.TmfExperimentContext
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfDataProvider
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
35 * A simple indexer that manages the trace index as an array of trace
36 * checkpoints. Checkpoints are stored at fixed intervals (event rank) in
37 * ascending timestamp order.
39 * The goal being to access a random trace event reasonably fast from the user's
40 * standpoint, picking the right interval value becomes a trade-off between speed
41 * and memory usage (a shorter inter-event interval is faster but requires more
44 * Locating a specific checkpoint is trivial for both rank (rank % interval) and
45 * timestamp (bsearch in the array).
48 * @author Francois Chouinard
53 public class TmfCheckpointIndexer
implements ITmfTraceIndexer
{
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
59 /** The event trace to index */
60 protected final ITmfTrace fTrace
;
62 /** The interval between checkpoints */
63 private final int fCheckpointInterval
;
65 /** The event trace to index */
66 private boolean fIsIndexing
;
69 * The trace index. It is composed of checkpoints taken at intervals of
70 * fCheckpointInterval events.
72 protected final List
<ITmfCheckpoint
> fTraceIndex
;
75 * The indexing request
77 private ITmfEventRequest fIndexingRequest
= null;
79 // ------------------------------------------------------------------------
81 // ------------------------------------------------------------------------
84 * Basic constructor that uses the default trace block size as checkpoints
87 * @param trace the trace to index
89 public TmfCheckpointIndexer(final ITmfTrace trace
) {
90 this(trace
, TmfDataProvider
.DEFAULT_BLOCK_SIZE
);
96 * @param trace the trace to index
97 * @param interval the checkpoints interval
99 public TmfCheckpointIndexer(final ITmfTrace trace
, final int interval
) {
101 fCheckpointInterval
= interval
;
102 fTraceIndex
= new ArrayList
<ITmfCheckpoint
>();
107 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#dispose()
110 public void dispose() {
111 if ((fIndexingRequest
!= null) && !fIndexingRequest
.isCompleted()) {
112 fIndexingRequest
.cancel();
117 // ------------------------------------------------------------------------
118 // ITmfTraceIndexer - isIndexing
119 // ------------------------------------------------------------------------
122 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#isIndexing()
125 public boolean isIndexing() {
129 // ------------------------------------------------------------------------
130 // ITmfTraceIndexer - buildIndex
131 // ------------------------------------------------------------------------
135 * The index is a list of contexts that point to events at regular interval
136 * (rank-wise) in the trace. After it is built, the index can be used to
137 * quickly access any event by rank or timestamp (using seekIndex()).
139 * The index is built simply by reading the trace
141 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#buildIndex(long, org.eclipse.linuxtools.tmf.core.event.TmfTimeRange, boolean)
144 public void buildIndex(final long offset
, final TmfTimeRange range
, final boolean waitForCompletion
) {
146 // Don't do anything if we are already indexing
147 synchronized (fTraceIndex
) {
154 // The monitoring job
155 final Job job
= new Job("Indexing " + fTrace
.getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
157 protected IStatus
run(final IProgressMonitor monitor
) {
158 while (!monitor
.isCanceled()) {
161 } catch (final InterruptedException e
) {
162 return Status
.OK_STATUS
;
166 return Status
.OK_STATUS
;
171 // Build a background request for all the trace data. The index is
172 // updated as we go by readNextEvent().
173 fIndexingRequest
= new TmfEventRequest(ITmfEvent
.class,
174 range
, offset
, TmfDataRequest
.ALL_DATA
, fCheckpointInterval
, ITmfDataRequest
.ExecutionType
.BACKGROUND
)
177 public void handleData(final ITmfEvent event
) {
178 super.handleData(event
);
180 // Update the trace status at regular intervals
181 if ((getNbRead() % fCheckpointInterval
) == 0) {
188 public void handleSuccess() {
193 public void handleCompleted() {
195 super.handleCompleted();
199 private void updateTraceStatus() {
200 if (fTrace
.getNbEvents() > 0) {
201 signalNewTimeRange(fTrace
.getStartTime(), fTrace
.getEndTime());
206 // Submit the request and wait for completion if required
207 fTrace
.sendRequest(fIndexingRequest
);
208 if (waitForCompletion
) {
210 fIndexingRequest
.waitForCompletion();
211 } catch (final InterruptedException e
) {
217 * Notify the interested parties that the trace time range has changed
219 * @param startTime the new start time
220 * @param endTime the new end time
222 private void signalNewTimeRange(final ITmfTimestamp startTime
, final ITmfTimestamp endTime
) {
223 fTrace
.broadcast(new TmfTraceUpdatedSignal(fTrace
, fTrace
, new TmfTimeRange(startTime
, endTime
)));
226 // ------------------------------------------------------------------------
227 // ITmfTraceIndexer - updateIndex
228 // ------------------------------------------------------------------------
231 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#updateIndex(org.eclipse.linuxtools.tmf.core.trace.ITmfContext, org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
234 public synchronized void updateIndex(final ITmfContext context
, final ITmfTimestamp timestamp
) {
235 final long rank
= context
.getRank();
236 if ((rank
% fCheckpointInterval
) == 0) {
237 // Determine the table position
238 final long position
= rank
/ fCheckpointInterval
;
239 // Add new entry at proper location (if empty)
240 if (fTraceIndex
.size() == position
) {
241 fTraceIndex
.add(new TmfCheckpoint(timestamp
, saveContext(context
)));
246 // ------------------------------------------------------------------------
247 // ITmfTraceIndexer - seekIndex
248 // ------------------------------------------------------------------------
251 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
254 public synchronized ITmfContext
seekIndex(final ITmfTimestamp timestamp
) {
256 // A null timestamp indicates to seek the first event
257 if (timestamp
== null) {
258 return fTrace
.seekEvent(0);
261 // Find the checkpoint at or before the requested timestamp.
262 // In the very likely event that the timestamp is not at a checkpoint
263 // boundary, bsearch will return index = (- (insertion point + 1)).
264 // It is then trivial to compute the index of the previous checkpoint.
265 int index
= Collections
.binarySearch(fTraceIndex
, new TmfCheckpoint(timestamp
, null));
267 index
= Math
.max(0, -(index
+ 2));
269 // If timestamp was in the list, use previous index to be able to find the
270 // first event with the same timestamp before the checkpoint
271 index
= Math
.max(0, index
- 1);
274 // Position the trace at the checkpoint
275 return restoreCheckpoint(index
);
279 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(long)
282 public ITmfContext
seekIndex(final long rank
) {
284 // A rank < 0 indicates to seek the first event
286 return fTrace
.seekEvent(0);
289 // Find the checkpoint at or before the requested rank.
290 final int index
= (int) rank
/ fCheckpointInterval
;
292 // Position the trace at the checkpoint
293 return restoreCheckpoint(index
);
297 * Position the trace at the given checkpoint
299 * @param checkpoint the checkpoint index
300 * @return the corresponding context
302 private ITmfContext
restoreCheckpoint(final int checkpoint
) {
303 ITmfLocation location
= null;
305 synchronized (fTraceIndex
) {
306 if (!fTraceIndex
.isEmpty()) {
308 if (index
>= fTraceIndex
.size()) {
309 index
= fTraceIndex
.size() - 1;
311 return restoreContext(fTraceIndex
.get(index
).getContext());
314 final ITmfContext context
= fTrace
.seekEvent(location
);
315 context
.setRank((long) index
* fCheckpointInterval
);
319 // ------------------------------------------------------------------------
321 // ------------------------------------------------------------------------
324 * @return the trace index
326 protected List
<ITmfCheckpoint
> getTraceIndex() {
330 // ------------------------------------------------------------------------
331 // Context conversion functions
332 // ------------------------------------------------------------------------
334 private static ITmfContext
saveContext(ITmfContext context
) {
335 if (context
instanceof TmfExperimentContext
) {
336 return saveExpContext(context
);
338 TmfContext ctx
= new TmfContext(context
.getLocation(), context
.getRank());
342 private static ITmfContext
saveExpContext(ITmfContext context
) {
343 TmfExperimentContext expContext
= (TmfExperimentContext
) context
;
344 int size
= expContext
.getContexts().length
;
345 ITmfContext
[] trcCtxts
= new TmfContext
[size
];
346 for (int i
= 0; i
< size
; i
++) {
347 ITmfContext ctx
= expContext
.getContexts()[i
];
348 trcCtxts
[i
] = (ctx
!= null) ?
new TmfContext(ctx
.getLocation(), ctx
.getRank()) : null;
350 TmfExperimentContext expCtx
= new TmfExperimentContext(trcCtxts
);
351 expCtx
.setLocation(context
.getLocation());
352 expCtx
.setRank(context
.getRank());
353 ITmfEvent
[] trcEvts
= expCtx
.getEvents();
354 for (int i
= 0; i
< size
; i
++) {
355 ITmfEvent event
= expContext
.getEvents()[i
];
361 private ITmfContext
restoreContext(ITmfContext context
) {
362 if (context
instanceof TmfExperimentContext
) {
363 return restoreExpContext(context
);
365 ITmfContext ctx
= fTrace
.seekEvent(context
.getLocation());
366 ctx
.setRank(context
.getRank());
370 private ITmfContext
restoreExpContext(ITmfContext context
) {
371 TmfExperimentContext expContext
= (TmfExperimentContext
) context
;
372 int size
= expContext
.getContexts().length
;
373 ITmfContext
[] trcCtxts
= new ITmfContext
[size
];
374 for (int i
= 0; i
< size
; i
++) {
375 ITmfTrace trace
= ((TmfExperiment
) fTrace
).getTraces()[i
];
376 ITmfContext ctx
= expContext
.getContexts()[i
];
377 trcCtxts
[i
] = trace
.seekEvent(ctx
.getLocation());
378 trcCtxts
[i
].setRank(ctx
.getRank());
380 TmfExperimentContext ctx
= new TmfExperimentContext(trcCtxts
);
381 ctx
.setLocation(context
.getLocation());
382 ctx
.setRank(context
.getRank());
383 ITmfEvent
[] trcEvts
= expContext
.getEvents();
384 for (int i
= 0; i
< size
; i
++) {
385 ITmfEvent event
= trcEvts
[i
];
386 ctx
.getEvents()[i
] = event
;