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