tmf: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / tmf / 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.jdt.annotation.NonNull;
21 import org.eclipse.tracecompass.internal.tmf.core.Messages;
22 import org.eclipse.tracecompass.internal.tmf.core.trace.indexer.TmfMemoryIndex;
23 import org.eclipse.tracecompass.tmf.core.component.TmfEventProvider;
24 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
25 import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
26 import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest;
27 import org.eclipse.tracecompass.tmf.core.signal.TmfTraceUpdatedSignal;
28 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
29 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
30 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
31 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
32 import org.eclipse.tracecompass.tmf.core.trace.ITmfTraceCompleteness;
33 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
34 import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
35
36 /**
37 * A simple indexer that manages the trace index as an array of trace
38 * checkpoints. Checkpoints are stored in memory at fixed intervals (event rank) in
39 * ascending timestamp order.
40 * <p>
41 * The goal being to access a random trace event reasonably fast from the user's
42 * standpoint, picking the right interval value becomes a trade-off between speed
43 * and memory usage (a shorter inter-event interval is faster but requires more
44 * checkpoints).
45 * <p>
46 * Locating a specific checkpoint is trivial for both rank (rank % interval) and
47 * timestamp (bsearch in the array).
48 * *
49 * @see ITmfTrace
50 * @see ITmfEvent
51 *
52 * @author Francois Chouinard
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, TmfEventProvider.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 */
114 protected ITmfCheckpointIndex createIndex(final ITmfTrace trace) {
115 return new TmfMemoryIndex(trace);
116 }
117
118 @Override
119 public void dispose() {
120 if ((fIndexingRequest != null) && !fIndexingRequest.isCompleted()) {
121 fIndexingRequest.cancel();
122 }
123
124 fTraceIndex.dispose();
125 }
126
127 // ------------------------------------------------------------------------
128 // ITmfTraceIndexer - isIndexing
129 // ------------------------------------------------------------------------
130
131 @Override
132 public boolean isIndexing() {
133 return fIsIndexing;
134 }
135
136 // ------------------------------------------------------------------------
137 // ITmfTraceIndexer - buildIndex
138 // ------------------------------------------------------------------------
139
140 @Override
141 public void buildIndex(final long offset, final TmfTimeRange range, final boolean waitForCompletion) {
142
143 // Don't do anything if we are already indexing
144 synchronized (fTraceIndex) {
145 if (fIsIndexing) {
146 return;
147 }
148 fIsIndexing = true;
149 }
150
151 // No need to build the index, it has been restored
152 if (!fTraceIndex.isCreatedFromScratch()) {
153 // Set some trace attributes that depends on indexing
154 TmfTraceUpdatedSignal signal = new TmfTraceUpdatedSignal(this, fTrace, new TmfTimeRange(fTraceIndex.getTimeRange().getStartTime(), fTraceIndex.getTimeRange().getEndTime()), fTraceIndex.getNbEvents());
155 if (waitForCompletion) {
156 fTrace.broadcast(signal);
157 } else {
158 fTrace.broadcastAsync(signal);
159 }
160 fIsIndexing = false;
161 return;
162 }
163
164 // The monitoring job
165 final Job job = new Job("Indexing " + fTrace.getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
166 @Override
167 protected IStatus run(final IProgressMonitor monitor) {
168 monitor.beginTask("", IProgressMonitor.UNKNOWN); //$NON-NLS-1$
169 while (!monitor.isCanceled()) {
170 try {
171 long prevNbEvents = fTrace.getNbEvents();
172 Thread.sleep(250);
173 long nbEvents = fTrace.getNbEvents();
174 setName(Messages.TmfCheckpointIndexer_Indexing + ' ' + fTrace.getName() + " (" + nbEvents + ")"); //$NON-NLS-1$ //$NON-NLS-2$
175 // setName doesn't refresh the UI, setTaskName does
176 long rate = (nbEvents - prevNbEvents) * 4;
177 monitor.setTaskName(rate + " " + Messages.TmfCheckpointIndexer_EventsPerSecond); //$NON-NLS-1$
178 } catch (final InterruptedException e) {
179 return Status.OK_STATUS;
180 }
181 }
182 monitor.done();
183 return Status.OK_STATUS;
184 }
185 };
186 job.setSystem(!isCompleteTrace(fTrace));
187 job.schedule();
188
189 // Build a background request for all the trace data. The index is
190 // updated as we go by readNextEvent().
191 fIndexingRequest = new TmfEventRequest(ITmfEvent.class,
192 range, offset, ITmfEventRequest.ALL_DATA,
193 ITmfEventRequest.ExecutionType.BACKGROUND) {
194 @Override
195 public void handleData(final ITmfEvent event) {
196 super.handleData(event);
197 // Update the trace status at regular intervals
198 if ((getNbRead() % fCheckpointInterval) == 0) {
199 updateTraceStatus();
200 }
201 }
202
203 @Override
204 public void handleSuccess() {
205 fTraceIndex.setTimeRange(fTrace.getTimeRange());
206 fTraceIndex.setNbEvents(fTrace.getNbEvents());
207 if (isCompleteTrace(fTrace)) {
208 fTraceIndex.setIndexComplete();
209 }
210 updateTraceStatus();
211 }
212
213 @Override
214 public void handleCompleted() {
215 job.cancel();
216 super.handleCompleted();
217 fIsIndexing = false;
218 }
219
220 private void updateTraceStatus() {
221 if (fTrace.getNbEvents() > 0) {
222 signalNewTimeRange(fTrace.getStartTime(), fTrace.getEndTime());
223 }
224 }
225 };
226
227 // Submit the request and wait for completion if required
228 fTrace.sendRequest(fIndexingRequest);
229 if (waitForCompletion) {
230 try {
231 fIndexingRequest.waitForCompletion();
232 } catch (final InterruptedException e) {
233 }
234 }
235 }
236
237 /**
238 * Notify the interested parties that the trace time range has changed
239 *
240 * @param startTime the new start time
241 * @param endTime the new end time
242 */
243 private void signalNewTimeRange(final @NonNull ITmfTimestamp startTime, final @NonNull ITmfTimestamp endTime) {
244 fTrace.broadcast(new TmfTraceUpdatedSignal(fTrace, fTrace, new TmfTimeRange(startTime, endTime), fTrace.getNbEvents()));
245 }
246
247 // ------------------------------------------------------------------------
248 // ITmfTraceIndexer - updateIndex
249 // ------------------------------------------------------------------------
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 @Override
268 public synchronized ITmfContext seekIndex(final ITmfTimestamp timestamp) {
269
270 // A null timestamp indicates to seek the first event
271 if (timestamp == null) {
272 return fTrace.seekEvent(0);
273 }
274
275 // Find the checkpoint at or before the requested timestamp.
276 // In the very likely event that the timestamp is not at a checkpoint
277 // boundary, bsearch will return index = (- (insertion point + 1)).
278 // It is then trivial to compute the index of the previous checkpoint.
279 long index = fTraceIndex.binarySearch(new TmfCheckpoint(timestamp, null, 0));
280 if (index < 0) {
281 index = Math.max(0, -(index + 2));
282 } else {
283 // If timestamp was in the list, use previous index to be able to find the
284 // first event with the same timestamp before the checkpoint
285 index = Math.max(0, index - 1);
286 }
287
288 // Position the trace at the checkpoint
289 return restoreCheckpoint(index);
290 }
291
292 @Override
293 public ITmfContext seekIndex(final long rank) {
294
295 // A rank < 0 indicates to seek the first event
296 if (rank < 0) {
297 return fTrace.seekEvent(0);
298 }
299
300 // Find the checkpoint at or before the requested rank.
301 final int index = (int) rank / fCheckpointInterval;
302
303 // Position the trace at the checkpoint
304 return restoreCheckpoint(index);
305 }
306
307 /**
308 * Position the trace at the given checkpoint
309 *
310 * @param checkpoint the checkpoint index
311 * @return the corresponding context
312 */
313 private ITmfContext restoreCheckpoint(final long checkpoint) {
314 ITmfLocation location = null;
315 long index = 0;
316 synchronized (fTraceIndex) {
317 if (!fTraceIndex.isEmpty()) {
318 index = checkpoint;
319 if (index >= fTraceIndex.size()) {
320 index = fTraceIndex.size() - 1;
321 }
322 location = fTraceIndex.get(index).getLocation();
323 }
324 }
325 final ITmfContext context = fTrace.seekEvent(location);
326 context.setRank(index * fCheckpointInterval);
327 return context;
328 }
329
330 // ------------------------------------------------------------------------
331 // Getters
332 // ------------------------------------------------------------------------
333
334 /**
335 * @return the trace index
336 */
337 protected ITmfCheckpointIndex getTraceIndex() {
338 return fTraceIndex;
339 }
340
341 private static boolean isCompleteTrace(ITmfTrace trace) {
342 return !(trace instanceof ITmfTraceCompleteness) || ((ITmfTraceCompleteness)trace).isComplete();
343 }
344 }
This page took 0.041275 seconds and 5 git commands to generate.