1 /*******************************************************************************
2 * Copyright (c) 2013, 2015 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
.tracecompass
.tmf
.ctf
.core
.event
;
15 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
16 import org
.eclipse
.jdt
.annotation
.Nullable
;
17 import org
.eclipse
.tracecompass
.ctf
.core
.CTFStrings
;
18 import org
.eclipse
.tracecompass
.ctf
.core
.event
.EventDefinition
;
19 import org
.eclipse
.tracecompass
.ctf
.core
.event
.IEventDeclaration
;
20 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDefinition
;
21 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDefinition
;
22 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfNanoTimestamp
;
23 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
25 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.CtfTmfTrace
;
28 * Factory for CtfTmfEvent's.
30 * This code was moved out of CtfTmfEvent to provide better separation between
31 * the parsing/instantiation of events, and the usual TMF API implementations.
33 * @author Alexandre Montplaisir
36 public class CtfTmfEventFactory
{
38 private static final CtfTmfEventFactory INSTANCE
= new CtfTmfEventFactory();
41 * The file name to use when none is specified.
47 protected static final String NO_STREAM
= "No stream"; //$NON-NLS-1$
50 * Protected constructor, only for use by sub-classes. Users should call
51 * the {@link #instance()} method instead.
55 protected CtfTmfEventFactory() {}
58 * Get the singleton factory instance
60 * @return The instance
63 public static CtfTmfEventFactory
instance() {
68 * Factory method to instantiate new CTF events.
71 * The trace to which the new event will belong
73 * CTF EventDefinition object corresponding to this trace event
75 * The path to the trace file
76 * @return The newly-built CtfTmfEvent
79 public CtfTmfEvent
createEvent(CtfTmfTrace trace
, EventDefinition eventDef
, @Nullable String fileName
) {
81 /* Prepare what to pass to CtfTmfEvent's constructor */
82 final IEventDeclaration eventDecl
= eventDef
.getDeclaration();
83 final long ts
= eventDef
.getTimestamp();
84 final TmfNanoTimestamp timestamp
= trace
.createTimestamp(trace
.timestampCyclesToNanos(ts
));
86 int sourceCPU
= eventDef
.getCPU();
88 String reference
= (fileName
== null ? NO_STREAM
: fileName
);
90 /* Handle the special case of lost events */
91 if (eventDecl
.getName().equals(CTFStrings
.LOST_EVENT_NAME
)) {
92 return createLostEvent(trace
, eventDef
, eventDecl
, ts
, timestamp
, sourceCPU
, reference
);
95 /* Handle standard event types */
96 return new CtfTmfEvent(trace
,
97 ITmfContext
.UNKNOWN_RANK
,
99 reference
, // filename
106 * Create a new CTF lost event.
109 * The trace to which the new event will belong
111 * The CTF event definition
113 * The CTF event declaration
115 * The event's timestamp
117 * The event's timestamp (FIXME again??)
122 * @return The new lost event
125 protected static CtfTmfEvent
createLostEvent(CtfTmfTrace trace
,
126 EventDefinition eventDef
,
127 final IEventDeclaration eventDecl
,
129 final TmfNanoTimestamp timestamp
,
133 IDefinition nbLostEventsDef
= eventDef
.getFields().getDefinition(CTFStrings
.LOST_EVENTS_FIELD
);
134 IDefinition durationDef
= eventDef
.getFields().getDefinition(CTFStrings
.LOST_EVENTS_DURATION
);
135 if (!(nbLostEventsDef
instanceof IntegerDefinition
) || !(durationDef
instanceof IntegerDefinition
)) {
137 * One or both of these fields doesn't exist, or is not of the right
138 * type. The event claims to be a "lost event", but is malformed.
139 * Log it and return a null event instead.
141 return getNullEvent(trace
);
143 long nbLostEvents
= ((IntegerDefinition
) nbLostEventsDef
).getValue();
144 long duration
= ((IntegerDefinition
) durationDef
).getValue();
145 TmfNanoTimestamp timestampEnd
= trace
.createTimestamp(
146 trace
.timestampCyclesToNanos(ts
) + duration
);
148 CtfTmfLostEvent lostEvent
= new CtfTmfLostEvent(trace
,
149 ITmfContext
.UNKNOWN_RANK
,
153 new TmfTimeRange(timestamp
, timestampEnd
),
160 * Get an instance of a null event.
163 * The trace to which the new null event will belong
164 * @return An empty event
167 public static CtfTmfEvent
getNullEvent(CtfTmfTrace trace
) {
168 return new CtfTmfEvent(trace
);