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