Update TmfTrace javadoc
[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.Collections;
16 import java.util.Vector;
17
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.core.runtime.jobs.Job;
22 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
23 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
24 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
25 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
26 import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
27 import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
28 import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
29 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
30
31 /**
32 * A simple indexer that manages the trace index as an array of trace
33 * checkpoints. Checkpoints are stored at fixed intervals (event rank) in
34 * ascending timestamp order.
35 * <p>
36 * The goal being to access a random trace event reasonably fast from the user's
37 * standpoint, picking the right interval value becomes a trade-off between speed
38 * and memory usage (a shorter inter-event interval is faster but requires more
39 * checkpoints).
40 * <p>
41 * Locating a specific checkpoint is trivial for both rank (rank % interval) and
42 * timestamp (bsearch in the array).
43 *
44 * @since 1.0
45 * @version 1.0
46 * @author Francois Chouinard
47 *
48 * @see ITmfTraceIndexer
49 * @see ITmfTrace
50 * @see ITmfEvent
51 */
52 public class TmfCheckpointIndexer<T extends ITmfTrace<ITmfEvent>> implements ITmfTraceIndexer<T> {
53
54 // ------------------------------------------------------------------------
55 // Attributes
56 // ------------------------------------------------------------------------
57
58 /**
59 * The event trace to index
60 */
61 private final ITmfTrace<ITmfEvent> fTrace;
62
63 /**
64 * The interval between checkpoints
65 */
66 protected final int fCheckpointInterval;
67
68 /**
69 * The trace index. It is composed of checkpoints taken at intervals of
70 * fCheckpointInterval events.
71 */
72 protected final Vector<TmfCheckpoint> fTraceIndex;
73
74 // ------------------------------------------------------------------------
75 // Construction
76 // ------------------------------------------------------------------------
77
78 /**
79 * Basic constructor that uses the default trace block size as checkpoints
80 * intervals
81 *
82 * @param trace the trace to index
83 */
84 public TmfCheckpointIndexer(final ITmfTrace<ITmfEvent> trace) {
85 this(trace, TmfTrace.DEFAULT_BLOCK_SIZE);
86 }
87
88 /**
89 * Full trace indexer
90 *
91 * @param trace the trace to index
92 * @param interval the checkpoints interval
93 */
94 public TmfCheckpointIndexer(final ITmfTrace<ITmfEvent> trace, final int interval) {
95 fTrace = trace;
96 fCheckpointInterval = interval;
97 fTraceIndex = new Vector<TmfCheckpoint>();
98 }
99
100 // ------------------------------------------------------------------------
101 // ITmfTraceIndexer - buildIndex
102 // ------------------------------------------------------------------------
103
104 /* (non-Javadoc)
105 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#indexTrace(boolean)
106 *
107 * The index is a list of contexts that point to events at regular interval
108 * (rank-wise) in the trace. After it is built, the index can be used to
109 * quickly access any event by rank or timestamp (using seekIndex()).
110 *
111 * The index is built simply by reading the trace
112 */
113 @Override
114 public void buildIndex(final boolean waitForCompletion) {
115
116 // The monitoring job
117 final Job job = new Job("Indexing " + fTrace.getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
118 @Override
119 protected IStatus run(final IProgressMonitor monitor) {
120 while (!monitor.isCanceled()) {
121 try {
122 Thread.sleep(100);
123 } catch (final InterruptedException e) {
124 return Status.OK_STATUS;
125 }
126 }
127 monitor.done();
128 return Status.OK_STATUS;
129 }
130 };
131 job.schedule();
132
133 // Clear the checkpoints
134 fTraceIndex.clear();
135
136 // Build a background request for all the trace data. The index is
137 // updated as we go by getNextEvent().
138 final ITmfEventRequest<ITmfEvent> request = new TmfEventRequest<ITmfEvent>(ITmfEvent.class, TmfTimeRange.ETERNITY,
139 TmfDataRequest.ALL_DATA, fCheckpointInterval, ITmfDataRequest.ExecutionType.BACKGROUND)
140 {
141 ITmfTimestamp startTime = null;
142 ITmfTimestamp lastTime = null;
143
144 @Override
145 public void handleData(final ITmfEvent event) {
146 super.handleData(event);
147 if (event != null) {
148 final ITmfTimestamp timestamp = event.getTimestamp();
149 if (startTime == null) {
150 startTime = timestamp.clone();
151 }
152 lastTime = timestamp.clone();
153
154 // Update the trace status at regular intervals
155 if ((getNbRead() % fCheckpointInterval) == 0) {
156 updateTraceStatus();
157 }
158 }
159 }
160
161 @Override
162 public void handleSuccess() {
163 updateTraceStatus();
164 }
165
166 @Override
167 public void handleCompleted() {
168 job.cancel();
169 super.handleCompleted();
170 }
171
172 private void updateTraceStatus() {
173 if (getNbRead() != 0) {
174 signalNewTimeRange(startTime, lastTime);
175 }
176 }
177 };
178
179 // Submit the request and wait for completion if required
180 fTrace.sendRequest(request);
181 if (waitForCompletion) {
182 try {
183 request.waitForCompletion();
184 } catch (final InterruptedException e) {
185 }
186 }
187 }
188
189 /**
190 * Notify the interested parties that the trace time range has changed
191 *
192 * @param startTime the new start time
193 * @param endTime the new end time
194 */
195 private void signalNewTimeRange(final ITmfTimestamp startTime, final ITmfTimestamp endTime) {
196 fTrace.broadcast(new TmfTraceUpdatedSignal(fTrace, fTrace, new TmfTimeRange(startTime, endTime)));
197 }
198
199 // ------------------------------------------------------------------------
200 // ITmfTraceIndexer - updateIndex
201 // ------------------------------------------------------------------------
202
203 /* (non-Javadoc)
204 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#updateIndex(org.eclipse.linuxtools.tmf.core.trace.ITmfContext, org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
205 */
206 @Override
207 public synchronized void updateIndex(final ITmfContext context, final ITmfTimestamp timestamp) {
208 final long rank = context.getRank();
209 if ((rank % fCheckpointInterval) == 0) {
210 // Determine the table position
211 final long position = rank / fCheckpointInterval;
212 // Add new entry at proper location (if empty)
213 if (fTraceIndex.size() == position) {
214 final ITmfLocation<?> location = context.getLocation().clone();
215 fTraceIndex.add(new TmfCheckpoint(timestamp.clone(), location));
216 }
217 }
218 }
219
220 // ------------------------------------------------------------------------
221 // ITmfTraceIndexer - seekIndex
222 // ------------------------------------------------------------------------
223
224 /* (non-Javadoc)
225 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
226 */
227 @Override
228 public synchronized ITmfContext seekIndex(final ITmfTimestamp timestamp) {
229
230 // A null timestamp indicates to seek the first event
231 if (timestamp == null)
232 return fTrace.seekEvent(0);
233
234 // Find the checkpoint at or before the requested timestamp.
235 // In the very likely event that the timestamp is not at a checkpoint
236 // boundary, bsearch will return index = (- (insertion point + 1)).
237 // It is then trivial to compute the index of the previous checkpoint.
238 int index = Collections.binarySearch(fTraceIndex, new TmfCheckpoint(timestamp, null));
239 if (index < 0) {
240 index = Math.max(0, -(index + 2));
241 }
242
243 // Position the trace at the checkpoint
244 return seekCheckpoint(index);
245 }
246
247 /* (non-Javadoc)
248 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(long)
249 */
250 @Override
251 public ITmfContext seekIndex(final long rank) {
252
253 // A rank < 0 indicates to seek the first event
254 if (rank < 0)
255 return fTrace.seekEvent(0);
256
257 // Find the checkpoint at or before the requested rank.
258 final int index = (int) rank / fCheckpointInterval;
259
260 // Position the trace at the checkpoint
261 return seekCheckpoint(index);
262 }
263
264 /**
265 * Position the trace at the given checkpoint
266 *
267 * @param index
268 * the checkpoint index
269 * @return the corresponding context
270 */
271 private ITmfContext seekCheckpoint(int index) {
272 ITmfLocation<?> location;
273 synchronized (fTraceIndex) {
274 if (!fTraceIndex.isEmpty()) {
275 if (index >= fTraceIndex.size()) {
276 index = fTraceIndex.size() - 1;
277 }
278 location = fTraceIndex.elementAt(index).getLocation();
279 } else {
280 location = null;
281 }
282 }
283 final ITmfContext context = fTrace.seekEvent(location);
284 context.setRank((long) index * fCheckpointInterval);
285 return context;
286 }
287
288 }
This page took 0.037719 seconds and 6 git commands to generate.