Add support in the UI for LTTng 2.0.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfTrace.java
1 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
2
3 import java.io.FileNotFoundException;
4
5 import org.eclipse.core.resources.IProject;
6 import org.eclipse.core.resources.IResource;
7 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
8 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
9 import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider;
10 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
11 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
12 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
13 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
14 import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
15 import org.eclipse.linuxtools.tmf.core.signal.TmfSignal;
16 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
17 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
18 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
19 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
20
21 public class CtfTmfTrace extends TmfEventProvider<CtfTmfEvent> implements
22 ITmfTrace<CtfTmfEvent> {
23
24 // ------------------------------------------------------------------------
25 // Constants
26 // ------------------------------------------------------------------------
27
28 // ------------------------------------------------------------------------
29 // Attributes
30 // ------------------------------------------------------------------------
31
32 // the Ctf Trace
33 private CTFTrace fTrace;
34
35 // The number of events collected
36 protected long fNbEvents = 0;
37
38 // The time span of the event stream
39 private ITmfTimestamp fStartTime = TmfTimestamp.BIG_CRUNCH;
40 private ITmfTimestamp fEndTime = TmfTimestamp.BIG_BANG;
41
42 // The trace resource
43 private IResource fResource;
44
45 /*
46 * Since in TMF, "traces" can read events, this trace here will have its own
47 * iterator. The user can instantiate extra iterator if they want to seek at
48 * many places at the same time.
49 */
50 protected CtfIterator iterator;
51
52 // ------------------------------------------------------------------------
53 // Constructors
54 // ------------------------------------------------------------------------
55
56 public CtfTmfTrace() {
57 super();
58 }
59
60 @Override
61 public void initTrace(String name, String path, Class<CtfTmfEvent> eventType)
62 throws FileNotFoundException {
63 try {
64 this.fTrace = new CTFTrace(path);
65 } catch (CTFReaderException e) {
66 /*
67 * If it failed at the init(), we can assume it's because the file
68 * was not found or was not recognized as a CTF trace. Throw into
69 * the new type of exception expected by the rest of TMF.
70 */
71 System.err.println("Cannot find file " + path); //$NON-NLS-1$
72 throw new FileNotFoundException(e.getMessage());
73 }
74 this.iterator = new CtfIterator(this, 0, 0);
75 setStartTime(iterator.getCurrentEvent().getTimestamp());
76 TmfSignalManager.register(this);
77 // this.currLocation.setTimestamp(this.fEvent.getTimestamp().getValue());
78 // this.fStartTime = new TmfSimpleTimestamp(this.currLocation
79 // .getLocation().getStartTime());
80 // this.fEndTime = new TmfSimpleTimestamp(this.currLocation
81 // .getLocation().getEndTime());
82 // setTimeRange(new TmfTimeRange(this.fStartTime.clone(),
83 // this.fEndTime.clone()));
84 }
85
86 @Override
87 public void indexTrace(boolean waitForCompletion) {
88 }
89
90 @Override
91 public void dispose() {
92 TmfSignalManager.deregister(this);
93 }
94
95 @Override
96 public void broadcast(TmfSignal signal) {
97 TmfSignalManager.dispatchSignal(signal);
98 }
99
100 @Override
101 public boolean validate(IProject project, String path) {
102 try {
103 final CTFTrace temp = new CTFTrace(path);
104 return temp.majortIsSet(); // random test
105 } catch (CTFReaderException e) {
106 /* Nope, not a CTF trace we can read */
107 return false;
108 }
109 }
110
111 @Override
112 public CtfTmfTrace clone() throws CloneNotSupportedException {
113 CtfTmfTrace clone = null;
114 clone = (CtfTmfTrace) super.clone();
115 clone.fStartTime = this.fStartTime.clone();
116 clone.fEndTime = this.fEndTime.clone();
117 clone.fTrace = this.fTrace;
118 return clone;
119 }
120
121 // ------------------------------------------------------------------------
122 // Accessors
123 // ------------------------------------------------------------------------
124
125 /**
126 * @return the trace path
127 */
128 @Override
129 public String getPath() {
130 return this.fTrace.getPath();
131 }
132
133 @Override
134 public String getName() {
135 String temp[] = this.fTrace.getPath().split(
136 System.getProperty("file.separator")); //$NON-NLS-1$
137 if (temp.length > 2) {
138 return temp[temp.length - 1];
139 }
140 return temp[0];
141 }
142
143 @Override
144 public int getIndexPageSize() {
145 return this.fIndexPageSize;
146 }
147
148 @Override
149 public long getNbEvents() {
150 return this.fNbEvents;
151 }
152
153 @Override
154 public TmfTimeRange getTimeRange() {
155 return new TmfTimeRange(this.fStartTime, this.fEndTime);
156 }
157
158 @Override
159 public ITmfTimestamp getStartTime() {
160 return this.fStartTime;
161 }
162
163 @Override
164 public ITmfTimestamp getEndTime() {
165 return this.fEndTime;
166 }
167
168 @Override
169 public ITmfLocation<?> getCurrentLocation() {
170 return iterator.getLocation();
171 }
172
173 @Override
174 public long getRank(ITmfTimestamp timestamp) {
175 ITmfContext context = seekEvent(timestamp);
176 return context.getRank();
177 }
178
179 // ------------------------------------------------------------------------
180 // Operators
181 // ------------------------------------------------------------------------
182
183 protected void setTimeRange(TmfTimeRange range) {
184 this.fStartTime = range.getStartTime();
185 this.fEndTime = range.getEndTime();
186 }
187
188 protected void setStartTime(ITmfTimestamp startTime) {
189 this.fStartTime = startTime;
190 }
191
192 protected void setEndTime(ITmfTimestamp endTime) {
193 this.fEndTime = endTime;
194 }
195
196 // ------------------------------------------------------------------------
197 // TmfProvider
198 // ------------------------------------------------------------------------
199
200 @Override
201 public ITmfContext armRequest(ITmfDataRequest<CtfTmfEvent> request) {
202 if ((request instanceof ITmfEventRequest<?>)
203 && !TmfTimestamp.BIG_BANG
204 .equals(((ITmfEventRequest<CtfTmfEvent>) request)
205 .getRange().getStartTime())
206 && (request.getIndex() == 0)) {
207 ITmfContext context = seekEvent(((ITmfEventRequest<CtfTmfEvent>) request)
208 .getRange().getStartTime());
209 ((ITmfEventRequest<CtfTmfEvent>) request)
210 .setStartIndex((int) context.getRank());
211 return context;
212 }
213 return seekEvent(request.getIndex());
214 }
215
216 /**
217 * The trace reader keeps its own iterator: the "context" parameter here
218 * will be ignored.
219 *
220 * If you wish to specify a new context, instantiate a new CtfIterator and
221 * seek() it to where you want, and use that to read events.
222 *
223 * FIXME merge with getNextEvent below once they both use the same parameter
224 * type.
225 */
226 @Override
227 public CtfTmfEvent getNext(ITmfContext context) {
228 iterator.advance();
229 return iterator.getCurrentEvent();
230 }
231
232 // ------------------------------------------------------------------------
233 // ITmfTrace
234 // ------------------------------------------------------------------------
235
236 @Override
237 public ITmfContext seekLocation(ITmfLocation<?> location) {
238 if (location == null) {
239 location = new CtfLocation(0L);
240 }
241 iterator.setLocation(location);
242 return iterator;
243 }
244
245 @Override
246 public double getLocationRatio(ITmfLocation<?> location) {
247 CtfIterator curLocation = (CtfIterator) location;
248 return ((double) curLocation.getCurrentEvent().getTimestampValue() - curLocation
249 .getStartTime())
250 / (curLocation.getEndTime() - curLocation.getStartTime());
251 }
252
253 @Override
254 public long getStreamingInterval() {
255 return 0;
256 }
257
258 @Override
259 public ITmfContext seekEvent(ITmfTimestamp timestamp) {
260 iterator.seek(timestamp.getValue());
261 return iterator;
262 }
263
264 /**
265 * Seek by rank
266 */
267 @Override
268 public ITmfContext seekEvent(long rank) {
269 iterator.setRank(rank);
270 return iterator;
271 }
272
273 /**
274 * Seek rank ratio
275 */
276 @Override
277 public ITmfContext seekLocation(double ratio) {
278 iterator.seek((long) (this.fNbEvents * ratio));
279 return iterator;
280 }
281
282 @Override
283 public CtfTmfEvent getNextEvent(ITmfContext context) {
284 iterator.advance();
285 return iterator.getCurrentEvent();
286 }
287
288 @Override
289 public CtfTmfEvent parseEvent(ITmfContext context) {
290 return iterator.getCurrentEvent();
291 }
292
293 @Override
294 public IResource getResource() {
295 return this.fResource;
296 }
297
298 @Override
299 public void setResource(IResource fResource) {
300 this.fResource = fResource;
301 }
302
303 CTFTrace getCTFTrace() {
304 return fTrace;
305 }
306
307 }
This page took 0.040181 seconds and 6 git commands to generate.