1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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
.indexer
.checkpoint
;
15 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
16 import org
.eclipse
.core
.runtime
.IStatus
;
17 import org
.eclipse
.core
.runtime
.Status
;
18 import org
.eclipse
.core
.runtime
.jobs
.Job
;
19 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Messages
;
20 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
.indexer
.TmfMemoryIndex
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfDataProvider
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
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
, TmfDataProvider
.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 fTrace
.broadcast(new TmfTraceUpdatedSignal(this, fTrace
, new TmfTimeRange(fTraceIndex
.getTimeRange().getStartTime(), fTraceIndex
.getTimeRange().getEndTime()), fTraceIndex
.getNbEvents()));
162 // The monitoring job
163 final Job job
= new Job("Indexing " + fTrace
.getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
165 protected IStatus
run(final IProgressMonitor monitor
) {
166 monitor
.beginTask("", IProgressMonitor
.UNKNOWN
); //$NON-NLS-1$
167 while (!monitor
.isCanceled()) {
169 long prevNbEvents
= fTrace
.getNbEvents();
171 long nbEvents
= fTrace
.getNbEvents();
172 setName(Messages
.TmfCheckpointIndexer_Indexing
+ ' ' + fTrace
.getName() + " (" + nbEvents
+ ")"); //$NON-NLS-1$ //$NON-NLS-2$
173 // setName doesn't refresh the UI, setTaskName does
174 long rate
= (nbEvents
- prevNbEvents
) * 4;
175 monitor
.setTaskName(rate
+ " " + Messages
.TmfCheckpointIndexer_EventsPerSecond
); //$NON-NLS-1$
176 } catch (final InterruptedException e
) {
177 return Status
.OK_STATUS
;
181 return Status
.OK_STATUS
;
186 // Build a background request for all the trace data. The index is
187 // updated as we go by readNextEvent().
188 fIndexingRequest
= new TmfEventRequest(ITmfEvent
.class,
189 range
, offset
, TmfDataRequest
.ALL_DATA
,
190 ITmfDataRequest
.ExecutionType
.BACKGROUND
) {
192 public void handleData(final ITmfEvent event
) {
193 super.handleData(event
);
195 // Update the trace status at regular intervals
196 if ((getNbRead() % fCheckpointInterval
) == 0) {
203 public void handleSuccess() {
204 fTraceIndex
.setTimeRange(fTrace
.getTimeRange());
205 fTraceIndex
.setNbEvents(fTrace
.getNbEvents());
206 fTraceIndex
.setIndexComplete();
211 public void handleCompleted() {
213 super.handleCompleted();
217 private void updateTraceStatus() {
218 if (fTrace
.getNbEvents() > 0) {
219 signalNewTimeRange(fTrace
.getStartTime(), fTrace
.getEndTime());
224 // Submit the request and wait for completion if required
225 fTrace
.sendRequest(fIndexingRequest
);
226 if (waitForCompletion
) {
228 fIndexingRequest
.waitForCompletion();
229 } catch (final InterruptedException e
) {
235 * Notify the interested parties that the trace time range has changed
237 * @param startTime the new start time
238 * @param endTime the new end time
240 private void signalNewTimeRange(final ITmfTimestamp startTime
, final ITmfTimestamp endTime
) {
241 fTrace
.broadcast(new TmfTraceUpdatedSignal(fTrace
, fTrace
, new TmfTimeRange(startTime
, endTime
), fTrace
.getNbEvents()));
244 // ------------------------------------------------------------------------
245 // ITmfTraceIndexer - updateIndex
246 // ------------------------------------------------------------------------
252 public synchronized void updateIndex(final ITmfContext context
, final ITmfTimestamp timestamp
) {
253 if ((context
.getRank() % fCheckpointInterval
) == 0) {
254 // Determine the table position
255 final long position
= context
.getRank() / fCheckpointInterval
;
256 // Add new entry at proper location (if empty)
257 if (fTraceIndex
.size() == position
) {
258 fTraceIndex
.insert(new TmfCheckpoint(timestamp
, context
.getLocation(), position
));
263 // ------------------------------------------------------------------------
264 // ITmfTraceIndexer - seekIndex
265 // ------------------------------------------------------------------------
271 public synchronized ITmfContext
seekIndex(final ITmfTimestamp timestamp
) {
273 // A null timestamp indicates to seek the first event
274 if (timestamp
== null) {
275 return fTrace
.seekEvent(0);
278 // Find the checkpoint at or before the requested timestamp.
279 // In the very likely event that the timestamp is not at a checkpoint
280 // boundary, bsearch will return index = (- (insertion point + 1)).
281 // It is then trivial to compute the index of the previous checkpoint.
282 long index
= fTraceIndex
.binarySearch(new TmfCheckpoint(timestamp
, null, 0));
284 index
= Math
.max(0, -(index
+ 2));
286 // If timestamp was in the list, use previous index to be able to find the
287 // first event with the same timestamp before the checkpoint
288 index
= Math
.max(0, index
- 1);
291 // Position the trace at the checkpoint
292 return restoreCheckpoint(index
);
296 public ITmfContext
seekIndex(final long rank
) {
298 // A rank < 0 indicates to seek the first event
300 return fTrace
.seekEvent(0);
303 // Find the checkpoint at or before the requested rank.
304 final int index
= (int) rank
/ fCheckpointInterval
;
306 // Position the trace at the checkpoint
307 return restoreCheckpoint(index
);
311 * Position the trace at the given checkpoint
313 * @param checkpoint the checkpoint index
314 * @return the corresponding context
316 private ITmfContext
restoreCheckpoint(final long checkpoint
) {
317 ITmfLocation location
= null;
319 synchronized (fTraceIndex
) {
320 if (!fTraceIndex
.isEmpty()) {
322 if (index
>= fTraceIndex
.size()) {
323 index
= fTraceIndex
.size() - 1;
325 location
= fTraceIndex
.get(index
).getLocation();
328 final ITmfContext context
= fTrace
.seekEvent(location
);
329 context
.setRank(index
* fCheckpointInterval
);
333 // ------------------------------------------------------------------------
335 // ------------------------------------------------------------------------
338 * @return the trace index
341 protected ITmfCheckpointIndex
getTraceIndex() {