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