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