Commit | Line | Data |
---|---|---|
6cfa0200 | 1 | /******************************************************************************* |
da707390 | 2 | * Copyright (c) 2013, 2015 Ericsson |
6cfa0200 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 | * | |
9 | * Contributors: | |
10 | * Alexandre Montplaisir - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
9722e5d7 | 13 | package org.eclipse.tracecompass.tmf.ctf.core.event; |
6cfa0200 | 14 | |
ca5b04ad | 15 | import org.eclipse.jdt.annotation.NonNull; |
f357bcd4 AM |
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; | |
da707390 | 21 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp; |
2bdf0193 AM |
22 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange; |
23 | import org.eclipse.tracecompass.tmf.core.trace.ITmfContext; | |
9722e5d7 | 24 | import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace; |
6cfa0200 AM |
25 | |
26 | /** | |
27 | * Factory for CtfTmfEvent's. | |
28 | * | |
29 | * This code was moved out of CtfTmfEvent to provide better separation between | |
30 | * the parsing/instantiation of events, and the usual TMF API implementations. | |
31 | * | |
32 | * @author Alexandre Montplaisir | |
6cfa0200 | 33 | */ |
60fb38b8 | 34 | public final class CtfTmfEventFactory { |
6cfa0200 | 35 | |
a4fa4e36 MK |
36 | private static final String NO_STREAM = "No stream"; //$NON-NLS-1$ |
37 | ||
60fb38b8 PT |
38 | /** |
39 | * Don't let anyone instantiate this class. | |
40 | */ | |
41 | private CtfTmfEventFactory() {} | |
6cfa0200 AM |
42 | |
43 | /** | |
44 | * Factory method to instantiate new {@link CtfTmfEvent}'s. | |
45 | * | |
46 | * @param eventDef | |
47 | * CTF EventDefinition object corresponding to this trace event | |
48 | * @param fileName | |
49 | * The path to the trace file | |
50 | * @param originTrace | |
51 | * The trace from which this event originates | |
52 | * @return The newly-built CtfTmfEvent | |
53 | */ | |
54 | public static CtfTmfEvent createEvent(EventDefinition eventDef, | |
55 | String fileName, CtfTmfTrace originTrace) { | |
56 | ||
57 | /* Prepare what to pass to CtfTmfEvent's constructor */ | |
c26d0fe0 AM |
58 | final IEventDeclaration eventDecl = eventDef.getDeclaration(); |
59 | final long ts = eventDef.getTimestamp(); | |
da707390 | 60 | final TmfNanoTimestamp timestamp = originTrace.createTimestamp( |
fe71057b | 61 | originTrace.timestampCyclesToNanos(ts)); |
6cfa0200 AM |
62 | |
63 | int sourceCPU = eventDef.getCPU(); | |
64 | ||
a4fa4e36 | 65 | String reference = fileName == null ? NO_STREAM : fileName; |
6cfa0200 | 66 | |
c26d0fe0 AM |
67 | /* Handle the special case of lost events */ |
68 | if (eventDecl.getName().equals(CTFStrings.LOST_EVENT_NAME)) { | |
cc98c947 MK |
69 | IDefinition nbLostEventsDef = eventDef.getFields().getDefinition(CTFStrings.LOST_EVENTS_FIELD); |
70 | IDefinition durationDef = eventDef.getFields().getDefinition(CTFStrings.LOST_EVENTS_DURATION); | |
c26d0fe0 AM |
71 | if (!(nbLostEventsDef instanceof IntegerDefinition) || !(durationDef instanceof IntegerDefinition)) { |
72 | /* | |
73 | * One or both of these fields doesn't exist, or is not of the | |
74 | * right type. The event claims to be a "lost event", but is | |
75 | * malformed. Log it and return a null event instead. | |
76 | */ | |
ca5b04ad | 77 | return getNullEvent(originTrace); |
c26d0fe0 AM |
78 | } |
79 | long nbLostEvents = ((IntegerDefinition) nbLostEventsDef).getValue(); | |
80 | long duration = ((IntegerDefinition) durationDef).getValue(); | |
da707390 | 81 | TmfNanoTimestamp timestampEnd = new TmfNanoTimestamp( |
fe71057b | 82 | originTrace.timestampCyclesToNanos(ts) + duration); |
c26d0fe0 AM |
83 | |
84 | CtfTmfLostEvent lostEvent = new CtfTmfLostEvent(originTrace, | |
85 | ITmfContext.UNKNOWN_RANK, | |
c26d0fe0 AM |
86 | reference, // filename |
87 | sourceCPU, | |
88 | eventDecl, | |
89 | new TmfTimeRange(timestamp, timestampEnd), | |
a4fa4e36 MK |
90 | nbLostEvents, |
91 | eventDef); | |
c26d0fe0 AM |
92 | return lostEvent; |
93 | } | |
94 | ||
95 | /* Handle standard event types */ | |
6cfa0200 AM |
96 | CtfTmfEvent event = new CtfTmfEvent( |
97 | originTrace, | |
98 | ITmfContext.UNKNOWN_RANK, | |
99 | timestamp, | |
c26d0fe0 | 100 | reference, // filename |
6cfa0200 | 101 | sourceCPU, |
a4fa4e36 MK |
102 | eventDecl, |
103 | eventDef); | |
6cfa0200 AM |
104 | return event; |
105 | } | |
106 | ||
107 | /* Singleton instance of a null event */ | |
108 | private static CtfTmfEvent nullEvent = null; | |
109 | ||
110 | /** | |
111 | * Get an instance of a null event. | |
112 | * | |
ca5b04ad GB |
113 | * @param trace |
114 | * A trace to associate with this null event | |
6cfa0200 AM |
115 | * @return An empty event |
116 | */ | |
ca5b04ad | 117 | public static CtfTmfEvent getNullEvent(@NonNull CtfTmfTrace trace) { |
6cfa0200 | 118 | if (nullEvent == null) { |
ca5b04ad | 119 | nullEvent = new CtfTmfEvent(trace); |
6cfa0200 AM |
120 | } |
121 | return nullEvent; | |
122 | } | |
123 | ||
437bb7c0 | 124 | |
6cfa0200 | 125 | } |