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
;
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
.Messages
;
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
.request
.ITmfDataRequest
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
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 public void dispose() {
108 if ((fIndexingRequest
!= null) && !fIndexingRequest
.isCompleted()) {
109 fIndexingRequest
.cancel();
114 // ------------------------------------------------------------------------
115 // ITmfTraceIndexer - isIndexing
116 // ------------------------------------------------------------------------
119 public boolean isIndexing() {
123 // ------------------------------------------------------------------------
124 // ITmfTraceIndexer - buildIndex
125 // ------------------------------------------------------------------------
131 public void buildIndex(final long offset
, final TmfTimeRange range
, final boolean waitForCompletion
) {
133 // Don't do anything if we are already indexing
134 synchronized (fTraceIndex
) {
141 // The monitoring job
142 final Job job
= new Job("Indexing " + fTrace
.getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
144 protected IStatus
run(final IProgressMonitor monitor
) {
145 monitor
.beginTask("", IProgressMonitor
.UNKNOWN
); //$NON-NLS-1$
146 while (!monitor
.isCanceled()) {
148 long prevNbEvents
= fTrace
.getNbEvents();
150 long nbEvents
= fTrace
.getNbEvents();
151 setName(Messages
.TmfCheckpointIndexer_Indexing
+ ' ' + fTrace
.getName() + " (" + nbEvents
+ ")"); //$NON-NLS-1$ //$NON-NLS-2$
152 // setName doesn't refresh the UI, setTaskName does
153 long rate
= (nbEvents
- prevNbEvents
) * 4;
154 monitor
.setTaskName(rate
+ " " + Messages
.TmfCheckpointIndexer_EventsPerSecond
); //$NON-NLS-1$
155 } catch (final InterruptedException e
) {
156 return Status
.OK_STATUS
;
160 return Status
.OK_STATUS
;
165 // Build a background request for all the trace data. The index is
166 // updated as we go by readNextEvent().
167 fIndexingRequest
= new TmfEventRequest(ITmfEvent
.class,
168 range
, offset
, TmfDataRequest
.ALL_DATA
,
169 ITmfDataRequest
.ExecutionType
.BACKGROUND
) {
171 public void handleData(final ITmfEvent event
) {
172 super.handleData(event
);
174 // Update the trace status at regular intervals
175 if ((getNbRead() % fCheckpointInterval
) == 0) {
182 public void handleSuccess() {
187 public void handleCompleted() {
189 super.handleCompleted();
193 private void updateTraceStatus() {
194 if (fTrace
.getNbEvents() > 0) {
195 signalNewTimeRange(fTrace
.getStartTime(), fTrace
.getEndTime());
200 // Submit the request and wait for completion if required
201 fTrace
.sendRequest(fIndexingRequest
);
202 if (waitForCompletion
) {
204 fIndexingRequest
.waitForCompletion();
205 } catch (final InterruptedException e
) {
211 * Notify the interested parties that the trace time range has changed
213 * @param startTime the new start time
214 * @param endTime the new end time
216 private void signalNewTimeRange(final ITmfTimestamp startTime
, final ITmfTimestamp endTime
) {
217 fTrace
.broadcast(new TmfTraceUpdatedSignal(fTrace
, fTrace
, new TmfTimeRange(startTime
, endTime
)));
220 // ------------------------------------------------------------------------
221 // ITmfTraceIndexer - updateIndex
222 // ------------------------------------------------------------------------
228 public synchronized void updateIndex(final ITmfContext context
, final ITmfTimestamp timestamp
) {
229 if ((context
.getRank() % fCheckpointInterval
) == 0) {
230 // Determine the table position
231 final long position
= context
.getRank() / fCheckpointInterval
;
232 // Add new entry at proper location (if empty)
233 if (fTraceIndex
.size() == position
) {
234 fTraceIndex
.add(new TmfCheckpoint(timestamp
, context
.getLocation()));
239 // ------------------------------------------------------------------------
240 // ITmfTraceIndexer - seekIndex
241 // ------------------------------------------------------------------------
247 public synchronized ITmfContext
seekIndex(final ITmfTimestamp timestamp
) {
249 // A null timestamp indicates to seek the first event
250 if (timestamp
== null) {
251 return fTrace
.seekEvent(0);
254 // Find the checkpoint at or before the requested timestamp.
255 // In the very likely event that the timestamp is not at a checkpoint
256 // boundary, bsearch will return index = (- (insertion point + 1)).
257 // It is then trivial to compute the index of the previous checkpoint.
258 int index
= Collections
.binarySearch(fTraceIndex
, new TmfCheckpoint(timestamp
, null));
260 index
= Math
.max(0, -(index
+ 2));
262 // If timestamp was in the list, use previous index to be able to find the
263 // first event with the same timestamp before the checkpoint
264 index
= Math
.max(0, index
- 1);
267 // Position the trace at the checkpoint
268 return restoreCheckpoint(index
);
272 public ITmfContext
seekIndex(final long rank
) {
274 // A rank < 0 indicates to seek the first event
276 return fTrace
.seekEvent(0);
279 // Find the checkpoint at or before the requested rank.
280 final int index
= (int) rank
/ fCheckpointInterval
;
282 // Position the trace at the checkpoint
283 return restoreCheckpoint(index
);
287 * Position the trace at the given checkpoint
289 * @param checkpoint the checkpoint index
290 * @return the corresponding context
292 private ITmfContext
restoreCheckpoint(final int checkpoint
) {
293 ITmfLocation location
= null;
295 synchronized (fTraceIndex
) {
296 if (!fTraceIndex
.isEmpty()) {
298 if (index
>= fTraceIndex
.size()) {
299 index
= fTraceIndex
.size() - 1;
301 location
= fTraceIndex
.get(index
).getLocation();
304 final ITmfContext context
= fTrace
.seekEvent(location
);
305 context
.setRank((long) index
* fCheckpointInterval
);
309 // ------------------------------------------------------------------------
311 // ------------------------------------------------------------------------
314 * @return the trace index
316 protected List
<ITmfCheckpoint
> getTraceIndex() {