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 indexer that manages the trace index as an array of trace
33 * checkpoints. Checkpoints are stored at fixed intervals (event rank) in
34 * ascending timestamp order.
36 * The goal being to access a random trace event reasonably fast from the user's
37 * standpoint, picking the right interval value becomes a trade-off between speed
38 * and memory usage (a shorter inter-event interval is faster but requires more
41 * Locating a specific checkpoint is trivial for both rank (rank % interval) and
42 * timestamp (bsearch in the array).
46 * @author Francois Chouinard
48 * @see ITmfTraceIndexer
52 public class TmfCheckpointIndexer
<T
extends ITmfTrace
<ITmfEvent
>> implements ITmfTraceIndexer
<T
> {
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
59 * The event trace to index
61 private final ITmfTrace
<ITmfEvent
> fTrace
;
64 * The interval between checkpoints
66 protected final int fCheckpointInterval
;
69 * The trace index. It is composed of checkpoints taken at intervals of
70 * fCheckpointInterval events.
72 protected final Vector
<TmfCheckpoint
> fTraceIndex
;
74 // ------------------------------------------------------------------------
76 // ------------------------------------------------------------------------
79 * Basic constructor that uses the default trace block size as checkpoints
82 * @param trace the trace to index
84 public TmfCheckpointIndexer(final ITmfTrace
<ITmfEvent
> trace
) {
85 this(trace
, TmfTrace
.DEFAULT_BLOCK_SIZE
);
91 * @param trace the trace to index
92 * @param interval the checkpoints interval
94 public TmfCheckpointIndexer(final ITmfTrace
<ITmfEvent
> trace
, final int interval
) {
96 fCheckpointInterval
= interval
;
97 fTraceIndex
= new Vector
<TmfCheckpoint
>();
100 // ------------------------------------------------------------------------
101 // ITmfTraceIndexer - buildIndex
102 // ------------------------------------------------------------------------
105 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#indexTrace(boolean)
107 * The index is a list of contexts that point to events at regular interval
108 * (rank-wise) in the trace. After it is built, the index can be used to
109 * quickly access any event by rank or timestamp (using seekIndex()).
111 * The index is built simply by reading the trace
114 public void buildIndex(final boolean waitForCompletion
) {
116 // The monitoring job
117 final Job job
= new Job("Indexing " + fTrace
.getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
119 protected IStatus
run(final IProgressMonitor monitor
) {
120 while (!monitor
.isCanceled()) {
123 } catch (final InterruptedException e
) {
124 return Status
.OK_STATUS
;
128 return Status
.OK_STATUS
;
133 // Clear the checkpoints
136 // Build a background request for all the trace data. The index is
137 // updated as we go by getNextEvent().
138 final ITmfEventRequest
<ITmfEvent
> request
= new TmfEventRequest
<ITmfEvent
>(ITmfEvent
.class, TmfTimeRange
.ETERNITY
,
139 TmfDataRequest
.ALL_DATA
, fCheckpointInterval
, ITmfDataRequest
.ExecutionType
.BACKGROUND
)
141 ITmfTimestamp startTime
= null;
142 ITmfTimestamp lastTime
= null;
145 public void handleData(final ITmfEvent event
) {
146 super.handleData(event
);
148 final ITmfTimestamp timestamp
= event
.getTimestamp();
149 if (startTime
== null) {
150 startTime
= timestamp
.clone();
152 lastTime
= timestamp
.clone();
154 // Update the trace status at regular intervals
155 if ((getNbRead() % fCheckpointInterval
) == 0) {
162 public void handleSuccess() {
167 public void handleCompleted() {
169 super.handleCompleted();
172 private void updateTraceStatus() {
173 if (getNbRead() != 0) {
174 signalNewTimeRange(startTime
, lastTime
);
179 // Submit the request and wait for completion if required
180 fTrace
.sendRequest(request
);
181 if (waitForCompletion
) {
183 request
.waitForCompletion();
184 } catch (final InterruptedException e
) {
190 * Notify the interested parties that the trace time range has changed
192 * @param startTime the new start time
193 * @param endTime the new end time
195 private void signalNewTimeRange(final ITmfTimestamp startTime
, final ITmfTimestamp endTime
) {
196 fTrace
.broadcast(new TmfTraceUpdatedSignal(fTrace
, fTrace
, new TmfTimeRange(startTime
, endTime
)));
199 // ------------------------------------------------------------------------
200 // ITmfTraceIndexer - updateIndex
201 // ------------------------------------------------------------------------
204 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#updateIndex(org.eclipse.linuxtools.tmf.core.trace.ITmfContext, org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
207 public synchronized void updateIndex(final ITmfContext context
, final ITmfTimestamp timestamp
) {
208 final long rank
= context
.getRank();
209 if ((rank
% fCheckpointInterval
) == 0) {
210 // Determine the table position
211 final long position
= rank
/ fCheckpointInterval
;
212 // Add new entry at proper location (if empty)
213 if (fTraceIndex
.size() == position
) {
214 final ITmfLocation
<?
> location
= context
.getLocation().clone();
215 fTraceIndex
.add(new TmfCheckpoint(timestamp
.clone(), location
));
220 // ------------------------------------------------------------------------
221 // ITmfTraceIndexer - seekIndex
222 // ------------------------------------------------------------------------
225 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
228 public synchronized ITmfContext
seekIndex(final ITmfTimestamp timestamp
) {
230 // A null timestamp indicates to seek the first event
231 if (timestamp
== null)
232 return fTrace
.seekEvent(0);
234 // Find the checkpoint at or before the requested timestamp.
235 // In the very likely event that the timestamp is not at a checkpoint
236 // boundary, bsearch will return index = (- (insertion point + 1)).
237 // It is then trivial to compute the index of the previous checkpoint.
238 int index
= Collections
.binarySearch(fTraceIndex
, new TmfCheckpoint(timestamp
, null));
240 index
= Math
.max(0, -(index
+ 2));
243 // Position the trace at the checkpoint
244 return seekCheckpoint(index
);
248 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(long)
251 public ITmfContext
seekIndex(final long rank
) {
253 // A rank < 0 indicates to seek the first event
255 return fTrace
.seekEvent(0);
257 // Find the checkpoint at or before the requested rank.
258 final int index
= (int) rank
/ fCheckpointInterval
;
260 // Position the trace at the checkpoint
261 return seekCheckpoint(index
);
265 * Position the trace at the given checkpoint
268 * the checkpoint index
269 * @return the corresponding context
271 private ITmfContext
seekCheckpoint(int index
) {
272 ITmfLocation
<?
> location
;
273 synchronized (fTraceIndex
) {
274 if (!fTraceIndex
.isEmpty()) {
275 if (index
>= fTraceIndex
.size()) {
276 index
= fTraceIndex
.size() - 1;
278 location
= fTraceIndex
.elementAt(index
).getLocation();
283 final ITmfContext context
= fTrace
.seekEvent(location
);
284 context
.setRank((long) index
* fCheckpointInterval
);
This page took 0.037719 seconds and 6 git commands to generate.