BTree index on disk
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / indexer / checkpoint / TmfCheckpointIndexer.java
CommitLineData
20658947 1/*******************************************************************************
61759503 2 * Copyright (c) 2012, 2013 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
a3db8436 13package org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint;
20658947 14
20658947
FC
15import org.eclipse.core.runtime.IProgressMonitor;
16import org.eclipse.core.runtime.IStatus;
17import org.eclipse.core.runtime.Status;
18import org.eclipse.core.runtime.jobs.Job;
b7952ebe 19import org.eclipse.linuxtools.internal.tmf.core.Messages;
032ecd45 20import org.eclipse.linuxtools.internal.tmf.core.trace.indexer.TmfMemoryIndex;
9b749023 21import org.eclipse.linuxtools.tmf.core.component.TmfDataProvider;
20658947 22import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
5419a136
AM
23import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
24import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
25import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
26import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
20658947 27import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
3bd46eef
AM
28import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
29import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
a3db8436
AM
30import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
31import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
32import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfTraceIndexer;
33import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
20658947
FC
34
35/**
2848c377 36 * A simple indexer that manages the trace index as an array of trace
032ecd45 37 * checkpoints. Checkpoints are stored in memory at fixed intervals (event rank) in
2848c377 38 * ascending timestamp order.
20658947
FC
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).
a3db8436 47 * *
f7703ed6
FC
48 * @see ITmfTrace
49 * @see ITmfEvent
a3db8436
AM
50 *
51 * @author Francois Chouinard
52 * @since 3.0
20658947 53 */
6256d8ad 54public class TmfCheckpointIndexer implements ITmfTraceIndexer {
20658947
FC
55
56 // ------------------------------------------------------------------------
57 // Attributes
58 // ------------------------------------------------------------------------
59
6f4e8ec0 60 /** The event trace to index */
6256d8ad 61 protected final ITmfTrace fTrace;
20658947 62
6f4e8ec0 63 /** The interval between checkpoints */
0316808c 64 private final int fCheckpointInterval;
20658947 65
6f4e8ec0 66 /** The event trace to index */
9e0640dc
FC
67 private boolean fIsIndexing;
68
20658947
FC
69 /**
70 * The trace index. It is composed of checkpoints taken at intervals of
71 * fCheckpointInterval events.
72 */
032ecd45 73 protected final ITmfCheckpointIndex fTraceIndex;
20658947 74
b5ee6881 75 /**
9b749023 76 * The indexing request
b5ee6881 77 */
5419a136 78 private ITmfEventRequest fIndexingRequest = null;
9b749023 79
20658947
FC
80 // ------------------------------------------------------------------------
81 // Construction
82 // ------------------------------------------------------------------------
83
84 /**
85 * Basic constructor that uses the default trace block size as checkpoints
86 * intervals
9b749023 87 *
20658947
FC
88 * @param trace the trace to index
89 */
6256d8ad 90 public TmfCheckpointIndexer(final ITmfTrace trace) {
9b749023 91 this(trace, TmfDataProvider.DEFAULT_BLOCK_SIZE);
20658947
FC
92 }
93
94 /**
95 * Full trace indexer
9b749023 96 *
20658947
FC
97 * @param trace the trace to index
98 * @param interval the checkpoints interval
99 */
6256d8ad 100 public TmfCheckpointIndexer(final ITmfTrace trace, final int interval) {
20658947
FC
101 fTrace = trace;
102 fCheckpointInterval = interval;
032ecd45 103 fTraceIndex = createIndex(trace);
9e0640dc
FC
104 fIsIndexing = false;
105 }
106
032ecd45
MAL
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
b5ee6881
FC
119 @Override
120 public void dispose() {
121 if ((fIndexingRequest != null) && !fIndexingRequest.isCompleted()) {
122 fIndexingRequest.cancel();
b5ee6881 123 }
032ecd45
MAL
124
125 fTraceIndex.dispose();
b5ee6881
FC
126 }
127
9e0640dc
FC
128 // ------------------------------------------------------------------------
129 // ITmfTraceIndexer - isIndexing
130 // ------------------------------------------------------------------------
131
9e0640dc
FC
132 @Override
133 public boolean isIndexing() {
134 return fIsIndexing;
20658947
FC
135 }
136
137 // ------------------------------------------------------------------------
1703b536 138 // ITmfTraceIndexer - buildIndex
20658947
FC
139 // ------------------------------------------------------------------------
140
3bd46eef
AM
141 /**
142 * @since 2.0
20658947
FC
143 */
144 @Override
9e0640dc
FC
145 public void buildIndex(final long offset, final TmfTimeRange range, final boolean waitForCompletion) {
146
9b749023 147 // Don't do anything if we are already indexing
9e0640dc
FC
148 synchronized (fTraceIndex) {
149 if (fIsIndexing) {
150 return;
151 }
152 fIsIndexing = true;
153 }
20658947 154
032ecd45
MAL
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
20658947
FC
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) {
b7952ebe 166 monitor.beginTask("", IProgressMonitor.UNKNOWN); //$NON-NLS-1$
20658947
FC
167 while (!monitor.isCanceled()) {
168 try {
b7952ebe
PT
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$
20658947
FC
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
20658947 186 // Build a background request for all the trace data. The index is
07671572 187 // updated as we go by readNextEvent().
5419a136 188 fIndexingRequest = new TmfEventRequest(ITmfEvent.class,
672a642a
AM
189 range, offset, TmfDataRequest.ALL_DATA,
190 ITmfDataRequest.ExecutionType.BACKGROUND) {
20658947 191 @Override
5419a136
AM
192 public void handleData(final ITmfEvent event) {
193 super.handleData(event);
20658947 194 if (event != null) {
20658947 195 // Update the trace status at regular intervals
5419a136 196 if ((getNbRead() % fCheckpointInterval) == 0) {
20658947
FC
197 updateTraceStatus();
198 }
199 }
200 }
201
202 @Override
203 public void handleSuccess() {
032ecd45
MAL
204 fTraceIndex.setTimeRange(fTrace.getTimeRange());
205 fTraceIndex.setNbEvents(fTrace.getNbEvents());
206 fTraceIndex.setIndexComplete();
20658947
FC
207 updateTraceStatus();
208 }
209
210 @Override
5419a136 211 public void handleCompleted() {
20658947
FC
212 job.cancel();
213 super.handleCompleted();
9e0640dc 214 fIsIndexing = false;
20658947
FC
215 }
216
217 private void updateTraceStatus() {
736988de
PT
218 if (fTrace.getNbEvents() > 0) {
219 signalNewTimeRange(fTrace.getStartTime(), fTrace.getEndTime());
20658947
FC
220 }
221 }
d337369a 222 };
20658947 223
d337369a 224 // Submit the request and wait for completion if required
b5ee6881 225 fTrace.sendRequest(fIndexingRequest);
d337369a
FC
226 if (waitForCompletion) {
227 try {
b5ee6881 228 fIndexingRequest.waitForCompletion();
d337369a
FC
229 } catch (final InterruptedException e) {
230 }
231 }
20658947
FC
232 }
233
7e6347b0
FC
234 /**
235 * Notify the interested parties that the trace time range has changed
9b749023 236 *
7e6347b0
FC
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) {
032ecd45 241 fTrace.broadcast(new TmfTraceUpdatedSignal(fTrace, fTrace, new TmfTimeRange(startTime, endTime), fTrace.getNbEvents()));
1703b536
FC
242 }
243
244 // ------------------------------------------------------------------------
245 // ITmfTraceIndexer - updateIndex
246 // ------------------------------------------------------------------------
247
3bd46eef
AM
248 /**
249 * @since 2.0
d337369a 250 */
1703b536 251 @Override
d337369a 252 public synchronized void updateIndex(final ITmfContext context, final ITmfTimestamp timestamp) {
ea271da6 253 if ((context.getRank() % fCheckpointInterval) == 0) {
1703b536 254 // Determine the table position
ea271da6 255 final long position = context.getRank() / fCheckpointInterval;
1703b536
FC
256 // Add new entry at proper location (if empty)
257 if (fTraceIndex.size() == position) {
032ecd45 258 fTraceIndex.insert(new TmfCheckpoint(timestamp, context.getLocation(), position));
1703b536
FC
259 }
260 }
261 }
262
263 // ------------------------------------------------------------------------
264 // ITmfTraceIndexer - seekIndex
265 // ------------------------------------------------------------------------
20658947 266
3bd46eef
AM
267 /**
268 * @since 2.0
20658947
FC
269 */
270 @Override
1703b536 271 public synchronized ITmfContext seekIndex(final ITmfTimestamp timestamp) {
20658947 272
1703b536 273 // A null timestamp indicates to seek the first event
0316808c 274 if (timestamp == null) {
7e6347b0 275 return fTrace.seekEvent(0);
0316808c 276 }
20658947 277
1703b536
FC
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.
032ecd45 282 long index = fTraceIndex.binarySearch(new TmfCheckpoint(timestamp, null, 0));
20658947
FC
283 if (index < 0) {
284 index = Math.max(0, -(index + 2));
3eade83c
BH
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);
20658947
FC
289 }
290
291 // Position the trace at the checkpoint
408e65d2 292 return restoreCheckpoint(index);
20658947
FC
293 }
294
295 @Override
296 public ITmfContext seekIndex(final long rank) {
297
f6ad2e3d
FC
298 // A rank < 0 indicates to seek the first event
299 if (rank < 0) {
300 return fTrace.seekEvent(0);
301 }
1703b536 302
f6ad2e3d
FC
303 // Find the checkpoint at or before the requested rank.
304 final int index = (int) rank / fCheckpointInterval;
1703b536 305
f6ad2e3d 306 // Position the trace at the checkpoint
408e65d2 307 return restoreCheckpoint(index);
1703b536
FC
308 }
309
310 /**
311 * Position the trace at the given checkpoint
9b749023 312 *
0316808c 313 * @param checkpoint the checkpoint index
1703b536
FC
314 * @return the corresponding context
315 */
032ecd45 316 private ITmfContext restoreCheckpoint(final long checkpoint) {
1e1bef82 317 ITmfLocation location = null;
032ecd45 318 long index = 0;
20658947 319 synchronized (fTraceIndex) {
1703b536 320 if (!fTraceIndex.isEmpty()) {
3427112b 321 index = checkpoint;
20658947
FC
322 if (index >= fTraceIndex.size()) {
323 index = fTraceIndex.size() - 1;
324 }
ea271da6 325 location = fTraceIndex.get(index).getLocation();
20658947
FC
326 }
327 }
7e6347b0 328 final ITmfContext context = fTrace.seekEvent(location);
032ecd45 329 context.setRank(index * fCheckpointInterval);
20658947
FC
330 return context;
331 }
332
0316808c
FC
333 // ------------------------------------------------------------------------
334 // Getters
335 // ------------------------------------------------------------------------
336
337 /**
338 * @return the trace index
032ecd45 339 * @since 3.0
0316808c 340 */
032ecd45 341 protected ITmfCheckpointIndex getTraceIndex() {
0316808c
FC
342 return fTraceIndex;
343 }
3bd44ac8 344
20658947 345}
This page took 0.058569 seconds and 5 git commands to generate.