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