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
.Collections
;
16 import java
.util
.Vector
;
18 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
19 import org
.eclipse
.core
.runtime
.IStatus
;
20 import org
.eclipse
.core
.runtime
.Status
;
21 import org
.eclipse
.core
.runtime
.jobs
.Job
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
32 * <b><u>TmfTraceIndexer</u></b>
34 * A simple trace indexer that builds an array of trace checkpoints. Checkpoints
35 * are stored at fixed intervals (event rank) in 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).
45 public class TmfCheckpointIndexer
<T
extends ITmfTrace
<ITmfEvent
>> implements ITmfTraceIndexer
<T
> {
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
52 * The event trace to index
54 private final ITmfTrace
<ITmfEvent
> fTrace
;
57 * The interval between checkpoints
59 protected final int fCheckpointInterval
;
62 * The trace index. It is composed of checkpoints taken at intervals of
63 * fCheckpointInterval events.
65 protected final Vector
<TmfCheckpoint
> fTraceIndex
;
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
72 * Basic constructor that uses the default trace block size as checkpoints
75 * @param trace the trace to index
77 public TmfCheckpointIndexer(final ITmfTrace
<ITmfEvent
> trace
) {
78 this(trace
, TmfTrace
.DEFAULT_BLOCK_SIZE
);
84 * @param trace the trace to index
85 * @param interval the checkpoints interval
87 public TmfCheckpointIndexer(final ITmfTrace
<ITmfEvent
> trace
, final int interval
) {
89 fCheckpointInterval
= interval
;
90 fTraceIndex
= new Vector
<TmfCheckpoint
>();
93 // ------------------------------------------------------------------------
94 // ITmfTraceIndexer - buildIndex
95 // ------------------------------------------------------------------------
98 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#indexTrace(boolean)
100 * The index is a list of contexts that point to events at regular interval
101 * (rank-wise) in the trace. After it is built, the index can be used to
102 * quickly access any event by rank or timestamp (using seekIndex()).
104 * The index is built simply by reading the trace
107 public void buildIndex(final boolean waitForCompletion
) {
109 // The monitoring job
110 final Job job
= new Job("Indexing " + fTrace
.getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
112 protected IStatus
run(final IProgressMonitor monitor
) {
113 while (!monitor
.isCanceled()) {
116 } catch (final InterruptedException e
) {
117 return Status
.OK_STATUS
;
121 return Status
.OK_STATUS
;
126 // Clear the checkpoints
129 // Build a background request for all the trace data. The index is
130 // updated as we go by getNextEvent().
131 final ITmfEventRequest
<ITmfEvent
> request
= new TmfEventRequest
<ITmfEvent
>(ITmfEvent
.class, TmfTimeRange
.ETERNITY
,
132 TmfDataRequest
.ALL_DATA
, fCheckpointInterval
, ITmfDataRequest
.ExecutionType
.BACKGROUND
)
134 ITmfTimestamp startTime
= null;
135 ITmfTimestamp lastTime
= null;
138 public void handleData(final ITmfEvent event
) {
139 super.handleData(event
);
141 final ITmfTimestamp timestamp
= event
.getTimestamp();
142 if (startTime
== null) {
143 startTime
= timestamp
.clone();
145 lastTime
= timestamp
.clone();
147 // Update the trace status at regular intervals
148 if ((getNbRead() % fCheckpointInterval
) == 0) {
155 public void handleSuccess() {
160 public void handleCompleted() {
162 super.handleCompleted();
165 private void updateTraceStatus() {
166 if (getNbRead() != 0) {
167 signalNewTimeRange(startTime
, lastTime
);
172 // Submit the request and wait for completion if required
173 fTrace
.sendRequest(request
);
174 if (waitForCompletion
) {
176 request
.waitForCompletion();
177 } catch (final InterruptedException e
) {
183 * Notify the interested parties that the trace time range has changed
185 * @param startTime the new start time
186 * @param endTime the new end time
188 private void signalNewTimeRange(final ITmfTimestamp startTime
, final ITmfTimestamp endTime
) {
189 fTrace
.broadcast(new TmfTraceUpdatedSignal(fTrace
, fTrace
, new TmfTimeRange(startTime
, endTime
)));
192 // ------------------------------------------------------------------------
193 // ITmfTraceIndexer - updateIndex
194 // ------------------------------------------------------------------------
197 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#updateIndex(org.eclipse.linuxtools.tmf.core.trace.ITmfContext, org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
200 public synchronized void updateIndex(final ITmfContext context
, final ITmfTimestamp timestamp
) {
201 final long rank
= context
.getRank();
202 if ((rank
% fCheckpointInterval
) == 0) {
203 // Determine the table position
204 final long position
= rank
/ fCheckpointInterval
;
205 // Add new entry at proper location (if empty)
206 if (fTraceIndex
.size() == position
) {
207 final ITmfLocation
<?
> location
= context
.getLocation().clone();
208 fTraceIndex
.add(new TmfCheckpoint(timestamp
.clone(), location
));
213 // ------------------------------------------------------------------------
214 // ITmfTraceIndexer - seekIndex
215 // ------------------------------------------------------------------------
218 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
221 public synchronized ITmfContext
seekIndex(final ITmfTimestamp timestamp
) {
223 // A null timestamp indicates to seek the first event
224 if (timestamp
== null)
225 return fTrace
.seekEvent(0);
227 // Find the checkpoint at or before the requested timestamp.
228 // In the very likely event that the timestamp is not at a checkpoint
229 // boundary, bsearch will return index = (- (insertion point + 1)).
230 // It is then trivial to compute the index of the previous checkpoint.
231 int index
= Collections
.binarySearch(fTraceIndex
, new TmfCheckpoint(timestamp
, null));
233 index
= Math
.max(0, -(index
+ 2));
236 // Position the trace at the checkpoint
237 return seekCheckpoint(index
);
241 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(long)
244 public ITmfContext
seekIndex(final long rank
) {
246 // A rank < 0 indicates to seek the first event
248 return fTrace
.seekEvent(0);
250 // Find the checkpoint at or before the requested rank.
251 final int index
= (int) rank
/ fCheckpointInterval
;
253 // Position the trace at the checkpoint
254 return seekCheckpoint(index
);
258 * Position the trace at the given checkpoint
261 * the checkpoint index
262 * @return the corresponding context
264 private ITmfContext
seekCheckpoint(int index
) {
265 ITmfLocation
<?
> location
;
266 synchronized (fTraceIndex
) {
267 if (!fTraceIndex
.isEmpty()) {
268 if (index
>= fTraceIndex
.size()) {
269 index
= fTraceIndex
.size() - 1;
271 location
= fTraceIndex
.elementAt(index
).getLocation();
276 final ITmfContext context
= fTrace
.seekEvent(location
);
277 context
.setRank((long) index
* fCheckpointInterval
);
This page took 0.039344 seconds and 5 git commands to generate.