tmf: Fix regression in event requests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / TmfCheckpointIndexer.java
CommitLineData
20658947
FC
1/*******************************************************************************
2 * Copyright (c) 2012 Ericsson
9b749023 3 *
20658947
FC
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
9b749023 8 *
20658947
FC
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.tmf.core.trace;
14
0316808c 15import java.util.ArrayList;
20658947 16import java.util.Collections;
0316808c 17import java.util.List;
20658947
FC
18
19import org.eclipse.core.runtime.IProgressMonitor;
20import org.eclipse.core.runtime.IStatus;
21import org.eclipse.core.runtime.Status;
22import org.eclipse.core.runtime.jobs.Job;
3bd44ac8 23import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentContext;
9b749023 24import org.eclipse.linuxtools.tmf.core.component.TmfDataProvider;
20658947
FC
25import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
26import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
27import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
5419a136
AM
28import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
29import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
30import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
31import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
20658947
FC
32import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
33
34/**
2848c377 35 * A simple indexer that manages the trace index as an array of trace
9b749023 36 * checkpoints. Checkpoints are stored at fixed intervals (event rank) in
2848c377 37 * ascending timestamp order.
20658947
FC
38 * <p>
39 * The goal being to access a random trace event reasonably fast from the user's
40 * standpoint, picking the right interval value becomes a trade-off between speed
41 * and memory usage (a shorter inter-event interval is faster but requires more
42 * checkpoints).
43 * <p>
44 * Locating a specific checkpoint is trivial for both rank (rank % interval) and
45 * timestamp (bsearch in the array).
9b749023 46 *
f7703ed6
FC
47 * @version 1.0
48 * @author Francois Chouinard
49 *
f7703ed6
FC
50 * @see ITmfTrace
51 * @see ITmfEvent
20658947 52 */
6256d8ad 53public class TmfCheckpointIndexer implements ITmfTraceIndexer {
20658947
FC
54
55 // ------------------------------------------------------------------------
56 // Attributes
57 // ------------------------------------------------------------------------
58
6f4e8ec0 59 /** The event trace to index */
6256d8ad 60 protected final ITmfTrace fTrace;
20658947 61
6f4e8ec0 62 /** The interval between checkpoints */
0316808c 63 private final int fCheckpointInterval;
20658947 64
6f4e8ec0 65 /** The event trace to index */
9e0640dc
FC
66 private boolean fIsIndexing;
67
20658947
FC
68 /**
69 * The trace index. It is composed of checkpoints taken at intervals of
70 * fCheckpointInterval events.
71 */
3bd44ac8 72 protected final List<ITmfCheckpoint> fTraceIndex;
20658947 73
b5ee6881 74 /**
9b749023 75 * The indexing request
b5ee6881 76 */
5419a136 77 private ITmfEventRequest fIndexingRequest = null;
9b749023 78
20658947
FC
79 // ------------------------------------------------------------------------
80 // Construction
81 // ------------------------------------------------------------------------
82
83 /**
84 * Basic constructor that uses the default trace block size as checkpoints
85 * intervals
9b749023 86 *
20658947
FC
87 * @param trace the trace to index
88 */
6256d8ad 89 public TmfCheckpointIndexer(final ITmfTrace trace) {
9b749023 90 this(trace, TmfDataProvider.DEFAULT_BLOCK_SIZE);
20658947
FC
91 }
92
93 /**
94 * Full trace indexer
9b749023 95 *
20658947
FC
96 * @param trace the trace to index
97 * @param interval the checkpoints interval
98 */
6256d8ad 99 public TmfCheckpointIndexer(final ITmfTrace trace, final int interval) {
20658947
FC
100 fTrace = trace;
101 fCheckpointInterval = interval;
3427112b 102 fTraceIndex = new ArrayList<ITmfCheckpoint>();
9e0640dc
FC
103 fIsIndexing = false;
104 }
105
b5ee6881
FC
106 /* (non-Javadoc)
107 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#dispose()
108 */
109 @Override
110 public void dispose() {
111 if ((fIndexingRequest != null) && !fIndexingRequest.isCompleted()) {
112 fIndexingRequest.cancel();
113 fTraceIndex.clear();
114 }
115 }
116
9e0640dc
FC
117 // ------------------------------------------------------------------------
118 // ITmfTraceIndexer - isIndexing
119 // ------------------------------------------------------------------------
120
121 /* (non-Javadoc)
122 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#isIndexing()
123 */
124 @Override
125 public boolean isIndexing() {
126 return fIsIndexing;
20658947
FC
127 }
128
129 // ------------------------------------------------------------------------
1703b536 130 // ITmfTraceIndexer - buildIndex
20658947
FC
131 // ------------------------------------------------------------------------
132
133 /* (non-Javadoc)
9b749023 134 *
20658947
FC
135 * The index is a list of contexts that point to events at regular interval
136 * (rank-wise) in the trace. After it is built, the index can be used to
137 * quickly access any event by rank or timestamp (using seekIndex()).
9b749023 138 *
20658947 139 * The index is built simply by reading the trace
9e0640dc
FC
140 *
141 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#buildIndex(long, org.eclipse.linuxtools.tmf.core.event.TmfTimeRange, boolean)
20658947
FC
142 */
143 @Override
9e0640dc
FC
144 public void buildIndex(final long offset, final TmfTimeRange range, final boolean waitForCompletion) {
145
9b749023 146 // Don't do anything if we are already indexing
9e0640dc
FC
147 synchronized (fTraceIndex) {
148 if (fIsIndexing) {
149 return;
150 }
151 fIsIndexing = true;
152 }
20658947
FC
153
154 // The monitoring job
155 final Job job = new Job("Indexing " + fTrace.getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
156 @Override
157 protected IStatus run(final IProgressMonitor monitor) {
158 while (!monitor.isCanceled()) {
159 try {
160 Thread.sleep(100);
161 } catch (final InterruptedException e) {
162 return Status.OK_STATUS;
163 }
164 }
165 monitor.done();
166 return Status.OK_STATUS;
167 }
168 };
169 job.schedule();
170
20658947 171 // Build a background request for all the trace data. The index is
07671572 172 // updated as we go by readNextEvent().
5419a136
AM
173 fIndexingRequest = new TmfEventRequest(ITmfEvent.class,
174 range, offset, TmfDataRequest.ALL_DATA, fCheckpointInterval, ITmfDataRequest.ExecutionType.BACKGROUND)
d337369a 175 {
20658947 176 @Override
5419a136
AM
177 public void handleData(final ITmfEvent event) {
178 super.handleData(event);
20658947 179 if (event != null) {
20658947 180 // Update the trace status at regular intervals
5419a136 181 if ((getNbRead() % fCheckpointInterval) == 0) {
20658947
FC
182 updateTraceStatus();
183 }
184 }
185 }
186
187 @Override
188 public void handleSuccess() {
189 updateTraceStatus();
190 }
191
192 @Override
5419a136 193 public void handleCompleted() {
20658947
FC
194 job.cancel();
195 super.handleCompleted();
9e0640dc 196 fIsIndexing = false;
20658947
FC
197 }
198
199 private void updateTraceStatus() {
736988de
PT
200 if (fTrace.getNbEvents() > 0) {
201 signalNewTimeRange(fTrace.getStartTime(), fTrace.getEndTime());
20658947
FC
202 }
203 }
d337369a 204 };
20658947 205
d337369a 206 // Submit the request and wait for completion if required
b5ee6881 207 fTrace.sendRequest(fIndexingRequest);
d337369a
FC
208 if (waitForCompletion) {
209 try {
b5ee6881 210 fIndexingRequest.waitForCompletion();
d337369a
FC
211 } catch (final InterruptedException e) {
212 }
213 }
20658947
FC
214 }
215
7e6347b0
FC
216 /**
217 * Notify the interested parties that the trace time range has changed
9b749023 218 *
7e6347b0
FC
219 * @param startTime the new start time
220 * @param endTime the new end time
221 */
222 private void signalNewTimeRange(final ITmfTimestamp startTime, final ITmfTimestamp endTime) {
1703b536
FC
223 fTrace.broadcast(new TmfTraceUpdatedSignal(fTrace, fTrace, new TmfTimeRange(startTime, endTime)));
224 }
225
226 // ------------------------------------------------------------------------
227 // ITmfTraceIndexer - updateIndex
228 // ------------------------------------------------------------------------
229
d337369a
FC
230 /* (non-Javadoc)
231 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#updateIndex(org.eclipse.linuxtools.tmf.core.trace.ITmfContext, org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
232 */
1703b536 233 @Override
d337369a
FC
234 public synchronized void updateIndex(final ITmfContext context, final ITmfTimestamp timestamp) {
235 final long rank = context.getRank();
1703b536
FC
236 if ((rank % fCheckpointInterval) == 0) {
237 // Determine the table position
238 final long position = rank / fCheckpointInterval;
239 // Add new entry at proper location (if empty)
240 if (fTraceIndex.size() == position) {
4593bd5b 241 fTraceIndex.add(new TmfCheckpoint(timestamp, saveContext(context)));
1703b536
FC
242 }
243 }
244 }
245
246 // ------------------------------------------------------------------------
247 // ITmfTraceIndexer - seekIndex
248 // ------------------------------------------------------------------------
20658947
FC
249
250 /* (non-Javadoc)
251 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
252 */
253 @Override
1703b536 254 public synchronized ITmfContext seekIndex(final ITmfTimestamp timestamp) {
20658947 255
1703b536 256 // A null timestamp indicates to seek the first event
0316808c 257 if (timestamp == null) {
7e6347b0 258 return fTrace.seekEvent(0);
0316808c 259 }
20658947 260
1703b536
FC
261 // Find the checkpoint at or before the requested timestamp.
262 // In the very likely event that the timestamp is not at a checkpoint
263 // boundary, bsearch will return index = (- (insertion point + 1)).
264 // It is then trivial to compute the index of the previous checkpoint.
265 int index = Collections.binarySearch(fTraceIndex, new TmfCheckpoint(timestamp, null));
20658947
FC
266 if (index < 0) {
267 index = Math.max(0, -(index + 2));
3eade83c
BH
268 } else {
269 // If timestamp was in the list, use previous index to be able to find the
270 // first event with the same timestamp before the checkpoint
271 index = Math.max(0, index - 1);
20658947
FC
272 }
273
274 // Position the trace at the checkpoint
408e65d2 275 return restoreCheckpoint(index);
20658947
FC
276 }
277
1703b536
FC
278 /* (non-Javadoc)
279 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(long)
280 */
20658947
FC
281 @Override
282 public ITmfContext seekIndex(final long rank) {
283
f6ad2e3d
FC
284 // A rank < 0 indicates to seek the first event
285 if (rank < 0) {
286 return fTrace.seekEvent(0);
287 }
1703b536 288
f6ad2e3d
FC
289 // Find the checkpoint at or before the requested rank.
290 final int index = (int) rank / fCheckpointInterval;
1703b536 291
f6ad2e3d 292 // Position the trace at the checkpoint
408e65d2 293 return restoreCheckpoint(index);
1703b536
FC
294 }
295
296 /**
297 * Position the trace at the given checkpoint
9b749023 298 *
0316808c 299 * @param checkpoint the checkpoint index
1703b536
FC
300 * @return the corresponding context
301 */
408e65d2 302 private ITmfContext restoreCheckpoint(final int checkpoint) {
1e1bef82 303 ITmfLocation location = null;
3427112b 304 int index = 0;
20658947 305 synchronized (fTraceIndex) {
1703b536 306 if (!fTraceIndex.isEmpty()) {
3427112b 307 index = checkpoint;
20658947
FC
308 if (index >= fTraceIndex.size()) {
309 index = fTraceIndex.size() - 1;
310 }
3bd44ac8 311 return restoreContext(fTraceIndex.get(index).getContext());
20658947
FC
312 }
313 }
7e6347b0 314 final ITmfContext context = fTrace.seekEvent(location);
afc86f78 315 context.setRank((long) index * fCheckpointInterval);
20658947
FC
316 return context;
317 }
318
0316808c
FC
319 // ------------------------------------------------------------------------
320 // Getters
321 // ------------------------------------------------------------------------
322
323 /**
324 * @return the trace index
325 */
3427112b 326 protected List<ITmfCheckpoint> getTraceIndex() {
0316808c
FC
327 return fTraceIndex;
328 }
3bd44ac8
FC
329
330 // ------------------------------------------------------------------------
331 // Context conversion functions
332 // ------------------------------------------------------------------------
333
17324c9a 334 private static ITmfContext saveContext(ITmfContext context) {
3bd44ac8 335 if (context instanceof TmfExperimentContext) {
17324c9a 336 return saveExpContext(context);
3bd44ac8 337 }
d62bb185 338 TmfContext ctx = new TmfContext(context.getLocation(), context.getRank());
3bd44ac8
FC
339 return ctx;
340 }
341
17324c9a 342 private static ITmfContext saveExpContext(ITmfContext context) {
3bd44ac8
FC
343 TmfExperimentContext expContext = (TmfExperimentContext) context;
344 int size = expContext.getContexts().length;
345 ITmfContext[] trcCtxts = new TmfContext[size];
346 for (int i = 0; i < size; i++) {
347 ITmfContext ctx = expContext.getContexts()[i];
d62bb185 348 trcCtxts[i] = (ctx != null) ? new TmfContext(ctx.getLocation(), ctx.getRank()) : null;
3bd44ac8
FC
349 }
350 TmfExperimentContext expCtx = new TmfExperimentContext(trcCtxts);
d62bb185 351 expCtx.setLocation(context.getLocation());
3bd44ac8
FC
352 expCtx.setRank(context.getRank());
353 ITmfEvent[] trcEvts = expCtx.getEvents();
354 for (int i = 0; i < size; i++) {
355 ITmfEvent event = expContext.getEvents()[i];
356 trcEvts[i] = (event != null) ? event.clone() : null;
357 }
358 return expCtx;
359 }
360
361 private ITmfContext restoreContext(ITmfContext context) {
362 if (context instanceof TmfExperimentContext) {
363 return restoreExpContext(context);
364 }
365 ITmfContext ctx = fTrace.seekEvent(context.getLocation());
366 ctx.setRank(context.getRank());
367 return ctx;
368 }
369
370 private ITmfContext restoreExpContext(ITmfContext context) {
371 TmfExperimentContext expContext = (TmfExperimentContext) context;
372 int size = expContext.getContexts().length;
373 ITmfContext[] trcCtxts = new ITmfContext[size];
374 for (int i = 0; i < size; i++) {
6256d8ad 375 ITmfTrace trace = ((TmfExperiment) fTrace).getTraces()[i];
3bd44ac8 376 ITmfContext ctx = expContext.getContexts()[i];
d62bb185 377 trcCtxts[i] = trace.seekEvent(ctx.getLocation());
3bd44ac8
FC
378 trcCtxts[i].setRank(ctx.getRank());
379 }
380 TmfExperimentContext ctx = new TmfExperimentContext(trcCtxts);
d62bb185 381 ctx.setLocation(context.getLocation());
3bd44ac8
FC
382 ctx.setRank(context.getRank());
383 ITmfEvent[] trcEvts = expContext.getEvents();
384 for (int i = 0; i < size; i++) {
385 ITmfEvent event = trcEvts[i];
386 ctx.getEvents()[i] = (event != null) ? event.clone() : null;
387 }
388 return ctx;
389 }
17324c9a 390
20658947 391}
This page took 0.055442 seconds and 5 git commands to generate.