Format internal request tracing
[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 System.out.println("TmfCheckpointIndexer.buildIndex()");;
129
130 // Clear the checkpoints
131 fTraceIndex.clear();
132
133 // Build a background request for all the trace data. The index is
134 // updated as we go by readNextEvent().
135 final ITmfEventRequest<ITmfEvent> request = new TmfEventRequest<ITmfEvent>(ITmfEvent.class, TmfTimeRange.ETERNITY,
136 TmfDataRequest.ALL_DATA, fCheckpointInterval, ITmfDataRequest.ExecutionType.BACKGROUND)
137 {
138 private ITmfTimestamp startTime = null;
139 private ITmfTimestamp lastTime = null;
140
141 @Override
142 public void handleData(final ITmfEvent event) {
143 super.handleData(event);
144 if (event != null) {
145 final ITmfTimestamp timestamp = event.getTimestamp();
146 if (startTime == null) {
147 startTime = timestamp.clone();
148 }
149 lastTime = timestamp.clone();
150
151 // Update the trace status at regular intervals
152 if ((getNbRead() % fCheckpointInterval) == 0) {
153 updateTraceStatus();
154 }
155 }
156 }
157
158 @Override
159 public void handleSuccess() {
160 updateTraceStatus();
161 }
162
163 @Override
164 public void handleCompleted() {
165 job.cancel();
166 super.handleCompleted();
167 }
168
169 private void updateTraceStatus() {
170 if (getNbRead() != 0) {
171 signalNewTimeRange(startTime, lastTime);
172 }
173 }
174 };
175
176 // Submit the request and wait for completion if required
177 fTrace.sendRequest(request);
178 if (waitForCompletion) {
179 try {
180 request.waitForCompletion();
181 } catch (final InterruptedException e) {
182 }
183 }
184 }
185
186 /**
187 * Notify the interested parties that the trace time range has changed
188 *
189 * @param startTime the new start time
190 * @param endTime the new end time
191 */
192 private void signalNewTimeRange(final ITmfTimestamp startTime, final ITmfTimestamp endTime) {
193 fTrace.broadcast(new TmfTraceUpdatedSignal(fTrace, fTrace, new TmfTimeRange(startTime, endTime)));
194 }
195
196 // ------------------------------------------------------------------------
197 // ITmfTraceIndexer - updateIndex
198 // ------------------------------------------------------------------------
199
200 /* (non-Javadoc)
201 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#updateIndex(org.eclipse.linuxtools.tmf.core.trace.ITmfContext, org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
202 */
203 @Override
204 public synchronized void updateIndex(final ITmfContext context, final ITmfTimestamp timestamp) {
205 final long rank = context.getRank();
206 if ((rank % fCheckpointInterval) == 0) {
207 // Determine the table position
208 final long position = rank / fCheckpointInterval;
209 // Add new entry at proper location (if empty)
210 if (fTraceIndex.size() == position) {
211 final ITmfLocation<?> location = context.getLocation().clone();
212 fTraceIndex.add(new TmfCheckpoint(timestamp.clone(), location));
213 System.out.println("TmfCheckpointIndexer.updateIndex()" + "[" + (fTraceIndex.size() - 1) + "] " + timestamp + ", " + location.toString());
214 }
215 }
216 }
217
218 // ------------------------------------------------------------------------
219 // ITmfTraceIndexer - seekIndex
220 // ------------------------------------------------------------------------
221
222 /* (non-Javadoc)
223 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
224 */
225 @Override
226 public synchronized ITmfContext seekIndex(final ITmfTimestamp timestamp) {
227
228 // A null timestamp indicates to seek the first event
229 if (timestamp == null) {
230 return fTrace.seekEvent(0);
231 }
232
233 // Find the checkpoint at or before the requested timestamp.
234 // In the very likely event that the timestamp is not at a checkpoint
235 // boundary, bsearch will return index = (- (insertion point + 1)).
236 // It is then trivial to compute the index of the previous checkpoint.
237 int index = Collections.binarySearch(fTraceIndex, new TmfCheckpoint(timestamp, null));
238 if (index < 0) {
239 index = Math.max(0, -(index + 2));
240 }
241
242 // Position the trace at the checkpoint
243 return seekCheckpoint(index);
244 }
245
246 /* (non-Javadoc)
247 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(long)
248 */
249 @Override
250 public ITmfContext seekIndex(final long rank) {
251
252 // A rank < 0 indicates to seek the first event
253 if (rank < 0) {
254 return fTrace.seekEvent(0);
255 }
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 checkpoint the checkpoint index
268 * @return the corresponding context
269 */
270 private ITmfContext seekCheckpoint(final int checkpoint) {
271 ITmfLocation<?> location = null;
272 int index = checkpoint;
273 synchronized (fTraceIndex) {
274 if (!fTraceIndex.isEmpty()) {
275 if (index >= fTraceIndex.size()) {
276 index = fTraceIndex.size() - 1;
277 }
278 location = fTraceIndex.get(index).getLocation();
279 }
280 }
281 final ITmfContext context = fTrace.seekEvent(location);
282 context.setRank((long) index * fCheckpointInterval);
283 return context;
284 }
285
286 // ------------------------------------------------------------------------
287 // Getters
288 // ------------------------------------------------------------------------
289
290 /**
291 * @return the trace index
292 */
293 protected List<TmfCheckpoint> getTraceIndex() {
294 return fTraceIndex;
295 }
296 }
This page took 0.052975 seconds and 6 git commands to generate.