Commit | Line | Data |
---|---|---|
a3fc8213 AM |
1 | package org.eclipse.linuxtools.tmf.core.ctfadaptor; |
2 | ||
3 | import java.io.FileNotFoundException; | |
a3fc8213 AM |
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; | |
a3fc8213 AM |
20 | |
21 | public class CtfTmfTrace extends TmfEventProvider<CtfTmfEvent> implements | |
22 | ITmfTrace<CtfTmfEvent> { | |
23 | ||
24 | // ------------------------------------------------------------------------ | |
25 | // Constants | |
26 | // ------------------------------------------------------------------------ | |
27 | ||
a3fc8213 AM |
28 | // ------------------------------------------------------------------------ |
29 | // Attributes | |
30 | // ------------------------------------------------------------------------ | |
31 | ||
32 | // the Ctf Trace | |
33 | private CTFTrace fTrace; | |
34 | ||
a3fc8213 AM |
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 | |
3118edf1 | 61 | public void initTrace(String name, String path, Class<CtfTmfEvent> eventType) |
a3fc8213 AM |
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 | |
8636b448 | 87 | public void indexTrace(boolean waitForCompletion) { |
a3fc8213 AM |
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 | |
90235d6b AM |
105 | } catch (CTFReaderException e) { |
106 | /* Nope, not a CTF trace we can read */ | |
107 | return false; | |
a3fc8213 | 108 | } |
a3fc8213 AM |
109 | } |
110 | ||
111 | @Override | |
112 | public CtfTmfTrace clone() throws CloneNotSupportedException { | |
113 | CtfTmfTrace clone = null; | |
114 | clone = (CtfTmfTrace) super.clone(); | |
a3fc8213 AM |
115 | clone.fStartTime = this.fStartTime.clone(); |
116 | clone.fEndTime = this.fEndTime.clone(); | |
117 | clone.fTrace = this.fTrace; | |
118 | return clone; | |
119 | } | |
120 | ||
a3fc8213 AM |
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 | |
3118edf1 | 144 | public int getIndexPageSize() { |
a3fc8213 AM |
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<?>) | |
4dbe1f0e MK |
203 | && !TmfTimestamp.BIG_BANG |
204 | .equals(((ITmfEventRequest<CtfTmfEvent>) request) | |
205 | .getRange().getStartTime()) | |
a3fc8213 | 206 | && (request.getIndex() == 0)) { |
4dbe1f0e MK |
207 | ITmfContext context = seekEvent(((ITmfEventRequest<CtfTmfEvent>) request) |
208 | .getRange().getStartTime()); | |
209 | ((ITmfEventRequest<CtfTmfEvent>) request) | |
210 | .setStartIndex((int) context.getRank()); | |
a3fc8213 AM |
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. | |
4dbe1f0e | 219 | * |
a3fc8213 AM |
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. | |
4dbe1f0e | 222 | * |
a3fc8213 AM |
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 | iterator.setLocation(location); | |
239 | return iterator; | |
240 | } | |
241 | ||
242 | @Override | |
243 | public double getLocationRatio(ITmfLocation<?> location) { | |
4dbe1f0e MK |
244 | CtfIterator curLocation = (CtfIterator) location; |
245 | return ((double) curLocation.getCurrentEvent().getTimestampValue() - | |
246 | curLocation.getStartTime()) | |
247 | / (curLocation.getEndTime() - curLocation.getStartTime()); | |
a3fc8213 AM |
248 | } |
249 | ||
250 | @Override | |
251 | public long getStreamingInterval() { | |
252 | return 0; | |
253 | } | |
254 | ||
255 | @Override | |
256 | public ITmfContext seekEvent(ITmfTimestamp timestamp) { | |
257 | iterator.seek(timestamp.getValue()); | |
258 | return iterator; | |
259 | } | |
260 | ||
261 | /** | |
262 | * Seek by rank | |
263 | */ | |
264 | @Override | |
265 | public ITmfContext seekEvent(long rank) { | |
266 | iterator.setRank(rank); | |
267 | return iterator; | |
268 | } | |
269 | ||
270 | /** | |
271 | * Seek rank ratio | |
272 | */ | |
273 | @Override | |
274 | public ITmfContext seekLocation(double ratio) { | |
275 | iterator.seek((long) (this.fNbEvents * ratio)); | |
276 | return iterator; | |
277 | } | |
278 | ||
279 | @Override | |
280 | public CtfTmfEvent getNextEvent(ITmfContext context) { | |
281 | iterator.advance(); | |
282 | return iterator.getCurrentEvent(); | |
283 | } | |
284 | ||
285 | @Override | |
286 | public CtfTmfEvent parseEvent(ITmfContext context) { | |
287 | return iterator.getCurrentEvent(); | |
288 | } | |
289 | ||
a3fc8213 AM |
290 | @Override |
291 | public IResource getResource() { | |
292 | return this.fResource; | |
293 | } | |
294 | ||
295 | @Override | |
296 | public void setResource(IResource fResource) { | |
297 | this.fResource = fResource; | |
298 | } | |
299 | ||
90235d6b | 300 | CTFTrace getCTFTrace() { |
a3fc8213 AM |
301 | return fTrace; |
302 | } | |
8636b448 | 303 | |
a3fc8213 | 304 | } |