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