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