Commit | Line | Data |
---|---|---|
866e5b51 | 1 | /******************************************************************************* |
8e964be1 | 2 | * Copyright (c) 2011-2013 Ericsson, Ecole Polytechnique de Montreal and others |
866e5b51 FC |
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 | * Contributors: Simon Marchi - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.ctf.core.event; | |
14 | ||
866e5b51 | 15 | import java.util.List; |
843f986b | 16 | import java.util.Map; |
824b8985 | 17 | import java.util.Map.Entry; |
866e5b51 FC |
18 | |
19 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; | |
20 | import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope; | |
824b8985 | 21 | import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; |
866e5b51 FC |
22 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; |
23 | import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader; | |
24 | ||
25 | /** | |
be6df2d8 | 26 | * Representation of a particular instance of an event. |
866e5b51 FC |
27 | */ |
28 | public class EventDefinition implements IDefinitionScope { | |
29 | ||
30 | // ------------------------------------------------------------------------ | |
31 | // Attributes | |
32 | // ------------------------------------------------------------------------ | |
33 | ||
34 | /** | |
35 | * The corresponding event declaration. | |
36 | */ | |
8e40ce4c | 37 | private final IEventDeclaration fDeclaration; |
866e5b51 FC |
38 | |
39 | /** | |
40 | * The timestamp of the current event. | |
41 | */ | |
8e40ce4c | 42 | private long fTimestamp; |
866e5b51 FC |
43 | |
44 | /** | |
45 | * The event context structure definition. | |
46 | */ | |
8e40ce4c | 47 | private StructDefinition fContext; |
866e5b51 FC |
48 | |
49 | /** | |
50 | * The event fields structure definition. | |
51 | */ | |
8e40ce4c | 52 | private StructDefinition fFields; |
866e5b51 FC |
53 | |
54 | /** | |
55 | * The StreamInputReader that reads this event definition. | |
56 | */ | |
8e40ce4c | 57 | private final StreamInputReader fStreamInputReader; |
866e5b51 FC |
58 | |
59 | // ------------------------------------------------------------------------ | |
60 | // Constructors | |
61 | // ------------------------------------------------------------------------ | |
62 | ||
63 | /** | |
64 | * Constructs an event definition. | |
65 | * | |
66 | * @param declaration | |
be6df2d8 AM |
67 | * The corresponding event declaration |
68 | * @param streamInputReader | |
69 | * The SIR from where this EventDef was read | |
8e964be1 | 70 | * @since 2.0 |
866e5b51 | 71 | */ |
8e964be1 | 72 | public EventDefinition(IEventDeclaration declaration, |
866e5b51 | 73 | StreamInputReader streamInputReader) { |
8e40ce4c MK |
74 | fDeclaration = declaration; |
75 | fStreamInputReader = streamInputReader; | |
866e5b51 FC |
76 | } |
77 | ||
78 | // ------------------------------------------------------------------------ | |
79 | // Getters/Setters/Predicates | |
80 | // ------------------------------------------------------------------------ | |
81 | ||
82 | @Override | |
83 | public String getPath() { | |
84 | return "event"; //$NON-NLS-1$ | |
85 | } | |
86 | ||
9ac2eb62 MK |
87 | /** |
88 | * Gets the declaration (the form) of the data | |
89 | * | |
90 | * @return the event declaration | |
8e964be1 | 91 | * @since 2.0 |
9ac2eb62 | 92 | */ |
8e964be1 | 93 | public IEventDeclaration getDeclaration() { |
8e40ce4c | 94 | return fDeclaration; |
866e5b51 FC |
95 | } |
96 | ||
9ac2eb62 MK |
97 | /** |
98 | * Gets the fields of a definition | |
99 | * | |
100 | * @return the fields of a definition in struct form. Can be null. | |
101 | */ | |
866e5b51 | 102 | public StructDefinition getFields() { |
8e40ce4c | 103 | return fFields; |
866e5b51 FC |
104 | } |
105 | ||
9ac2eb62 | 106 | /** |
824b8985 | 107 | * Gets the context of this event without the context of the stream |
9ac2eb62 MK |
108 | * |
109 | * @return the context in struct form | |
824b8985 | 110 | * @since 1.2 |
9ac2eb62 | 111 | */ |
824b8985 | 112 | public StructDefinition getEventContext() { |
8e40ce4c | 113 | return fContext; |
866e5b51 FC |
114 | } |
115 | ||
824b8985 MK |
116 | /** |
117 | * Gets the context of this event within a stream | |
118 | * | |
119 | * @return the context in struct form | |
120 | */ | |
121 | public StructDefinition getContext() { | |
122 | final StructDefinition streamContext = | |
8e40ce4c | 123 | fStreamInputReader.getPacketReader().getStreamEventContextDef(); |
824b8985 MK |
124 | |
125 | /* Most common case so far */ | |
126 | if (streamContext == null) { | |
8e40ce4c | 127 | return fContext; |
824b8985 MK |
128 | } |
129 | ||
130 | /* streamContext is not null, but the context of the event is null */ | |
8e40ce4c | 131 | if (fContext == null) { |
824b8985 MK |
132 | return streamContext; |
133 | } | |
134 | ||
135 | /* The stream context and event context are assigned. */ | |
136 | StructDeclaration mergedDeclaration = new StructDeclaration(1); | |
137 | ||
138 | /* Add fields from the stream */ | |
843f986b | 139 | Map<String, Definition> defs = streamContext.getDefinitions(); |
824b8985 MK |
140 | for (Entry<String, Definition> entry : defs.entrySet()) { |
141 | mergedDeclaration.addField(entry.getKey(), entry.getValue().getDeclaration()); | |
142 | } | |
143 | ||
144 | /* Add fields from the event context, overwrite the stream ones if needed. */ | |
8e40ce4c | 145 | for (Entry<String, Definition> entry : fContext.getDefinitions().entrySet()) { |
824b8985 MK |
146 | mergedDeclaration.addField(entry.getKey(), entry.getValue().getDeclaration()); |
147 | } | |
148 | ||
149 | StructDefinition mergedContext = mergedDeclaration.createDefinition(null, "context"); //$NON-NLS-1$ | |
150 | for (String key : mergedContext.getDefinitions().keySet()) { | |
8e40ce4c | 151 | final Definition lookupDefinition = fContext.lookupDefinition(key); |
824b8985 MK |
152 | /* |
153 | * If the key is in the event context, add it from there, if it is | |
154 | * not, then it's in the stream. There is a priority with scoping so | |
155 | * if there is a field like "context" in both stream and context, | |
156 | * you display the context. | |
157 | */ | |
158 | if (lookupDefinition != null) { | |
159 | mergedContext.getDefinitions().put(key, lookupDefinition); | |
160 | } else { | |
161 | mergedContext.getDefinitions().put(key, streamContext.lookupDefinition(key)); | |
162 | } | |
163 | } | |
164 | return mergedContext; | |
165 | } | |
166 | ||
9ac2eb62 MK |
167 | /** |
168 | * Gets the stream input reader that this event was made by | |
169 | * | |
170 | * @return the parent | |
171 | */ | |
866e5b51 | 172 | public StreamInputReader getStreamInputReader() { |
8e40ce4c | 173 | return fStreamInputReader; |
866e5b51 FC |
174 | } |
175 | ||
9ac2eb62 MK |
176 | /** |
177 | * Gets the context of packet the event is in. | |
178 | * | |
179 | * @return the packet context | |
180 | */ | |
866e5b51 | 181 | public StructDefinition getPacketContext() { |
8e40ce4c | 182 | return fStreamInputReader.getCurrentPacketContext(); |
866e5b51 FC |
183 | } |
184 | ||
9ac2eb62 MK |
185 | /** |
186 | * gets the CPU the event was generated by. Slightly LTTng specific | |
187 | * | |
188 | * @return The CPU the event was generated by | |
189 | */ | |
866e5b51 | 190 | public int getCPU() { |
8e40ce4c | 191 | return fStreamInputReader.getCPU(); |
866e5b51 FC |
192 | } |
193 | ||
aa572e22 MK |
194 | /** |
195 | * @return the timestamp | |
196 | */ | |
197 | public long getTimestamp() { | |
8e40ce4c | 198 | return fTimestamp; |
aa572e22 MK |
199 | } |
200 | ||
201 | /** | |
9ac2eb62 MK |
202 | * @param timestamp |
203 | * the timestamp to set | |
aa572e22 MK |
204 | */ |
205 | public void setTimestamp(long timestamp) { | |
8e40ce4c | 206 | fTimestamp = timestamp; |
aa572e22 MK |
207 | } |
208 | ||
209 | /** | |
9ac2eb62 MK |
210 | * @param context |
211 | * the context to set | |
aa572e22 MK |
212 | */ |
213 | public void setContext(StructDefinition context) { | |
8e40ce4c | 214 | fContext = context; |
aa572e22 MK |
215 | } |
216 | ||
217 | /** | |
9ac2eb62 MK |
218 | * @param fields |
219 | * the fields to set | |
aa572e22 MK |
220 | */ |
221 | public void setFields(StructDefinition fields) { | |
8e40ce4c | 222 | fFields = fields; |
aa572e22 MK |
223 | } |
224 | ||
866e5b51 FC |
225 | // ------------------------------------------------------------------------ |
226 | // Operations | |
227 | // ------------------------------------------------------------------------ | |
228 | ||
229 | @Override | |
230 | public Definition lookupDefinition(String lookupPath) { | |
231 | if (lookupPath.equals("context")) { //$NON-NLS-1$ | |
8e40ce4c | 232 | return fContext; |
866e5b51 | 233 | } else if (lookupPath.equals("fields")) { //$NON-NLS-1$ |
8e40ce4c | 234 | return fFields; |
866e5b51 FC |
235 | } else { |
236 | return null; | |
237 | } | |
238 | } | |
239 | ||
240 | @Override | |
241 | public String toString() { | |
843f986b | 242 | Map<String, Definition> definitions; |
866e5b51 | 243 | List<String> list; |
07002e0a MK |
244 | StringBuilder retString = new StringBuilder(); |
245 | final String cr = System.getProperty("line.separator");//$NON-NLS-1$ | |
866e5b51 | 246 | |
8e40ce4c MK |
247 | retString.append("Event type: " + fDeclaration.getName() + cr); //$NON-NLS-1$ |
248 | retString.append("Timestamp: " + Long.toString(fTimestamp) + cr); //$NON-NLS-1$ | |
866e5b51 | 249 | |
8e40ce4c MK |
250 | if (fContext != null) { |
251 | definitions = fContext.getDefinitions(); | |
252 | list = fContext.getDeclaration().getFieldsList(); | |
866e5b51 FC |
253 | |
254 | for (String field : list) { | |
9ac2eb62 MK |
255 | retString.append(field |
256 | + " : " + definitions.get(field).toString() + cr); //$NON-NLS-1$ | |
866e5b51 FC |
257 | } |
258 | } | |
259 | ||
8e40ce4c MK |
260 | if (fFields != null) { |
261 | definitions = fFields.getDefinitions(); | |
262 | list = fFields.getDeclaration().getFieldsList(); | |
866e5b51 FC |
263 | |
264 | for (String field : list) { | |
9ac2eb62 MK |
265 | retString.append(field |
266 | + " : " + definitions.get(field).toString() + cr); //$NON-NLS-1$ | |
866e5b51 FC |
267 | } |
268 | } | |
269 | ||
07002e0a | 270 | return retString.toString(); |
866e5b51 FC |
271 | } |
272 | ||
273 | } |