Commit | Line | Data |
---|---|---|
a3fc8213 AM |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011 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: Alexandre Montplaisir - Initial API and implementation | |
10 | *******************************************************************************/ | |
11 | ||
12 | package org.eclipse.linuxtools.tmf.core.ctfadaptor; | |
13 | ||
14 | import java.util.ArrayList; | |
15 | import java.util.HashMap; | |
16 | import java.util.List; | |
17 | import java.util.Map.Entry; | |
18 | ||
19 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; | |
20 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; | |
21 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; | |
22 | import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader; | |
23 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; | |
24 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; | |
25 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventType; | |
26 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; | |
27 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField; | |
28 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; | |
29 | ||
30 | /** | |
31 | * <b><u>CTFEvent</u></b> | |
32 | * <p> | |
33 | * This is a wrapper class around CTF's Event Definition/Declaration so that we | |
34 | * can map all types of Declaration to native Java types. | |
35 | */ | |
36 | public final class CtfTmfEvent implements ITmfEvent { | |
37 | ||
38 | // ------------------------------------------------------------------------ | |
39 | // Constants | |
40 | // ------------------------------------------------------------------------ | |
41 | ||
42 | private static final String NO_STREAM = "No stream"; //$NON-NLS-1$ | |
43 | private static final String EMPTY_CTF_EVENT_NAME = "Empty CTF event"; //$NON-NLS-1$ | |
44 | ||
45 | // ------------------------------------------------------------------------ | |
46 | // Attributes | |
47 | // ------------------------------------------------------------------------ | |
48 | ||
49 | private final CtfTmfTrace fTrace; | |
50 | private final long timestamp; | |
51 | private final int sourceCPU; | |
52 | private final long typeId; | |
53 | private final String eventName; | |
54 | private final String fileName; | |
55 | ||
56 | private final TmfEventField fContent; | |
57 | ||
58 | // ------------------------------------------------------------------------ | |
59 | // Constructors | |
60 | // ------------------------------------------------------------------------ | |
61 | ||
62 | /** | |
63 | * Usual CTFEvent constructor, where we read an event from the trace (via | |
64 | * the StreamInputReader). | |
65 | * | |
66 | * @param eventDef | |
67 | * @param top | |
68 | */ | |
69 | public CtfTmfEvent(EventDefinition eventDef, StreamInputReader top, | |
70 | CtfTmfTrace originTrace) { | |
71 | this.fTrace = originTrace; | |
72 | ||
73 | if (eventDef == null) { | |
74 | this.timestamp = -1; | |
75 | this.sourceCPU = -1; | |
76 | this.typeId = -1; | |
77 | this.fileName = NO_STREAM; | |
78 | this.eventName = EMPTY_CTF_EVENT_NAME; | |
79 | this.fContent = null; | |
80 | return; | |
81 | } | |
82 | ||
83 | /* Read the base event info */ | |
84 | // FIXME restore once the CTF parser with clocks gets merged | |
85 | //Long offset = originTrace.getCTFTrace().getOffset(); | |
86 | Long offset = 0L; | |
87 | this.timestamp = eventDef.timestamp + offset; | |
88 | this.sourceCPU = eventDef.getCPU(); | |
89 | this.typeId = eventDef.getDeclaration().getId(); | |
90 | this.eventName = eventDef.getDeclaration().getName(); | |
91 | this.fileName = top.getStreamInput().getFilename(); | |
92 | ||
93 | /* Read the fields */ | |
94 | this.fContent = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, | |
95 | parseFields(eventDef)); | |
96 | } | |
97 | ||
98 | /** | |
99 | * Extract the field information from the structDefinition haze-inducing | |
100 | * mess, and put them into something ITmfEventField can cope with. | |
101 | * | |
102 | * @param eventDef | |
103 | * @return | |
104 | */ | |
105 | private static CtfTmfEventField[] parseFields(EventDefinition eventDef) { | |
106 | List<CtfTmfEventField> fields = new ArrayList<CtfTmfEventField>(); | |
107 | ||
108 | StructDefinition structFields = eventDef.getFields(); | |
109 | HashMap<String, Definition> definitions = structFields.getDefinitions(); | |
110 | String curFieldName; | |
111 | Definition curFieldDef; | |
112 | CtfTmfEventField curField; | |
113 | ||
114 | for (Entry<String, Definition> entry : definitions.entrySet()) { | |
115 | curFieldName = entry.getKey(); | |
116 | curFieldDef = entry.getValue(); | |
117 | curField = CtfTmfEventField.parseField(curFieldDef, curFieldName); | |
118 | if (curField == null) { | |
119 | // TmfCorePlugin.getDefault().log( | |
120 | // "We've parsed an unimplemented field type for event \"" + this.eventName //$NON-NLS-1$ | |
121 | // + "\", field \"" + curFieldName + "\" of type " + curFieldDef.getClass().toString()); //$NON-NLS-1$ //$NON-NLS-2$ | |
122 | } | |
123 | fields.add(curField); | |
124 | } | |
125 | ||
126 | return fields.toArray(new CtfTmfEventField[fields.size()]); | |
127 | } | |
128 | ||
129 | /** | |
130 | * Copy constructor | |
131 | * | |
132 | * @param other | |
133 | */ | |
134 | public CtfTmfEvent(CtfTmfEvent other) { | |
135 | this.fTrace = other.getTrace(); | |
136 | /* Primitives, those will be copied by value */ | |
137 | this.timestamp = other.timestamp; | |
138 | this.sourceCPU = other.sourceCPU; | |
139 | this.typeId = other.typeId; | |
140 | ||
141 | /* Strings are immutable, it's safe to shallow-copy them */ | |
142 | this.eventName = other.eventName; | |
143 | this.fileName = other.fileName; | |
144 | ||
145 | /* Copy the fields over */ | |
146 | this.fContent = other.fContent.clone(); | |
147 | } | |
148 | ||
149 | /** | |
150 | * Inner constructor to create "null" events. Don't use this directly, use | |
151 | * CTFEvent.getNullEvent(); | |
152 | */ | |
153 | private CtfTmfEvent() { | |
154 | this.fTrace = null; | |
155 | this.timestamp = -1; | |
156 | this.sourceCPU = -1; | |
157 | this.typeId = -1; | |
158 | this.fileName = NO_STREAM; | |
159 | this.eventName = EMPTY_CTF_EVENT_NAME; | |
160 | this.fContent = null; | |
161 | } | |
162 | ||
163 | // ------------------------------------------------------------------------ | |
164 | // Getters/Setters/Predicates | |
165 | // ------------------------------------------------------------------------ | |
166 | ||
167 | private static CtfTmfEvent nullEvent = null; | |
168 | ||
169 | /** | |
170 | * Get a null event | |
171 | * | |
172 | * @return an empty event. | |
173 | */ | |
174 | public static CtfTmfEvent getNullEvent() { | |
175 | if (nullEvent == null) { | |
176 | nullEvent = new CtfTmfEvent(); | |
177 | } | |
178 | return nullEvent; | |
179 | } | |
180 | ||
181 | /** | |
182 | * Gets the current timestamp of the event | |
183 | * | |
184 | * @return the current timestamp (long) | |
185 | */ | |
186 | public long getTimestampValue() { | |
187 | return this.timestamp; | |
188 | } | |
189 | ||
190 | /** | |
191 | * Gets the cpu core the event was recorded on. | |
192 | * | |
193 | * @return the cpu id for a given source. In lttng it's from CPUINFO | |
194 | */ | |
195 | public int getCPU() { | |
196 | return this.sourceCPU; | |
197 | } | |
198 | ||
199 | /** | |
200 | * Return this event's ID, according to the trace's metadata. Watch out, | |
201 | * this ID is not constant from one trace to another for the same event | |
202 | * types! Use "getEventName()" for a constant reference. | |
203 | * | |
204 | * @return | |
205 | */ | |
206 | public long getID() { | |
207 | return this.typeId; | |
208 | } | |
209 | ||
210 | /** | |
211 | * Gets the name of a current event. | |
212 | * | |
213 | * @return the event name | |
214 | */ | |
215 | public String getEventName() { | |
216 | return eventName; | |
217 | } | |
218 | ||
219 | /** | |
220 | * Gets the channel name of a field. | |
221 | * | |
222 | * @return the channel name. | |
223 | */ | |
224 | public String getChannelName() { | |
225 | return this.fileName; | |
226 | } | |
227 | ||
228 | @Override | |
229 | public CtfTmfTrace getTrace() { | |
230 | return fTrace; | |
231 | } | |
232 | ||
233 | @Override | |
234 | public long getRank() { | |
235 | // TODO Auto-generated method stub | |
236 | return 0; | |
237 | } | |
238 | ||
239 | private ITmfTimestamp fTimestamp = null; | |
240 | ||
241 | // TODO Benchmark if the singleton approach is faster than just | |
242 | // instantiating a final fTimestramp right away at creation time | |
243 | @Override | |
244 | public ITmfTimestamp getTimestamp() { | |
245 | if (fTimestamp == null) { | |
246 | fTimestamp = new TmfTimestamp(timestamp); | |
247 | } | |
248 | return fTimestamp; | |
249 | } | |
250 | ||
251 | @Override | |
252 | public String getSource() { | |
f13dfe18 AM |
253 | // TODO Returns eventName for now |
254 | return eventName; | |
a3fc8213 AM |
255 | } |
256 | ||
257 | @Override | |
258 | public ITmfEventType getType() { | |
259 | // TODO Auto-generated method stub | |
260 | return null; | |
261 | } | |
262 | ||
263 | @Override | |
264 | public ITmfEventField getContent() { | |
265 | return fContent; | |
266 | } | |
267 | ||
268 | @Override | |
269 | public String getReference() { | |
270 | // TODO Auto-generated method stub | |
271 | return null; | |
272 | } | |
273 | ||
274 | @Override | |
275 | public CtfTmfEvent clone() { | |
276 | return new CtfTmfEvent(this); | |
277 | } | |
278 | } |