Commit | Line | Data |
---|---|---|
20658947 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2012 Ericsson | |
9b749023 | 3 | * |
20658947 FC |
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 | |
9b749023 | 8 | * |
20658947 FC |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.core.trace; | |
14 | ||
0316808c | 15 | import java.util.ArrayList; |
20658947 | 16 | import java.util.Collections; |
0316808c | 17 | import java.util.List; |
20658947 FC |
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; | |
3bd44ac8 | 23 | import org.eclipse.linuxtools.internal.tmf.core.trace.TmfExperimentContext; |
9b749023 | 24 | import org.eclipse.linuxtools.tmf.core.component.TmfDataProvider; |
20658947 FC |
25 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
26 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; | |
27 | import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange; | |
20658947 FC |
28 | import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest; |
29 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; | |
30 | import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest; | |
31 | import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest; | |
32 | import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal; | |
33 | ||
34 | /** | |
2848c377 | 35 | * A simple indexer that manages the trace index as an array of trace |
9b749023 | 36 | * checkpoints. Checkpoints are stored at fixed intervals (event rank) in |
2848c377 | 37 | * ascending timestamp order. |
20658947 FC |
38 | * <p> |
39 | * The goal being to access a random trace event reasonably fast from the user's | |
40 | * standpoint, picking the right interval value becomes a trade-off between speed | |
41 | * and memory usage (a shorter inter-event interval is faster but requires more | |
42 | * checkpoints). | |
43 | * <p> | |
44 | * Locating a specific checkpoint is trivial for both rank (rank % interval) and | |
45 | * timestamp (bsearch in the array). | |
9b749023 | 46 | * |
0283f7ff FC |
47 | * @param <T> The trace event type |
48 | * | |
f7703ed6 FC |
49 | * @version 1.0 |
50 | * @author Francois Chouinard | |
51 | * | |
f7703ed6 FC |
52 | * @see ITmfTrace |
53 | * @see ITmfEvent | |
20658947 | 54 | */ |
7e6347b0 | 55 | public class TmfCheckpointIndexer<T extends ITmfTrace<ITmfEvent>> implements ITmfTraceIndexer<T> { |
20658947 FC |
56 | |
57 | // ------------------------------------------------------------------------ | |
58 | // Attributes | |
59 | // ------------------------------------------------------------------------ | |
60 | ||
0316808c | 61 | // The event trace to index |
3bd44ac8 | 62 | protected final ITmfTrace<ITmfEvent> fTrace; |
20658947 | 63 | |
0316808c FC |
64 | // The interval between checkpoints |
65 | private final int fCheckpointInterval; | |
20658947 | 66 | |
9e0640dc FC |
67 | // The event trace to index |
68 | private boolean fIsIndexing; | |
69 | ||
20658947 FC |
70 | /** |
71 | * The trace index. It is composed of checkpoints taken at intervals of | |
72 | * fCheckpointInterval events. | |
73 | */ | |
3bd44ac8 | 74 | protected final List<ITmfCheckpoint> fTraceIndex; |
20658947 | 75 | |
b5ee6881 | 76 | /** |
9b749023 | 77 | * The indexing request |
b5ee6881 FC |
78 | */ |
79 | private ITmfEventRequest<ITmfEvent> fIndexingRequest = null; | |
9b749023 | 80 | |
20658947 FC |
81 | // ------------------------------------------------------------------------ |
82 | // Construction | |
83 | // ------------------------------------------------------------------------ | |
84 | ||
85 | /** | |
86 | * Basic constructor that uses the default trace block size as checkpoints | |
87 | * intervals | |
9b749023 | 88 | * |
20658947 FC |
89 | * @param trace the trace to index |
90 | */ | |
7e6347b0 | 91 | public TmfCheckpointIndexer(final ITmfTrace<ITmfEvent> trace) { |
9b749023 | 92 | this(trace, TmfDataProvider.DEFAULT_BLOCK_SIZE); |
20658947 FC |
93 | } |
94 | ||
95 | /** | |
96 | * Full trace indexer | |
9b749023 | 97 | * |
20658947 FC |
98 | * @param trace the trace to index |
99 | * @param interval the checkpoints interval | |
100 | */ | |
7e6347b0 | 101 | public TmfCheckpointIndexer(final ITmfTrace<ITmfEvent> trace, final int interval) { |
20658947 FC |
102 | fTrace = trace; |
103 | fCheckpointInterval = interval; | |
3427112b | 104 | fTraceIndex = new ArrayList<ITmfCheckpoint>(); |
9e0640dc FC |
105 | fIsIndexing = false; |
106 | } | |
107 | ||
b5ee6881 FC |
108 | /* (non-Javadoc) |
109 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#dispose() | |
110 | */ | |
111 | @Override | |
112 | public void dispose() { | |
113 | if ((fIndexingRequest != null) && !fIndexingRequest.isCompleted()) { | |
114 | fIndexingRequest.cancel(); | |
115 | fTraceIndex.clear(); | |
116 | } | |
117 | } | |
118 | ||
9e0640dc FC |
119 | // ------------------------------------------------------------------------ |
120 | // ITmfTraceIndexer - isIndexing | |
121 | // ------------------------------------------------------------------------ | |
122 | ||
123 | /* (non-Javadoc) | |
124 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#isIndexing() | |
125 | */ | |
126 | @Override | |
127 | public boolean isIndexing() { | |
128 | return fIsIndexing; | |
20658947 FC |
129 | } |
130 | ||
131 | // ------------------------------------------------------------------------ | |
1703b536 | 132 | // ITmfTraceIndexer - buildIndex |
20658947 FC |
133 | // ------------------------------------------------------------------------ |
134 | ||
135 | /* (non-Javadoc) | |
9b749023 | 136 | * |
20658947 FC |
137 | * The index is a list of contexts that point to events at regular interval |
138 | * (rank-wise) in the trace. After it is built, the index can be used to | |
139 | * quickly access any event by rank or timestamp (using seekIndex()). | |
9b749023 | 140 | * |
20658947 | 141 | * The index is built simply by reading the trace |
9e0640dc FC |
142 | * |
143 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#buildIndex(long, org.eclipse.linuxtools.tmf.core.event.TmfTimeRange, boolean) | |
20658947 FC |
144 | */ |
145 | @Override | |
9e0640dc FC |
146 | public void buildIndex(final long offset, final TmfTimeRange range, final boolean waitForCompletion) { |
147 | ||
9b749023 | 148 | // Don't do anything if we are already indexing |
9e0640dc FC |
149 | synchronized (fTraceIndex) { |
150 | if (fIsIndexing) { | |
151 | return; | |
152 | } | |
153 | fIsIndexing = true; | |
154 | } | |
20658947 FC |
155 | |
156 | // The monitoring job | |
157 | final Job job = new Job("Indexing " + fTrace.getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$ | |
158 | @Override | |
159 | protected IStatus run(final IProgressMonitor monitor) { | |
160 | while (!monitor.isCanceled()) { | |
161 | try { | |
162 | Thread.sleep(100); | |
163 | } catch (final InterruptedException e) { | |
164 | return Status.OK_STATUS; | |
165 | } | |
166 | } | |
167 | monitor.done(); | |
168 | return Status.OK_STATUS; | |
169 | } | |
170 | }; | |
171 | job.schedule(); | |
172 | ||
20658947 | 173 | // Build a background request for all the trace data. The index is |
07671572 | 174 | // updated as we go by readNextEvent(). |
b5ee6881 | 175 | fIndexingRequest = new TmfEventRequest<ITmfEvent>(ITmfEvent.class, |
9e0640dc | 176 | range, offset, TmfDataRequest.ALL_DATA, fCheckpointInterval, ITmfDataRequest.ExecutionType.BACKGROUND) |
d337369a | 177 | { |
20658947 FC |
178 | @Override |
179 | public void handleData(final ITmfEvent event) { | |
180 | super.handleData(event); | |
181 | if (event != null) { | |
20658947 FC |
182 | // Update the trace status at regular intervals |
183 | if ((getNbRead() % fCheckpointInterval) == 0) { | |
184 | updateTraceStatus(); | |
185 | } | |
186 | } | |
187 | } | |
188 | ||
189 | @Override | |
190 | public void handleSuccess() { | |
191 | updateTraceStatus(); | |
192 | } | |
193 | ||
194 | @Override | |
195 | public void handleCompleted() { | |
196 | job.cancel(); | |
197 | super.handleCompleted(); | |
9e0640dc | 198 | fIsIndexing = false; |
20658947 FC |
199 | } |
200 | ||
201 | private void updateTraceStatus() { | |
736988de PT |
202 | if (fTrace.getNbEvents() > 0) { |
203 | signalNewTimeRange(fTrace.getStartTime(), fTrace.getEndTime()); | |
20658947 FC |
204 | } |
205 | } | |
d337369a | 206 | }; |
20658947 | 207 | |
d337369a | 208 | // Submit the request and wait for completion if required |
b5ee6881 | 209 | fTrace.sendRequest(fIndexingRequest); |
d337369a FC |
210 | if (waitForCompletion) { |
211 | try { | |
b5ee6881 | 212 | fIndexingRequest.waitForCompletion(); |
d337369a FC |
213 | } catch (final InterruptedException e) { |
214 | } | |
215 | } | |
20658947 FC |
216 | } |
217 | ||
7e6347b0 FC |
218 | /** |
219 | * Notify the interested parties that the trace time range has changed | |
9b749023 | 220 | * |
7e6347b0 FC |
221 | * @param startTime the new start time |
222 | * @param endTime the new end time | |
223 | */ | |
224 | private void signalNewTimeRange(final ITmfTimestamp startTime, final ITmfTimestamp endTime) { | |
1703b536 FC |
225 | fTrace.broadcast(new TmfTraceUpdatedSignal(fTrace, fTrace, new TmfTimeRange(startTime, endTime))); |
226 | } | |
227 | ||
228 | // ------------------------------------------------------------------------ | |
229 | // ITmfTraceIndexer - updateIndex | |
230 | // ------------------------------------------------------------------------ | |
231 | ||
d337369a FC |
232 | /* (non-Javadoc) |
233 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#updateIndex(org.eclipse.linuxtools.tmf.core.trace.ITmfContext, org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp) | |
234 | */ | |
1703b536 | 235 | @Override |
d337369a FC |
236 | public synchronized void updateIndex(final ITmfContext context, final ITmfTimestamp timestamp) { |
237 | final long rank = context.getRank(); | |
1703b536 FC |
238 | if ((rank % fCheckpointInterval) == 0) { |
239 | // Determine the table position | |
240 | final long position = rank / fCheckpointInterval; | |
241 | // Add new entry at proper location (if empty) | |
242 | if (fTraceIndex.size() == position) { | |
17324c9a | 243 | fTraceIndex.add(new TmfCheckpoint(timestamp.clone(), saveContext(context))); |
1703b536 FC |
244 | } |
245 | } | |
246 | } | |
247 | ||
248 | // ------------------------------------------------------------------------ | |
249 | // ITmfTraceIndexer - seekIndex | |
250 | // ------------------------------------------------------------------------ | |
20658947 FC |
251 | |
252 | /* (non-Javadoc) | |
253 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp) | |
254 | */ | |
255 | @Override | |
1703b536 | 256 | public synchronized ITmfContext seekIndex(final ITmfTimestamp timestamp) { |
20658947 | 257 | |
1703b536 | 258 | // A null timestamp indicates to seek the first event |
0316808c | 259 | if (timestamp == null) { |
7e6347b0 | 260 | return fTrace.seekEvent(0); |
0316808c | 261 | } |
20658947 | 262 | |
1703b536 FC |
263 | // Find the checkpoint at or before the requested timestamp. |
264 | // In the very likely event that the timestamp is not at a checkpoint | |
265 | // boundary, bsearch will return index = (- (insertion point + 1)). | |
266 | // It is then trivial to compute the index of the previous checkpoint. | |
267 | int index = Collections.binarySearch(fTraceIndex, new TmfCheckpoint(timestamp, null)); | |
20658947 FC |
268 | if (index < 0) { |
269 | index = Math.max(0, -(index + 2)); | |
270 | } | |
271 | ||
272 | // Position the trace at the checkpoint | |
408e65d2 | 273 | return restoreCheckpoint(index); |
20658947 FC |
274 | } |
275 | ||
1703b536 FC |
276 | /* (non-Javadoc) |
277 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(long) | |
278 | */ | |
20658947 FC |
279 | @Override |
280 | public ITmfContext seekIndex(final long rank) { | |
281 | ||
f6ad2e3d FC |
282 | // A rank < 0 indicates to seek the first event |
283 | if (rank < 0) { | |
284 | return fTrace.seekEvent(0); | |
285 | } | |
1703b536 | 286 | |
f6ad2e3d FC |
287 | // Find the checkpoint at or before the requested rank. |
288 | final int index = (int) rank / fCheckpointInterval; | |
1703b536 | 289 | |
f6ad2e3d | 290 | // Position the trace at the checkpoint |
408e65d2 | 291 | return restoreCheckpoint(index); |
1703b536 FC |
292 | } |
293 | ||
294 | /** | |
295 | * Position the trace at the given checkpoint | |
9b749023 | 296 | * |
0316808c | 297 | * @param checkpoint the checkpoint index |
1703b536 FC |
298 | * @return the corresponding context |
299 | */ | |
408e65d2 | 300 | private ITmfContext restoreCheckpoint(final int checkpoint) { |
07671572 | 301 | ITmfLocation<?> location = null; |
3427112b | 302 | int index = 0; |
20658947 | 303 | synchronized (fTraceIndex) { |
1703b536 | 304 | if (!fTraceIndex.isEmpty()) { |
3427112b | 305 | index = checkpoint; |
20658947 FC |
306 | if (index >= fTraceIndex.size()) { |
307 | index = fTraceIndex.size() - 1; | |
308 | } | |
3bd44ac8 | 309 | return restoreContext(fTraceIndex.get(index).getContext()); |
20658947 FC |
310 | } |
311 | } | |
7e6347b0 | 312 | final ITmfContext context = fTrace.seekEvent(location); |
afc86f78 | 313 | context.setRank((long) index * fCheckpointInterval); |
20658947 FC |
314 | return context; |
315 | } | |
316 | ||
0316808c FC |
317 | // ------------------------------------------------------------------------ |
318 | // Getters | |
319 | // ------------------------------------------------------------------------ | |
320 | ||
321 | /** | |
322 | * @return the trace index | |
323 | */ | |
3427112b | 324 | protected List<ITmfCheckpoint> getTraceIndex() { |
0316808c FC |
325 | return fTraceIndex; |
326 | } | |
3bd44ac8 FC |
327 | |
328 | // ------------------------------------------------------------------------ | |
329 | // Context conversion functions | |
330 | // ------------------------------------------------------------------------ | |
331 | ||
17324c9a | 332 | private static ITmfContext saveContext(ITmfContext context) { |
3bd44ac8 | 333 | if (context instanceof TmfExperimentContext) { |
17324c9a | 334 | return saveExpContext(context); |
3bd44ac8 FC |
335 | } |
336 | TmfContext ctx = new TmfContext(context.getLocation().clone(), context.getRank()); | |
337 | return ctx; | |
338 | } | |
339 | ||
17324c9a | 340 | private static ITmfContext saveExpContext(ITmfContext context) { |
3bd44ac8 FC |
341 | TmfExperimentContext expContext = (TmfExperimentContext) context; |
342 | int size = expContext.getContexts().length; | |
343 | ITmfContext[] trcCtxts = new TmfContext[size]; | |
344 | for (int i = 0; i < size; i++) { | |
345 | ITmfContext ctx = expContext.getContexts()[i]; | |
346 | trcCtxts[i] = (ctx != null) ? new TmfContext(ctx.getLocation().clone(), ctx.getRank()) : null; | |
347 | } | |
348 | TmfExperimentContext expCtx = new TmfExperimentContext(trcCtxts); | |
349 | expCtx.setLocation(context.getLocation().clone()); | |
350 | expCtx.setRank(context.getRank()); | |
351 | ITmfEvent[] trcEvts = expCtx.getEvents(); | |
352 | for (int i = 0; i < size; i++) { | |
353 | ITmfEvent event = expContext.getEvents()[i]; | |
354 | trcEvts[i] = (event != null) ? event.clone() : null; | |
355 | } | |
356 | return expCtx; | |
357 | } | |
358 | ||
359 | private ITmfContext restoreContext(ITmfContext context) { | |
360 | if (context instanceof TmfExperimentContext) { | |
361 | return restoreExpContext(context); | |
362 | } | |
363 | ITmfContext ctx = fTrace.seekEvent(context.getLocation()); | |
364 | ctx.setRank(context.getRank()); | |
365 | return ctx; | |
366 | } | |
367 | ||
368 | private ITmfContext restoreExpContext(ITmfContext context) { | |
369 | TmfExperimentContext expContext = (TmfExperimentContext) context; | |
370 | int size = expContext.getContexts().length; | |
371 | ITmfContext[] trcCtxts = new ITmfContext[size]; | |
372 | for (int i = 0; i < size; i++) { | |
373 | ITmfTrace<?> trace = ((TmfExperiment<?>) fTrace).getTraces()[i]; | |
374 | ITmfContext ctx = expContext.getContexts()[i]; | |
375 | trcCtxts[i] = trace.seekEvent(ctx.getLocation().clone()); | |
376 | trcCtxts[i].setRank(ctx.getRank()); | |
377 | } | |
378 | TmfExperimentContext ctx = new TmfExperimentContext(trcCtxts); | |
379 | ctx.setLocation(context.getLocation().clone()); | |
380 | ctx.setRank(context.getRank()); | |
381 | ITmfEvent[] trcEvts = expContext.getEvents(); | |
382 | for (int i = 0; i < size; i++) { | |
383 | ITmfEvent event = trcEvts[i]; | |
384 | ctx.getEvents()[i] = (event != null) ? event.clone() : null; | |
385 | } | |
386 | return ctx; | |
387 | } | |
17324c9a | 388 | |
20658947 | 389 | } |