Bug 378402: Implementation of ControlFlow view and Resources view for
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / TmfCheckpointIndexer.java
CommitLineData
20658947
FC
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
13package org.eclipse.linuxtools.tmf.core.trace;
14
0316808c 15import java.util.ArrayList;
20658947 16import java.util.Collections;
0316808c 17import java.util.List;
20658947
FC
18
19import org.eclipse.core.runtime.IProgressMonitor;
20import org.eclipse.core.runtime.IStatus;
21import org.eclipse.core.runtime.Status;
22import org.eclipse.core.runtime.jobs.Job;
23import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
24import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
25import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
20658947
FC
26import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
27import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
28import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
29import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
30import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
31
32/**
2848c377
FC
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.
20658947
FC
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).
f7703ed6 44 *
f7703ed6
FC
45 * @version 1.0
46 * @author Francois Chouinard
47 *
f7703ed6
FC
48 * @see ITmfTrace
49 * @see ITmfEvent
20658947 50 */
7e6347b0 51public class TmfCheckpointIndexer<T extends ITmfTrace<ITmfEvent>> implements ITmfTraceIndexer<T> {
20658947
FC
52
53 // ------------------------------------------------------------------------
54 // Attributes
55 // ------------------------------------------------------------------------
56
0316808c 57 // The event trace to index
20658947
FC
58 private final ITmfTrace<ITmfEvent> fTrace;
59
0316808c
FC
60 // The interval between checkpoints
61 private final int fCheckpointInterval;
20658947 62
9e0640dc
FC
63 // The event trace to index
64 private boolean fIsIndexing;
65
20658947
FC
66 /**
67 * The trace index. It is composed of checkpoints taken at intervals of
68 * fCheckpointInterval events.
69 */
3427112b 70 private final List<ITmfCheckpoint> fTraceIndex;
20658947 71
b5ee6881
FC
72 /**
73 * The indexing request
74 */
75 private ITmfEventRequest<ITmfEvent> fIndexingRequest = null;
76
20658947
FC
77 // ------------------------------------------------------------------------
78 // Construction
79 // ------------------------------------------------------------------------
80
81 /**
82 * Basic constructor that uses the default trace block size as checkpoints
83 * intervals
84 *
85 * @param trace the trace to index
86 */
7e6347b0 87 public TmfCheckpointIndexer(final ITmfTrace<ITmfEvent> trace) {
20658947
FC
88 this(trace, TmfTrace.DEFAULT_BLOCK_SIZE);
89 }
90
91 /**
92 * Full trace indexer
93 *
94 * @param trace the trace to index
95 * @param interval the checkpoints interval
96 */
7e6347b0 97 public TmfCheckpointIndexer(final ITmfTrace<ITmfEvent> trace, final int interval) {
20658947
FC
98 fTrace = trace;
99 fCheckpointInterval = interval;
3427112b 100 fTraceIndex = new ArrayList<ITmfCheckpoint>();
9e0640dc
FC
101 fIsIndexing = false;
102 }
103
b5ee6881
FC
104 /* (non-Javadoc)
105 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#dispose()
106 */
107 @Override
108 public void dispose() {
109 if ((fIndexingRequest != null) && !fIndexingRequest.isCompleted()) {
110 fIndexingRequest.cancel();
111 fTraceIndex.clear();
112 }
113 }
114
9e0640dc
FC
115 // ------------------------------------------------------------------------
116 // ITmfTraceIndexer - isIndexing
117 // ------------------------------------------------------------------------
118
119 /* (non-Javadoc)
120 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#isIndexing()
121 */
122 @Override
123 public boolean isIndexing() {
124 return fIsIndexing;
20658947
FC
125 }
126
127 // ------------------------------------------------------------------------
1703b536 128 // ITmfTraceIndexer - buildIndex
20658947
FC
129 // ------------------------------------------------------------------------
130
131 /* (non-Javadoc)
20658947
FC
132 *
133 * The index is a list of contexts that point to events at regular interval
134 * (rank-wise) in the trace. After it is built, the index can be used to
135 * quickly access any event by rank or timestamp (using seekIndex()).
136 *
137 * The index is built simply by reading the trace
9e0640dc
FC
138 *
139 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#buildIndex(long, org.eclipse.linuxtools.tmf.core.event.TmfTimeRange, boolean)
20658947
FC
140 */
141 @Override
9e0640dc
FC
142 public void buildIndex(final long offset, final TmfTimeRange range, final boolean waitForCompletion) {
143
144 // Don't do anything if we are already indexing
145 synchronized (fTraceIndex) {
146 if (fIsIndexing) {
147 return;
148 }
149 fIsIndexing = true;
150 }
20658947
FC
151
152 // The monitoring job
153 final Job job = new Job("Indexing " + fTrace.getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
154 @Override
155 protected IStatus run(final IProgressMonitor monitor) {
156 while (!monitor.isCanceled()) {
157 try {
158 Thread.sleep(100);
159 } catch (final InterruptedException e) {
160 return Status.OK_STATUS;
161 }
162 }
163 monitor.done();
164 return Status.OK_STATUS;
165 }
166 };
167 job.schedule();
168
20658947 169 // Build a background request for all the trace data. The index is
07671572 170 // updated as we go by readNextEvent().
b5ee6881 171 fIndexingRequest = new TmfEventRequest<ITmfEvent>(ITmfEvent.class,
9e0640dc 172 range, offset, TmfDataRequest.ALL_DATA, fCheckpointInterval, ITmfDataRequest.ExecutionType.BACKGROUND)
d337369a 173 {
0316808c
FC
174 private ITmfTimestamp startTime = null;
175 private ITmfTimestamp lastTime = null;
20658947
FC
176
177 @Override
178 public void handleData(final ITmfEvent event) {
179 super.handleData(event);
180 if (event != null) {
181 final ITmfTimestamp timestamp = event.getTimestamp();
182 if (startTime == null) {
183 startTime = timestamp.clone();
184 }
185 lastTime = timestamp.clone();
186
187 // Update the trace status at regular intervals
188 if ((getNbRead() % fCheckpointInterval) == 0) {
189 updateTraceStatus();
190 }
191 }
192 }
193
194 @Override
195 public void handleSuccess() {
196 updateTraceStatus();
197 }
198
199 @Override
200 public void handleCompleted() {
201 job.cancel();
202 super.handleCompleted();
9e0640dc 203 fIsIndexing = false;
20658947
FC
204 }
205
206 private void updateTraceStatus() {
207 if (getNbRead() != 0) {
7e6347b0 208 signalNewTimeRange(startTime, lastTime);
20658947
FC
209 }
210 }
d337369a 211 };
20658947 212
d337369a 213 // Submit the request and wait for completion if required
b5ee6881 214 fTrace.sendRequest(fIndexingRequest);
d337369a
FC
215 if (waitForCompletion) {
216 try {
b5ee6881 217 fIndexingRequest.waitForCompletion();
d337369a
FC
218 } catch (final InterruptedException e) {
219 }
220 }
20658947
FC
221 }
222
7e6347b0
FC
223 /**
224 * Notify the interested parties that the trace time range has changed
225 *
226 * @param startTime the new start time
227 * @param endTime the new end time
228 */
229 private void signalNewTimeRange(final ITmfTimestamp startTime, final ITmfTimestamp endTime) {
1703b536
FC
230 fTrace.broadcast(new TmfTraceUpdatedSignal(fTrace, fTrace, new TmfTimeRange(startTime, endTime)));
231 }
232
233 // ------------------------------------------------------------------------
234 // ITmfTraceIndexer - updateIndex
235 // ------------------------------------------------------------------------
236
d337369a
FC
237 /* (non-Javadoc)
238 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#updateIndex(org.eclipse.linuxtools.tmf.core.trace.ITmfContext, org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
239 */
1703b536 240 @Override
d337369a
FC
241 public synchronized void updateIndex(final ITmfContext context, final ITmfTimestamp timestamp) {
242 final long rank = context.getRank();
1703b536
FC
243 if ((rank % fCheckpointInterval) == 0) {
244 // Determine the table position
245 final long position = rank / fCheckpointInterval;
246 // Add new entry at proper location (if empty)
247 if (fTraceIndex.size() == position) {
03648eab 248 fTraceIndex.add(new TmfCheckpoint(timestamp.clone(), context.clone2()));
1703b536
FC
249 }
250 }
251 }
252
253 // ------------------------------------------------------------------------
254 // ITmfTraceIndexer - seekIndex
255 // ------------------------------------------------------------------------
20658947
FC
256
257 /* (non-Javadoc)
258 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
259 */
260 @Override
1703b536 261 public synchronized ITmfContext seekIndex(final ITmfTimestamp timestamp) {
20658947 262
1703b536 263 // A null timestamp indicates to seek the first event
0316808c 264 if (timestamp == null) {
7e6347b0 265 return fTrace.seekEvent(0);
0316808c 266 }
20658947 267
1703b536
FC
268 // Find the checkpoint at or before the requested timestamp.
269 // In the very likely event that the timestamp is not at a checkpoint
270 // boundary, bsearch will return index = (- (insertion point + 1)).
271 // It is then trivial to compute the index of the previous checkpoint.
272 int index = Collections.binarySearch(fTraceIndex, new TmfCheckpoint(timestamp, null));
20658947
FC
273 if (index < 0) {
274 index = Math.max(0, -(index + 2));
275 }
276
277 // Position the trace at the checkpoint
1703b536 278 return seekCheckpoint(index);
20658947
FC
279 }
280
1703b536
FC
281 /* (non-Javadoc)
282 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(long)
283 */
20658947
FC
284 @Override
285 public ITmfContext seekIndex(final long rank) {
286
f6ad2e3d
FC
287 // A rank < 0 indicates to seek the first event
288 if (rank < 0) {
289 return fTrace.seekEvent(0);
290 }
1703b536 291
f6ad2e3d
FC
292 // Find the checkpoint at or before the requested rank.
293 final int index = (int) rank / fCheckpointInterval;
1703b536 294
f6ad2e3d
FC
295 // Position the trace at the checkpoint
296 return seekCheckpoint(index);
1703b536
FC
297 }
298
299 /**
300 * Position the trace at the given checkpoint
301 *
0316808c 302 * @param checkpoint the checkpoint index
1703b536
FC
303 * @return the corresponding context
304 */
0316808c 305 private ITmfContext seekCheckpoint(final int checkpoint) {
07671572 306 ITmfLocation<?> location = null;
3427112b 307 int index = 0;
20658947 308 synchronized (fTraceIndex) {
1703b536 309 if (!fTraceIndex.isEmpty()) {
3427112b 310 index = checkpoint;
20658947
FC
311 if (index >= fTraceIndex.size()) {
312 index = fTraceIndex.size() - 1;
313 }
03648eab
FC
314 final ITmfContext context = fTraceIndex.get(index).getContext().clone2();
315// fTrace.seekEvent(context.getLocation());
316 return context;
20658947
FC
317 }
318 }
7e6347b0 319 final ITmfContext context = fTrace.seekEvent(location);
afc86f78 320 context.setRank((long) index * fCheckpointInterval);
20658947
FC
321 return context;
322 }
323
0316808c
FC
324 // ------------------------------------------------------------------------
325 // Getters
326 // ------------------------------------------------------------------------
327
328 /**
329 * @return the trace index
330 */
3427112b 331 protected List<ITmfCheckpoint> getTraceIndex() {
0316808c
FC
332 return fTraceIndex;
333 }
20658947 334}
This page took 0.043262 seconds and 5 git commands to generate.