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