21fe14cf362229b2e4fc6b1f00760652e8dd587b
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ctf.core / src / org / eclipse / tracecompass / tmf / ctf / core / event / CtfTmfEventFactory.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2015 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.tracecompass.tmf.ctf.core.event;
14
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.TmfNanoTimestamp;
22 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
23 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
24 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
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
33 */
34 public final class CtfTmfEventFactory {
35
36 private static final String NO_STREAM = "No stream"; //$NON-NLS-1$
37
38 /**
39 * Don't let anyone instantiate this class.
40 */
41 private CtfTmfEventFactory() {}
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 */
58 final IEventDeclaration eventDecl = eventDef.getDeclaration();
59 final long ts = eventDef.getTimestamp();
60 final TmfNanoTimestamp timestamp = originTrace.createTimestamp(
61 originTrace.timestampCyclesToNanos(ts));
62
63 int sourceCPU = eventDef.getCPU();
64
65 String reference = fileName == null ? NO_STREAM : fileName;
66
67 /* Handle the special case of lost events */
68 if (eventDecl.getName().equals(CTFStrings.LOST_EVENT_NAME)) {
69 IDefinition nbLostEventsDef = eventDef.getFields().getDefinition(CTFStrings.LOST_EVENTS_FIELD);
70 IDefinition durationDef = eventDef.getFields().getDefinition(CTFStrings.LOST_EVENTS_DURATION);
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 */
77 return getNullEvent(originTrace);
78 }
79 long nbLostEvents = ((IntegerDefinition) nbLostEventsDef).getValue();
80 long duration = ((IntegerDefinition) durationDef).getValue();
81 TmfNanoTimestamp timestampEnd = new TmfNanoTimestamp(
82 originTrace.timestampCyclesToNanos(ts) + duration);
83
84 CtfTmfLostEvent lostEvent = new CtfTmfLostEvent(originTrace,
85 ITmfContext.UNKNOWN_RANK,
86 reference, // filename
87 sourceCPU,
88 eventDecl,
89 new TmfTimeRange(timestamp, timestampEnd),
90 nbLostEvents,
91 eventDef);
92 return lostEvent;
93 }
94
95 /* Handle standard event types */
96 CtfTmfEvent event = new CtfTmfEvent(
97 originTrace,
98 ITmfContext.UNKNOWN_RANK,
99 timestamp,
100 reference, // filename
101 sourceCPU,
102 eventDecl,
103 eventDef);
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 *
113 * @param trace
114 * A trace to associate with this null event
115 * @return An empty event
116 */
117 public static CtfTmfEvent getNullEvent(@NonNull CtfTmfTrace trace) {
118 if (nullEvent == null) {
119 nullEvent = new CtfTmfEvent(trace);
120 }
121 return nullEvent;
122 }
123
124
125 }
This page took 0.043899 seconds and 4 git commands to generate.