BTree index on disk
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / indexer / checkpoint / TmfCheckpointIndexer.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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.indexer.checkpoint;
14
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.core.runtime.jobs.Job;
19 import org.eclipse.linuxtools.internal.tmf.core.Messages;
20 import org.eclipse.linuxtools.internal.tmf.core.trace.indexer.TmfMemoryIndex;
21 import org.eclipse.linuxtools.tmf.core.component.TmfDataProvider;
22 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
23 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
24 import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
25 import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
26 import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
27 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
28 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
29 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
30 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
31 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
32 import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfTraceIndexer;
33 import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
34
35 /**
36 * A simple indexer that manages the trace index as an array of trace
37 * checkpoints. Checkpoints are stored in memory at fixed intervals (event rank) in
38 * ascending timestamp order.
39 * <p>
40 * The goal being to access a random trace event reasonably fast from the user's
41 * standpoint, picking the right interval value becomes a trade-off between speed
42 * and memory usage (a shorter inter-event interval is faster but requires more
43 * checkpoints).
44 * <p>
45 * Locating a specific checkpoint is trivial for both rank (rank % interval) and
46 * timestamp (bsearch in the array).
47 * *
48 * @see ITmfTrace
49 * @see ITmfEvent
50 *
51 * @author Francois Chouinard
52 * @since 3.0
53 */
54 public class TmfCheckpointIndexer implements ITmfTraceIndexer {
55
56 // ------------------------------------------------------------------------
57 // Attributes
58 // ------------------------------------------------------------------------
59
60 /** The event trace to index */
61 protected final ITmfTrace fTrace;
62
63 /** The interval between checkpoints */
64 private final int fCheckpointInterval;
65
66 /** The event trace to index */
67 private boolean fIsIndexing;
68
69 /**
70 * The trace index. It is composed of checkpoints taken at intervals of
71 * fCheckpointInterval events.
72 */
73 protected final ITmfCheckpointIndex fTraceIndex;
74
75 /**
76 * The indexing request
77 */
78 private ITmfEventRequest fIndexingRequest = null;
79
80 // ------------------------------------------------------------------------
81 // Construction
82 // ------------------------------------------------------------------------
83
84 /**
85 * Basic constructor that uses the default trace block size as checkpoints
86 * intervals
87 *
88 * @param trace the trace to index
89 */
90 public TmfCheckpointIndexer(final ITmfTrace trace) {
91 this(trace, TmfDataProvider.DEFAULT_BLOCK_SIZE);
92 }
93
94 /**
95 * Full trace indexer
96 *
97 * @param trace the trace to index
98 * @param interval the checkpoints interval
99 */
100 public TmfCheckpointIndexer(final ITmfTrace trace, final int interval) {
101 fTrace = trace;
102 fCheckpointInterval = interval;
103 fTraceIndex = createIndex(trace);
104 fIsIndexing = false;
105 }
106
107 /**
108 * Creates the index instance. Classes extending this class
109 * can override this to provide a different index implementation.
110 *
111 * @param trace the trace to index
112 * @return the index
113 * @since 3.0
114 */
115 protected ITmfCheckpointIndex createIndex(final ITmfTrace trace) {
116 return new TmfMemoryIndex(trace);
117 }
118
119 @Override
120 public void dispose() {
121 if ((fIndexingRequest != null) && !fIndexingRequest.isCompleted()) {
122 fIndexingRequest.cancel();
123 }
124
125 fTraceIndex.dispose();
126 }
127
128 // ------------------------------------------------------------------------
129 // ITmfTraceIndexer - isIndexing
130 // ------------------------------------------------------------------------
131
132 @Override
133 public boolean isIndexing() {
134 return fIsIndexing;
135 }
136
137 // ------------------------------------------------------------------------
138 // ITmfTraceIndexer - buildIndex
139 // ------------------------------------------------------------------------
140
141 /**
142 * @since 2.0
143 */
144 @Override
145 public void buildIndex(final long offset, final TmfTimeRange range, final boolean waitForCompletion) {
146
147 // Don't do anything if we are already indexing
148 synchronized (fTraceIndex) {
149 if (fIsIndexing) {
150 return;
151 }
152 fIsIndexing = true;
153 }
154
155 // No need to build the index, it has been restored
156 if (!fTraceIndex.isCreatedFromScratch()) {
157 // Set some trace attributes that depends on indexing
158 fTrace.broadcast(new TmfTraceUpdatedSignal(this, fTrace, new TmfTimeRange(fTraceIndex.getTimeRange().getStartTime(), fTraceIndex.getTimeRange().getEndTime()), fTraceIndex.getNbEvents()));
159 return;
160 }
161
162 // The monitoring job
163 final Job job = new Job("Indexing " + fTrace.getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
164 @Override
165 protected IStatus run(final IProgressMonitor monitor) {
166 monitor.beginTask("", IProgressMonitor.UNKNOWN); //$NON-NLS-1$
167 while (!monitor.isCanceled()) {
168 try {
169 long prevNbEvents = fTrace.getNbEvents();
170 Thread.sleep(250);
171 long nbEvents = fTrace.getNbEvents();
172 setName(Messages.TmfCheckpointIndexer_Indexing + ' ' + fTrace.getName() + " (" + nbEvents + ")"); //$NON-NLS-1$ //$NON-NLS-2$
173 // setName doesn't refresh the UI, setTaskName does
174 long rate = (nbEvents - prevNbEvents) * 4;
175 monitor.setTaskName(rate + " " + Messages.TmfCheckpointIndexer_EventsPerSecond); //$NON-NLS-1$
176 } catch (final InterruptedException e) {
177 return Status.OK_STATUS;
178 }
179 }
180 monitor.done();
181 return Status.OK_STATUS;
182 }
183 };
184 job.schedule();
185
186 // Build a background request for all the trace data. The index is
187 // updated as we go by readNextEvent().
188 fIndexingRequest = new TmfEventRequest(ITmfEvent.class,
189 range, offset, TmfDataRequest.ALL_DATA,
190 ITmfDataRequest.ExecutionType.BACKGROUND) {
191 @Override
192 public void handleData(final ITmfEvent event) {
193 super.handleData(event);
194 if (event != null) {
195 // Update the trace status at regular intervals
196 if ((getNbRead() % fCheckpointInterval) == 0) {
197 updateTraceStatus();
198 }
199 }
200 }
201
202 @Override
203 public void handleSuccess() {
204 fTraceIndex.setTimeRange(fTrace.getTimeRange());
205 fTraceIndex.setNbEvents(fTrace.getNbEvents());
206 fTraceIndex.setIndexComplete();
207 updateTraceStatus();
208 }
209
210 @Override
211 public void handleCompleted() {
212 job.cancel();
213 super.handleCompleted();
214 fIsIndexing = false;
215 }
216
217 private void updateTraceStatus() {
218 if (fTrace.getNbEvents() > 0) {
219 signalNewTimeRange(fTrace.getStartTime(), fTrace.getEndTime());
220 }
221 }
222 };
223
224 // Submit the request and wait for completion if required
225 fTrace.sendRequest(fIndexingRequest);
226 if (waitForCompletion) {
227 try {
228 fIndexingRequest.waitForCompletion();
229 } catch (final InterruptedException e) {
230 }
231 }
232 }
233
234 /**
235 * Notify the interested parties that the trace time range has changed
236 *
237 * @param startTime the new start time
238 * @param endTime the new end time
239 */
240 private void signalNewTimeRange(final ITmfTimestamp startTime, final ITmfTimestamp endTime) {
241 fTrace.broadcast(new TmfTraceUpdatedSignal(fTrace, fTrace, new TmfTimeRange(startTime, endTime), fTrace.getNbEvents()));
242 }
243
244 // ------------------------------------------------------------------------
245 // ITmfTraceIndexer - updateIndex
246 // ------------------------------------------------------------------------
247
248 /**
249 * @since 2.0
250 */
251 @Override
252 public synchronized void updateIndex(final ITmfContext context, final ITmfTimestamp timestamp) {
253 if ((context.getRank() % fCheckpointInterval) == 0) {
254 // Determine the table position
255 final long position = context.getRank() / fCheckpointInterval;
256 // Add new entry at proper location (if empty)
257 if (fTraceIndex.size() == position) {
258 fTraceIndex.insert(new TmfCheckpoint(timestamp, context.getLocation(), position));
259 }
260 }
261 }
262
263 // ------------------------------------------------------------------------
264 // ITmfTraceIndexer - seekIndex
265 // ------------------------------------------------------------------------
266
267 /**
268 * @since 2.0
269 */
270 @Override
271 public synchronized ITmfContext seekIndex(final ITmfTimestamp timestamp) {
272
273 // A null timestamp indicates to seek the first event
274 if (timestamp == null) {
275 return fTrace.seekEvent(0);
276 }
277
278 // Find the checkpoint at or before the requested timestamp.
279 // In the very likely event that the timestamp is not at a checkpoint
280 // boundary, bsearch will return index = (- (insertion point + 1)).
281 // It is then trivial to compute the index of the previous checkpoint.
282 long index = fTraceIndex.binarySearch(new TmfCheckpoint(timestamp, null, 0));
283 if (index < 0) {
284 index = Math.max(0, -(index + 2));
285 } else {
286 // If timestamp was in the list, use previous index to be able to find the
287 // first event with the same timestamp before the checkpoint
288 index = Math.max(0, index - 1);
289 }
290
291 // Position the trace at the checkpoint
292 return restoreCheckpoint(index);
293 }
294
295 @Override
296 public ITmfContext seekIndex(final long rank) {
297
298 // A rank < 0 indicates to seek the first event
299 if (rank < 0) {
300 return fTrace.seekEvent(0);
301 }
302
303 // Find the checkpoint at or before the requested rank.
304 final int index = (int) rank / fCheckpointInterval;
305
306 // Position the trace at the checkpoint
307 return restoreCheckpoint(index);
308 }
309
310 /**
311 * Position the trace at the given checkpoint
312 *
313 * @param checkpoint the checkpoint index
314 * @return the corresponding context
315 */
316 private ITmfContext restoreCheckpoint(final long checkpoint) {
317 ITmfLocation location = null;
318 long index = 0;
319 synchronized (fTraceIndex) {
320 if (!fTraceIndex.isEmpty()) {
321 index = checkpoint;
322 if (index >= fTraceIndex.size()) {
323 index = fTraceIndex.size() - 1;
324 }
325 location = fTraceIndex.get(index).getLocation();
326 }
327 }
328 final ITmfContext context = fTrace.seekEvent(location);
329 context.setRank(index * fCheckpointInterval);
330 return context;
331 }
332
333 // ------------------------------------------------------------------------
334 // Getters
335 // ------------------------------------------------------------------------
336
337 /**
338 * @return the trace index
339 * @since 3.0
340 */
341 protected ITmfCheckpointIndex getTraceIndex() {
342 return fTraceIndex;
343 }
344
345 }
This page took 0.039279 seconds and 5 git commands to generate.