Merge master in TmfTraceModel
[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 @SuppressWarnings("unused")
61 @Override
62 public void initTrace(String name, String path, Class<CtfTmfEvent> eventType)
63 throws FileNotFoundException {
64 try {
65 this.fTrace = new CTFTrace(path);
66 } catch (CTFReaderException e) {
67 /*
68 * If it failed at the init(), we can assume it's because the file
69 * was not found or was not recognized as a CTF trace. Throw into
70 * the new type of exception expected by the rest of TMF.
71 */
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 @SuppressWarnings("unused")
87 @Override
88 public void indexTrace(boolean waitForCompletion) {
89 // do nothing
90 }
91
92 @Override
93 public void dispose() {
94 TmfSignalManager.deregister(this);
95 }
96
97 @Override
98 public void broadcast(TmfSignal signal) {
99 TmfSignalManager.dispatchSignal(signal);
100 }
101
102 @SuppressWarnings("unused")
103 @Override
104 public boolean validate(IProject project, String path) {
105 try {
106 final CTFTrace temp = new CTFTrace(path);
107 return temp.majortIsSet(); // random test
108 } catch (CTFReaderException e) {
109 /* Nope, not a CTF trace we can read */
110 return false;
111 }
112 }
113
114 @Override
115 public CtfTmfTrace clone() throws CloneNotSupportedException {
116 CtfTmfTrace clone = null;
117 clone = (CtfTmfTrace) super.clone();
118 clone.fStartTime = this.fStartTime.clone();
119 clone.fEndTime = this.fEndTime.clone();
120 clone.fTrace = this.fTrace;
121 return clone;
122 }
123
124 // ------------------------------------------------------------------------
125 // Accessors
126 // ------------------------------------------------------------------------
127
128 public int getNbEnvVars() {
129 return this.fTrace.getEnvironment().size();
130 }
131
132
133 public String[] getEnvNames() {
134 String[] s = new String[getNbEnvVars()];
135 return this.fTrace.getEnvironment().keySet().toArray(s);
136 }
137
138 public String getEnvValue(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 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 }
158 return temp[0];
159 }
160
161 @Override
162 public int getIndexPageSize() {
163 return 50000; // not true, but it works
164 }
165
166 @Override
167 public long getNbEvents() {
168 return this.fNbEvents;
169 }
170
171 @Override
172 public TmfTimeRange getTimeRange() {
173 return new TmfTimeRange(this.fStartTime, this.fEndTime);
174 }
175
176 @Override
177 public ITmfTimestamp getStartTime() {
178 return this.fStartTime;
179 }
180
181 @Override
182 public ITmfTimestamp getEndTime() {
183 return this.fEndTime;
184 }
185
186 @Override
187 public ITmfLocation<?> getCurrentLocation() {
188 return iterator.getLocation();
189 }
190
191 @Override
192 public long getRank(ITmfTimestamp timestamp) {
193 ITmfContext context = seekEvent(timestamp);
194 return context.getRank();
195 }
196
197 // ------------------------------------------------------------------------
198 // Operators
199 // ------------------------------------------------------------------------
200
201 protected void setTimeRange(TmfTimeRange range) {
202 this.fStartTime = range.getStartTime();
203 this.fEndTime = range.getEndTime();
204 }
205
206 protected void setStartTime(ITmfTimestamp startTime) {
207 this.fStartTime = startTime;
208 }
209
210 protected void setEndTime(ITmfTimestamp endTime) {
211 this.fEndTime = endTime;
212 }
213
214 // ------------------------------------------------------------------------
215 // TmfProvider
216 // ------------------------------------------------------------------------
217
218 @Override
219 public ITmfContext armRequest(ITmfDataRequest<CtfTmfEvent> request) {
220 if ((request instanceof ITmfEventRequest<?>)
221 && !TmfTimestamp.BIG_BANG
222 .equals(((ITmfEventRequest<CtfTmfEvent>) request)
223 .getRange().getStartTime())
224 && (request.getIndex() == 0)) {
225 ITmfContext context = seekEvent(((ITmfEventRequest<CtfTmfEvent>) request)
226 .getRange().getStartTime());
227 ((ITmfEventRequest<CtfTmfEvent>) request)
228 .setStartIndex((int) context.getRank());
229 return context;
230 }
231 return seekEvent(request.getIndex());
232 }
233
234 /**
235 * The trace reader keeps its own iterator: the "context" parameter here
236 * will be ignored.
237 *
238 * If you wish to specify a new context, instantiate a new CtfIterator and
239 * seek() it to where you want, and use that to read events.
240 *
241 * FIXME merge with getNextEvent below once they both use the same parameter
242 * type.
243 */
244 @SuppressWarnings("unused")
245 @Override
246 public CtfTmfEvent getNext(ITmfContext context) {
247 iterator.advance();
248 return iterator.getCurrentEvent();
249 }
250
251 // ------------------------------------------------------------------------
252 // ITmfTrace
253 // ------------------------------------------------------------------------
254
255 @Override
256 public ITmfContext seekLocation(ITmfLocation<?> location) {
257 CtfLocation currentLocation = (CtfLocation) location;
258 if (currentLocation == null) {
259 currentLocation = new CtfLocation(0L);
260 }
261 iterator.setLocation(currentLocation);
262 return iterator;
263 }
264
265 @Override
266 public double getLocationRatio(ITmfLocation<?> location) {
267 CtfLocation curLocation = (CtfLocation) location;
268 iterator.seek(curLocation.getLocation());
269 return ((double) iterator.getCurrentEvent().getTimestampValue() - iterator
270 .getStartTime())
271 / (iterator.getEndTime() - iterator.getStartTime());
272 }
273
274 @Override
275 public long getStreamingInterval() {
276 return 0;
277 }
278
279 @Override
280 public ITmfContext seekEvent(ITmfTimestamp timestamp) {
281 iterator.seek(timestamp.getValue());
282 return iterator;
283 }
284
285 /**
286 * Seek by rank
287 */
288 @Override
289 public ITmfContext seekEvent(long rank) {
290 iterator.setRank(rank);
291 return iterator;
292 }
293
294 /**
295 * Seek rank ratio
296 */
297 @Override
298 public ITmfContext seekLocation(double ratio) {
299 iterator.seek((long) (this.fNbEvents * ratio));
300 return iterator;
301 }
302
303 @SuppressWarnings("unused")
304 @Override
305 public CtfTmfEvent getNextEvent(ITmfContext context) {
306 iterator.advance();
307 return iterator.getCurrentEvent();
308 }
309
310 @SuppressWarnings("unused")
311 @Override
312 public CtfTmfEvent parseEvent(ITmfContext context) {
313 return iterator.getCurrentEvent();
314 }
315
316 @Override
317 public IResource getResource() {
318 return this.fResource;
319 }
320
321 @Override
322 public void setResource(IResource fResource) {
323 this.fResource = fResource;
324 }
325
326 CTFTrace getCTFTrace() {
327 return fTrace;
328 }
329
330
331
332 }
This page took 0.092362 seconds and 5 git commands to generate.