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