tmf: keep event fields ordering as per ctf metadata
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfEventFactory.java
1 /*******************************************************************************
2 * Copyright (c) 2013 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:
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
14
15 import java.util.ArrayList;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Map.Entry;
20
21 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
22 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
23 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
24 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
25 import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
26 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
27
28 /**
29 * Factory for CtfTmfEvent's.
30 *
31 * This code was moved out of CtfTmfEvent to provide better separation between
32 * the parsing/instantiation of events, and the usual TMF API implementations.
33 *
34 * @author Alexandre Montplaisir
35 * @since 2.0
36 */
37 public final class CtfTmfEventFactory {
38
39 /**
40 * Don't let anyone instantiate this class.
41 */
42 private CtfTmfEventFactory() {}
43
44 /**
45 * Factory method to instantiate new {@link CtfTmfEvent}'s.
46 *
47 * @param eventDef
48 * CTF EventDefinition object corresponding to this trace event
49 * @param fileName
50 * The path to the trace file
51 * @param originTrace
52 * The trace from which this event originates
53 * @return The newly-built CtfTmfEvent
54 */
55 public static CtfTmfEvent createEvent(EventDefinition eventDef,
56 String fileName, CtfTmfTrace originTrace) {
57
58 /* Prepare what to pass to CtfTmfEvent's constructor */
59 long ts = eventDef.getTimestamp();
60 CtfTmfTimestamp timestamp = new CtfTmfTimestamp(
61 originTrace.getCTFTrace().timestampCyclesToNanos(ts));
62
63 int sourceCPU = eventDef.getCPU();
64
65 ITmfEventField content = new TmfEventField(
66 ITmfEventField.ROOT_FIELD_ID, parseFields(eventDef));
67
68 String reference = fileName == null ? CtfTmfEvent.NO_STREAM : fileName;
69
70 /* Construct and return the object */
71 CtfTmfEvent event = new CtfTmfEvent(
72 originTrace,
73 ITmfContext.UNKNOWN_RANK,
74 timestamp,
75 content,
76 reference,
77 sourceCPU,
78 eventDef.getDeclaration()
79 );
80 return event;
81 }
82
83 /* Singleton instance of a null event */
84 private static CtfTmfEvent nullEvent = null;
85
86 /**
87 * Get an instance of a null event.
88 *
89 * @return An empty event
90 */
91 public static CtfTmfEvent getNullEvent() {
92 if (nullEvent == null) {
93 nullEvent = new CtfTmfEvent();
94 }
95 return nullEvent;
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 private static CtfTmfEventField[] parseFields(EventDefinition eventDef) {
103 List<CtfTmfEventField> fields = new ArrayList<CtfTmfEventField>();
104
105 StructDefinition structFields = eventDef.getFields();
106 Map<String, Definition> definitions = structFields.getDefinitions();
107 String curFieldName = null;
108 Definition curFieldDef;
109 CtfTmfEventField curField;
110 Iterator<Entry<String, Definition>> it = definitions.entrySet().iterator();
111 while(it.hasNext()) {
112 Entry<String, Definition> entry = it.next();
113 curFieldName = entry.getKey();
114 curFieldDef = entry.getValue();
115 curField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
116 fields.add(curField);
117 }
118
119 /* Add context information as CtfTmfEventField */
120 StructDefinition structContext = eventDef.getContext();
121 if (structContext != null) {
122 definitions = structContext.getDefinitions();
123 String curContextName;
124 Definition curContextDef;
125 CtfTmfEventField curContext;
126 it = definitions.entrySet().iterator();
127 while(it.hasNext()) {
128 Entry<String, Definition> entry = it.next();
129 /* Prefix field name */
130 curContextName = CtfConstants.CONTEXT_FIELD_PREFIX + entry.getKey();
131 curContextDef = entry.getValue();
132 curContext = CtfTmfEventField.parseField(curContextDef, curContextName);
133 fields.add(curContext);
134 }
135 }
136 return fields.toArray(new CtfTmfEventField[fields.size()]);
137 }
138 }
This page took 0.033954 seconds and 6 git commands to generate.