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
.jdt
.annotation
.NonNull
;
21 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Messages
;
22 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.trace
.indexer
.TmfMemoryIndex
;
23 import org
.eclipse
.tracecompass
.tmf
.core
.component
.TmfEventProvider
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.request
.TmfEventRequest
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTraceCompleteness
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
37 * A simple indexer that manages the trace index as an array of trace
38 * checkpoints. Checkpoints are stored in memory at fixed intervals (event rank) in
39 * ascending timestamp order.
41 * The goal being to access a random trace event reasonably fast from the user's
42 * standpoint, picking the right interval value becomes a trade-off between speed
43 * and memory usage (a shorter inter-event interval is faster but requires more
46 * Locating a specific checkpoint is trivial for both rank (rank % interval) and
47 * timestamp (bsearch in the array).
52 * @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
114 protected ITmfCheckpointIndex
createIndex(final ITmfTrace trace
) {
115 return new TmfMemoryIndex(trace
);
119 public void dispose() {
120 if ((fIndexingRequest
!= null) && !fIndexingRequest
.isCompleted()) {
121 fIndexingRequest
.cancel();
124 fTraceIndex
.dispose();
127 // ------------------------------------------------------------------------
128 // ITmfTraceIndexer - isIndexing
129 // ------------------------------------------------------------------------
132 public boolean isIndexing() {
136 // ------------------------------------------------------------------------
137 // ITmfTraceIndexer - buildIndex
138 // ------------------------------------------------------------------------
141 public void buildIndex(final long offset
, final TmfTimeRange range
, final boolean waitForCompletion
) {
143 // Don't do anything if we are already indexing
144 synchronized (fTraceIndex
) {
151 // No need to build the index, it has been restored
152 if (!fTraceIndex
.isCreatedFromScratch()) {
153 // Set some trace attributes that depends on indexing
154 TmfTraceUpdatedSignal signal
= new TmfTraceUpdatedSignal(this, fTrace
, new TmfTimeRange(fTraceIndex
.getTimeRange().getStartTime(), fTraceIndex
.getTimeRange().getEndTime()), fTraceIndex
.getNbEvents());
155 if (waitForCompletion
) {
156 fTrace
.broadcast(signal
);
158 fTrace
.broadcastAsync(signal
);
164 // The monitoring job
165 final Job job
= new Job("Indexing " + fTrace
.getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
167 protected IStatus
run(final IProgressMonitor monitor
) {
168 monitor
.beginTask("", IProgressMonitor
.UNKNOWN
); //$NON-NLS-1$
169 while (!monitor
.isCanceled()) {
171 long prevNbEvents
= fTrace
.getNbEvents();
173 long nbEvents
= fTrace
.getNbEvents();
174 setName(Messages
.TmfCheckpointIndexer_Indexing
+ ' ' + fTrace
.getName() + " (" + nbEvents
+ ")"); //$NON-NLS-1$ //$NON-NLS-2$
175 // setName doesn't refresh the UI, setTaskName does
176 long rate
= (nbEvents
- prevNbEvents
) * 4;
177 monitor
.setTaskName(rate
+ " " + Messages
.TmfCheckpointIndexer_EventsPerSecond
); //$NON-NLS-1$
178 } catch (final InterruptedException e
) {
179 return Status
.OK_STATUS
;
183 return Status
.OK_STATUS
;
186 job
.setSystem(!isCompleteTrace(fTrace
));
189 // Build a background request for all the trace data. The index is
190 // updated as we go by readNextEvent().
191 fIndexingRequest
= new TmfEventRequest(ITmfEvent
.class,
192 range
, offset
, ITmfEventRequest
.ALL_DATA
,
193 ITmfEventRequest
.ExecutionType
.BACKGROUND
) {
195 public void handleData(final ITmfEvent event
) {
196 super.handleData(event
);
197 // Update the trace status at regular intervals
198 if ((getNbRead() % fCheckpointInterval
) == 0) {
204 public void handleSuccess() {
205 fTraceIndex
.setTimeRange(fTrace
.getTimeRange());
206 fTraceIndex
.setNbEvents(fTrace
.getNbEvents());
207 if (isCompleteTrace(fTrace
)) {
208 fTraceIndex
.setIndexComplete();
214 public void handleCompleted() {
216 super.handleCompleted();
220 private void updateTraceStatus() {
221 if (fTrace
.getNbEvents() > 0) {
222 signalNewTimeRange(fTrace
.getStartTime(), fTrace
.getEndTime());
227 // Submit the request and wait for completion if required
228 fTrace
.sendRequest(fIndexingRequest
);
229 if (waitForCompletion
) {
231 fIndexingRequest
.waitForCompletion();
232 } catch (final InterruptedException e
) {
238 * Notify the interested parties that the trace time range has changed
240 * @param startTime the new start time
241 * @param endTime the new end time
243 private void signalNewTimeRange(final @NonNull ITmfTimestamp startTime
, final @NonNull ITmfTimestamp endTime
) {
244 fTrace
.broadcast(new TmfTraceUpdatedSignal(fTrace
, fTrace
, new TmfTimeRange(startTime
, endTime
), fTrace
.getNbEvents()));
247 // ------------------------------------------------------------------------
248 // ITmfTraceIndexer - updateIndex
249 // ------------------------------------------------------------------------
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 // ------------------------------------------------------------------------
268 public synchronized ITmfContext
seekIndex(final ITmfTimestamp timestamp
) {
270 // A null timestamp indicates to seek the first event
271 if (timestamp
== null) {
272 return fTrace
.seekEvent(0);
275 // Find the checkpoint at or before the requested timestamp.
276 // In the very likely event that the timestamp is not at a checkpoint
277 // boundary, bsearch will return index = (- (insertion point + 1)).
278 // It is then trivial to compute the index of the previous checkpoint.
279 long index
= fTraceIndex
.binarySearch(new TmfCheckpoint(timestamp
, null, 0));
281 index
= Math
.max(0, -(index
+ 2));
283 // If timestamp was in the list, use previous index to be able to find the
284 // first event with the same timestamp before the checkpoint
285 index
= Math
.max(0, index
- 1);
288 // Position the trace at the checkpoint
289 return restoreCheckpoint(index
);
293 public ITmfContext
seekIndex(final long rank
) {
295 // A rank < 0 indicates to seek the first event
297 return fTrace
.seekEvent(0);
300 // Find the checkpoint at or before the requested rank.
301 final int index
= (int) rank
/ fCheckpointInterval
;
303 // Position the trace at the checkpoint
304 return restoreCheckpoint(index
);
308 * Position the trace at the given checkpoint
310 * @param checkpoint the checkpoint index
311 * @return the corresponding context
313 private ITmfContext
restoreCheckpoint(final long checkpoint
) {
314 ITmfLocation location
= null;
316 synchronized (fTraceIndex
) {
317 if (!fTraceIndex
.isEmpty()) {
319 if (index
>= fTraceIndex
.size()) {
320 index
= fTraceIndex
.size() - 1;
322 location
= fTraceIndex
.get(index
).getLocation();
325 final ITmfContext context
= fTrace
.seekEvent(location
);
326 context
.setRank(index
* fCheckpointInterval
);
330 // ------------------------------------------------------------------------
332 // ------------------------------------------------------------------------
335 * @return the trace index
337 protected ITmfCheckpointIndex
getTraceIndex() {
341 private static boolean isCompleteTrace(ITmfTrace trace
) {
342 return !(trace
instanceof ITmfTraceCompleteness
) || ((ITmfTraceCompleteness
)trace
).isComplete();