d6b62f86ca7806eddb512940426207a9a8c9c1fd
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
;
64 * The trace index. It is composed of checkpoints taken at intervals of
65 * fCheckpointInterval events.
67 private final List
<TmfCheckpoint
> fTraceIndex
;
69 // ------------------------------------------------------------------------
71 // ------------------------------------------------------------------------
74 * Basic constructor that uses the default trace block size as checkpoints
77 * @param trace the trace to index
79 public TmfCheckpointIndexer(final ITmfTrace
<ITmfEvent
> trace
) {
80 this(trace
, TmfTrace
.DEFAULT_BLOCK_SIZE
);
86 * @param trace the trace to index
87 * @param interval the checkpoints interval
89 public TmfCheckpointIndexer(final ITmfTrace
<ITmfEvent
> trace
, final int interval
) {
91 fCheckpointInterval
= interval
;
92 fTraceIndex
= new ArrayList
<TmfCheckpoint
>();
95 // ------------------------------------------------------------------------
96 // ITmfTraceIndexer - buildIndex
97 // ------------------------------------------------------------------------
100 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#indexTrace(boolean)
102 * The index is a list of contexts that point to events at regular interval
103 * (rank-wise) in the trace. After it is built, the index can be used to
104 * quickly access any event by rank or timestamp (using seekIndex()).
106 * The index is built simply by reading the trace
109 public void buildIndex(final boolean waitForCompletion
) {
111 // The monitoring job
112 final Job job
= new Job("Indexing " + fTrace
.getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
114 protected IStatus
run(final IProgressMonitor monitor
) {
115 while (!monitor
.isCanceled()) {
118 } catch (final InterruptedException e
) {
119 return Status
.OK_STATUS
;
123 return Status
.OK_STATUS
;
128 // Clear the checkpoints
131 // Build a background request for all the trace data. The index is
132 // updated as we go by readNextEvent().
133 final ITmfEventRequest
<ITmfEvent
> request
= new TmfEventRequest
<ITmfEvent
>(ITmfEvent
.class, TmfTimeRange
.ETERNITY
,
134 TmfDataRequest
.ALL_DATA
, fCheckpointInterval
, ITmfDataRequest
.ExecutionType
.BACKGROUND
)
136 private ITmfTimestamp startTime
= null;
137 private ITmfTimestamp lastTime
= null;
140 public void handleData(final ITmfEvent event
) {
141 super.handleData(event
);
143 final ITmfTimestamp timestamp
= event
.getTimestamp();
144 if (startTime
== null) {
145 startTime
= timestamp
.clone();
147 lastTime
= timestamp
.clone();
149 // Update the trace status at regular intervals
150 if ((getNbRead() % fCheckpointInterval
) == 0) {
157 public void handleSuccess() {
162 public void handleCompleted() {
164 super.handleCompleted();
167 private void updateTraceStatus() {
168 if (getNbRead() != 0) {
169 signalNewTimeRange(startTime
, lastTime
);
174 // Submit the request and wait for completion if required
175 fTrace
.sendRequest(request
);
176 if (waitForCompletion
) {
178 request
.waitForCompletion();
179 } catch (final InterruptedException e
) {
185 * Notify the interested parties that the trace time range has changed
187 * @param startTime the new start time
188 * @param endTime the new end time
190 private void signalNewTimeRange(final ITmfTimestamp startTime
, final ITmfTimestamp endTime
) {
191 fTrace
.broadcast(new TmfTraceUpdatedSignal(fTrace
, fTrace
, new TmfTimeRange(startTime
, endTime
)));
194 // ------------------------------------------------------------------------
195 // ITmfTraceIndexer - updateIndex
196 // ------------------------------------------------------------------------
199 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#updateIndex(org.eclipse.linuxtools.tmf.core.trace.ITmfContext, org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
202 public synchronized void updateIndex(final ITmfContext context
, final ITmfTimestamp timestamp
) {
203 final long rank
= context
.getRank();
204 if ((rank
% fCheckpointInterval
) == 0) {
205 // Determine the table position
206 final long position
= rank
/ fCheckpointInterval
;
207 // Add new entry at proper location (if empty)
208 if (fTraceIndex
.size() == position
) {
209 final ITmfLocation
<?
> location
= context
.getLocation().clone();
210 fTraceIndex
.add(new TmfCheckpoint(timestamp
.clone(), location
));
215 // ------------------------------------------------------------------------
216 // ITmfTraceIndexer - seekIndex
217 // ------------------------------------------------------------------------
220 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
223 public synchronized ITmfContext
seekIndex(final ITmfTimestamp timestamp
) {
225 // A null timestamp indicates to seek the first event
226 if (timestamp
== null) {
227 return fTrace
.seekEvent(0);
230 // Find the checkpoint at or before the requested timestamp.
231 // In the very likely event that the timestamp is not at a checkpoint
232 // boundary, bsearch will return index = (- (insertion point + 1)).
233 // It is then trivial to compute the index of the previous checkpoint.
234 int index
= Collections
.binarySearch(fTraceIndex
, new TmfCheckpoint(timestamp
, null));
236 index
= Math
.max(0, -(index
+ 2));
239 // Position the trace at the checkpoint
240 return seekCheckpoint(index
);
244 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(long)
247 public ITmfContext
seekIndex(final long rank
) {
249 // A rank < 0 indicates to seek the first event
251 return fTrace
.seekEvent(0);
254 // Find the checkpoint at or before the requested rank.
255 final int index
= (int) rank
/ fCheckpointInterval
;
257 // Position the trace at the checkpoint
258 return seekCheckpoint(index
);
262 * Position the trace at the given checkpoint
264 * @param checkpoint the checkpoint index
265 * @return the corresponding context
267 private ITmfContext
seekCheckpoint(final int checkpoint
) {
268 ITmfLocation
<?
> location
= null;
269 int index
= checkpoint
;
270 synchronized (fTraceIndex
) {
271 if (!fTraceIndex
.isEmpty()) {
272 if (index
>= fTraceIndex
.size()) {
273 index
= fTraceIndex
.size() - 1;
275 location
= fTraceIndex
.get(index
).getLocation();
278 final ITmfContext context
= fTrace
.seekEvent(location
);
279 context
.setRank((long) index
* fCheckpointInterval
);
283 // ------------------------------------------------------------------------
285 // ------------------------------------------------------------------------
288 * @return the trace index
290 protected List
<TmfCheckpoint
> getTraceIndex() {
This page took 0.036397 seconds and 5 git commands to generate.