Remove all existing @since annotations
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / trace / indexer / checkpoint / TmfCheckpointIndexer.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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 * Bernd Hufmann - Update way of broadcasting of TmfTraceUpdatedSignal
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint;
15
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.core.runtime.jobs.Job;
20 import org.eclipse.tracecompass.internal.tmf.core.Messages;
21 import org.eclipse.tracecompass.internal.tmf.core.trace.indexer.TmfMemoryIndex;
22 import org.eclipse.tracecompass.tmf.core.component.TmfEventProvider;
23 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
24 import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
25 import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest;
26 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceUpdatedSignal;
27 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
28 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
29 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
30 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
31 import org.eclipse.tracecompass.tmf.core.trace.ITmfTraceCompleteness;
32 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
33 import org.eclipse.tracecompass.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 */
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 ITmfCheckpointIndex 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, TmfEventProvider.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 = createIndex(trace);
103 fIsIndexing = false;
104 }
105
106 /**
107 * Creates the index instance. Classes extending this class
108 * can override this to provide a different index implementation.
109 *
110 * @param trace the trace to index
111 * @return the index
112 */
113 protected ITmfCheckpointIndex createIndex(final ITmfTrace trace) {
114 return new TmfMemoryIndex(trace);
115 }
116
117 @Override
118 public void dispose() {
119 if ((fIndexingRequest != null) && !fIndexingRequest.isCompleted()) {
120 fIndexingRequest.cancel();
121 }
122
123 fTraceIndex.dispose();
124 }
125
126 // ------------------------------------------------------------------------
127 // ITmfTraceIndexer - isIndexing
128 // ------------------------------------------------------------------------
129
130 @Override
131 public boolean isIndexing() {
132 return fIsIndexing;
133 }
134
135 // ------------------------------------------------------------------------
136 // ITmfTraceIndexer - buildIndex
137 // ------------------------------------------------------------------------
138
139 @Override
140 public void buildIndex(final long offset, final TmfTimeRange range, final boolean waitForCompletion) {
141
142 // Don't do anything if we are already indexing
143 synchronized (fTraceIndex) {
144 if (fIsIndexing) {
145 return;
146 }
147 fIsIndexing = true;
148 }
149
150 // No need to build the index, it has been restored
151 if (!fTraceIndex.isCreatedFromScratch()) {
152 // Set some trace attributes that depends on indexing
153 TmfTraceUpdatedSignal signal = new TmfTraceUpdatedSignal(this, fTrace, new TmfTimeRange(fTraceIndex.getTimeRange().getStartTime(), fTraceIndex.getTimeRange().getEndTime()), fTraceIndex.getNbEvents());
154 if (waitForCompletion) {
155 fTrace.broadcast(signal);
156 } else {
157 fTrace.broadcastAsync(signal);
158 }
159 fIsIndexing = false;
160 return;
161 }
162
163 // The monitoring job
164 final Job job = new Job("Indexing " + fTrace.getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
165 @Override
166 protected IStatus run(final IProgressMonitor monitor) {
167 monitor.beginTask("", IProgressMonitor.UNKNOWN); //$NON-NLS-1$
168 while (!monitor.isCanceled()) {
169 try {
170 long prevNbEvents = fTrace.getNbEvents();
171 Thread.sleep(250);
172 long nbEvents = fTrace.getNbEvents();
173 setName(Messages.TmfCheckpointIndexer_Indexing + ' ' + fTrace.getName() + " (" + nbEvents + ")"); //$NON-NLS-1$ //$NON-NLS-2$
174 // setName doesn't refresh the UI, setTaskName does
175 long rate = (nbEvents - prevNbEvents) * 4;
176 monitor.setTaskName(rate + " " + Messages.TmfCheckpointIndexer_EventsPerSecond); //$NON-NLS-1$
177 } catch (final InterruptedException e) {
178 return Status.OK_STATUS;
179 }
180 }
181 monitor.done();
182 return Status.OK_STATUS;
183 }
184 };
185 job.setSystem(!isCompleteTrace(fTrace));
186 job.schedule();
187
188 // Build a background request for all the trace data. The index is
189 // updated as we go by readNextEvent().
190 fIndexingRequest = new TmfEventRequest(ITmfEvent.class,
191 range, offset, ITmfEventRequest.ALL_DATA,
192 ITmfEventRequest.ExecutionType.BACKGROUND) {
193 @Override
194 public void handleData(final ITmfEvent event) {
195 super.handleData(event);
196 // Update the trace status at regular intervals
197 if ((getNbRead() % fCheckpointInterval) == 0) {
198 updateTraceStatus();
199 }
200 }
201
202 @Override
203 public void handleSuccess() {
204 fTraceIndex.setTimeRange(fTrace.getTimeRange());
205 fTraceIndex.setNbEvents(fTrace.getNbEvents());
206 if (isCompleteTrace(fTrace)) {
207 fTraceIndex.setIndexComplete();
208 }
209 updateTraceStatus();
210 }
211
212 @Override
213 public void handleCompleted() {
214 job.cancel();
215 super.handleCompleted();
216 fIsIndexing = false;
217 }
218
219 private void updateTraceStatus() {
220 if (fTrace.getNbEvents() > 0) {
221 signalNewTimeRange(fTrace.getStartTime(), fTrace.getEndTime());
222 }
223 }
224 };
225
226 // Submit the request and wait for completion if required
227 fTrace.sendRequest(fIndexingRequest);
228 if (waitForCompletion) {
229 try {
230 fIndexingRequest.waitForCompletion();
231 } catch (final InterruptedException e) {
232 }
233 }
234 }
235
236 /**
237 * Notify the interested parties that the trace time range has changed
238 *
239 * @param startTime the new start time
240 * @param endTime the new end time
241 */
242 private void signalNewTimeRange(final ITmfTimestamp startTime, final ITmfTimestamp endTime) {
243 fTrace.broadcast(new TmfTraceUpdatedSignal(fTrace, fTrace, new TmfTimeRange(startTime, endTime), fTrace.getNbEvents()));
244 }
245
246 // ------------------------------------------------------------------------
247 // ITmfTraceIndexer - updateIndex
248 // ------------------------------------------------------------------------
249
250 @Override
251 public synchronized void updateIndex(final ITmfContext context, final ITmfTimestamp timestamp) {
252 if ((context.getRank() % fCheckpointInterval) == 0) {
253 // Determine the table position
254 final long position = context.getRank() / fCheckpointInterval;
255 // Add new entry at proper location (if empty)
256 if (fTraceIndex.size() == position) {
257 fTraceIndex.insert(new TmfCheckpoint(timestamp, context.getLocation(), position));
258 }
259 }
260 }
261
262 // ------------------------------------------------------------------------
263 // ITmfTraceIndexer - seekIndex
264 // ------------------------------------------------------------------------
265
266 @Override
267 public synchronized ITmfContext seekIndex(final ITmfTimestamp timestamp) {
268
269 // A null timestamp indicates to seek the first event
270 if (timestamp == null) {
271 return fTrace.seekEvent(0);
272 }
273
274 // Find the checkpoint at or before the requested timestamp.
275 // In the very likely event that the timestamp is not at a checkpoint
276 // boundary, bsearch will return index = (- (insertion point + 1)).
277 // It is then trivial to compute the index of the previous checkpoint.
278 long index = fTraceIndex.binarySearch(new TmfCheckpoint(timestamp, null, 0));
279 if (index < 0) {
280 index = Math.max(0, -(index + 2));
281 } else {
282 // If timestamp was in the list, use previous index to be able to find the
283 // first event with the same timestamp before the checkpoint
284 index = Math.max(0, index - 1);
285 }
286
287 // Position the trace at the checkpoint
288 return restoreCheckpoint(index);
289 }
290
291 @Override
292 public ITmfContext seekIndex(final long rank) {
293
294 // A rank < 0 indicates to seek the first event
295 if (rank < 0) {
296 return fTrace.seekEvent(0);
297 }
298
299 // Find the checkpoint at or before the requested rank.
300 final int index = (int) rank / fCheckpointInterval;
301
302 // Position the trace at the checkpoint
303 return restoreCheckpoint(index);
304 }
305
306 /**
307 * Position the trace at the given checkpoint
308 *
309 * @param checkpoint the checkpoint index
310 * @return the corresponding context
311 */
312 private ITmfContext restoreCheckpoint(final long checkpoint) {
313 ITmfLocation location = null;
314 long index = 0;
315 synchronized (fTraceIndex) {
316 if (!fTraceIndex.isEmpty()) {
317 index = checkpoint;
318 if (index >= fTraceIndex.size()) {
319 index = fTraceIndex.size() - 1;
320 }
321 location = fTraceIndex.get(index).getLocation();
322 }
323 }
324 final ITmfContext context = fTrace.seekEvent(location);
325 context.setRank(index * fCheckpointInterval);
326 return context;
327 }
328
329 // ------------------------------------------------------------------------
330 // Getters
331 // ------------------------------------------------------------------------
332
333 /**
334 * @return the trace index
335 */
336 protected ITmfCheckpointIndex getTraceIndex() {
337 return fTraceIndex;
338 }
339
340 private static boolean isCompleteTrace(ITmfTrace trace) {
341 return !(trace instanceof ITmfTraceCompleteness) || ((ITmfTraceCompleteness)trace).isComplete();
342 }
343 }
This page took 0.0413 seconds and 5 git commands to generate.