7c388abdfa73f0667fb2c1c8364f65337f97ee88
1 /*******************************************************************************
2 * Copyright (c) 2012 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
.tmf
.core
.event
.ITmfEvent
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
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
;
33 * A simple indexer that manages the trace index as an array of trace
34 * checkpoints. Checkpoints are stored at fixed intervals (event rank) in
35 * ascending timestamp order.
37 * The goal being to access a random trace event reasonably fast from the user's
38 * standpoint, picking the right interval value becomes a trade-off between speed
39 * and memory usage (a shorter inter-event interval is faster but requires more
42 * Locating a specific checkpoint is trivial for both rank (rank % interval) and
43 * timestamp (bsearch in the array).
46 * @author Francois Chouinard
51 public class TmfCheckpointIndexer
<T
extends ITmfTrace
<ITmfEvent
>> implements ITmfTraceIndexer
<T
> {
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
57 // The event trace to index
58 private final ITmfTrace
<ITmfEvent
> fTrace
;
60 // The interval between checkpoints
61 private final int fCheckpointInterval
;
63 // The event trace to index
64 private boolean fIsIndexing
;
67 * The trace index. It is composed of checkpoints taken at intervals of
68 * fCheckpointInterval events.
70 private final List
<ITmfCheckpoint
> fTraceIndex
;
73 * The indexing request
75 private ITmfEventRequest
<ITmfEvent
> fIndexingRequest
= null;
77 // ------------------------------------------------------------------------
79 // ------------------------------------------------------------------------
82 * Basic constructor that uses the default trace block size as checkpoints
85 * @param trace the trace to index
87 public TmfCheckpointIndexer(final ITmfTrace
<ITmfEvent
> trace
) {
88 this(trace
, TmfTrace
.DEFAULT_BLOCK_SIZE
);
94 * @param trace the trace to index
95 * @param interval the checkpoints interval
97 public TmfCheckpointIndexer(final ITmfTrace
<ITmfEvent
> trace
, final int interval
) {
99 fCheckpointInterval
= interval
;
100 fTraceIndex
= new ArrayList
<ITmfCheckpoint
>();
105 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#dispose()
108 public void dispose() {
109 if ((fIndexingRequest
!= null) && !fIndexingRequest
.isCompleted()) {
110 fIndexingRequest
.cancel();
115 // ------------------------------------------------------------------------
116 // ITmfTraceIndexer - isIndexing
117 // ------------------------------------------------------------------------
120 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#isIndexing()
123 public boolean isIndexing() {
127 // ------------------------------------------------------------------------
128 // ITmfTraceIndexer - buildIndex
129 // ------------------------------------------------------------------------
133 * The index is a list of contexts that point to events at regular interval
134 * (rank-wise) in the trace. After it is built, the index can be used to
135 * quickly access any event by rank or timestamp (using seekIndex()).
137 * The index is built simply by reading the trace
139 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#buildIndex(long, org.eclipse.linuxtools.tmf.core.event.TmfTimeRange, boolean)
142 public void buildIndex(final long offset
, final TmfTimeRange range
, final boolean waitForCompletion
) {
144 // Don't do anything if we are already indexing
145 synchronized (fTraceIndex
) {
152 // The monitoring job
153 final Job job
= new Job("Indexing " + fTrace
.getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
155 protected IStatus
run(final IProgressMonitor monitor
) {
156 while (!monitor
.isCanceled()) {
159 } catch (final InterruptedException e
) {
160 return Status
.OK_STATUS
;
164 return Status
.OK_STATUS
;
169 // Build a background request for all the trace data. The index is
170 // updated as we go by readNextEvent().
171 fIndexingRequest
= new TmfEventRequest
<ITmfEvent
>(ITmfEvent
.class,
172 range
, offset
, TmfDataRequest
.ALL_DATA
, fCheckpointInterval
, ITmfDataRequest
.ExecutionType
.BACKGROUND
)
174 private ITmfTimestamp startTime
= null;
175 private ITmfTimestamp lastTime
= null;
178 public void handleData(final ITmfEvent event
) {
179 super.handleData(event
);
181 final ITmfTimestamp timestamp
= event
.getTimestamp();
182 if (startTime
== null) {
183 startTime
= timestamp
.clone();
185 lastTime
= timestamp
.clone();
187 // Update the trace status at regular intervals
188 if ((getNbRead() % fCheckpointInterval
) == 0) {
195 public void handleSuccess() {
200 public void handleCompleted() {
202 super.handleCompleted();
206 private void updateTraceStatus() {
207 if (getNbRead() != 0) {
208 signalNewTimeRange(startTime
, lastTime
);
213 // Submit the request and wait for completion if required
214 fTrace
.sendRequest(fIndexingRequest
);
215 if (waitForCompletion
) {
217 fIndexingRequest
.waitForCompletion();
218 } catch (final InterruptedException e
) {
224 * Notify the interested parties that the trace time range has changed
226 * @param startTime the new start time
227 * @param endTime the new end time
229 private void signalNewTimeRange(final ITmfTimestamp startTime
, final ITmfTimestamp endTime
) {
230 fTrace
.broadcast(new TmfTraceUpdatedSignal(fTrace
, fTrace
, new TmfTimeRange(startTime
, endTime
)));
233 // ------------------------------------------------------------------------
234 // ITmfTraceIndexer - updateIndex
235 // ------------------------------------------------------------------------
238 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#updateIndex(org.eclipse.linuxtools.tmf.core.trace.ITmfContext, org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
241 public synchronized void updateIndex(final ITmfContext context
, final ITmfTimestamp timestamp
) {
242 final long rank
= context
.getRank();
243 if ((rank
% fCheckpointInterval
) == 0) {
244 // Determine the table position
245 final long position
= rank
/ fCheckpointInterval
;
246 // Add new entry at proper location (if empty)
247 if (fTraceIndex
.size() == position
) {
248 fTraceIndex
.add(new TmfCheckpoint(timestamp
.clone(), context
.clone()));
253 // ------------------------------------------------------------------------
254 // ITmfTraceIndexer - seekIndex
255 // ------------------------------------------------------------------------
258 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
261 public synchronized ITmfContext
seekIndex(final ITmfTimestamp timestamp
) {
263 // A null timestamp indicates to seek the first event
264 if (timestamp
== null) {
265 return fTrace
.seekEvent(0);
268 // Find the checkpoint at or before the requested timestamp.
269 // In the very likely event that the timestamp is not at a checkpoint
270 // boundary, bsearch will return index = (- (insertion point + 1)).
271 // It is then trivial to compute the index of the previous checkpoint.
272 int index
= Collections
.binarySearch(fTraceIndex
, new TmfCheckpoint(timestamp
, null));
274 index
= Math
.max(0, -(index
+ 2));
277 // Position the trace at the checkpoint
278 return seekCheckpoint(index
);
282 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(long)
285 public ITmfContext
seekIndex(final long rank
) {
287 // A rank < 0 indicates to seek the first event
289 return fTrace
.seekEvent(0);
292 // Find the checkpoint at or before the requested rank.
293 final int index
= (int) rank
/ fCheckpointInterval
;
295 // Position the trace at the checkpoint
296 return seekCheckpoint(index
);
300 * Position the trace at the given checkpoint
302 * @param checkpoint the checkpoint index
303 * @return the corresponding context
305 private ITmfContext
seekCheckpoint(final int checkpoint
) {
306 ITmfLocation
<?
> location
= null;
308 synchronized (fTraceIndex
) {
309 if (!fTraceIndex
.isEmpty()) {
311 if (index
>= fTraceIndex
.size()) {
312 index
= fTraceIndex
.size() - 1;
314 location
= fTraceIndex
.get(index
).getLocation();
317 final ITmfContext context
= fTrace
.seekEvent(location
);
318 context
.setRank((long) index
* fCheckpointInterval
);
322 // ------------------------------------------------------------------------
324 // ------------------------------------------------------------------------
327 * @return the trace index
329 protected List
<ITmfCheckpoint
> getTraceIndex() {
This page took 0.037667 seconds and 5 git commands to generate.