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