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