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