909e34aa0a29ebb394ba5371809dbc07e255a5a0
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ctf.core / src / org / eclipse / tracecompass / tmf / ctf / core / CtfIterator.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson, École Polytechnique de Montréal
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * 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 * Matthew Khouzam - Initial API and implementation
11 * Florian Wininger - Performance improvements
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.ctf.core;
15
16 import org.eclipse.tracecompass.ctf.core.trace.CTFReaderException;
17 import org.eclipse.tracecompass.ctf.core.trace.CTFStreamInputReader;
18 import org.eclipse.tracecompass.ctf.core.trace.CTFTraceReader;
19 import org.eclipse.tracecompass.internal.tmf.ctf.core.Activator;
20 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
21 import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
22
23 /**
24 * The CTF trace reader iterator.
25 *
26 * It doesn't reserve a file handle, so many iterators can be used without
27 * worries of I/O errors or resource exhaustion.
28 *
29 * @author Matthew Khouzam
30 */
31 public class CtfIterator extends CTFTraceReader
32 implements ITmfContext, Comparable<CtfIterator> {
33
34 /** An invalid location */
35 public static final CtfLocation NULL_LOCATION = new CtfLocation(CtfLocation.INVALID_LOCATION);
36
37 private final CtfTmfTrace fTrace;
38
39 private CtfLocation fCurLocation;
40 private long fCurRank;
41
42 private CtfLocation fPreviousLocation;
43 private CtfTmfEvent fPreviousEvent;
44
45 // ------------------------------------------------------------------------
46 // Constructors
47 // ------------------------------------------------------------------------
48
49 /**
50 * Create a new CTF trace iterator, which initially points at the first
51 * event in the trace.
52 *
53 * @param trace
54 * The trace to iterate over
55 * @throws CTFReaderException
56 * If the iterator couldn't not be instantiated, probably due to
57 * a read error.
58 */
59 public CtfIterator(CtfTmfTrace trace) throws CTFReaderException {
60 super(trace.getCTFTrace());
61 fTrace = trace;
62 if (hasMoreEvents()) {
63 fCurLocation = new CtfLocation(trace.getStartTime());
64 fCurRank = 0;
65 } else {
66 setUnknownLocation();
67 }
68 }
69
70 /**
71 * Create a new CTF trace iterator, which will initially point to the given
72 * location/rank.
73 *
74 * @param trace
75 * The trace to iterate over
76 * @param ctfLocationData
77 * The initial timestamp the iterator will be pointing to
78 * @param rank
79 * The initial rank
80 * @throws CTFReaderException
81 * If the iterator couldn't not be instantiated, probably due to
82 * a read error.
83 * @since 2.0
84 */
85 public CtfIterator(CtfTmfTrace trace, CtfLocationInfo ctfLocationData, long rank)
86 throws CTFReaderException {
87 super(trace.getCTFTrace());
88
89 this.fTrace = trace;
90 if (this.hasMoreEvents()) {
91 this.fCurLocation = new CtfLocation(ctfLocationData);
92 if (this.getCurrentEvent().getTimestamp().getValue() != ctfLocationData.getTimestamp()) {
93 this.seek(ctfLocationData);
94 this.fCurRank = rank;
95 }
96 } else {
97 setUnknownLocation();
98 }
99 }
100
101 @Override
102 public void dispose() {
103 close();
104 }
105
106 private void setUnknownLocation() {
107 fCurLocation = NULL_LOCATION;
108 fCurRank = UNKNOWN_RANK;
109 }
110
111 // ------------------------------------------------------------------------
112 // Accessors
113 // ------------------------------------------------------------------------
114
115 /**
116 * Return this iterator's trace.
117 *
118 * @return CtfTmfTrace The iterator's trace
119 */
120 public CtfTmfTrace getCtfTmfTrace() {
121 return fTrace;
122 }
123
124 /**
125 * Return the current event pointed to by the iterator.
126 *
127 * @return CtfTmfEvent The current event
128 */
129 public synchronized CtfTmfEvent getCurrentEvent() {
130 final CTFStreamInputReader top = super.getPrio().peek();
131 if (top != null) {
132 if (!fCurLocation.equals(fPreviousLocation)) {
133 fPreviousLocation = fCurLocation;
134 fPreviousEvent = CtfTmfEventFactory.createEvent(top.getCurrentEvent(),
135 top.getFilename(), fTrace);
136 }
137 return fPreviousEvent;
138 }
139 return null;
140 }
141
142 /**
143 * Return the current timestamp location pointed to by the iterator.
144 * This is the timestamp for use in CtfLocation, not the event timestamp.
145 *
146 * @return long The current timestamp location
147 */
148 public synchronized long getCurrentTimestamp() {
149 final CTFStreamInputReader top = super.getPrio().peek();
150 if (top != null) {
151 long ts = top.getCurrentEvent().getTimestamp();
152 return fTrace.getCTFTrace().timestampCyclesToNanos(ts);
153 }
154 return 0;
155 }
156
157 /**
158 * Seek this iterator to a given location.
159 *
160 * @param ctfLocationData
161 * The LocationData representing the position to seek to
162 * @return boolean True if the seek was successful, false if there was an
163 * error seeking.
164 * @since 2.0
165 */
166 public synchronized boolean seek(CtfLocationInfo ctfLocationData) {
167 boolean ret = false;
168
169 /* Avoid the cost of seeking at the current location. */
170 if (fCurLocation.getLocationInfo().equals(ctfLocationData)) {
171 return super.hasMoreEvents();
172 }
173
174 /* Adjust the timestamp depending on the trace's offset */
175 long currTimestamp = ctfLocationData.getTimestamp();
176 final long offsetTimestamp = this.getCtfTmfTrace().getCTFTrace().timestampNanoToCycles(currTimestamp);
177 try {
178 if (offsetTimestamp < 0) {
179 ret = super.seek(0L);
180 } else {
181 ret = super.seek(offsetTimestamp);
182 }
183 } catch (CTFReaderException e) {
184 Activator.getDefault().logError(e.getMessage(), e);
185 return false;
186 }
187 /*
188 * Check if there is already one or more events for that timestamp, and
189 * assign the location index correctly
190 */
191 long index = 0;
192 final CtfTmfEvent currentEvent = this.getCurrentEvent();
193 if (currentEvent != null) {
194 currTimestamp = currentEvent.getTimestamp().getValue();
195
196 for (long i = 0; i < ctfLocationData.getIndex(); i++) {
197 if (currTimestamp == currentEvent.getTimestamp().getValue()) {
198 index++;
199 } else {
200 index = 0;
201 }
202 this.advance();
203 }
204 } else {
205 ret = false;
206 }
207 /* Seek the current location accordingly */
208 if (ret) {
209 fCurLocation = new CtfLocation(new CtfLocationInfo(getCurrentEvent().getTimestamp().getValue(), index));
210 } else {
211 fCurLocation = NULL_LOCATION;
212 }
213
214 return ret;
215 }
216
217 // ------------------------------------------------------------------------
218 // CTFTraceReader
219 // ------------------------------------------------------------------------
220
221 @Override
222 public boolean seek(long timestamp) {
223 return seek(new CtfLocationInfo(timestamp, 0));
224 }
225
226 @Override
227 public synchronized boolean advance() {
228 boolean ret = false;
229 try {
230 ret = super.advance();
231 } catch (CTFReaderException e) {
232 Activator.getDefault().logError(e.getMessage(), e);
233 }
234
235 if (ret) {
236 long timestamp = fCurLocation.getLocationInfo().getTimestamp();
237 final long timestampValue = getCurrentTimestamp();
238 if (timestamp == timestampValue) {
239 long index = fCurLocation.getLocationInfo().getIndex();
240 fCurLocation = new CtfLocation(timestampValue, index + 1);
241 } else {
242 fCurLocation = new CtfLocation(timestampValue, 0L);
243 }
244 } else {
245 fCurLocation = NULL_LOCATION;
246 }
247 return ret;
248 }
249
250 // ------------------------------------------------------------------------
251 // ITmfContext
252 // ------------------------------------------------------------------------
253
254 @Override
255 public long getRank() {
256 return fCurRank;
257 }
258
259 @Override
260 public void setRank(long rank) {
261 fCurRank = rank;
262 }
263
264 @Override
265 public void increaseRank() {
266 /* Only increase the rank if it's valid */
267 if (hasValidRank()) {
268 fCurRank++;
269 }
270 }
271
272 @Override
273 public boolean hasValidRank() {
274 return (getRank() >= 0);
275 }
276
277 /**
278 * @since 3.0
279 */
280 @Override
281 public void setLocation(ITmfLocation location) {
282 // FIXME alex: isn't there a cleaner way than a cast here?
283 fCurLocation = (CtfLocation) location;
284 seek(((CtfLocation) location).getLocationInfo());
285 }
286
287 @Override
288 public CtfLocation getLocation() {
289 return fCurLocation;
290 }
291
292 // ------------------------------------------------------------------------
293 // Comparable
294 // ------------------------------------------------------------------------
295
296 @Override
297 public int compareTo(final CtfIterator o) {
298 if (getRank() < o.getRank()) {
299 return -1;
300 } else if (getRank() > o.getRank()) {
301 return 1;
302 }
303 return 0;
304 }
305
306 // ------------------------------------------------------------------------
307 // Object
308 // ------------------------------------------------------------------------
309
310 @Override
311 public int hashCode() {
312 final int prime = 31;
313 int result = super.hashCode();
314 result = (prime * result)
315 + ((fTrace == null) ? 0 : fTrace.hashCode());
316 result = (prime * result)
317 + ((fCurLocation == null) ? 0 : fCurLocation.hashCode());
318 result = (prime * result) + (int) (fCurRank ^ (fCurRank >>> 32));
319 return result;
320 }
321
322 @Override
323 public boolean equals(Object obj) {
324 if (this == obj) {
325 return true;
326 }
327 if (!super.equals(obj)) {
328 return false;
329 }
330 if (!(obj instanceof CtfIterator)) {
331 return false;
332 }
333 CtfIterator other = (CtfIterator) obj;
334 if (fTrace == null) {
335 if (other.fTrace != null) {
336 return false;
337 }
338 } else if (!fTrace.equals(other.fTrace)) {
339 return false;
340 }
341 if (fCurLocation == null) {
342 if (other.fCurLocation != null) {
343 return false;
344 }
345 } else if (!fCurLocation.equals(other.fCurLocation)) {
346 return false;
347 }
348 if (fCurRank != other.fCurRank) {
349 return false;
350 }
351 return true;
352 }
353 }
This page took 0.05129 seconds and 4 git commands to generate.