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
.linuxtools
.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
.linuxtools
.internal
.tmf
.core
.Messages
;
21 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
.indexer
.TmfMemoryIndex
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfEventProvider
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTraceCompleteness
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
33 import org
.eclipse
.linuxtools
.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
54 public class TmfCheckpointIndexer
implements ITmfTraceIndexer
{
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
60 /** The event trace to index */
61 protected final ITmfTrace fTrace
;
63 /** The interval between checkpoints */
64 private final int fCheckpointInterval
;
66 /** The event trace to index */
67 private boolean fIsIndexing
;
70 * The trace index. It is composed of checkpoints taken at intervals of
71 * fCheckpointInterval events.
73 protected final ITmfCheckpointIndex fTraceIndex
;
76 * The indexing request
78 private ITmfEventRequest fIndexingRequest
= null;
80 // ------------------------------------------------------------------------
82 // ------------------------------------------------------------------------
85 * Basic constructor that uses the default trace block size as checkpoints
88 * @param trace the trace to index
90 public TmfCheckpointIndexer(final ITmfTrace trace
) {
91 this(trace
, TmfEventProvider
.DEFAULT_BLOCK_SIZE
);
97 * @param trace the trace to index
98 * @param interval the checkpoints interval
100 public TmfCheckpointIndexer(final ITmfTrace trace
, final int interval
) {
102 fCheckpointInterval
= interval
;
103 fTraceIndex
= createIndex(trace
);
108 * Creates the index instance. Classes extending this class
109 * can override this to provide a different index implementation.
111 * @param trace the trace to index
115 protected ITmfCheckpointIndex
createIndex(final ITmfTrace trace
) {
116 return new TmfMemoryIndex(trace
);
120 public void dispose() {
121 if ((fIndexingRequest
!= null) && !fIndexingRequest
.isCompleted()) {
122 fIndexingRequest
.cancel();
125 fTraceIndex
.dispose();
128 // ------------------------------------------------------------------------
129 // ITmfTraceIndexer - isIndexing
130 // ------------------------------------------------------------------------
133 public boolean isIndexing() {
137 // ------------------------------------------------------------------------
138 // ITmfTraceIndexer - buildIndex
139 // ------------------------------------------------------------------------
145 public void buildIndex(final long offset
, final TmfTimeRange range
, final boolean waitForCompletion
) {
147 // Don't do anything if we are already indexing
148 synchronized (fTraceIndex
) {
155 // No need to build the index, it has been restored
156 if (!fTraceIndex
.isCreatedFromScratch()) {
157 // Set some trace attributes that depends on indexing
158 TmfTraceUpdatedSignal signal
= new TmfTraceUpdatedSignal(this, fTrace
, new TmfTimeRange(fTraceIndex
.getTimeRange().getStartTime(), fTraceIndex
.getTimeRange().getEndTime()), fTraceIndex
.getNbEvents());
159 if (waitForCompletion
) {
160 fTrace
.broadcast(signal
);
162 fTrace
.broadcastAsync(signal
);
168 // The monitoring job
169 final Job job
= new Job("Indexing " + fTrace
.getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
171 protected IStatus
run(final IProgressMonitor monitor
) {
172 monitor
.beginTask("", IProgressMonitor
.UNKNOWN
); //$NON-NLS-1$
173 while (!monitor
.isCanceled()) {
175 long prevNbEvents
= fTrace
.getNbEvents();
177 long nbEvents
= fTrace
.getNbEvents();
178 setName(Messages
.TmfCheckpointIndexer_Indexing
+ ' ' + fTrace
.getName() + " (" + nbEvents
+ ")"); //$NON-NLS-1$ //$NON-NLS-2$
179 // setName doesn't refresh the UI, setTaskName does
180 long rate
= (nbEvents
- prevNbEvents
) * 4;
181 monitor
.setTaskName(rate
+ " " + Messages
.TmfCheckpointIndexer_EventsPerSecond
); //$NON-NLS-1$
182 } catch (final InterruptedException e
) {
183 return Status
.OK_STATUS
;
187 return Status
.OK_STATUS
;
190 job
.setSystem(!isCompleteTrace(fTrace
));
193 // Build a background request for all the trace data. The index is
194 // updated as we go by readNextEvent().
195 fIndexingRequest
= new TmfEventRequest(ITmfEvent
.class,
196 range
, offset
, ITmfEventRequest
.ALL_DATA
,
197 ITmfEventRequest
.ExecutionType
.BACKGROUND
) {
199 public void handleData(final ITmfEvent event
) {
200 super.handleData(event
);
201 // Update the trace status at regular intervals
202 if ((getNbRead() % fCheckpointInterval
) == 0) {
208 public void handleSuccess() {
209 fTraceIndex
.setTimeRange(fTrace
.getTimeRange());
210 fTraceIndex
.setNbEvents(fTrace
.getNbEvents());
211 if (isCompleteTrace(fTrace
)) {
212 fTraceIndex
.setIndexComplete();
218 public void handleCompleted() {
220 super.handleCompleted();
224 private void updateTraceStatus() {
225 if (fTrace
.getNbEvents() > 0) {
226 signalNewTimeRange(fTrace
.getStartTime(), fTrace
.getEndTime());
231 // Submit the request and wait for completion if required
232 fTrace
.sendRequest(fIndexingRequest
);
233 if (waitForCompletion
) {
235 fIndexingRequest
.waitForCompletion();
236 } catch (final InterruptedException e
) {
242 * Notify the interested parties that the trace time range has changed
244 * @param startTime the new start time
245 * @param endTime the new end time
247 private void signalNewTimeRange(final ITmfTimestamp startTime
, final ITmfTimestamp endTime
) {
248 fTrace
.broadcast(new TmfTraceUpdatedSignal(fTrace
, fTrace
, new TmfTimeRange(startTime
, endTime
), fTrace
.getNbEvents()));
251 // ------------------------------------------------------------------------
252 // ITmfTraceIndexer - updateIndex
253 // ------------------------------------------------------------------------
259 public synchronized void updateIndex(final ITmfContext context
, final ITmfTimestamp timestamp
) {
260 if ((context
.getRank() % fCheckpointInterval
) == 0) {
261 // Determine the table position
262 final long position
= context
.getRank() / fCheckpointInterval
;
263 // Add new entry at proper location (if empty)
264 if (fTraceIndex
.size() == position
) {
265 fTraceIndex
.insert(new TmfCheckpoint(timestamp
, context
.getLocation(), position
));
270 // ------------------------------------------------------------------------
271 // ITmfTraceIndexer - seekIndex
272 // ------------------------------------------------------------------------
278 public synchronized ITmfContext
seekIndex(final ITmfTimestamp timestamp
) {
280 // A null timestamp indicates to seek the first event
281 if (timestamp
== null) {
282 return fTrace
.seekEvent(0);
285 // Find the checkpoint at or before the requested timestamp.
286 // In the very likely event that the timestamp is not at a checkpoint
287 // boundary, bsearch will return index = (- (insertion point + 1)).
288 // It is then trivial to compute the index of the previous checkpoint.
289 long index
= fTraceIndex
.binarySearch(new TmfCheckpoint(timestamp
, null, 0));
291 index
= Math
.max(0, -(index
+ 2));
293 // If timestamp was in the list, use previous index to be able to find the
294 // first event with the same timestamp before the checkpoint
295 index
= Math
.max(0, index
- 1);
298 // Position the trace at the checkpoint
299 return restoreCheckpoint(index
);
303 public ITmfContext
seekIndex(final long rank
) {
305 // A rank < 0 indicates to seek the first event
307 return fTrace
.seekEvent(0);
310 // Find the checkpoint at or before the requested rank.
311 final int index
= (int) rank
/ fCheckpointInterval
;
313 // Position the trace at the checkpoint
314 return restoreCheckpoint(index
);
318 * Position the trace at the given checkpoint
320 * @param checkpoint the checkpoint index
321 * @return the corresponding context
323 private ITmfContext
restoreCheckpoint(final long checkpoint
) {
324 ITmfLocation location
= null;
326 synchronized (fTraceIndex
) {
327 if (!fTraceIndex
.isEmpty()) {
329 if (index
>= fTraceIndex
.size()) {
330 index
= fTraceIndex
.size() - 1;
332 location
= fTraceIndex
.get(index
).getLocation();
335 final ITmfContext context
= fTrace
.seekEvent(location
);
336 context
.setRank(index
* fCheckpointInterval
);
340 // ------------------------------------------------------------------------
342 // ------------------------------------------------------------------------
345 * @return the trace index
348 protected ITmfCheckpointIndex
getTraceIndex() {
352 private static boolean isCompleteTrace(ITmfTrace trace
) {
353 return !(trace
instanceof ITmfTraceCompleteness
) || ((ITmfTraceCompleteness
)trace
).isComplete();