Commit | Line | Data |
---|---|---|
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 | ||
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; | |
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; | |
20658947 FC |
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 | /** | |
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 | 51 | public 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 FC |
62 | |
63 | /** | |
64 | * The trace index. It is composed of checkpoints taken at intervals of | |
65 | * fCheckpointInterval events. | |
66 | */ | |
0316808c | 67 | private final List<TmfCheckpoint> fTraceIndex; |
20658947 FC |
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 | */ | |
7e6347b0 | 79 | public TmfCheckpointIndexer(final ITmfTrace<ITmfEvent> trace) { |
20658947 FC |
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 | */ | |
7e6347b0 | 89 | public TmfCheckpointIndexer(final ITmfTrace<ITmfEvent> trace, final int interval) { |
20658947 FC |
90 | fTrace = trace; |
91 | fCheckpointInterval = interval; | |
0316808c | 92 | fTraceIndex = new ArrayList<TmfCheckpoint>(); |
20658947 FC |
93 | } |
94 | ||
95 | // ------------------------------------------------------------------------ | |
1703b536 | 96 | // ITmfTraceIndexer - buildIndex |
20658947 FC |
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 | ||
90891c08 FC |
128 | System.out.println("TmfCheckpointIndexer.buildIndex()");; |
129 | ||
20658947 FC |
130 | // Clear the checkpoints |
131 | fTraceIndex.clear(); | |
132 | ||
133 | // Build a background request for all the trace data. The index is | |
07671572 | 134 | // updated as we go by readNextEvent(). |
20658947 FC |
135 | final ITmfEventRequest<ITmfEvent> request = new TmfEventRequest<ITmfEvent>(ITmfEvent.class, TmfTimeRange.ETERNITY, |
136 | TmfDataRequest.ALL_DATA, fCheckpointInterval, ITmfDataRequest.ExecutionType.BACKGROUND) | |
d337369a | 137 | { |
0316808c FC |
138 | private ITmfTimestamp startTime = null; |
139 | private ITmfTimestamp lastTime = null; | |
20658947 FC |
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) { | |
7e6347b0 | 171 | signalNewTimeRange(startTime, lastTime); |
20658947 FC |
172 | } |
173 | } | |
d337369a | 174 | }; |
20658947 | 175 | |
d337369a FC |
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 | } | |
20658947 FC |
184 | } |
185 | ||
7e6347b0 FC |
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) { | |
1703b536 FC |
193 | fTrace.broadcast(new TmfTraceUpdatedSignal(fTrace, fTrace, new TmfTimeRange(startTime, endTime))); |
194 | } | |
195 | ||
196 | // ------------------------------------------------------------------------ | |
197 | // ITmfTraceIndexer - updateIndex | |
198 | // ------------------------------------------------------------------------ | |
199 | ||
d337369a FC |
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 | */ | |
1703b536 | 203 | @Override |
d337369a FC |
204 | public synchronized void updateIndex(final ITmfContext context, final ITmfTimestamp timestamp) { |
205 | final long rank = context.getRank(); | |
1703b536 FC |
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)); | |
90891c08 | 213 | System.out.println("TmfCheckpointIndexer.updateIndex()" + "[" + (fTraceIndex.size() - 1) + "] " + timestamp + ", " + location.toString()); |
1703b536 FC |
214 | } |
215 | } | |
216 | } | |
217 | ||
218 | // ------------------------------------------------------------------------ | |
219 | // ITmfTraceIndexer - seekIndex | |
220 | // ------------------------------------------------------------------------ | |
20658947 FC |
221 | |
222 | /* (non-Javadoc) | |
223 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp) | |
224 | */ | |
225 | @Override | |
1703b536 | 226 | public synchronized ITmfContext seekIndex(final ITmfTimestamp timestamp) { |
20658947 | 227 | |
1703b536 | 228 | // A null timestamp indicates to seek the first event |
0316808c | 229 | if (timestamp == null) { |
7e6347b0 | 230 | return fTrace.seekEvent(0); |
0316808c | 231 | } |
20658947 | 232 | |
1703b536 FC |
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)); | |
20658947 FC |
238 | if (index < 0) { |
239 | index = Math.max(0, -(index + 2)); | |
240 | } | |
241 | ||
242 | // Position the trace at the checkpoint | |
1703b536 | 243 | return seekCheckpoint(index); |
20658947 FC |
244 | } |
245 | ||
1703b536 FC |
246 | /* (non-Javadoc) |
247 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(long) | |
248 | */ | |
20658947 FC |
249 | @Override |
250 | public ITmfContext seekIndex(final long rank) { | |
251 | ||
f6ad2e3d FC |
252 | // A rank < 0 indicates to seek the first event |
253 | if (rank < 0) { | |
254 | return fTrace.seekEvent(0); | |
255 | } | |
1703b536 | 256 | |
f6ad2e3d FC |
257 | // Find the checkpoint at or before the requested rank. |
258 | final int index = (int) rank / fCheckpointInterval; | |
1703b536 | 259 | |
f6ad2e3d FC |
260 | // Position the trace at the checkpoint |
261 | return seekCheckpoint(index); | |
1703b536 FC |
262 | } |
263 | ||
264 | /** | |
265 | * Position the trace at the given checkpoint | |
266 | * | |
0316808c | 267 | * @param checkpoint the checkpoint index |
1703b536 FC |
268 | * @return the corresponding context |
269 | */ | |
0316808c | 270 | private ITmfContext seekCheckpoint(final int checkpoint) { |
07671572 | 271 | ITmfLocation<?> location = null; |
0316808c | 272 | int index = checkpoint; |
20658947 | 273 | synchronized (fTraceIndex) { |
1703b536 | 274 | if (!fTraceIndex.isEmpty()) { |
20658947 FC |
275 | if (index >= fTraceIndex.size()) { |
276 | index = fTraceIndex.size() - 1; | |
277 | } | |
0316808c | 278 | location = fTraceIndex.get(index).getLocation(); |
20658947 FC |
279 | } |
280 | } | |
7e6347b0 | 281 | final ITmfContext context = fTrace.seekEvent(location); |
afc86f78 | 282 | context.setRank((long) index * fCheckpointInterval); |
20658947 FC |
283 | return context; |
284 | } | |
285 | ||
0316808c FC |
286 | // ------------------------------------------------------------------------ |
287 | // Getters | |
288 | // ------------------------------------------------------------------------ | |
289 | ||
290 | /** | |
291 | * @return the trace index | |
292 | */ | |
293 | protected List<TmfCheckpoint> getTraceIndex() { | |
294 | return fTraceIndex; | |
295 | } | |
20658947 | 296 | } |