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
.tracecompass
.tmf
.ctf
.core
;
15 import org
.eclipse
.jdt
.annotation
.NonNull
;
16 import org
.eclipse
.tracecompass
.ctf
.core
.CTFStrings
;
17 import org
.eclipse
.tracecompass
.ctf
.core
.event
.EventDefinition
;
18 import org
.eclipse
.tracecompass
.ctf
.core
.event
.IEventDeclaration
;
19 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDefinition
;
20 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDefinition
;
21 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
22 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
25 * Factory for CtfTmfEvent's.
27 * This code was moved out of CtfTmfEvent to provide better separation between
28 * the parsing/instantiation of events, and the usual TMF API implementations.
30 * @author Alexandre Montplaisir
33 public final class CtfTmfEventFactory
{
35 private static final String NO_STREAM
= "No stream"; //$NON-NLS-1$
38 * Don't let anyone instantiate this class.
40 private CtfTmfEventFactory() {}
43 * Factory method to instantiate new {@link CtfTmfEvent}'s.
46 * CTF EventDefinition object corresponding to this trace event
48 * The path to the trace file
50 * The trace from which this event originates
51 * @return The newly-built CtfTmfEvent
53 public static CtfTmfEvent
createEvent(EventDefinition eventDef
,
54 String fileName
, CtfTmfTrace originTrace
) {
56 /* Prepare what to pass to CtfTmfEvent's constructor */
57 final IEventDeclaration eventDecl
= eventDef
.getDeclaration();
58 final long ts
= eventDef
.getTimestamp();
59 final CtfTmfTimestamp timestamp
= originTrace
.createTimestamp(
60 originTrace
.getCTFTrace().timestampCyclesToNanos(ts
));
62 int sourceCPU
= eventDef
.getCPU();
64 String reference
= fileName
== null ? NO_STREAM
: fileName
;
66 /* Handle the special case of lost events */
67 if (eventDecl
.getName().equals(CTFStrings
.LOST_EVENT_NAME
)) {
68 IDefinition nbLostEventsDef
= eventDef
.getFields().getDefinition(CTFStrings
.LOST_EVENTS_FIELD
);
69 IDefinition durationDef
= eventDef
.getFields().getDefinition(CTFStrings
.LOST_EVENTS_DURATION
);
70 if (!(nbLostEventsDef
instanceof IntegerDefinition
) || !(durationDef
instanceof IntegerDefinition
)) {
72 * One or both of these fields doesn't exist, or is not of the
73 * right type. The event claims to be a "lost event", but is
74 * malformed. Log it and return a null event instead.
76 return getNullEvent(originTrace
);
78 long nbLostEvents
= ((IntegerDefinition
) nbLostEventsDef
).getValue();
79 long duration
= ((IntegerDefinition
) durationDef
).getValue();
80 CtfTmfTimestamp timestampEnd
= new CtfTmfTimestamp(
81 originTrace
.getCTFTrace().timestampCyclesToNanos(ts
) + duration
);
83 CtfTmfLostEvent lostEvent
= new CtfTmfLostEvent(originTrace
,
84 ITmfContext
.UNKNOWN_RANK
,
85 reference
, // filename
88 new TmfTimeRange(timestamp
, timestampEnd
),
94 /* Handle standard event types */
95 CtfTmfEvent event
= new CtfTmfEvent(
97 ITmfContext
.UNKNOWN_RANK
,
99 reference
, // filename
106 /* Singleton instance of a null event */
107 private static CtfTmfEvent nullEvent
= null;
110 * Get an instance of a null event.
113 * A trace to associate with this null event
114 * @return An empty event
116 public static CtfTmfEvent
getNullEvent(@NonNull CtfTmfTrace trace
) {
117 if (nullEvent
== null) {
118 nullEvent
= new CtfTmfEvent(trace
);