Merge branch 'master'
[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 /**
64 * The trace index. It is composed of checkpoints taken at intervals of
65 * fCheckpointInterval events.
66 */
67 private final List<TmfCheckpoint> fTraceIndex;
68
69 // ------------------------------------------------------------------------
70 // Construction
71 // ------------------------------------------------------------------------
72
73 /**
74 * Basic constructor that uses the default trace block size as checkpoints
75 * intervals
76 *
77 * @param trace the trace to index
78 */
79 public TmfCheckpointIndexer(final ITmfTrace<ITmfEvent> trace) {
80 this(trace, TmfTrace.DEFAULT_BLOCK_SIZE);
81 }
82
83 /**
84 * Full trace indexer
85 *
86 * @param trace the trace to index
87 * @param interval the checkpoints interval
88 */
89 public TmfCheckpointIndexer(final ITmfTrace<ITmfEvent> trace, final int interval) {
90 fTrace = trace;
91 fCheckpointInterval = interval;
92 fTraceIndex = new ArrayList<TmfCheckpoint>();
93 }
94
95 // ------------------------------------------------------------------------
96 // ITmfTraceIndexer - buildIndex
97 // ------------------------------------------------------------------------
98
99 /* (non-Javadoc)
100 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#indexTrace(boolean)
101 *
102 * The index is a list of contexts that point to events at regular interval
103 * (rank-wise) in the trace. After it is built, the index can be used to
104 * quickly access any event by rank or timestamp (using seekIndex()).
105 *
106 * The index is built simply by reading the trace
107 */
108 @Override
109 public void buildIndex(final boolean waitForCompletion) {
110
111 // The monitoring job
112 final Job job = new Job("Indexing " + fTrace.getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
113 @Override
114 protected IStatus run(final IProgressMonitor monitor) {
115 while (!monitor.isCanceled()) {
116 try {
117 Thread.sleep(100);
118 } catch (final InterruptedException e) {
119 return Status.OK_STATUS;
120 }
121 }
122 monitor.done();
123 return Status.OK_STATUS;
124 }
125 };
126 job.schedule();
127
128 // Clear the checkpoints
129 fTraceIndex.clear();
130
131 // Build a background request for all the trace data. The index is
132 // updated as we go by readNextEvent().
133 final ITmfEventRequest<ITmfEvent> request = new TmfEventRequest<ITmfEvent>(ITmfEvent.class, TmfTimeRange.ETERNITY,
134 TmfDataRequest.ALL_DATA, fCheckpointInterval, ITmfDataRequest.ExecutionType.BACKGROUND)
135 {
136 private ITmfTimestamp startTime = null;
137 private ITmfTimestamp lastTime = null;
138
139 @Override
140 public void handleData(final ITmfEvent event) {
141 super.handleData(event);
142 if (event != null) {
143 final ITmfTimestamp timestamp = event.getTimestamp();
144 if (startTime == null) {
145 startTime = timestamp.clone();
146 }
147 lastTime = timestamp.clone();
148
149 // Update the trace status at regular intervals
150 if ((getNbRead() % fCheckpointInterval) == 0) {
151 updateTraceStatus();
152 }
153 }
154 }
155
156 @Override
157 public void handleSuccess() {
158 updateTraceStatus();
159 }
160
161 @Override
162 public void handleCompleted() {
163 job.cancel();
164 super.handleCompleted();
165 }
166
167 private void updateTraceStatus() {
168 if (getNbRead() != 0) {
169 signalNewTimeRange(startTime, lastTime);
170 }
171 }
172 };
173
174 // Submit the request and wait for completion if required
175 fTrace.sendRequest(request);
176 if (waitForCompletion) {
177 try {
178 request.waitForCompletion();
179 } catch (final InterruptedException e) {
180 }
181 }
182 }
183
184 /**
185 * Notify the interested parties that the trace time range has changed
186 *
187 * @param startTime the new start time
188 * @param endTime the new end time
189 */
190 private void signalNewTimeRange(final ITmfTimestamp startTime, final ITmfTimestamp endTime) {
191 fTrace.broadcast(new TmfTraceUpdatedSignal(fTrace, fTrace, new TmfTimeRange(startTime, endTime)));
192 }
193
194 // ------------------------------------------------------------------------
195 // ITmfTraceIndexer - updateIndex
196 // ------------------------------------------------------------------------
197
198 /* (non-Javadoc)
199 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#updateIndex(org.eclipse.linuxtools.tmf.core.trace.ITmfContext, org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
200 */
201 @Override
202 public synchronized void updateIndex(final ITmfContext context, final ITmfTimestamp timestamp) {
203 final long rank = context.getRank();
204 if ((rank % fCheckpointInterval) == 0) {
205 // Determine the table position
206 final long position = rank / fCheckpointInterval;
207 // Add new entry at proper location (if empty)
208 if (fTraceIndex.size() == position) {
209 final ITmfLocation<?> location = context.getLocation().clone();
210 fTraceIndex.add(new TmfCheckpoint(timestamp.clone(), location));
211 }
212 }
213 }
214
215 // ------------------------------------------------------------------------
216 // ITmfTraceIndexer - seekIndex
217 // ------------------------------------------------------------------------
218
219 /* (non-Javadoc)
220 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
221 */
222 @Override
223 public synchronized ITmfContext seekIndex(final ITmfTimestamp timestamp) {
224
225 // A null timestamp indicates to seek the first event
226 if (timestamp == null) {
227 return fTrace.seekEvent(0);
228 }
229
230 // Find the checkpoint at or before the requested timestamp.
231 // In the very likely event that the timestamp is not at a checkpoint
232 // boundary, bsearch will return index = (- (insertion point + 1)).
233 // It is then trivial to compute the index of the previous checkpoint.
234 int index = Collections.binarySearch(fTraceIndex, new TmfCheckpoint(timestamp, null));
235 if (index < 0) {
236 index = Math.max(0, -(index + 2));
237 }
238
239 // Position the trace at the checkpoint
240 return seekCheckpoint(index);
241 }
242
243 /* (non-Javadoc)
244 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(long)
245 */
246 @Override
247 public ITmfContext seekIndex(final long rank) {
248
249 // A rank < 0 indicates to seek the first event
250 if (rank < 0) {
251 return fTrace.seekEvent(0);
252 }
253
254 // Find the checkpoint at or before the requested rank.
255 final int index = (int) rank / fCheckpointInterval;
256
257 // Position the trace at the checkpoint
258 return seekCheckpoint(index);
259 }
260
261 /**
262 * Position the trace at the given checkpoint
263 *
264 * @param checkpoint the checkpoint index
265 * @return the corresponding context
266 */
267 private ITmfContext seekCheckpoint(final int checkpoint) {
268 ITmfLocation<?> location = null;
269 int index = checkpoint;
270 synchronized (fTraceIndex) {
271 if (!fTraceIndex.isEmpty()) {
272 if (index >= fTraceIndex.size()) {
273 index = fTraceIndex.size() - 1;
274 }
275 location = fTraceIndex.get(index).getLocation();
276 }
277 }
278 final ITmfContext context = fTrace.seekEvent(location);
279 context.setRank((long) index * fCheckpointInterval);
280 return context;
281 }
282
283 // ------------------------------------------------------------------------
284 // Getters
285 // ------------------------------------------------------------------------
286
287 /**
288 * @return the trace index
289 */
290 protected List<TmfCheckpoint> getTraceIndex() {
291 return fTraceIndex;
292 }
293 }
This page took 0.037606 seconds and 6 git commands to generate.