Merge branch 'master' into lttng-kepler
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / TmfCheckpointIndexer.java
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
13 package org.eclipse.linuxtools.tmf.core.trace;
14
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18
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.trace.TmfExperimentContext;
24 import org.eclipse.linuxtools.tmf.core.component.TmfDataProvider;
25 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
26 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
27 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
28 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
29 import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
30 import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
31 import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
32 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
33
34 /**
35 * A simple indexer that manages the trace index as an array of trace
36 * checkpoints. Checkpoints are stored at fixed intervals (event rank) in
37 * ascending timestamp order.
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).
46 *
47 * @version 1.0
48 * @author Francois Chouinard
49 *
50 * @see ITmfTrace
51 * @see ITmfEvent
52 */
53 public class TmfCheckpointIndexer implements ITmfTraceIndexer {
54
55 // ------------------------------------------------------------------------
56 // Attributes
57 // ------------------------------------------------------------------------
58
59 // The event trace to index
60 protected final ITmfTrace fTrace;
61
62 // The interval between checkpoints
63 private final int fCheckpointInterval;
64
65 // The event trace to index
66 private boolean fIsIndexing;
67
68 /**
69 * The trace index. It is composed of checkpoints taken at intervals of
70 * fCheckpointInterval events.
71 */
72 protected final List<ITmfCheckpoint> fTraceIndex;
73
74 /**
75 * The indexing request
76 */
77 private ITmfEventRequest fIndexingRequest = null;
78
79 // ------------------------------------------------------------------------
80 // Construction
81 // ------------------------------------------------------------------------
82
83 /**
84 * Basic constructor that uses the default trace block size as checkpoints
85 * intervals
86 *
87 * @param trace the trace to index
88 */
89 public TmfCheckpointIndexer(final ITmfTrace trace) {
90 this(trace, TmfDataProvider.DEFAULT_BLOCK_SIZE);
91 }
92
93 /**
94 * Full trace indexer
95 *
96 * @param trace the trace to index
97 * @param interval the checkpoints interval
98 */
99 public TmfCheckpointIndexer(final ITmfTrace trace, final int interval) {
100 fTrace = trace;
101 fCheckpointInterval = interval;
102 fTraceIndex = new ArrayList<ITmfCheckpoint>();
103 fIsIndexing = false;
104 }
105
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
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;
127 }
128
129 // ------------------------------------------------------------------------
130 // ITmfTraceIndexer - buildIndex
131 // ------------------------------------------------------------------------
132
133 /* (non-Javadoc)
134 *
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()).
138 *
139 * The index is built simply by reading the trace
140 *
141 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#buildIndex(long, org.eclipse.linuxtools.tmf.core.event.TmfTimeRange, boolean)
142 */
143 @Override
144 public void buildIndex(final long offset, final TmfTimeRange range, final boolean waitForCompletion) {
145
146 // Don't do anything if we are already indexing
147 synchronized (fTraceIndex) {
148 if (fIsIndexing) {
149 return;
150 }
151 fIsIndexing = true;
152 }
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
171 // Build a background request for all the trace data. The index is
172 // updated as we go by readNextEvent().
173 fIndexingRequest = new TmfEventRequest(ITmfEvent.class,
174 range, offset, TmfDataRequest.ALL_DATA, fCheckpointInterval, ITmfDataRequest.ExecutionType.BACKGROUND)
175 {
176 @Override
177 public void handleData(final ITmfEvent event) {
178 super.handleData(event);
179 if (event != null) {
180 // Update the trace status at regular intervals
181 if ((getNbRead() % fCheckpointInterval) == 0) {
182 updateTraceStatus();
183 }
184 }
185 }
186
187 @Override
188 public void handleSuccess() {
189 updateTraceStatus();
190 }
191
192 @Override
193 public void handleCompleted() {
194 job.cancel();
195 super.handleCompleted();
196 fIsIndexing = false;
197 }
198
199 private void updateTraceStatus() {
200 if (fTrace.getNbEvents() > 0) {
201 signalNewTimeRange(fTrace.getStartTime(), fTrace.getEndTime());
202 }
203 }
204 };
205
206 // Submit the request and wait for completion if required
207 fTrace.sendRequest(fIndexingRequest);
208 if (waitForCompletion) {
209 try {
210 fIndexingRequest.waitForCompletion();
211 } catch (final InterruptedException e) {
212 }
213 }
214 }
215
216 /**
217 * Notify the interested parties that the trace time range has changed
218 *
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) {
223 fTrace.broadcast(new TmfTraceUpdatedSignal(fTrace, fTrace, new TmfTimeRange(startTime, endTime)));
224 }
225
226 // ------------------------------------------------------------------------
227 // ITmfTraceIndexer - updateIndex
228 // ------------------------------------------------------------------------
229
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 */
233 @Override
234 public synchronized void updateIndex(final ITmfContext context, final ITmfTimestamp timestamp) {
235 final long rank = context.getRank();
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) {
241 fTraceIndex.add(new TmfCheckpoint(timestamp.clone(), saveContext(context)));
242 }
243 }
244 }
245
246 // ------------------------------------------------------------------------
247 // ITmfTraceIndexer - seekIndex
248 // ------------------------------------------------------------------------
249
250 /* (non-Javadoc)
251 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
252 */
253 @Override
254 public synchronized ITmfContext seekIndex(final ITmfTimestamp timestamp) {
255
256 // A null timestamp indicates to seek the first event
257 if (timestamp == null) {
258 return fTrace.seekEvent(0);
259 }
260
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));
266 if (index < 0) {
267 index = Math.max(0, -(index + 2));
268 }
269
270 // Position the trace at the checkpoint
271 return restoreCheckpoint(index);
272 }
273
274 /* (non-Javadoc)
275 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(long)
276 */
277 @Override
278 public ITmfContext seekIndex(final long rank) {
279
280 // A rank < 0 indicates to seek the first event
281 if (rank < 0) {
282 return fTrace.seekEvent(0);
283 }
284
285 // Find the checkpoint at or before the requested rank.
286 final int index = (int) rank / fCheckpointInterval;
287
288 // Position the trace at the checkpoint
289 return restoreCheckpoint(index);
290 }
291
292 /**
293 * Position the trace at the given checkpoint
294 *
295 * @param checkpoint the checkpoint index
296 * @return the corresponding context
297 */
298 private ITmfContext restoreCheckpoint(final int checkpoint) {
299 ITmfLocation location = null;
300 int index = 0;
301 synchronized (fTraceIndex) {
302 if (!fTraceIndex.isEmpty()) {
303 index = checkpoint;
304 if (index >= fTraceIndex.size()) {
305 index = fTraceIndex.size() - 1;
306 }
307 return restoreContext(fTraceIndex.get(index).getContext());
308 }
309 }
310 final ITmfContext context = fTrace.seekEvent(location);
311 context.setRank((long) index * fCheckpointInterval);
312 return context;
313 }
314
315 // ------------------------------------------------------------------------
316 // Getters
317 // ------------------------------------------------------------------------
318
319 /**
320 * @return the trace index
321 */
322 protected List<ITmfCheckpoint> getTraceIndex() {
323 return fTraceIndex;
324 }
325
326 // ------------------------------------------------------------------------
327 // Context conversion functions
328 // ------------------------------------------------------------------------
329
330 private static ITmfContext saveContext(ITmfContext context) {
331 if (context instanceof TmfExperimentContext) {
332 return saveExpContext(context);
333 }
334 TmfContext ctx = new TmfContext(context.getLocation().clone(), context.getRank());
335 return ctx;
336 }
337
338 private static ITmfContext saveExpContext(ITmfContext context) {
339 TmfExperimentContext expContext = (TmfExperimentContext) context;
340 int size = expContext.getContexts().length;
341 ITmfContext[] trcCtxts = new TmfContext[size];
342 for (int i = 0; i < size; i++) {
343 ITmfContext ctx = expContext.getContexts()[i];
344 trcCtxts[i] = (ctx != null) ? new TmfContext(ctx.getLocation().clone(), ctx.getRank()) : null;
345 }
346 TmfExperimentContext expCtx = new TmfExperimentContext(trcCtxts);
347 expCtx.setLocation(context.getLocation().clone());
348 expCtx.setRank(context.getRank());
349 ITmfEvent[] trcEvts = expCtx.getEvents();
350 for (int i = 0; i < size; i++) {
351 ITmfEvent event = expContext.getEvents()[i];
352 trcEvts[i] = (event != null) ? event.clone() : null;
353 }
354 return expCtx;
355 }
356
357 private ITmfContext restoreContext(ITmfContext context) {
358 if (context instanceof TmfExperimentContext) {
359 return restoreExpContext(context);
360 }
361 ITmfContext ctx = fTrace.seekEvent(context.getLocation());
362 ctx.setRank(context.getRank());
363 return ctx;
364 }
365
366 private ITmfContext restoreExpContext(ITmfContext context) {
367 TmfExperimentContext expContext = (TmfExperimentContext) context;
368 int size = expContext.getContexts().length;
369 ITmfContext[] trcCtxts = new ITmfContext[size];
370 for (int i = 0; i < size; i++) {
371 ITmfTrace trace = ((TmfExperiment) fTrace).getTraces()[i];
372 ITmfContext ctx = expContext.getContexts()[i];
373 trcCtxts[i] = trace.seekEvent(ctx.getLocation().clone());
374 trcCtxts[i].setRank(ctx.getRank());
375 }
376 TmfExperimentContext ctx = new TmfExperimentContext(trcCtxts);
377 ctx.setLocation(context.getLocation().clone());
378 ctx.setRank(context.getRank());
379 ITmfEvent[] trcEvts = expContext.getEvents();
380 for (int i = 0; i < size; i++) {
381 ITmfEvent event = trcEvts[i];
382 ctx.getEvents()[i] = (event != null) ? event.clone() : null;
383 }
384 return ctx;
385 }
386
387 }
This page took 0.044687 seconds and 6 git commands to generate.