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
.internal
.tmf
.core
.Tracer
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
34 * A simple indexer that manages the trace index as an array of trace
35 * checkpoints. Checkpoints are stored at fixed intervals (event rank) in
36 * ascending timestamp order.
38 * The goal being to access a random trace event reasonably fast from the user's
39 * standpoint, picking the right interval value becomes a trade-off between speed
40 * and memory usage (a shorter inter-event interval is faster but requires more
43 * Locating a specific checkpoint is trivial for both rank (rank % interval) and
44 * timestamp (bsearch in the array).
47 * @author Francois Chouinard
52 public class TmfCheckpointIndexer
<T
extends ITmfTrace
<ITmfEvent
>> implements ITmfTraceIndexer
<T
> {
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
58 // The event trace to index
59 private final ITmfTrace
<ITmfEvent
> fTrace
;
61 // The interval between checkpoints
62 private final int fCheckpointInterval
;
65 * The trace index. It is composed of checkpoints taken at intervals of
66 * fCheckpointInterval events.
68 private final List
<TmfCheckpoint
> fTraceIndex
;
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
75 * Basic constructor that uses the default trace block size as checkpoints
78 * @param trace the trace to index
80 public TmfCheckpointIndexer(final ITmfTrace
<ITmfEvent
> trace
) {
81 this(trace
, TmfTrace
.DEFAULT_BLOCK_SIZE
);
87 * @param trace the trace to index
88 * @param interval the checkpoints interval
90 public TmfCheckpointIndexer(final ITmfTrace
<ITmfEvent
> trace
, final int interval
) {
92 fCheckpointInterval
= interval
;
93 fTraceIndex
= new ArrayList
<TmfCheckpoint
>();
96 // ------------------------------------------------------------------------
97 // ITmfTraceIndexer - buildIndex
98 // ------------------------------------------------------------------------
101 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#indexTrace(boolean)
103 * The index is a list of contexts that point to events at regular interval
104 * (rank-wise) in the trace. After it is built, the index can be used to
105 * quickly access any event by rank or timestamp (using seekIndex()).
107 * The index is built simply by reading the trace
110 public void buildIndex(final boolean waitForCompletion
) {
112 // The monitoring job
113 final Job job
= new Job("Indexing " + fTrace
.getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
115 protected IStatus
run(final IProgressMonitor monitor
) {
116 while (!monitor
.isCanceled()) {
119 } catch (final InterruptedException e
) {
120 return Status
.OK_STATUS
;
124 return Status
.OK_STATUS
;
129 System
.out
.println("TmfCheckpointIndexer.buildIndex()");;
131 // Clear the checkpoints
134 // Build a background request for all the trace data. The index is
135 // updated as we go by readNextEvent().
136 final ITmfEventRequest
<ITmfEvent
> request
= new TmfEventRequest
<ITmfEvent
>(ITmfEvent
.class, TmfTimeRange
.ETERNITY
,
137 TmfDataRequest
.ALL_DATA
, fCheckpointInterval
, ITmfDataRequest
.ExecutionType
.BACKGROUND
)
139 private ITmfTimestamp startTime
= null;
140 private ITmfTimestamp lastTime
= null;
143 public void handleData(final ITmfEvent event
) {
144 super.handleData(event
);
146 final ITmfTimestamp timestamp
= event
.getTimestamp();
147 if (startTime
== null) {
148 startTime
= timestamp
.clone();
150 lastTime
= timestamp
.clone();
152 if (Tracer
.isEventTraced()) {
153 Tracer
.traceEvent(fTrace
, this, event
);
156 // Update the trace status at regular intervals
157 if ((getNbRead() % fCheckpointInterval
) == 0) {
164 public void handleSuccess() {
169 public void handleCompleted() {
171 super.handleCompleted();
174 private void updateTraceStatus() {
175 if (getNbRead() != 0) {
176 signalNewTimeRange(startTime
, lastTime
);
181 // Submit the request and wait for completion if required
182 fTrace
.sendRequest(request
);
183 if (waitForCompletion
) {
185 request
.waitForCompletion();
186 } catch (final InterruptedException e
) {
192 * Notify the interested parties that the trace time range has changed
194 * @param startTime the new start time
195 * @param endTime the new end time
197 private void signalNewTimeRange(final ITmfTimestamp startTime
, final ITmfTimestamp endTime
) {
198 fTrace
.broadcast(new TmfTraceUpdatedSignal(fTrace
, fTrace
, new TmfTimeRange(startTime
, endTime
)));
201 // ------------------------------------------------------------------------
202 // ITmfTraceIndexer - updateIndex
203 // ------------------------------------------------------------------------
206 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#updateIndex(org.eclipse.linuxtools.tmf.core.trace.ITmfContext, org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
209 public synchronized void updateIndex(final ITmfContext context
, final ITmfTimestamp timestamp
) {
210 final long rank
= context
.getRank();
211 if ((rank
% fCheckpointInterval
) == 0) {
212 // Determine the table position
213 final long position
= rank
/ fCheckpointInterval
;
214 // Add new entry at proper location (if empty)
215 if (fTraceIndex
.size() == position
) {
216 final ITmfLocation
<?
> location
= context
.getLocation().clone();
217 fTraceIndex
.add(new TmfCheckpoint(timestamp
.clone(), location
));
218 System
.out
.println("TmfCheckpointIndexer.updateIndex()" + "[" + (fTraceIndex
.size() - 1) + "] " + timestamp
+ ", " + location
.toString());
223 // ------------------------------------------------------------------------
224 // ITmfTraceIndexer - seekIndex
225 // ------------------------------------------------------------------------
228 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
231 public synchronized ITmfContext
seekIndex(final ITmfTimestamp timestamp
) {
233 // A null timestamp indicates to seek the first event
234 if (timestamp
== null) {
235 return fTrace
.seekEvent(0);
238 // Find the checkpoint at or before the requested timestamp.
239 // In the very likely event that the timestamp is not at a checkpoint
240 // boundary, bsearch will return index = (- (insertion point + 1)).
241 // It is then trivial to compute the index of the previous checkpoint.
242 int index
= Collections
.binarySearch(fTraceIndex
, new TmfCheckpoint(timestamp
, null));
244 index
= Math
.max(0, -(index
+ 2));
247 // Position the trace at the checkpoint
248 return seekCheckpoint(index
);
252 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(long)
255 public ITmfContext
seekIndex(final long rank
) {
257 // A rank < 0 indicates to seek the first event
259 return fTrace
.seekEvent(0);
262 // Find the checkpoint at or before the requested rank.
263 final int index
= (int) rank
/ fCheckpointInterval
;
265 // Position the trace at the checkpoint
266 return seekCheckpoint(index
);
270 * Position the trace at the given checkpoint
272 * @param checkpoint the checkpoint index
273 * @return the corresponding context
275 private ITmfContext
seekCheckpoint(final int checkpoint
) {
276 ITmfLocation
<?
> location
= null;
277 int index
= checkpoint
;
278 synchronized (fTraceIndex
) {
279 if (!fTraceIndex
.isEmpty()) {
280 if (index
>= fTraceIndex
.size()) {
281 index
= fTraceIndex
.size() - 1;
283 location
= fTraceIndex
.get(index
).getLocation();
286 final ITmfContext context
= fTrace
.seekEvent(location
);
287 context
.setRank((long) index
* fCheckpointInterval
);
291 // ------------------------------------------------------------------------
293 // ------------------------------------------------------------------------
296 * @return the trace index
298 protected List
<TmfCheckpoint
> getTraceIndex() {
This page took 0.037992 seconds and 6 git commands to generate.