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 * A simple trace indexer that builds an array of trace checkpoints. Checkpoints
33 * are stored at fixed intervals (event rank) in ascending timestamp order.
35 * The goal being to access a random trace event reasonably fast from the user's
36 * standpoint, picking the right interval value becomes a trade-off between speed
37 * and memory usage (a shorter inter-event interval is faster but requires more
40 * Locating a specific checkpoint is trivial for both rank (rank % interval) and
41 * timestamp (bsearch in the array).
45 * @author Francois Chouinard
47 * @see ITmfTraceIndexer
51 public class TmfCheckpointIndexer
<T
extends ITmfTrace
<ITmfEvent
>> implements ITmfTraceIndexer
<T
> {
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
58 * The event trace to index
60 private final ITmfTrace
<ITmfEvent
> fTrace
;
63 * The interval between checkpoints
65 protected final int fCheckpointInterval
;
68 * The trace index. It is composed of checkpoints taken at intervals of
69 * fCheckpointInterval events.
71 protected final Vector
<TmfCheckpoint
> fTraceIndex
;
73 // ------------------------------------------------------------------------
75 // ------------------------------------------------------------------------
78 * Basic constructor that uses the default trace block size as checkpoints
81 * @param trace the trace to index
83 public TmfCheckpointIndexer(final ITmfTrace
<ITmfEvent
> trace
) {
84 this(trace
, TmfTrace
.DEFAULT_BLOCK_SIZE
);
90 * @param trace the trace to index
91 * @param interval the checkpoints interval
93 public TmfCheckpointIndexer(final ITmfTrace
<ITmfEvent
> trace
, final int interval
) {
95 fCheckpointInterval
= interval
;
96 fTraceIndex
= new Vector
<TmfCheckpoint
>();
99 // ------------------------------------------------------------------------
100 // ITmfTraceIndexer - buildIndex
101 // ------------------------------------------------------------------------
104 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#indexTrace(boolean)
106 * The index is a list of contexts that point to events at regular interval
107 * (rank-wise) in the trace. After it is built, the index can be used to
108 * quickly access any event by rank or timestamp (using seekIndex()).
110 * The index is built simply by reading the trace
113 public void buildIndex(final boolean waitForCompletion
) {
115 // The monitoring job
116 final Job job
= new Job("Indexing " + fTrace
.getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
118 protected IStatus
run(final IProgressMonitor monitor
) {
119 while (!monitor
.isCanceled()) {
122 } catch (final InterruptedException e
) {
123 return Status
.OK_STATUS
;
127 return Status
.OK_STATUS
;
132 // Clear the checkpoints
135 // Build a background request for all the trace data. The index is
136 // updated as we go by getNextEvent().
137 final ITmfEventRequest
<ITmfEvent
> request
= new TmfEventRequest
<ITmfEvent
>(ITmfEvent
.class, TmfTimeRange
.ETERNITY
,
138 TmfDataRequest
.ALL_DATA
, fCheckpointInterval
, ITmfDataRequest
.ExecutionType
.BACKGROUND
)
140 ITmfTimestamp startTime
= null;
141 ITmfTimestamp lastTime
= null;
144 public void handleData(final ITmfEvent event
) {
145 super.handleData(event
);
147 final ITmfTimestamp timestamp
= event
.getTimestamp();
148 if (startTime
== null) {
149 startTime
= timestamp
.clone();
151 lastTime
= timestamp
.clone();
153 // Update the trace status at regular intervals
154 if ((getNbRead() % fCheckpointInterval
) == 0) {
161 public void handleSuccess() {
166 public void handleCompleted() {
168 super.handleCompleted();
171 private void updateTraceStatus() {
172 if (getNbRead() != 0) {
173 signalNewTimeRange(startTime
, lastTime
);
178 // Submit the request and wait for completion if required
179 fTrace
.sendRequest(request
);
180 if (waitForCompletion
) {
182 request
.waitForCompletion();
183 } catch (final InterruptedException e
) {
189 * Notify the interested parties that the trace time range has changed
191 * @param startTime the new start time
192 * @param endTime the new end time
194 private void signalNewTimeRange(final ITmfTimestamp startTime
, final ITmfTimestamp endTime
) {
195 fTrace
.broadcast(new TmfTraceUpdatedSignal(fTrace
, fTrace
, new TmfTimeRange(startTime
, endTime
)));
198 // ------------------------------------------------------------------------
199 // ITmfTraceIndexer - updateIndex
200 // ------------------------------------------------------------------------
203 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#updateIndex(org.eclipse.linuxtools.tmf.core.trace.ITmfContext, org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
206 public synchronized void updateIndex(final ITmfContext context
, final ITmfTimestamp timestamp
) {
207 final long rank
= context
.getRank();
208 if ((rank
% fCheckpointInterval
) == 0) {
209 // Determine the table position
210 final long position
= rank
/ fCheckpointInterval
;
211 // Add new entry at proper location (if empty)
212 if (fTraceIndex
.size() == position
) {
213 final ITmfLocation
<?
> location
= context
.getLocation().clone();
214 fTraceIndex
.add(new TmfCheckpoint(timestamp
.clone(), location
));
219 // ------------------------------------------------------------------------
220 // ITmfTraceIndexer - seekIndex
221 // ------------------------------------------------------------------------
224 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
227 public synchronized ITmfContext
seekIndex(final ITmfTimestamp timestamp
) {
229 // A null timestamp indicates to seek the first event
230 if (timestamp
== null)
231 return fTrace
.seekEvent(0);
233 // Find the checkpoint at or before the requested timestamp.
234 // In the very likely event that the timestamp is not at a checkpoint
235 // boundary, bsearch will return index = (- (insertion point + 1)).
236 // It is then trivial to compute the index of the previous checkpoint.
237 int index
= Collections
.binarySearch(fTraceIndex
, new TmfCheckpoint(timestamp
, null));
239 index
= Math
.max(0, -(index
+ 2));
242 // Position the trace at the checkpoint
243 return seekCheckpoint(index
);
247 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(long)
250 public ITmfContext
seekIndex(final long rank
) {
252 // A rank < 0 indicates to seek the first event
254 return fTrace
.seekEvent(0);
256 // Find the checkpoint at or before the requested rank.
257 final int index
= (int) rank
/ fCheckpointInterval
;
259 // Position the trace at the checkpoint
260 return seekCheckpoint(index
);
264 * Position the trace at the given checkpoint
267 * the checkpoint index
268 * @return the corresponding context
270 private ITmfContext
seekCheckpoint(int index
) {
271 ITmfLocation
<?
> location
;
272 synchronized (fTraceIndex
) {
273 if (!fTraceIndex
.isEmpty()) {
274 if (index
>= fTraceIndex
.size()) {
275 index
= fTraceIndex
.size() - 1;
277 location
= fTraceIndex
.elementAt(index
).getLocation();
282 final ITmfContext context
= fTrace
.seekEvent(location
);
283 context
.setRank((long) index
* fCheckpointInterval
);
This page took 0.037894 seconds and 6 git commands to generate.