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