Commit | Line | Data |
---|---|---|
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 | ||
4b7c469f | 12 | |
a3fc8213 AM |
13 | package org.eclipse.linuxtools.tmf.core.ctfadaptor; |
14 | ||
a3fc8213 AM |
15 | import org.eclipse.core.resources.IProject; |
16 | import org.eclipse.core.resources.IResource; | |
139d5c1a | 17 | import org.eclipse.core.runtime.CoreException; |
aa572e22 MK |
18 | import org.eclipse.linuxtools.ctf.core.event.EventDeclaration; |
19 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; | |
a3fc8213 AM |
20 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
21 | import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; | |
1191a574 | 22 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTimestamp.TimestampType; |
aa572e22 | 23 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; |
a3fc8213 | 24 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; |
b4f71e4a | 25 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
a3fc8213 | 26 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager; |
18ab1d18 | 27 | import org.eclipse.linuxtools.tmf.core.statesystem.IStateSystemQuerier; |
a3fc8213 | 28 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
4b7c469f | 29 | import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser; |
a3fc8213 | 30 | import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; |
4b7c469f | 31 | import org.eclipse.linuxtools.tmf.core.trace.TmfTrace; |
a3fc8213 | 32 | |
bfe038ff | 33 | public class CtfTmfTrace extends TmfTrace<CtfTmfEvent> implements ITmfEventParser<CtfTmfEvent>{ |
a3fc8213 | 34 | |
4b7c469f MK |
35 | //------------------------------------------- |
36 | // Fields | |
37 | //------------------------------------------- | |
a3fc8213 | 38 | |
11d6f468 | 39 | /* Reference to the state system assigned to this trace */ |
d26f90fd | 40 | protected IStateSystemQuerier ss = null; |
11d6f468 | 41 | |
4b7c469f MK |
42 | /* Reference to the CTF Trace */ |
43 | private CTFTrace fTrace; | |
a3fc8213 | 44 | |
4b7c469f MK |
45 | //------------------------------------------- |
46 | // TmfTrace Overrides | |
47 | //------------------------------------------- | |
b1baa808 MK |
48 | /** |
49 | * Method initTrace. | |
50 | * @param resource IResource | |
51 | * @param path String | |
52 | * @param eventType Class<CtfTmfEvent> | |
53 | * @throws TmfTraceException | |
54 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(IResource, String, Class<CtfTmfEvent>) | |
55 | */ | |
a3fc8213 | 56 | @Override |
25e48683 | 57 | public void initTrace(final IResource resource, final String path, final Class<CtfTmfEvent> eventType) |
b4f71e4a | 58 | throws TmfTraceException { |
4b7c469f | 59 | super.initTrace(resource, path, eventType); |
e30ce12e AM |
60 | EventDeclaration ed; |
61 | ITmfEventField eventField; | |
62 | @SuppressWarnings("unused") | |
63 | CtfTmfEventType type; | |
64 | ||
a3fc8213 AM |
65 | try { |
66 | this.fTrace = new CTFTrace(path); | |
aa572e22 | 67 | for( int i =0 ; i< this.fTrace.getNbEventTypes(); i++) { |
e30ce12e AM |
68 | ed = this.fTrace.getEventType(i); |
69 | eventField = parseDeclaration(ed); | |
99b483fe AM |
70 | /* |
71 | * Populate the event manager with event types that are there in | |
72 | * the beginning. | |
73 | */ | |
e30ce12e | 74 | type = new CtfTmfEventType(ed.getName(), eventField); |
aa572e22 | 75 | } |
99b483fe AM |
76 | |
77 | /* Set the start and (current) end times for this trace */ | |
78 | final CtfIterator iterator = new CtfIterator(this, 0, 0); | |
79 | if(iterator.getLocation().equals(CtfIterator.NULL_LOCATION)) { | |
80 | /* Handle the case where the trace is empty */ | |
81 | this.setStartTime(TmfTimestamp.BIG_BANG); | |
82 | } else { | |
83 | this.setStartTime(iterator.getCurrentEvent().getTimestamp()); | |
f25b9816 MK |
84 | /* |
85 | * is the trace empty | |
86 | */ | |
87 | if( iterator.hasMoreEvents()){ | |
88 | iterator.goToLastEvent(); | |
89 | } | |
99b483fe AM |
90 | this.setEndTime(iterator.getCurrentEvent().getTimestamp()); |
91 | } | |
92 | ||
25e48683 | 93 | } catch (final CTFReaderException e) { |
a3fc8213 AM |
94 | /* |
95 | * If it failed at the init(), we can assume it's because the file | |
96 | * was not found or was not recognized as a CTF trace. Throw into | |
97 | * the new type of exception expected by the rest of TMF. | |
98 | */ | |
9fa32496 | 99 | throw new TmfTraceException(e.getMessage(), e); |
a3fc8213 | 100 | } |
99b483fe | 101 | |
a3fc8213 | 102 | TmfSignalManager.register(this); |
99b483fe | 103 | //FIXME This should be called via the ExperimentUpdated signal |
11d6f468 | 104 | buildStateSystem(); |
139d5c1a AM |
105 | |
106 | /* Refresh the project, so it can pick up new files that got created. */ | |
107 | if ( resource != null) { | |
108 | try { | |
109 | resource.getProject().refreshLocal(IResource.DEPTH_INFINITE, null); | |
110 | } catch (CoreException e) { | |
9fa32496 | 111 | throw new TmfTraceException(e.getMessage(), e); |
139d5c1a AM |
112 | } |
113 | } | |
a3fc8213 AM |
114 | } |
115 | ||
b1baa808 MK |
116 | /** |
117 | * Method validate. | |
118 | * @param project IProject | |
119 | * @param path String | |
120 | * @return boolean | |
121 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String) | |
122 | */ | |
a3fc8213 | 123 | @Override |
4b7c469f | 124 | public boolean validate(@SuppressWarnings("unused") final IProject project, final String path) { |
a3fc8213 AM |
125 | try { |
126 | final CTFTrace temp = new CTFTrace(path); | |
127 | return temp.majortIsSet(); // random test | |
25e48683 | 128 | } catch (final CTFReaderException e) { |
90235d6b AM |
129 | /* Nope, not a CTF trace we can read */ |
130 | return false; | |
a3fc8213 | 131 | } |
a3fc8213 AM |
132 | } |
133 | ||
b1baa808 | 134 | /** |
f474d36b PT |
135 | * Method getCurrentLocation. This is not applicable in CTF |
136 | * @return null, since the trace has no knowledge of the current location | |
b1baa808 MK |
137 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation() |
138 | */ | |
a3fc8213 AM |
139 | @Override |
140 | public ITmfLocation<?> getCurrentLocation() { | |
f474d36b | 141 | return null; |
a3fc8213 AM |
142 | } |
143 | ||
a3fc8213 | 144 | |
a3fc8213 AM |
145 | |
146 | @Override | |
4b7c469f MK |
147 | public double getLocationRatio(ITmfLocation<?> location) { |
148 | final CtfLocation curLocation = (CtfLocation) location; | |
149 | CtfIterator iterator = new CtfIterator(this); | |
150 | iterator.seek(curLocation.getLocation()); | |
151 | return ((double) iterator.getCurrentEvent().getTimestampValue() - iterator | |
152 | .getStartTime()) | |
153 | / (iterator.getEndTime() - iterator.getStartTime()); | |
a3fc8213 AM |
154 | } |
155 | ||
b1baa808 MK |
156 | /** |
157 | * Method seekEvent. | |
158 | * @param location ITmfLocation<?> | |
159 | * @return ITmfContext | |
160 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(ITmfLocation<?>) | |
161 | */ | |
a3fc8213 | 162 | @Override |
7e6347b0 | 163 | public ITmfContext seekEvent(final ITmfLocation<?> location) { |
ce2388e0 | 164 | CtfLocation currentLocation = (CtfLocation) location; |
11d6f468 | 165 | if (currentLocation == null) { |
ce2388e0 | 166 | currentLocation = new CtfLocation(0L); |
11d6f468 | 167 | } |
f474d36b | 168 | CtfIterator context = new CtfIterator(this); |
4b7c469f | 169 | |
1191a574 | 170 | if (currentLocation.getLocation() == CtfLocation.INVALID_LOCATION) { |
4cf201de FC |
171 | ((CtfTmfTimestamp) getEndTime()).setType(TimestampType.NANOS); |
172 | currentLocation.setLocation(getEndTime().getValue() + 1); | |
1191a574 | 173 | } |
f474d36b PT |
174 | context.setLocation(currentLocation); |
175 | context.setRank(ITmfContext.UNKNOWN_RANK); | |
176 | return context; | |
a3fc8213 AM |
177 | } |
178 | ||
a3fc8213 | 179 | |
a3fc8213 | 180 | @Override |
4b7c469f | 181 | public ITmfContext seekEvent(double ratio) { |
f474d36b | 182 | CtfIterator context = new CtfIterator(this); |
4b7c469f | 183 | context.seek((long) (this.getNbEvents() * ratio)); |
f474d36b PT |
184 | context.setRank(ITmfContext.UNKNOWN_RANK); |
185 | return context; | |
a3fc8213 AM |
186 | } |
187 | ||
b1baa808 MK |
188 | /** |
189 | * Method readNextEvent. | |
190 | * @param context ITmfContext | |
191 | * @return CtfTmfEvent | |
c32744d6 | 192 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext) |
b1baa808 | 193 | */ |
a3fc8213 | 194 | @Override |
4b7c469f | 195 | public synchronized CtfTmfEvent getNext(final ITmfContext context) { |
f474d36b PT |
196 | CtfTmfEvent event = null; |
197 | if (context instanceof CtfIterator) { | |
198 | CtfIterator ctfIterator = (CtfIterator) context; | |
199 | event = ctfIterator.getCurrentEvent(); | |
200 | ctfIterator.advance(); | |
201 | } | |
aa572e22 | 202 | return event; |
a3fc8213 AM |
203 | } |
204 | ||
b1baa808 | 205 | /** |
4b7c469f MK |
206 | * Suppressing the warning, because the 'throws' will usually happen in |
207 | * sub-classes. | |
208 | * @throws TmfTraceException | |
b1baa808 | 209 | */ |
4b7c469f MK |
210 | @SuppressWarnings({ "static-method", "unused" }) |
211 | protected void buildStateSystem() throws TmfTraceException { | |
212 | /* | |
213 | * Nothing is done in the basic implementation, please specify | |
214 | * how/if to build a state system in derived classes. | |
215 | */ | |
216 | return; | |
a3fc8213 AM |
217 | } |
218 | ||
b1baa808 MK |
219 | /** |
220 | * Method getStateSystem. | |
4b7c469f | 221 | * |
b1baa808 MK |
222 | * @return IStateSystemQuerier |
223 | */ | |
d26f90fd | 224 | public IStateSystemQuerier getStateSystem() { |
11d6f468 AM |
225 | return this.ss; |
226 | } | |
227 | ||
b1baa808 | 228 | /** |
4b7c469f MK |
229 | * |
230 | * @param ed | |
231 | * @return | |
b1baa808 | 232 | */ |
4b7c469f MK |
233 | private static ITmfEventField parseDeclaration(EventDeclaration ed) { |
234 | EventDefinition eventDef = ed.createDefinition(null); | |
235 | return new CtfTmfContent(ITmfEventField.ROOT_FIELD_ID, | |
236 | CtfTmfEvent.parseFields(eventDef)); | |
237 | } | |
238 | ||
239 | /** | |
240 | * gets the CTFtrace that this is wrapping | |
241 | * @return the CTF trace | |
242 | */ | |
243 | public CTFTrace getCTFTrace() { | |
a3fc8213 AM |
244 | return fTrace; |
245 | } | |
a1a24d68 | 246 | |
8636b448 | 247 | |
4b7c469f MK |
248 | //------------------------------------------- |
249 | // Environment Parameters | |
250 | //------------------------------------------- | |
d26f90fd | 251 | /** |
4b7c469f MK |
252 | * Method getNbEnvVars. |
253 | * | |
254 | * @return int | |
d26f90fd | 255 | */ |
4b7c469f MK |
256 | public int getNbEnvVars() { |
257 | return this.fTrace.getEnvironment().size(); | |
258 | } | |
259 | ||
260 | /** | |
261 | * Method getEnvNames. | |
262 | * | |
263 | * @return String[] | |
264 | */ | |
265 | public String[] getEnvNames() { | |
266 | final String[] s = new String[getNbEnvVars()]; | |
267 | return this.fTrace.getEnvironment().keySet().toArray(s); | |
268 | } | |
269 | ||
270 | /** | |
271 | * Method getEnvValue. | |
272 | * | |
273 | * @param key | |
274 | * String | |
275 | * @return String | |
276 | */ | |
277 | public String getEnvValue(final String key) { | |
278 | return this.fTrace.getEnvironment().get(key); | |
279 | } | |
280 | ||
bfe038ff MK |
281 | //------------------------------------------- |
282 | // Clocks | |
283 | //------------------------------------------- | |
284 | ||
285 | public long getOffset(){ | |
286 | if( fTrace != null ) { | |
287 | return fTrace.getOffset(); | |
288 | } | |
289 | return 0; | |
290 | } | |
291 | ||
4b7c469f MK |
292 | //------------------------------------------- |
293 | // Parser | |
294 | //------------------------------------------- | |
295 | ||
296 | @Override | |
bfe038ff | 297 | public CtfTmfEvent parseEvent(ITmfContext context) { |
4b7c469f MK |
298 | CtfTmfEvent event = null; |
299 | if( context instanceof CtfIterator ){ | |
300 | CtfIterator itt = (CtfIterator) context; | |
301 | event = itt.getCurrentEvent(); | |
302 | } | |
303 | return event; | |
11d6f468 | 304 | } |
ce2388e0 | 305 | |
a3fc8213 | 306 | } |