tmf: Work around a simple CtfTmfEventType warning
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfTrace.java
1 /*******************************************************************************
2 * Copyright (c) 2012 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11
12 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
13
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.linuxtools.ctf.core.event.EventDeclaration;
19 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
20 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
21 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
22 import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider;
23 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
24 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
25 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
26 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
27 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
28 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
29 import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
30 import org.eclipse.linuxtools.tmf.core.signal.TmfSignal;
31 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
32 import org.eclipse.linuxtools.tmf.core.statesystem.IStateSystemQuerier;
33 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
34 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
35 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
36
37 /**
38 */
39 public class CtfTmfTrace extends TmfEventProvider<CtfTmfEvent> implements ITmfTrace<CtfTmfEvent> {
40
41 // ------------------------------------------------------------------------
42 // Constants
43 // ------------------------------------------------------------------------
44
45 // ------------------------------------------------------------------------
46 // Attributes
47 // ------------------------------------------------------------------------
48
49 // the Ctf Trace
50 private CTFTrace fTrace;
51
52 // The number of events collected
53 protected long fNbEvents = 0;
54
55 // The time span of the event stream
56 private ITmfTimestamp fStartTime = TmfTimestamp.BIG_CRUNCH;
57 private ITmfTimestamp fEndTime = TmfTimestamp.BIG_BANG;
58
59 // The trace resource
60 private IResource fResource;
61
62 /* Reference to the state system assigned to this trace */
63 protected IStateSystemQuerier ss = null;
64
65 // ------------------------------------------------------------------------
66 // Constructors
67 // ------------------------------------------------------------------------
68
69 public CtfTmfTrace() {
70 super();
71 }
72
73 /**
74 * Method initTrace.
75 * @param resource IResource
76 * @param path String
77 * @param eventType Class<CtfTmfEvent>
78 * @throws TmfTraceException
79 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(IResource, String, Class<CtfTmfEvent>)
80 */
81 @Override
82 public void initTrace(final IResource resource, final String path, final Class<CtfTmfEvent> eventType)
83 throws TmfTraceException {
84 EventDeclaration ed;
85 ITmfEventField eventField;
86 @SuppressWarnings("unused")
87 CtfTmfEventType type;
88
89 this.fResource = resource;
90 try {
91 this.fTrace = new CTFTrace(path);
92 for( int i =0 ; i< this.fTrace.getNbEventTypes(); i++) {
93 ed = this.fTrace.getEventType(i);
94 eventField = parseDeclaration(ed);
95 /*
96 * Populate the event manager with event types that are there in
97 * the beginning.
98 */
99 type = new CtfTmfEventType(ed.getName(), eventField);
100 }
101
102 /* Set the start and (current) end times for this trace */
103 final CtfIterator iterator = new CtfIterator(this, 0, 0);
104 if(iterator.getLocation().equals(CtfIterator.NULL_LOCATION)) {
105 /* Handle the case where the trace is empty */
106 this.setStartTime(TmfTimestamp.BIG_BANG);
107 } else {
108 this.setStartTime(iterator.getCurrentEvent().getTimestamp());
109 iterator.goToLastEvent();
110 this.setEndTime(iterator.getCurrentEvent().getTimestamp());
111 }
112
113 } catch (final CTFReaderException e) {
114 /*
115 * If it failed at the init(), we can assume it's because the file
116 * was not found or was not recognized as a CTF trace. Throw into
117 * the new type of exception expected by the rest of TMF.
118 */
119 throw new TmfTraceException(e.getMessage());
120 }
121
122 TmfSignalManager.register(this);
123 //FIXME This should be called via the ExperimentUpdated signal
124 buildStateSystem();
125
126 /* Refresh the project, so it can pick up new files that got created. */
127 if ( resource != null) {
128 try {
129 resource.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
130 } catch (CoreException e) {
131 throw new TmfTraceException(e.getMessage());
132 }
133 }
134 }
135
136 private static ITmfEventField parseDeclaration(EventDeclaration ed) {
137 EventDefinition eventDef = ed.createDefinition(null);
138 return new CtfTmfContent(ITmfEventField.ROOT_FIELD_ID,
139 CtfTmfEvent.parseFields(eventDef));
140 }
141
142 /**
143 * Method dispose.
144 * @see org.eclipse.linuxtools.tmf.core.component.ITmfComponent#dispose()
145 */
146 @Override
147 public void dispose() {
148 TmfSignalManager.deregister(this);
149 }
150
151 /**
152 * Method broadcast.
153 * @param signal TmfSignal
154 * @see org.eclipse.linuxtools.tmf.core.component.ITmfComponent#broadcast(TmfSignal)
155 */
156 @Override
157 public void broadcast(final TmfSignal signal) {
158 TmfSignalManager.dispatchSignal(signal);
159 }
160
161 /**
162 * Method validate.
163 * @param project IProject
164 * @param path String
165 * @return boolean
166 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
167 */
168 @Override
169 public boolean validate(final IProject project, final String path) {
170 try {
171 final CTFTrace temp = new CTFTrace(path);
172 return temp.majortIsSet(); // random test
173 } catch (final CTFReaderException e) {
174 /* Nope, not a CTF trace we can read */
175 return false;
176 }
177 }
178
179 // ------------------------------------------------------------------------
180 // Accessors
181 // ------------------------------------------------------------------------
182
183 /**
184 * Method getEventType.
185 * @return the trace path
186 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEventType()
187 */
188 @Override
189 public Class<CtfTmfEvent> getEventType() {
190 return fType;
191 }
192
193 /**
194 * Method getNbEnvVars.
195 * @return int
196 */
197 public int getNbEnvVars() {
198 return this.fTrace.getEnvironment().size();
199 }
200
201
202 /**
203 * Method getEnvNames.
204 * @return String[]
205 */
206 public String[] getEnvNames() {
207 final String[] s = new String[getNbEnvVars()];
208 return this.fTrace.getEnvironment().keySet().toArray(s);
209 }
210
211 /**
212 * Method getEnvValue.
213 * @param key String
214 * @return String
215 */
216 public String getEnvValue(final String key) {
217 return this.fTrace.getEnvironment().get(key);
218 }
219
220
221 /**
222
223 * @return the trace path * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getPath()
224 */
225 @Override
226 public String getPath() {
227 return this.fTrace.getPath();
228 }
229
230 /**
231 * Method getName.
232 * @return String
233 * @see org.eclipse.linuxtools.tmf.core.component.ITmfComponent#getName()
234 */
235 @Override
236 public String getName() {
237 String traceName = (fResource != null) ? fResource.getName() : null;
238 // If no resource was provided, extract the display name the trace path
239 if (traceName == null) {
240 final String path = this.fTrace.getPath();
241 final int sep = path.lastIndexOf(IPath.SEPARATOR);
242 traceName = (sep >= 0) ? path.substring(sep + 1) : path;
243 }
244 return traceName;
245 }
246
247 /**
248 * Method getCacheSize.
249 * @return int
250 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCacheSize()
251 */
252 @Override
253 public int getCacheSize() {
254 return 50000; // not true, but it works
255 }
256
257 /**
258 * Method getNbEvents.
259 * @return long
260 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNbEvents()
261 */
262 @Override
263 public long getNbEvents() {
264 return this.fNbEvents;
265 }
266
267 /**
268 * Method getTimeRange.
269 * @return TmfTimeRange
270 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getTimeRange()
271 */
272 @Override
273 public TmfTimeRange getTimeRange() {
274 return new TmfTimeRange(this.fStartTime, this.fEndTime);
275 }
276
277 /**
278 * Method getStartTime.
279 * @return ITmfTimestamp
280 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStartTime()
281 */
282 @Override
283 public ITmfTimestamp getStartTime() {
284 return this.fStartTime;
285 }
286
287 /**
288 * Method getEndTime.
289 * @return ITmfTimestamp
290 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEndTime()
291 */
292 @Override
293 public ITmfTimestamp getEndTime() {
294 return this.fEndTime;
295 }
296
297 /**
298 * Method getCurrentLocation. This is not applicable in CTF
299 * @return null, since the trace has no knowledge of the current location
300 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
301 */
302 @Override
303 public ITmfLocation<?> getCurrentLocation() {
304 return null;
305 }
306
307 // ------------------------------------------------------------------------
308 // Operators
309 // ------------------------------------------------------------------------
310
311 /**
312 * Method setTimeRange.
313 * @param range TmfTimeRange
314 */
315 protected void setTimeRange(final TmfTimeRange range) {
316 this.fStartTime = range.getStartTime();
317 this.fEndTime = range.getEndTime();
318 }
319
320 /**
321 * Method setStartTime.
322 * @param startTime ITmfTimestamp
323 */
324 protected void setStartTime(final ITmfTimestamp startTime) {
325 this.fStartTime = startTime;
326 }
327
328 /**
329 * Method setEndTime.
330 * @param endTime ITmfTimestamp
331 */
332 protected void setEndTime(final ITmfTimestamp endTime) {
333 this.fEndTime = endTime;
334 }
335
336 // ------------------------------------------------------------------------
337 // TmfProvider
338 // ------------------------------------------------------------------------
339
340 /**
341 * Method armRequest.
342 * @param request ITmfDataRequest<CtfTmfEvent>
343 * @return ITmfContext
344 */
345 @Override
346 public ITmfContext armRequest(final ITmfDataRequest<CtfTmfEvent> request) {
347 if ((request instanceof ITmfEventRequest<?>)
348 && !TmfTimestamp.BIG_BANG
349 .equals(((ITmfEventRequest<CtfTmfEvent>) request)
350 .getRange().getStartTime())
351 && (request.getIndex() == 0)) {
352 final ITmfContext context = seekEvent(((ITmfEventRequest<CtfTmfEvent>) request)
353 .getRange().getStartTime());
354 ((ITmfEventRequest<CtfTmfEvent>) request)
355 .setStartIndex((int) context.getRank());
356 return context;
357 }
358 return seekEvent(request.getIndex());
359 }
360
361 /**
362 * The trace reader keeps its own iterator: the "context" parameter here
363 * will be ignored.
364 *
365 * If you wish to specify a new context, instantiate a new CtfIterator and
366 * seek() it to where you want, and use that to read events.
367 *
368 * FIXME merge with getNextEvent below once they both use the same parameter
369 * type.
370 * @param context ITmfContext
371 * @return CtfTmfEvent
372 */
373 @Override
374 public CtfTmfEvent getNext(final ITmfContext context) {
375 return readNextEvent(context);
376 }
377
378 // ------------------------------------------------------------------------
379 // ITmfTrace
380 // ------------------------------------------------------------------------
381
382 /**
383 * Method seekEvent.
384 * @param location ITmfLocation<?>
385 * @return ITmfContext
386 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(ITmfLocation<?>)
387 */
388 @Override
389 public ITmfContext seekEvent(final ITmfLocation<?> location) {
390 CtfLocation currentLocation = (CtfLocation) location;
391 if (currentLocation == null) {
392 currentLocation = new CtfLocation(0L);
393 }
394 CtfIterator context = new CtfIterator(this);
395 context.setLocation(currentLocation);
396 context.setRank(ITmfContext.UNKNOWN_RANK);
397 return context;
398 }
399
400 /**
401 * Method getLocationRatio.
402 * @param location ITmfLocation<?>
403 * @return double
404 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getLocationRatio(ITmfLocation<?>)
405 */
406 @Override
407 public double getLocationRatio(final ITmfLocation<?> location) {
408 final CtfLocation curLocation = (CtfLocation) location;
409 CtfIterator iterator = new CtfIterator(this);
410 iterator.seek(curLocation.getLocation());
411 return ((double) iterator.getCurrentEvent().getTimestampValue() - iterator
412 .getStartTime())
413 / (iterator.getEndTime() - iterator.getStartTime());
414 }
415
416 /**
417 * Method getStreamingInterval.
418 * @return long
419 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStreamingInterval()
420 */
421 @Override
422 public long getStreamingInterval() {
423 return 0;
424 }
425
426 /**
427 * Method seekEvent.
428 * @param timestamp ITmfTimestamp
429 * @return ITmfContext
430 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(ITmfTimestamp)
431 */
432 @Override
433 public ITmfContext seekEvent(final ITmfTimestamp timestamp) {
434 CtfIterator context = new CtfIterator(this);
435 context.seek(timestamp.getValue());
436 context.setRank(ITmfContext.UNKNOWN_RANK);
437 return context;
438 }
439
440 /**
441 * Seek by rank
442 * @param rank long
443 * @return ITmfContext
444 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(long)
445 */
446 @Override
447 public ITmfContext seekEvent(final long rank) {
448 CtfIterator context = new CtfIterator(this);
449 context.seekRank(rank);
450 context.setRank(rank);
451 return context;
452 }
453
454 /**
455 * Seek rank ratio
456 * @param ratio double
457 * @return ITmfContext
458 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(double)
459 */
460 @Override
461 public ITmfContext seekEvent(final double ratio) {
462 CtfIterator context = new CtfIterator(this);
463 context.seek((long) (this.fNbEvents * ratio));
464 context.setRank(ITmfContext.UNKNOWN_RANK);
465 return context;
466 }
467
468 /**
469 * Method readNextEvent.
470 * @param context ITmfContext
471 * @return CtfTmfEvent
472 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#readNextEvent(ITmfContext)
473 */
474 @Override
475 public CtfTmfEvent readNextEvent(final ITmfContext context) {
476 CtfTmfEvent event = null;
477 if (context instanceof CtfIterator) {
478 CtfIterator ctfIterator = (CtfIterator) context;
479 event = ctfIterator.getCurrentEvent();
480 ctfIterator.advance();
481 }
482 return event;
483 }
484
485 /**
486 * Method getResource.
487 * @return IResource
488 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
489 */
490 @Override
491 public IResource getResource() {
492 return this.fResource;
493 }
494
495 /**
496 * Method getStateSystem.
497 * @return IStateSystemQuerier
498 */
499 public IStateSystemQuerier getStateSystem() {
500 return this.ss;
501 }
502
503 /**
504 * Method getCTFTrace.
505 * @return CTFTrace
506 */
507 CTFTrace getCTFTrace() {
508 return fTrace;
509 }
510
511
512 /**
513 * Suppressing the warning, because the 'throws' will usually happen in
514 * sub-classes.
515 * @throws TmfTraceException
516 */
517 protected void buildStateSystem() throws TmfTraceException {
518 /*
519 * Nothing is done in the basic implementation, please specify
520 * how/if to build a state system in derived classes.
521 */
522 return;
523 }
524
525 }
This page took 0.042242 seconds and 6 git commands to generate.