65b2f83e65823d080ed02683633901b5390397e2
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 TmfTraceIndexer
<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 TmfTraceIndexer(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 TmfTraceIndexer(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 notifyListeners(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
) {
182 private void notifyListeners(final ITmfTimestamp startTime
, final ITmfTimestamp endTime
) {
183 fTrace
.broadcast(new TmfTraceUpdatedSignal(fTrace
, fTrace
, new TmfTimeRange(startTime
, endTime
)));
186 // ------------------------------------------------------------------------
187 // ITmfTraceIndexer - updateIndex
188 // ------------------------------------------------------------------------
191 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#updateIndex(org.eclipse.linuxtools.tmf.core.trace.ITmfContext, org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
194 public synchronized void updateIndex(final ITmfContext context
, final ITmfTimestamp timestamp
) {
195 final long rank
= context
.getRank();
196 if ((rank
% fCheckpointInterval
) == 0) {
197 // Determine the table position
198 final long position
= rank
/ fCheckpointInterval
;
199 // Add new entry at proper location (if empty)
200 if (fTraceIndex
.size() == position
) {
201 final ITmfLocation
<?
> location
= context
.getLocation().clone();
202 fTraceIndex
.add(new TmfCheckpoint(timestamp
.clone(), location
));
207 // ------------------------------------------------------------------------
208 // ITmfTraceIndexer - seekIndex
209 // ------------------------------------------------------------------------
212 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
215 public synchronized ITmfContext
seekIndex(final ITmfTimestamp timestamp
) {
217 // A null timestamp indicates to seek the first event
218 if (timestamp
== null)
219 return fTrace
.seekLocation(null);
221 // Find the checkpoint at or before the requested timestamp.
222 // In the very likely event that the timestamp is not at a checkpoint
223 // boundary, bsearch will return index = (- (insertion point + 1)).
224 // It is then trivial to compute the index of the previous checkpoint.
225 int index
= Collections
.binarySearch(fTraceIndex
, new TmfCheckpoint(timestamp
, null));
227 index
= Math
.max(0, -(index
+ 2));
230 // Position the trace at the checkpoint
231 return seekCheckpoint(index
);
235 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(long)
238 public ITmfContext
seekIndex(final long rank
) {
240 // A rank <= 0 indicates to seek the first event
242 return fTrace
.seekLocation(null);
244 // Find the checkpoint at or before the requested rank.
245 final int index
= (int) rank
/ fCheckpointInterval
;
247 // Position the trace at the checkpoint
248 return seekCheckpoint(index
);
252 * Position the trace at the given checkpoint
255 * the checkpoint index
256 * @return the corresponding context
258 private ITmfContext
seekCheckpoint(int index
) {
259 ITmfLocation
<?
> location
;
260 synchronized (fTraceIndex
) {
261 if (!fTraceIndex
.isEmpty()) {
262 if (index
>= fTraceIndex
.size()) {
263 index
= fTraceIndex
.size() - 1;
265 location
= fTraceIndex
.elementAt(index
).getLocation();
270 final ITmfContext context
= fTrace
.seekLocation(location
);
271 context
.setRank(index
* fCheckpointInterval
);
This page took 0.036444 seconds and 4 git commands to generate.