Commit | Line | Data |
---|---|---|
b1baa808 | 1 | /******************************************************************************* |
61759503 | 2 | * Copyright (c) 2012, 2013 Ericsson |
b1baa808 MK |
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: Matthew Khouzam - Initial API and implementation | |
10 | *******************************************************************************/ | |
a3fc8213 AM |
11 | package org.eclipse.linuxtools.tmf.core.ctfadaptor; |
12 | ||
13 | import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader; | |
14 | import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader; | |
15 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; | |
a3db8436 | 16 | import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation; |
a3fc8213 | 17 | |
b1baa808 | 18 | /** |
d09f973b | 19 | * The CTF trace reader iterator. |
3eaea7e6 | 20 | * |
d09f973b FC |
21 | * It doesn't reserve a file handle, so many iterators can be used without worries |
22 | * of I/O errors or resource exhaustion. | |
3eaea7e6 | 23 | * |
d09f973b FC |
24 | * @version 1.0 |
25 | * @author Matthew Khouzam | |
b1baa808 | 26 | */ |
132a02b0 | 27 | public class CtfIterator extends CTFTraceReader implements ITmfContext, |
81a2d02e | 28 | Comparable<CtfIterator> { |
a3fc8213 AM |
29 | |
30 | private final CtfTmfTrace ctfTmfTrace; | |
31 | ||
9ac2eb62 MK |
32 | /** |
33 | * An invalid location | |
34 | */ | |
132a02b0 MK |
35 | final public static CtfLocation NULL_LOCATION = new CtfLocation(CtfLocation.INVALID_LOCATION); |
36 | ||
a3fc8213 AM |
37 | private CtfLocation curLocation; |
38 | private long curRank; | |
39 | ||
40 | /** | |
41 | * Create a new CTF trace iterator, which initially points at the first | |
42 | * event in the trace. | |
43 | * | |
132a02b0 MK |
44 | * @param trace |
45 | * the trace to iterate over | |
a3fc8213 | 46 | */ |
ce2388e0 | 47 | public CtfIterator(final CtfTmfTrace trace) { |
a3fc8213 AM |
48 | super(trace.getCTFTrace()); |
49 | this.ctfTmfTrace = trace; | |
57c073c5 | 50 | if (this.hasMoreEvents()) { |
57c073c5 MK |
51 | this.curLocation = new CtfLocation(trace.getStartTime()); |
52 | this.curRank = 0; | |
53 | } else { | |
54 | setUnknownLocation(); | |
55 | } | |
a3fc8213 AM |
56 | } |
57 | ||
57c073c5 | 58 | private void setUnknownLocation() { |
f474d36b | 59 | this.curLocation = NULL_LOCATION; |
57c073c5 MK |
60 | this.curRank = UNKNOWN_RANK; |
61 | } | |
62 | ||
b1baa808 MK |
63 | /** |
64 | * Constructor for CtfIterator. | |
132a02b0 MK |
65 | * |
66 | * @param trace | |
67 | * CtfTmfTrace the trace | |
68 | * @param ctfLocationData | |
69 | * long the timestamp in ns of the trace for positioning | |
70 | * @param rank | |
71 | * long the index of the trace for positioning | |
72 | * @since 2.0 | |
b1baa808 | 73 | */ |
132a02b0 | 74 | public CtfIterator(final CtfTmfTrace trace, |
f5df94f8 | 75 | final CtfLocationInfo ctfLocationData, final long rank) { |
a3fc8213 | 76 | super(trace.getCTFTrace()); |
57c073c5 | 77 | |
a3fc8213 | 78 | this.ctfTmfTrace = trace; |
57c073c5 | 79 | if (this.hasMoreEvents()) { |
132a02b0 | 80 | this.curLocation = new CtfLocation(ctfLocationData); |
58f3bc52 | 81 | if (this.getCurrentEvent().getTimestamp().getValue() != ctfLocationData.getTimestamp()) { |
132a02b0 | 82 | this.seek(ctfLocationData); |
57c073c5 MK |
83 | this.curRank = rank; |
84 | } | |
85 | } else { | |
86 | setUnknownLocation(); | |
87 | } | |
a3fc8213 | 88 | |
a3fc8213 AM |
89 | } |
90 | ||
b1baa808 MK |
91 | /** |
92 | * Method getCtfTmfTrace. gets a CtfTmfTrace | |
93 | * @return CtfTmfTrace | |
94 | */ | |
a3fc8213 AM |
95 | public CtfTmfTrace getCtfTmfTrace() { |
96 | return ctfTmfTrace; | |
97 | } | |
98 | ||
b1baa808 MK |
99 | /** |
100 | * Method getCurrentEvent. gets the current event | |
101 | * @return CtfTmfEvent | |
102 | */ | |
a3fc8213 | 103 | public CtfTmfEvent getCurrentEvent() { |
0594c61c | 104 | final StreamInputReader top = super.getPrio().peek(); |
57c073c5 | 105 | if (top != null) { |
6cfa0200 AM |
106 | return CtfTmfEventFactory.createEvent(top.getCurrentEvent(), |
107 | top.getFilename(), ctfTmfTrace); | |
57c073c5 | 108 | } |
a3fc8213 AM |
109 | return null; |
110 | } | |
111 | ||
23974d8a PT |
112 | @Override |
113 | public boolean seek(long timestamp) { | |
f5df94f8 | 114 | return seek(new CtfLocationInfo(timestamp, 0)); |
23974d8a PT |
115 | } |
116 | ||
b1baa808 | 117 | /** |
132a02b0 MK |
118 | * Seek this iterator to a given location. |
119 | * | |
120 | * @param ctfLocationData | |
121 | * The LocationData representing the position to seek to | |
b1baa808 | 122 | * @return boolean |
132a02b0 | 123 | * @since 2.0 |
b1baa808 | 124 | */ |
b6220b93 | 125 | public synchronized boolean seek(final CtfLocationInfo ctfLocationData) { |
a3fc8213 | 126 | boolean ret = false; |
132a02b0 MK |
127 | |
128 | /* Adjust the timestamp depending on the trace's offset */ | |
129 | long currTimestamp = ctfLocationData.getTimestamp(); | |
77ef700d | 130 | final long offsetTimestamp = this.getCtfTmfTrace().getCTFTrace().timestampNanoToCycles(currTimestamp); |
57c073c5 | 131 | if (offsetTimestamp < 0) { |
3eaea7e6 | 132 | ret = super.seek(0L); |
57c073c5 | 133 | } else { |
ce2388e0 | 134 | ret = super.seek(offsetTimestamp); |
57c073c5 | 135 | } |
a3fc8213 | 136 | |
132a02b0 MK |
137 | /* |
138 | * Check if there is already one or more events for that timestamp, and | |
139 | * assign the location index correctly | |
140 | */ | |
132a02b0 | 141 | long index = 0; |
b6220b93 MK |
142 | final CtfTmfEvent currentEvent = this.getCurrentEvent(); |
143 | if (currentEvent != null) { | |
144 | currTimestamp = currentEvent.getTimestamp().getValue(); | |
77ef700d MK |
145 | |
146 | for (long i = 0; i < ctfLocationData.getIndex(); i++) { | |
b6220b93 | 147 | if (currTimestamp == currentEvent.getTimestamp().getValue()) { |
77ef700d MK |
148 | index++; |
149 | } else { | |
150 | index = 0; | |
151 | } | |
152 | this.advance(); | |
132a02b0 | 153 | } |
77ef700d MK |
154 | } else { |
155 | ret= false; | |
132a02b0 | 156 | } |
132a02b0 | 157 | /* Seek the current location accordingly */ |
57c073c5 | 158 | if (ret) { |
f5df94f8 | 159 | curLocation = new CtfLocation(new CtfLocationInfo(getCurrentEvent().getTimestamp().getValue(), index)); |
f474d36b PT |
160 | } else { |
161 | curLocation = NULL_LOCATION; | |
57c073c5 | 162 | } |
ce2388e0 FC |
163 | return ret; |
164 | } | |
165 | ||
b1baa808 MK |
166 | /** |
167 | * Method getRank. | |
168 | * @return long | |
169 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getRank() | |
170 | */ | |
a3fc8213 AM |
171 | @Override |
172 | public long getRank() { | |
f474d36b | 173 | return curRank; |
a3fc8213 AM |
174 | } |
175 | ||
b1baa808 MK |
176 | /** |
177 | * Method setRank. | |
178 | * @param rank long | |
179 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#setRank(long) | |
180 | */ | |
a3fc8213 | 181 | @Override |
ce2388e0 | 182 | public void setRank(final long rank) { |
f474d36b | 183 | curRank = rank; |
a3fc8213 AM |
184 | } |
185 | ||
a3fc8213 AM |
186 | @Override |
187 | public CtfIterator clone() { | |
188 | CtfIterator clone = null; | |
5976d44a | 189 | clone = new CtfIterator(ctfTmfTrace, this.getLocation().getLocationInfo(), curRank); |
a3fc8213 AM |
190 | return clone; |
191 | } | |
192 | ||
b1baa808 MK |
193 | /** |
194 | * Method dispose. | |
195 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#dispose() | |
196 | */ | |
a3fc8213 AM |
197 | @Override |
198 | public void dispose() { | |
5d1c6919 | 199 | super.dispose(); |
a3fc8213 AM |
200 | } |
201 | ||
b1baa808 MK |
202 | /** |
203 | * Method setLocation. | |
204 | * @param location ITmfLocation<?> | |
a3db8436 | 205 | * @since 3.0 |
b1baa808 | 206 | */ |
a3fc8213 | 207 | @Override |
1e1bef82 | 208 | public void setLocation(final ITmfLocation location) { |
a3fc8213 AM |
209 | // FIXME alex: isn't there a cleaner way than a cast here? |
210 | this.curLocation = (CtfLocation) location; | |
5976d44a | 211 | seek(((CtfLocation) location).getLocationInfo()); |
a3fc8213 AM |
212 | } |
213 | ||
b1baa808 MK |
214 | /** |
215 | * Method getLocation. | |
216 | * @return CtfLocation | |
217 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getLocation() | |
218 | */ | |
a3fc8213 AM |
219 | @Override |
220 | public CtfLocation getLocation() { | |
221 | return curLocation; | |
222 | } | |
223 | ||
b1baa808 MK |
224 | /** |
225 | * Method increaseRank. | |
226 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#increaseRank() | |
227 | */ | |
a3fc8213 | 228 | @Override |
cbdacf03 | 229 | public void increaseRank() { |
4a110860 AM |
230 | /* Only increase the rank if it's valid */ |
231 | if(hasValidRank()) { | |
232 | curRank++; | |
233 | } | |
a3fc8213 AM |
234 | } |
235 | ||
b1baa808 MK |
236 | /** |
237 | * Method hasValidRank, if the iterator is valid | |
238 | * @return boolean | |
239 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#hasValidRank() | |
240 | */ | |
a3fc8213 | 241 | @Override |
cbdacf03 | 242 | public boolean hasValidRank() { |
bcbea6a6 | 243 | return (getRank() >= 0); |
a3fc8213 AM |
244 | } |
245 | ||
b1baa808 MK |
246 | /** |
247 | * Method advance go to the next event | |
248 | * @return boolean successful or not | |
249 | */ | |
a3fc8213 | 250 | @Override |
b6220b93 | 251 | public synchronized boolean advance() { |
5976d44a FC |
252 | long index = curLocation.getLocationInfo().getIndex(); |
253 | long timestamp = curLocation.getLocationInfo().getTimestamp(); | |
f474d36b | 254 | boolean ret = super.advance(); |
132a02b0 | 255 | |
f474d36b | 256 | if (ret) { |
58f3bc52 | 257 | final long timestampValue = getCurrentEvent().getTimestamp().getValue(); |
132a02b0 | 258 | if (timestamp == timestampValue) { |
d62bb185 | 259 | curLocation = new CtfLocation(timestampValue, index + 1); |
132a02b0 | 260 | } else { |
d62bb185 | 261 | curLocation = new CtfLocation(timestampValue, 0L); |
132a02b0 | 262 | } |
f474d36b PT |
263 | } else { |
264 | curLocation = NULL_LOCATION; | |
265 | } | |
266 | return ret; | |
a3fc8213 AM |
267 | } |
268 | ||
b1baa808 MK |
269 | /** |
270 | * Method compareTo. | |
271 | * @param o CtfIterator | |
272 | * @return int -1, 0, 1 | |
273 | */ | |
a3fc8213 | 274 | @Override |
ce2388e0 | 275 | public int compareTo(final CtfIterator o) { |
57c073c5 | 276 | if (this.getRank() < o.getRank()) { |
a3fc8213 | 277 | return -1; |
57c073c5 | 278 | } else if (this.getRank() > o.getRank()) { |
a3fc8213 | 279 | return 1; |
57c073c5 | 280 | } |
a3fc8213 AM |
281 | return 0; |
282 | } | |
788ddcbc | 283 | |
b1baa808 MK |
284 | @Override |
285 | public int hashCode() { | |
286 | final int prime = 31; | |
287 | int result = super.hashCode(); | |
288 | result = (prime * result) | |
289 | + ((ctfTmfTrace == null) ? 0 : ctfTmfTrace.hashCode()); | |
290 | result = (prime * result) | |
291 | + ((curLocation == null) ? 0 : curLocation.hashCode()); | |
292 | result = (prime * result) + (int) (curRank ^ (curRank >>> 32)); | |
293 | return result; | |
294 | } | |
a3fc8213 | 295 | |
b1baa808 MK |
296 | @Override |
297 | public boolean equals(Object obj) { | |
298 | if (this == obj) { | |
299 | return true; | |
300 | } | |
301 | if (!super.equals(obj)) { | |
302 | return false; | |
303 | } | |
304 | if (!(obj instanceof CtfIterator)) { | |
305 | return false; | |
306 | } | |
307 | CtfIterator other = (CtfIterator) obj; | |
308 | if (ctfTmfTrace == null) { | |
309 | if (other.ctfTmfTrace != null) { | |
310 | return false; | |
311 | } | |
312 | } else if (!ctfTmfTrace.equals(other.ctfTmfTrace)) { | |
313 | return false; | |
314 | } | |
315 | if (curLocation == null) { | |
316 | if (other.curLocation != null) { | |
317 | return false; | |
318 | } | |
319 | } else if (!curLocation.equals(other.curLocation)) { | |
320 | return false; | |
321 | } | |
322 | if (curRank != other.curRank) { | |
323 | return false; | |
324 | } | |
325 | return true; | |
326 | } | |
ce2388e0 | 327 | } |