1 /*******************************************************************************
2 * Copyright (c) 2013 Ericsson
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
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
;
15 import java
.util
.ArrayList
;
16 import java
.util
.List
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.CTFStrings
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.IEventDeclaration
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
31 * Factory for CtfTmfEvent's.
33 * This code was moved out of CtfTmfEvent to provide better separation between
34 * the parsing/instantiation of events, and the usual TMF API implementations.
36 * @author Alexandre Montplaisir
39 public final class CtfTmfEventFactory
{
42 * Don't let anyone instantiate this class.
44 private CtfTmfEventFactory() {}
47 * Factory method to instantiate new {@link CtfTmfEvent}'s.
50 * CTF EventDefinition object corresponding to this trace event
52 * The path to the trace file
54 * The trace from which this event originates
55 * @return The newly-built CtfTmfEvent
57 public static CtfTmfEvent
createEvent(EventDefinition eventDef
,
58 String fileName
, CtfTmfTrace originTrace
) {
60 /* Prepare what to pass to CtfTmfEvent's constructor */
61 final IEventDeclaration eventDecl
= eventDef
.getDeclaration();
62 final long ts
= eventDef
.getTimestamp();
63 final CtfTmfTimestamp timestamp
= originTrace
.createTimestamp(
64 originTrace
.getCTFTrace().timestampCyclesToNanos(ts
));
66 int sourceCPU
= eventDef
.getCPU();
68 ITmfEventField content
= new TmfEventField(
69 ITmfEventField
.ROOT_FIELD_ID
, null, parseFields(eventDef
));
71 String reference
= fileName
== null ? CtfTmfEvent
.NO_STREAM
: fileName
;
73 /* Handle the special case of lost events */
74 if (eventDecl
.getName().equals(CTFStrings
.LOST_EVENT_NAME
)) {
75 Definition nbLostEventsDef
= eventDef
.getFields().getDefinitions().get(CTFStrings
.LOST_EVENTS_FIELD
);
76 Definition durationDef
= eventDef
.getFields().getDefinitions().get(CTFStrings
.LOST_EVENTS_DURATION
);
77 if (!(nbLostEventsDef
instanceof IntegerDefinition
) || !(durationDef
instanceof IntegerDefinition
)) {
79 * One or both of these fields doesn't exist, or is not of the
80 * right type. The event claims to be a "lost event", but is
81 * malformed. Log it and return a null event instead.
83 return getNullEvent();
85 long nbLostEvents
= ((IntegerDefinition
) nbLostEventsDef
).getValue();
86 long duration
= ((IntegerDefinition
) durationDef
).getValue();
87 CtfTmfTimestamp timestampEnd
= new CtfTmfTimestamp(
88 originTrace
.getCTFTrace().timestampCyclesToNanos(ts
) + duration
);
90 CtfTmfLostEvent lostEvent
= new CtfTmfLostEvent(originTrace
,
91 ITmfContext
.UNKNOWN_RANK
,
93 reference
, // filename
96 new TmfTimeRange(timestamp
, timestampEnd
),
101 /* Handle standard event types */
102 CtfTmfEvent event
= new CtfTmfEvent(
104 ITmfContext
.UNKNOWN_RANK
,
107 reference
, // filename
113 /* Singleton instance of a null event */
114 private static CtfTmfEvent nullEvent
= null;
117 * Get an instance of a null event.
119 * @return An empty event
121 public static CtfTmfEvent
getNullEvent() {
122 if (nullEvent
== null) {
123 nullEvent
= new CtfTmfEvent();
129 * Extract the field information from the structDefinition haze-inducing
130 * mess, and put them into something ITmfEventField can cope with.
132 private static CtfTmfEventField
[] parseFields(EventDefinition eventDef
) {
133 List
<CtfTmfEventField
> fields
= new ArrayList
<CtfTmfEventField
>();
135 StructDefinition structFields
= eventDef
.getFields();
136 for (Map
.Entry
<String
, Definition
> entry
: structFields
.getDefinitions().entrySet()) {
137 String curFieldName
= entry
.getKey();
138 Definition curFieldDef
= entry
.getValue();
139 CtfTmfEventField curField
= CtfTmfEventField
.parseField(curFieldDef
, curFieldName
);
140 fields
.add(curField
);
143 /* Add context information as CtfTmfEventField */
144 StructDefinition structContext
= eventDef
.getContext();
145 if (structContext
!= null) {
146 for (Map
.Entry
<String
, Definition
> entry
: structContext
.getDefinitions().entrySet()) {
147 /* Prefix field name */
148 String curContextName
= CtfConstants
.CONTEXT_FIELD_PREFIX
+ entry
.getKey();
149 Definition curContextDef
= entry
.getValue();
150 CtfTmfEventField curContext
= CtfTmfEventField
.parseField(curContextDef
, curContextName
);
151 fields
.add(curContext
);
155 return fields
.toArray(new CtfTmfEventField
[fields
.size()]);