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
.Activator
;
22 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Messages
;
23 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.TmfCoreTracer
;
24 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.trace
.indexer
.TmfMemoryIndex
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.component
.TmfEventProvider
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.request
.TmfEventRequest
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
39 * A simple indexer that manages the trace index as an array of trace
40 * checkpoints. Checkpoints are stored in memory at fixed intervals (event rank)
41 * in ascending timestamp order.
43 * The goal being to access a random trace event reasonably fast from the user's
44 * standpoint, picking the right interval value becomes a trade-off between
45 * speed and memory usage (a shorter inter-event interval is faster but requires
48 * Locating a specific checkpoint is trivial for both rank (rank % interval) and
49 * timestamp (bsearch in the array). *
54 * @author Francois Chouinard
56 public class TmfCheckpointIndexer
implements ITmfTraceIndexer
{
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
62 /** The event trace to index */
63 protected final ITmfTrace fTrace
;
65 /** The interval between checkpoints */
66 private final int fCheckpointInterval
;
68 /** The event trace to index */
69 private boolean fIsIndexing
;
72 * The trace index. It is composed of checkpoints taken at intervals of
73 * fCheckpointInterval events.
75 protected final ITmfCheckpointIndex fTraceIndex
;
78 * The indexing request
80 private ITmfEventRequest fIndexingRequest
= null;
82 /** Whether or not the index was built once */
83 private boolean fBuiltOnce
;
85 // ------------------------------------------------------------------------
87 // ------------------------------------------------------------------------
90 * Basic constructor that uses the default trace block size as checkpoints
96 public TmfCheckpointIndexer(final ITmfTrace trace
) {
97 this(trace
, TmfEventProvider
.DEFAULT_BLOCK_SIZE
);
106 * the checkpoints interval
108 public TmfCheckpointIndexer(final ITmfTrace trace
, final int interval
) {
110 fCheckpointInterval
= interval
;
111 fTraceIndex
= createIndex(trace
);
116 * Creates the index instance. Classes extending this class can override
117 * this to provide a different index implementation.
123 protected ITmfCheckpointIndex
createIndex(final ITmfTrace trace
) {
124 return new TmfMemoryIndex(trace
);
128 public void dispose() {
129 if ((fIndexingRequest
!= null) && !fIndexingRequest
.isCompleted()) {
130 fIndexingRequest
.cancel();
133 fTraceIndex
.dispose();
136 // ------------------------------------------------------------------------
137 // ITmfTraceIndexer - isIndexing
138 // ------------------------------------------------------------------------
141 public boolean isIndexing() {
145 // ------------------------------------------------------------------------
146 // ITmfTraceIndexer - buildIndex
147 // ------------------------------------------------------------------------
150 public void buildIndex(final long offset
, final TmfTimeRange range
, final boolean waitForCompletion
) {
152 long indexingOffset
= offset
;
153 TmfTimeRange indexingTimeRange
= range
;
155 // Don't do anything if we are already indexing
156 synchronized (fTraceIndex
) {
163 // Restore previously built index values
164 if (!fTraceIndex
.isCreatedFromScratch() && !fBuiltOnce
&& fTraceIndex
.getNbEvents() > 0) {
165 indexingOffset
= fTraceIndex
.getNbEvents();
166 indexingTimeRange
= new TmfTimeRange(fTraceIndex
.getTimeRange().getStartTime(), TmfTimestamp
.BIG_CRUNCH
);
167 TmfCoreTracer
.traceIndexer("restoring index. nbEvents: " + fTraceIndex
.getNbEvents() + " time range: " + fTraceIndex
.getTimeRange()); //$NON-NLS-1$ //$NON-NLS-2$
168 // Set some trace attributes that depends on indexing
169 TmfTraceUpdatedSignal signal
= new TmfTraceUpdatedSignal(this, fTrace
, new TmfTimeRange(fTraceIndex
.getTimeRange().getStartTime(), fTraceIndex
.getTimeRange().getEndTime()), indexingOffset
);
170 fTrace
.broadcast(signal
);
173 TmfCoreTracer
.traceIndexer("buildIndex. offset: " + indexingOffset
+ " (requested " + offset
+ ")" + " time range: " + range
); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
175 // The monitoring job
176 TmfIndexingJob job
= new TmfIndexingJob("Indexing " + fTrace
.getName() + "..."); //$NON-NLS-1$ //$NON-NLS-2$
177 job
.setSystem(fBuiltOnce
);
181 // Build a background request for all the trace data. The index is
182 // updated as we go by readNextEvent().
183 fIndexingRequest
= new TmfEventRequest(ITmfEvent
.class,
184 indexingTimeRange
, indexingOffset
, ITmfEventRequest
.ALL_DATA
,
185 ITmfEventRequest
.ExecutionType
.BACKGROUND
) {
187 public void handleData(final ITmfEvent event
) {
188 super.handleData(event
);
189 // Update the trace status at regular intervals
190 if ((getNbRead() % fCheckpointInterval
) == 0) {
196 public void handleSuccess() {
201 public void handleCompleted() {
203 fTraceIndex
.setTimeRange(fTrace
.getTimeRange());
204 fTraceIndex
.setNbEvents(fTrace
.getNbEvents());
205 super.handleCompleted();
207 TmfCoreTracer
.traceIndexer("Build index request completed. nbEvents: " + fTraceIndex
.getNbEvents() + " time range: " + fTraceIndex
.getTimeRange()); //$NON-NLS-1$ //$NON-NLS-2$
211 public void fail(Exception e
) {
216 private void updateTraceStatus() {
217 if (fTrace
.getNbEvents() > 0) {
218 signalNewTimeRange(fTrace
.getStartTime(), fTrace
.getEndTime());
223 // Submit the request and wait for completion if required
224 fTrace
.sendRequest(fIndexingRequest
);
225 if (waitForCompletion
) {
227 fIndexingRequest
.waitForCompletion();
228 } catch (final InterruptedException e
) {
234 * Notify the interested parties that the trace time range has changed
241 private void signalNewTimeRange(final @NonNull ITmfTimestamp startTime
, final @NonNull ITmfTimestamp endTime
) {
242 fTrace
.broadcast(new TmfTraceUpdatedSignal(fTrace
, fTrace
, new TmfTimeRange(startTime
, endTime
), fTrace
.getNbEvents()));
245 // ------------------------------------------------------------------------
246 // ITmfTraceIndexer - updateIndex
247 // ------------------------------------------------------------------------
250 public synchronized void updateIndex(final ITmfContext context
, final ITmfTimestamp timestamp
) {
251 if ((context
.getRank() % fCheckpointInterval
) == 0) {
252 // Determine the table position
253 final long position
= context
.getRank() / fCheckpointInterval
;
254 // Add new entry at proper location (if empty)
255 if (fTraceIndex
.size() == position
) {
256 TmfCheckpoint checkpoint
= new TmfCheckpoint(timestamp
, context
.getLocation(), position
);
257 TmfCoreTracer
.traceIndexer("Inserting checkpoint: " + checkpoint
); //$NON-NLS-1$
258 fTraceIndex
.insert(checkpoint
);
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
285 // first event with the same timestamp before the checkpoint
286 index
= Math
.max(0, index
- 1);
289 // Position the trace at the checkpoint
290 return restoreCheckpoint(index
);
294 public ITmfContext
seekIndex(final long rank
) {
296 // A rank < 0 indicates to seek the first event
298 return fTrace
.seekEvent(0);
301 // Find the checkpoint at or before the requested rank.
302 final int index
= (int) rank
/ fCheckpointInterval
;
304 // Position the trace at the checkpoint
305 return restoreCheckpoint(index
);
309 * Position the trace at the given checkpoint
311 * @param checkpointIndex
312 * the checkpoint index
313 * @return the corresponding context
315 private ITmfContext
restoreCheckpoint(final long checkpointIndex
) {
316 ITmfLocation location
= null;
318 synchronized (fTraceIndex
) {
319 if (!fTraceIndex
.isEmpty()) {
320 index
= checkpointIndex
;
321 if (index
>= fTraceIndex
.size()) {
322 index
= fTraceIndex
.size() - 1;
324 ITmfCheckpoint checkpoint
= fTraceIndex
.get(index
);
325 TmfCoreTracer
.traceIndexer("Restored checkpoint: " + checkpoint
); //$NON-NLS-1$
326 location
= checkpoint
.getLocation();
329 final ITmfContext context
= fTrace
.seekEvent(location
);
330 context
.setRank(index
* fCheckpointInterval
);
334 // ------------------------------------------------------------------------
336 // ------------------------------------------------------------------------
339 * @return the trace index
341 protected ITmfCheckpointIndex
getTraceIndex() {
345 private final class TmfIndexingJob
extends Job
{
346 private Exception fException
= null;
348 private TmfIndexingJob(String name
) {
353 protected IStatus
run(final IProgressMonitor monitor
) {
354 monitor
.beginTask("", IProgressMonitor
.UNKNOWN
); //$NON-NLS-1$
355 while (!monitor
.isCanceled()) {
357 long prevNbEvents
= fTrace
.getNbEvents();
359 long nbEvents
= fTrace
.getNbEvents();
360 setName(Messages
.TmfCheckpointIndexer_Indexing
+ ' ' + fTrace
.getName() + " (" + String
.format("%,d", nbEvents
) + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
361 // setName doesn't refresh the UI, setTaskName does
362 long rate
= (nbEvents
- prevNbEvents
) * 4;
363 monitor
.setTaskName(String
.format("%,d", rate
) + " " + Messages
.TmfCheckpointIndexer_EventsPerSecond
); //$NON-NLS-1$ //$NON-NLS-2$
364 } catch (final InterruptedException e
) {
365 return Status
.OK_STATUS
;
369 return fException
!= null ?
new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, fException
.getMessage(), fException
) : Status
.OK_STATUS
;
372 public void setException(Exception e
) {