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