ctf: potential memory optimization
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / EventDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2011-2013 Ericsson, Ecole Polytechnique de Montreal and others
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
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18
19 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
20 import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
21 import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
22 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
23 import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader;
24
25 /**
26 * Representation of a particular instance of an event.
27 */
28 public class EventDefinition implements IDefinitionScope {
29
30 // ------------------------------------------------------------------------
31 // Attributes
32 // ------------------------------------------------------------------------
33
34 /**
35 * The corresponding event declaration.
36 */
37 private final IEventDeclaration fDeclaration;
38
39 /**
40 * The timestamp of the current event.
41 */
42 private long fTimestamp;
43
44 /**
45 * The event context structure definition.
46 */
47 private StructDefinition fContext;
48
49 /**
50 * The event fields structure definition.
51 */
52 private StructDefinition fFields;
53
54 /**
55 * The StreamInputReader that reads this event definition.
56 */
57 private final StreamInputReader fStreamInputReader;
58
59 // ------------------------------------------------------------------------
60 // Constructors
61 // ------------------------------------------------------------------------
62
63 /**
64 * Constructs an event definition.
65 *
66 * @param declaration
67 * The corresponding event declaration
68 * @param streamInputReader
69 * The SIR from where this EventDef was read
70 * @since 2.0
71 */
72 public EventDefinition(IEventDeclaration declaration,
73 StreamInputReader streamInputReader) {
74 fDeclaration = declaration;
75 fStreamInputReader = streamInputReader;
76 }
77
78 // ------------------------------------------------------------------------
79 // Getters/Setters/Predicates
80 // ------------------------------------------------------------------------
81
82 @Override
83 public String getPath() {
84 return "event"; //$NON-NLS-1$
85 }
86
87 /**
88 * Gets the declaration (the form) of the data
89 *
90 * @return the event declaration
91 * @since 2.0
92 */
93 public IEventDeclaration getDeclaration() {
94 return fDeclaration;
95 }
96
97 /**
98 * Gets the fields of a definition
99 *
100 * @return the fields of a definition in struct form. Can be null.
101 */
102 public StructDefinition getFields() {
103 return fFields;
104 }
105
106 /**
107 * Gets the context of this event without the context of the stream
108 *
109 * @return the context in struct form
110 * @since 1.2
111 */
112 public StructDefinition getEventContext() {
113 return fContext;
114 }
115
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 =
123 fStreamInputReader.getPacketReader().getStreamEventContextDef();
124
125 /* Most common case so far */
126 if (streamContext == null) {
127 return fContext;
128 }
129
130 /* streamContext is not null, but the context of the event is null */
131 if (fContext == null) {
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 */
139 Map<String, Definition> defs = streamContext.getDefinitions();
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. */
145 for (Entry<String, Definition> entry : fContext.getDefinitions().entrySet()) {
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()) {
151 final Definition lookupDefinition = fContext.lookupDefinition(key);
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
167 /**
168 * Gets the stream input reader that this event was made by
169 *
170 * @return the parent
171 */
172 public StreamInputReader getStreamInputReader() {
173 return fStreamInputReader;
174 }
175
176 /**
177 * Gets the context of packet the event is in.
178 *
179 * @return the packet context
180 */
181 public StructDefinition getPacketContext() {
182 return fStreamInputReader.getCurrentPacketContext();
183 }
184
185 /**
186 * gets the CPU the event was generated by. Slightly LTTng specific
187 *
188 * @return The CPU the event was generated by
189 */
190 public int getCPU() {
191 return fStreamInputReader.getCPU();
192 }
193
194 /**
195 * @return the timestamp
196 */
197 public long getTimestamp() {
198 return fTimestamp;
199 }
200
201 /**
202 * @param timestamp
203 * the timestamp to set
204 */
205 public void setTimestamp(long timestamp) {
206 fTimestamp = timestamp;
207 }
208
209 /**
210 * @param context
211 * the context to set
212 */
213 public void setContext(StructDefinition context) {
214 fContext = context;
215 }
216
217 /**
218 * @param fields
219 * the fields to set
220 */
221 public void setFields(StructDefinition fields) {
222 fFields = fields;
223 }
224
225 // ------------------------------------------------------------------------
226 // Operations
227 // ------------------------------------------------------------------------
228
229 @Override
230 public Definition lookupDefinition(String lookupPath) {
231 if (lookupPath.equals("context")) { //$NON-NLS-1$
232 return fContext;
233 } else if (lookupPath.equals("fields")) { //$NON-NLS-1$
234 return fFields;
235 } else {
236 return null;
237 }
238 }
239
240 @Override
241 public String toString() {
242 Map<String, Definition> definitions;
243 List<String> list;
244 StringBuilder retString = new StringBuilder();
245 final String cr = System.getProperty("line.separator");//$NON-NLS-1$
246
247 retString.append("Event type: " + fDeclaration.getName() + cr); //$NON-NLS-1$
248 retString.append("Timestamp: " + Long.toString(fTimestamp) + cr); //$NON-NLS-1$
249
250 if (fContext != null) {
251 definitions = fContext.getDefinitions();
252 list = fContext.getDeclaration().getFieldsList();
253
254 for (String field : list) {
255 retString.append(field
256 + " : " + definitions.get(field).toString() + cr); //$NON-NLS-1$
257 }
258 }
259
260 if (fFields != null) {
261 definitions = fFields.getDefinitions();
262 list = fFields.getDeclaration().getFieldsList();
263
264 for (String field : list) {
265 retString.append(field
266 + " : " + definitions.get(field).toString() + cr); //$NON-NLS-1$
267 }
268 }
269
270 return retString.toString();
271 }
272
273 }
This page took 0.055079 seconds and 5 git commands to generate.