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