1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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 * Bernd Hufmann - Update way of broadcasting of TmfTraceUpdatedSignal
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
;
16 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
17 import org
.eclipse
.core
.runtime
.IStatus
;
18 import org
.eclipse
.core
.runtime
.Status
;
19 import org
.eclipse
.core
.runtime
.jobs
.Job
;
20 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Messages
;
21 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.trace
.indexer
.TmfMemoryIndex
;
22 import org
.eclipse
.tracecompass
.tmf
.core
.component
.TmfEventProvider
;
23 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.request
.TmfEventRequest
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTraceCompleteness
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
36 * A simple indexer that manages the trace index as an array of trace
37 * checkpoints. Checkpoints are stored in memory at fixed intervals (event rank) in
38 * ascending timestamp order.
40 * The goal being to access a random trace event reasonably fast from the user's
41 * standpoint, picking the right interval value becomes a trade-off between speed
42 * and memory usage (a shorter inter-event interval is faster but requires more
45 * Locating a specific checkpoint is trivial for both rank (rank % interval) and
46 * timestamp (bsearch in the array).
51 * @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 ITmfCheckpointIndex 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
, TmfEventProvider
.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
= createIndex(trace
);
107 * Creates the index instance. Classes extending this class
108 * can override this to provide a different index implementation.
110 * @param trace the trace to index
113 protected ITmfCheckpointIndex
createIndex(final ITmfTrace trace
) {
114 return new TmfMemoryIndex(trace
);
118 public void dispose() {
119 if ((fIndexingRequest
!= null) && !fIndexingRequest
.isCompleted()) {
120 fIndexingRequest
.cancel();
123 fTraceIndex
.dispose();
126 // ------------------------------------------------------------------------
127 // ITmfTraceIndexer - isIndexing
128 // ------------------------------------------------------------------------
131 public boolean isIndexing() {
135 // ------------------------------------------------------------------------
136 // ITmfTraceIndexer - buildIndex
137 // ------------------------------------------------------------------------
140 public void buildIndex(final long offset
, final TmfTimeRange range
, final boolean waitForCompletion
) {
142 // Don't do anything if we are already indexing
143 synchronized (fTraceIndex
) {
150 // No need to build the index, it has been restored
151 if (!fTraceIndex
.isCreatedFromScratch()) {
152 // Set some trace attributes that depends on indexing
153 TmfTraceUpdatedSignal signal
= new TmfTraceUpdatedSignal(this, fTrace
, new TmfTimeRange(fTraceIndex
.getTimeRange().getStartTime(), fTraceIndex
.getTimeRange().getEndTime()), fTraceIndex
.getNbEvents());
154 if (waitForCompletion
) {
155 fTrace
.broadcast(signal
);
157 fTrace
.broadcastAsync(signal
);
163 // The monitoring job
164 final Job job
= new Job("Indexing " + fTrace
.getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
166 protected IStatus
run(final IProgressMonitor monitor
) {
167 monitor
.beginTask("", IProgressMonitor
.UNKNOWN
); //$NON-NLS-1$
168 while (!monitor
.isCanceled()) {
170 long prevNbEvents
= fTrace
.getNbEvents();
172 long nbEvents
= fTrace
.getNbEvents();
173 setName(Messages
.TmfCheckpointIndexer_Indexing
+ ' ' + fTrace
.getName() + " (" + nbEvents
+ ")"); //$NON-NLS-1$ //$NON-NLS-2$
174 // setName doesn't refresh the UI, setTaskName does
175 long rate
= (nbEvents
- prevNbEvents
) * 4;
176 monitor
.setTaskName(rate
+ " " + Messages
.TmfCheckpointIndexer_EventsPerSecond
); //$NON-NLS-1$
177 } catch (final InterruptedException e
) {
178 return Status
.OK_STATUS
;
182 return Status
.OK_STATUS
;
185 job
.setSystem(!isCompleteTrace(fTrace
));
188 // Build a background request for all the trace data. The index is
189 // updated as we go by readNextEvent().
190 fIndexingRequest
= new TmfEventRequest(ITmfEvent
.class,
191 range
, offset
, ITmfEventRequest
.ALL_DATA
,
192 ITmfEventRequest
.ExecutionType
.BACKGROUND
) {
194 public void handleData(final ITmfEvent event
) {
195 super.handleData(event
);
196 // Update the trace status at regular intervals
197 if ((getNbRead() % fCheckpointInterval
) == 0) {
203 public void handleSuccess() {
204 fTraceIndex
.setTimeRange(fTrace
.getTimeRange());
205 fTraceIndex
.setNbEvents(fTrace
.getNbEvents());
206 if (isCompleteTrace(fTrace
)) {
207 fTraceIndex
.setIndexComplete();
213 public void handleCompleted() {
215 super.handleCompleted();
219 private void updateTraceStatus() {
220 if (fTrace
.getNbEvents() > 0) {
221 signalNewTimeRange(fTrace
.getStartTime(), fTrace
.getEndTime());
226 // Submit the request and wait for completion if required
227 fTrace
.sendRequest(fIndexingRequest
);
228 if (waitForCompletion
) {
230 fIndexingRequest
.waitForCompletion();
231 } catch (final InterruptedException e
) {
237 * Notify the interested parties that the trace time range has changed
239 * @param startTime the new start time
240 * @param endTime the new end time
242 private void signalNewTimeRange(final ITmfTimestamp startTime
, final ITmfTimestamp endTime
) {
243 fTrace
.broadcast(new TmfTraceUpdatedSignal(fTrace
, fTrace
, new TmfTimeRange(startTime
, endTime
), fTrace
.getNbEvents()));
246 // ------------------------------------------------------------------------
247 // ITmfTraceIndexer - updateIndex
248 // ------------------------------------------------------------------------
251 public synchronized void updateIndex(final ITmfContext context
, final ITmfTimestamp timestamp
) {
252 if ((context
.getRank() % fCheckpointInterval
) == 0) {
253 // Determine the table position
254 final long position
= context
.getRank() / fCheckpointInterval
;
255 // Add new entry at proper location (if empty)
256 if (fTraceIndex
.size() == position
) {
257 fTraceIndex
.insert(new TmfCheckpoint(timestamp
, context
.getLocation(), position
));
262 // ------------------------------------------------------------------------
263 // ITmfTraceIndexer - seekIndex
264 // ------------------------------------------------------------------------
267 public synchronized ITmfContext
seekIndex(final ITmfTimestamp timestamp
) {
269 // A null timestamp indicates to seek the first event
270 if (timestamp
== null) {
271 return fTrace
.seekEvent(0);
274 // Find the checkpoint at or before the requested timestamp.
275 // In the very likely event that the timestamp is not at a checkpoint
276 // boundary, bsearch will return index = (- (insertion point + 1)).
277 // It is then trivial to compute the index of the previous checkpoint.
278 long index
= fTraceIndex
.binarySearch(new TmfCheckpoint(timestamp
, null, 0));
280 index
= Math
.max(0, -(index
+ 2));
282 // If timestamp was in the list, use previous index to be able to find the
283 // first event with the same timestamp before the checkpoint
284 index
= Math
.max(0, index
- 1);
287 // Position the trace at the checkpoint
288 return restoreCheckpoint(index
);
292 public ITmfContext
seekIndex(final long rank
) {
294 // A rank < 0 indicates to seek the first event
296 return fTrace
.seekEvent(0);
299 // Find the checkpoint at or before the requested rank.
300 final int index
= (int) rank
/ fCheckpointInterval
;
302 // Position the trace at the checkpoint
303 return restoreCheckpoint(index
);
307 * Position the trace at the given checkpoint
309 * @param checkpoint the checkpoint index
310 * @return the corresponding context
312 private ITmfContext
restoreCheckpoint(final long checkpoint
) {
313 ITmfLocation location
= null;
315 synchronized (fTraceIndex
) {
316 if (!fTraceIndex
.isEmpty()) {
318 if (index
>= fTraceIndex
.size()) {
319 index
= fTraceIndex
.size() - 1;
321 location
= fTraceIndex
.get(index
).getLocation();
324 final ITmfContext context
= fTrace
.seekEvent(location
);
325 context
.setRank(index
* fCheckpointInterval
);
329 // ------------------------------------------------------------------------
331 // ------------------------------------------------------------------------
334 * @return the trace index
336 protected ITmfCheckpointIndex
getTraceIndex() {
340 private static boolean isCompleteTrace(ITmfTrace trace
) {
341 return !(trace
instanceof ITmfTraceCompleteness
) || ((ITmfTraceCompleteness
)trace
).isComplete();