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