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
<TmfCheckpoint
> fTraceIndex
;
72 // ------------------------------------------------------------------------
74 // ------------------------------------------------------------------------
77 * Basic constructor that uses the default trace block size as checkpoints
80 * @param trace the trace to index
82 public TmfCheckpointIndexer(final ITmfTrace
<ITmfEvent
> trace
) {
83 this(trace
, TmfTrace
.DEFAULT_BLOCK_SIZE
);
89 * @param trace the trace to index
90 * @param interval the checkpoints interval
92 public TmfCheckpointIndexer(final ITmfTrace
<ITmfEvent
> trace
, final int interval
) {
94 fCheckpointInterval
= interval
;
95 fTraceIndex
= new ArrayList
<TmfCheckpoint
>();
99 // ------------------------------------------------------------------------
100 // ITmfTraceIndexer - isIndexing
101 // ------------------------------------------------------------------------
104 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#isIndexing()
107 public boolean isIndexing() {
111 // ------------------------------------------------------------------------
112 // ITmfTraceIndexer - buildIndex
113 // ------------------------------------------------------------------------
117 * The index is a list of contexts that point to events at regular interval
118 * (rank-wise) in the trace. After it is built, the index can be used to
119 * quickly access any event by rank or timestamp (using seekIndex()).
121 * The index is built simply by reading the trace
123 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#buildIndex(long, org.eclipse.linuxtools.tmf.core.event.TmfTimeRange, boolean)
126 public void buildIndex(final long offset
, final TmfTimeRange range
, final boolean waitForCompletion
) {
128 // Don't do anything if we are already indexing
129 synchronized (fTraceIndex
) {
136 // The monitoring job
137 final Job job
= new Job("Indexing " + fTrace
.getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
139 protected IStatus
run(final IProgressMonitor monitor
) {
140 while (!monitor
.isCanceled()) {
143 } catch (final InterruptedException e
) {
144 return Status
.OK_STATUS
;
148 return Status
.OK_STATUS
;
153 // Clear the checkpoints
156 // Build a background request for all the trace data. The index is
157 // updated as we go by readNextEvent().
158 final ITmfEventRequest
<ITmfEvent
> request
= new TmfEventRequest
<ITmfEvent
>(ITmfEvent
.class,
159 range
, offset
, TmfDataRequest
.ALL_DATA
, fCheckpointInterval
, ITmfDataRequest
.ExecutionType
.BACKGROUND
)
161 private ITmfTimestamp startTime
= null;
162 private ITmfTimestamp lastTime
= null;
165 public void handleData(final ITmfEvent event
) {
166 super.handleData(event
);
168 final ITmfTimestamp timestamp
= event
.getTimestamp();
169 if (startTime
== null) {
170 startTime
= timestamp
.clone();
172 lastTime
= timestamp
.clone();
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 (getNbRead() != 0) {
195 signalNewTimeRange(startTime
, lastTime
);
200 // Submit the request and wait for completion if required
201 fTrace
.sendRequest(request
);
202 if (waitForCompletion
) {
204 request
.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 // ------------------------------------------------------------------------
225 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#updateIndex(org.eclipse.linuxtools.tmf.core.trace.ITmfContext, org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
228 public synchronized void updateIndex(final ITmfContext context
, final ITmfTimestamp timestamp
) {
229 final long rank
= context
.getRank();
230 if ((rank
% fCheckpointInterval
) == 0) {
231 // Determine the table position
232 final long position
= rank
/ fCheckpointInterval
;
233 // Add new entry at proper location (if empty)
234 if (fTraceIndex
.size() == position
) {
235 final ITmfLocation
<?
> location
= context
.getLocation().clone();
236 fTraceIndex
.add(new TmfCheckpoint(timestamp
.clone(), location
));
241 // ------------------------------------------------------------------------
242 // ITmfTraceIndexer - seekIndex
243 // ------------------------------------------------------------------------
246 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
249 public synchronized ITmfContext
seekIndex(final ITmfTimestamp timestamp
) {
251 // A null timestamp indicates to seek the first event
252 if (timestamp
== null) {
253 return fTrace
.seekEvent(0);
256 // Find the checkpoint at or before the requested timestamp.
257 // In the very likely event that the timestamp is not at a checkpoint
258 // boundary, bsearch will return index = (- (insertion point + 1)).
259 // It is then trivial to compute the index of the previous checkpoint.
260 int index
= Collections
.binarySearch(fTraceIndex
, new TmfCheckpoint(timestamp
, null));
262 index
= Math
.max(0, -(index
+ 2));
265 // Position the trace at the checkpoint
266 return seekCheckpoint(index
);
270 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(long)
273 public ITmfContext
seekIndex(final long rank
) {
275 // A rank < 0 indicates to seek the first event
277 return fTrace
.seekEvent(0);
280 // Find the checkpoint at or before the requested rank.
281 final int index
= (int) rank
/ fCheckpointInterval
;
283 // Position the trace at the checkpoint
284 return seekCheckpoint(index
);
288 * Position the trace at the given checkpoint
290 * @param checkpoint the checkpoint index
291 * @return the corresponding context
293 private ITmfContext
seekCheckpoint(final int checkpoint
) {
294 ITmfLocation
<?
> location
= null;
295 int index
= checkpoint
;
296 synchronized (fTraceIndex
) {
297 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
<TmfCheckpoint
> getTraceIndex() {
This page took 0.038163 seconds and 6 git commands to generate.