LTTng: CPU usage analysis from the LTTng kernel trace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfEventField.java
CommitLineData
a3fc8213 1/*******************************************************************************
60ae41e1 2 * Copyright (c) 2011, 2014 Ericsson, École Polytechnique de Montréal
a3fc8213
AM
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 *
d4a8d935
BH
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
404b264a 11 * Alexandre Montplaisir - Initial API and implementation, extend TmfEventField
d4a8d935 12 * Bernd Hufmann - Add Enum field handling
404b264a 13 * Geneviève Bastien - Add Struct and Variant field handling
459f705b 14 * Jean-Christian Kouame - Correct handling of unsigned integer fields
4591bed9 15 * François Doray - Add generic array field type
a3fc8213
AM
16 *******************************************************************************/
17
18package org.eclipse.linuxtools.tmf.core.ctfadaptor;
19
a6223d74 20import java.util.ArrayList;
7a6cee1a 21import java.util.Arrays;
a6223d74 22import java.util.List;
7a6cee1a 23import java.util.Map.Entry;
a6223d74 24
a3fc8213
AM
25import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition;
26import org.eclipse.linuxtools.ctf.core.event.types.Definition;
21fb02fa 27import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition;
a04464b1 28import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition;
a3fc8213
AM
29import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
30import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
31import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration;
32import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition;
33import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
7a6cee1a 34import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
404b264a
GB
35import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition;
36import org.eclipse.linuxtools.internal.tmf.core.Messages;
7a6cee1a 37import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
68b18f2f 38import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
a3fc8213
AM
39
40/**
7558a1e1
AM
41 * The CTF implementation of the TMF event field model
42 *
d4a8d935 43 * @version 2.0
7558a1e1 44 * @author Matthew Khouzam
d09f973b 45 * @author Alexandre Montplaisir
a3fc8213 46 */
68b18f2f 47public abstract class CtfTmfEventField extends TmfEventField {
a3fc8213 48
a3fc8213 49 // ------------------------------------------------------------------------
7558a1e1 50 // Constructor
a3fc8213
AM
51 // ------------------------------------------------------------------------
52
b1baa808 53 /**
7558a1e1
AM
54 * Standard constructor. Only to be used internally, call parseField() to
55 * generate a new field object.
56 *
57 * @param name
58 * The name of this field
68b18f2f
AM
59 * @param value
60 * The value of this field. Its type should match the field type.
81ed27a8
MK
61 * @param fields
62 * The children fields. Useful for composite fields
68b18f2f 63 * @since 2.0
b1baa808 64 */
81ed27a8 65 protected CtfTmfEventField(String name, Object value, ITmfEventField[] fields) {
68b18f2f
AM
66 super(/* Strip the underscore from the field name if there is one */
67 name.startsWith("_") ? name.substring(1) : name, //$NON-NLS-1$
68 value,
81ed27a8 69 fields);
a3fc8213
AM
70 }
71
72 // ------------------------------------------------------------------------
73 // Operations
74 // ------------------------------------------------------------------------
75
b1baa808 76 /**
7558a1e1
AM
77 * Factory method to instantiate CtfTmfEventField objects.
78 *
79 * @param fieldDef
80 * The CTF Definition of this event field
81 * @param fieldName
82 * String The name to assign to this field
83 * @return The resulting CtfTmfEventField object
b1baa808 84 */
a3fc8213
AM
85 public static CtfTmfEventField parseField(Definition fieldDef,
86 String fieldName) {
87 CtfTmfEventField field = null;
88
89 /* Determine the Definition type */
90 if (fieldDef instanceof IntegerDefinition) {
367bcd2b
AM
91 IntegerDefinition intDef = (IntegerDefinition) fieldDef;
92 int base = intDef.getDeclaration().getBase();
459f705b 93 field = new CTFIntegerField(fieldName, intDef.getValue(), base, intDef.getDeclaration().isSigned());
a3fc8213 94
21fb02fa
MK
95 } else if (fieldDef instanceof EnumDefinition) {
96 EnumDefinition enumDef = (EnumDefinition) fieldDef;
68b18f2f 97 field = new CTFEnumField(fieldName, new CtfEnumPair(enumDef.getValue(), enumDef.getIntegerValue()));
21fb02fa 98
a3fc8213 99 } else if (fieldDef instanceof StringDefinition) {
68b18f2f
AM
100 field = new CTFStringField(fieldName, ((StringDefinition) fieldDef).getValue());
101
102 } else if (fieldDef instanceof FloatDefinition) {
103 FloatDefinition floatDef = (FloatDefinition) fieldDef;
104 field = new CTFFloatField(fieldName, floatDef.getValue());
a3fc8213
AM
105
106 } else if (fieldDef instanceof ArrayDefinition) {
107 ArrayDefinition arrayDef = (ArrayDefinition) fieldDef;
a3fc8213 108
9639bf3a 109 if (arrayDef.getDeclaration().isString()) {
a3fc8213 110 /* This is an array of UTF-8 bytes, a.k.a. a String! */
68b18f2f 111 field = new CTFStringField(fieldName, fieldDef.toString());
a3fc8213 112
4591bed9
FD
113 } else {
114 /* Arrays of elements of any other type */
115 Definition[] definitions = arrayDef.getDefinitions();
116 CtfTmfEventField[] elements = new CtfTmfEventField[definitions.length];
117
118 /* Parse the elements of the array. */
119 for (int i = 0; i < definitions.length; i++) {
120 CtfTmfEventField curField = CtfTmfEventField.parseField(
121 definitions[i], fieldName + '[' + i + ']');
122 elements[i] = curField;
a3fc8213 123 }
a3fc8213 124
4591bed9
FD
125 field = new CTFArrayField(fieldName, elements);
126 }
a3fc8213
AM
127 } else if (fieldDef instanceof SequenceDefinition) {
128 SequenceDefinition seqDef = (SequenceDefinition) fieldDef;
129 SequenceDeclaration seqDecl = seqDef.getDeclaration();
130
131 if (seqDef.getLength() == 0) {
132 /* Some sequences have length = 0. Simply use an empty string */
68b18f2f 133 field = new CTFStringField(fieldName, ""); //$NON-NLS-1$
a3fc8213
AM
134 } else if (seqDef.isString()) {
135 /* Interpret this sequence as a String */
68b18f2f 136 field = new CTFStringField(fieldName, seqDef.toString());
a3fc8213
AM
137 } else if (seqDecl.getElementType() instanceof IntegerDeclaration) {
138 /* Sequence of integers => CTFIntegerArrayField */
0d69d993 139 long[] values = new long[seqDef.getLength()];
a3fc8213 140 for (int i = 0; i < seqDef.getLength(); i++) {
0d69d993 141 values[i] = ((IntegerDefinition) seqDef.getElem(i)).getValue();
a3fc8213 142 }
0d69d993 143 field = new CTFIntegerArrayField(fieldName, values,
cefe3edf 144 ((IntegerDeclaration) seqDecl.getElementType()).getBase(),
459f705b 145 ((IntegerDeclaration) seqDecl.getElementType()).isSigned());
0d69d993 146
a3fc8213
AM
147 }
148 /* Add other Sequence types here */
367bcd2b 149
7a6cee1a
GB
150 } else if (fieldDef instanceof StructDefinition) {
151 StructDefinition strDef = (StructDefinition) fieldDef;
152
153 String curFieldName = null;
154 Definition curFieldDef;
155 CtfTmfEventField curField;
a4524c1b 156 List<ITmfEventField> list = new ArrayList<>();
7a6cee1a
GB
157 /* Recursively parse the fields */
158 for (Entry<String, Definition> entry : strDef.getDefinitions().entrySet()) {
159 curFieldName = entry.getKey();
160 curFieldDef = entry.getValue();
161 curField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
162 list.add(curField);
163 }
164 field = new CTFStructField(fieldName, list.toArray(new CtfTmfEventField[list.size()]));
a0e9eac8 165
404b264a
GB
166 } else if (fieldDef instanceof VariantDefinition) {
167 VariantDefinition varDef = (VariantDefinition) fieldDef;
168
169 String curFieldName = varDef.getCurrentFieldName();
170 Definition curFieldDef = varDef.getDefinitions().get(curFieldName);
171 if (curFieldDef != null) {
51cc7ef4
GB
172 CtfTmfEventField subField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
173 field = new CTFVariantField(fieldName, subField);
404b264a
GB
174 } else {
175 /* A safe-guard, but curFieldDef should never be null */
176 field = new CTFStringField(curFieldName, ""); //$NON-NLS-1$
177 }
178
179 } else {
459f705b
JCK
180 /*
181 * Safe-guard, to avoid null exceptions later, field is expected not
182 * to be null
183 */
404b264a 184 field = new CTFStringField(fieldName, Messages.TmfEventField_UnsupportedType + fieldDef.getClass().toString());
a3fc8213 185 }
a3fc8213
AM
186 return field;
187 }
188
a3fc8213 189 @Override
68b18f2f 190 public String toString() {
51cc7ef4 191 return getName() + '=' + getFormattedValue();
a3fc8213
AM
192 }
193
a3fc8213
AM
194}
195
196/**
7558a1e1
AM
197 * The CTF field implementation for integer fields.
198 *
199 * @author alexmont
a3fc8213
AM
200 */
201final class CTFIntegerField extends CtfTmfEventField {
202
6fc4ce56
MK
203 private final int fBase;
204 private final boolean fSigned;
a3fc8213
AM
205
206 /**
207 * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
208 * Java parser this is interpreted as a long.
7558a1e1 209 *
7558a1e1
AM
210 * @param name
211 * The name of this field
459f705b
JCK
212 * @param longValue
213 * The integer value of this field
214 * @param signed
215 * Is the value signed or not
a3fc8213 216 */
459f705b 217 CTFIntegerField(String name, long longValue, int base, boolean signed) {
81ed27a8 218 super(name, longValue, null);
6fc4ce56
MK
219 fSigned = signed;
220 fBase = base;
367bcd2b
AM
221 }
222
a3fc8213
AM
223 @Override
224 public Long getValue() {
68b18f2f 225 return (Long) super.getValue();
a3fc8213
AM
226 }
227
8f86c552
GB
228 @Override
229 public String getFormattedValue() {
6fc4ce56 230 return IntegerDefinition.formatNumber(getValue(), fBase, fSigned);
8f86c552
GB
231 }
232
a3fc8213
AM
233}
234
235/**
7558a1e1
AM
236 * The CTF field implementation for string fields
237 *
238 * @author alexmont
a3fc8213
AM
239 */
240final class CTFStringField extends CtfTmfEventField {
241
b1baa808
MK
242 /**
243 * Constructor for CTFStringField.
7558a1e1
AM
244 *
245 * @param strValue
246 * The string value of this field
247 * @param name
248 * The name of this field
b1baa808 249 */
68b18f2f 250 CTFStringField(String name, String strValue) {
81ed27a8 251 super(name, strValue, null);
a3fc8213
AM
252 }
253
a3fc8213
AM
254 @Override
255 public String getValue() {
68b18f2f 256 return (String) super.getValue();
a3fc8213
AM
257 }
258}
259
260/**
7558a1e1
AM
261 * CTF field implementation for arrays of integers.
262 *
263 * @author alexmont
a3fc8213
AM
264 */
265final class CTFIntegerArrayField extends CtfTmfEventField {
266
6fc4ce56
MK
267 private final int fBase;
268 private final boolean fSigned;
269 private String fFormattedValue = null;
404b264a 270
b1baa808
MK
271 /**
272 * Constructor for CTFIntegerArrayField.
7558a1e1 273 *
459f705b
JCK
274 * @param name
275 * The name of this field
7558a1e1
AM
276 * @param longValues
277 * The array of integers (as longs) that compose this field's
278 * value
459f705b
JCK
279 * @param signed
280 * Are the values in the array signed or not
b1baa808 281 */
cefe3edf 282 CTFIntegerArrayField(String name, long[] longValues, int base, boolean signed) {
81ed27a8 283 super(name, longValues, null);
6fc4ce56
MK
284 fBase = base;
285 fSigned = signed;
a3fc8213
AM
286 }
287
a6223d74 288 @Override
cefe3edf
AM
289 public long[] getValue() {
290 return (long[]) super.getValue();
a3fc8213 291 }
404b264a 292
8f86c552 293 @Override
4591bed9 294 public synchronized String getFormattedValue() {
6fc4ce56 295 if (fFormattedValue == null) {
a4524c1b 296 List<String> strings = new ArrayList<>();
cefe3edf 297 for (long value : getValue()) {
6fc4ce56 298 strings.add(IntegerDefinition.formatNumber(value, fBase, fSigned));
8f86c552 299 }
6fc4ce56 300 fFormattedValue = strings.toString();
8f86c552 301 }
6fc4ce56 302 return fFormattedValue;
8f86c552
GB
303 }
304
a3fc8213
AM
305}
306
4591bed9
FD
307/**
308 * CTF field implementation for arrays of arbitrary types.
309 *
310 * @author fdoray
311 */
312final class CTFArrayField extends CtfTmfEventField {
313
6fc4ce56 314 private String fFormattedValue = null;
4591bed9
FD
315
316 /**
317 * Constructor for CTFArrayField.
318 *
319 * @param name
320 * The name of this field
321 * @param elements
322 * The array elements of this field
323 */
324 CTFArrayField(String name, CtfTmfEventField[] elements) {
325 super(name, elements, elements);
326 }
327
328 @Override
329 public CtfTmfEventField[] getValue() {
330 return (CtfTmfEventField[]) super.getValue();
331 }
332
333 @Override
334 public synchronized String getFormattedValue() {
6fc4ce56 335 if (fFormattedValue == null) {
a4524c1b 336 List<String> strings = new ArrayList<>();
4591bed9
FD
337 for (CtfTmfEventField element : getValue()) {
338 strings.add(element.getFormattedValue());
339 }
6fc4ce56 340 fFormattedValue = strings.toString();
4591bed9 341 }
6fc4ce56 342 return fFormattedValue;
4591bed9
FD
343 }
344}
345
b1baa808 346/**
7558a1e1
AM
347 * CTF field implementation for floats.
348 *
349 * @author emathko
b1baa808 350 */
a04464b1
MK
351final class CTFFloatField extends CtfTmfEventField {
352
b1baa808
MK
353 /**
354 * Constructor for CTFFloatField.
7558a1e1
AM
355 *
356 * @param value
357 * The float value (actually a double) of this field
358 * @param name
359 * The name of this field
b1baa808 360 */
68b18f2f 361 protected CTFFloatField(String name, double value) {
81ed27a8 362 super(name, value, null);
a04464b1
MK
363 }
364
a04464b1 365 @Override
81c8e6f7 366 public Double getValue() {
68b18f2f 367 return (Double) super.getValue();
a04464b1 368 }
a04464b1 369}
7558a1e1 370
d4a8d935
BH
371/**
372 * The CTF field implementation for Enum fields
373 *
374 * @author Bernd Hufmann
375 */
376final class CTFEnumField extends CtfTmfEventField {
377
d4a8d935
BH
378 /**
379 * Constructor for CTFEnumField.
380 *
381 * @param enumValue
459f705b
JCK
382 * The Enum value consisting of a pair of Enum value name and its
383 * long value
d4a8d935
BH
384 * @param name
385 * The name of this field
386 */
68b18f2f
AM
387 CTFEnumField(String name, CtfEnumPair enumValue) {
388 super(name, new CtfEnumPair(enumValue.getFirst(),
0126a8ca 389 enumValue.getSecond()), null);
d4a8d935
BH
390 }
391
d4a8d935 392 @Override
68b18f2f
AM
393 public CtfEnumPair getValue() {
394 return (CtfEnumPair) super.getValue();
d4a8d935
BH
395 }
396}
397
7a6cee1a 398/**
51cc7ef4 399 * The CTF field implementation for struct fields with sub-fields
7a6cee1a
GB
400 *
401 * @author gbastien
402 */
403final class CTFStructField extends CtfTmfEventField {
404
405 /**
51cc7ef4 406 * Constructor for CTFStructField.
7a6cee1a 407 *
51cc7ef4
GB
408 * @param fields
409 * The children of this field
7a6cee1a
GB
410 * @param name
411 * The name of this field
412 */
413 CTFStructField(String name, CtfTmfEventField[] fields) {
81ed27a8 414 super(name, fields, fields);
7a6cee1a
GB
415 }
416
7a6cee1a
GB
417 @Override
418 public CtfTmfEventField[] getValue() {
419 return (CtfTmfEventField[]) super.getValue();
420 }
421
422 @Override
51cc7ef4
GB
423 public String getFormattedValue() {
424 return Arrays.toString(getValue());
7a6cee1a 425 }
51cc7ef4
GB
426
427}
428
429/**
430 * The CTF field implementation for variant fields its child
431 *
432 * @author gbastien
433 */
434final class CTFVariantField extends CtfTmfEventField {
435
436 /**
437 * Constructor for CTFVariantField.
438 *
439 * @param field
440 * The field selected for this variant
441 * @param name
442 * The name of this field
443 */
444 CTFVariantField(String name, CtfTmfEventField field) {
459f705b 445 super(name, field, new CtfTmfEventField[] { field });
51cc7ef4
GB
446 }
447
448 @Override
449 public CtfTmfEventField getValue() {
450 return (CtfTmfEventField) super.getValue();
451 }
452
7a6cee1a
GB
453}
454
a3fc8213 455/* Implement other possible fields types here... */
This page took 0.074554 seconds and 5 git commands to generate.