Merge branch 'master' into lttng-luna
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfEventFactory.java
CommitLineData
6cfa0200
AM
1/*******************************************************************************
2 * Copyright (c) 2013 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
13package org.eclipse.linuxtools.tmf.core.ctfadaptor;
14
15import java.util.ArrayList;
6cfa0200 16import java.util.List;
843f986b 17import java.util.Map;
6cfa0200 18
6cfa0200
AM
19import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
20import org.eclipse.linuxtools.ctf.core.event.types.Definition;
6cfa0200
AM
21import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
22import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
23import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
24import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
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 * @since 2.0
34 */
60fb38b8 35public final class CtfTmfEventFactory {
6cfa0200 36
60fb38b8
PT
37 /**
38 * Don't let anyone instantiate this class.
39 */
40 private CtfTmfEventFactory() {}
6cfa0200
AM
41
42 /**
43 * Factory method to instantiate new {@link CtfTmfEvent}'s.
44 *
45 * @param eventDef
46 * CTF EventDefinition object corresponding to this trace event
47 * @param fileName
48 * The path to the trace file
49 * @param originTrace
50 * The trace from which this event originates
51 * @return The newly-built CtfTmfEvent
52 */
53 public static CtfTmfEvent createEvent(EventDefinition eventDef,
54 String fileName, CtfTmfTrace originTrace) {
55
56 /* Prepare what to pass to CtfTmfEvent's constructor */
57 long ts = eventDef.getTimestamp();
e73a4ba5 58 CtfTmfTimestamp timestamp = originTrace.createTimestamp(originTrace.getCTFTrace().timestampCyclesToNanos(ts));
6cfa0200
AM
59
60 int sourceCPU = eventDef.getCPU();
61
62 ITmfEventField content = new TmfEventField(
214cc822 63 ITmfEventField.ROOT_FIELD_ID, null, parseFields(eventDef));
6cfa0200
AM
64
65 String reference = fileName == null ? CtfTmfEvent.NO_STREAM : fileName;
66
67 /* Construct and return the object */
68 CtfTmfEvent event = new CtfTmfEvent(
69 originTrace,
70 ITmfContext.UNKNOWN_RANK,
71 timestamp,
72 content,
73 reference,
74 sourceCPU,
75 eventDef.getDeclaration()
76 );
77 return event;
78 }
79
80 /* Singleton instance of a null event */
81 private static CtfTmfEvent nullEvent = null;
82
83 /**
84 * Get an instance of a null event.
85 *
86 * @return An empty event
87 */
88 public static CtfTmfEvent getNullEvent() {
89 if (nullEvent == null) {
90 nullEvent = new CtfTmfEvent();
91 }
92 return nullEvent;
93 }
94
95 /**
96 * Extract the field information from the structDefinition haze-inducing
97 * mess, and put them into something ITmfEventField can cope with.
98 */
60fb38b8 99 private static CtfTmfEventField[] parseFields(EventDefinition eventDef) {
6cfa0200
AM
100 List<CtfTmfEventField> fields = new ArrayList<CtfTmfEventField>();
101
102 StructDefinition structFields = eventDef.getFields();
437bb7c0
AM
103 for (Map.Entry<String, Definition> entry : structFields.getDefinitions().entrySet()) {
104 String curFieldName = entry.getKey();
105 Definition curFieldDef = entry.getValue();
106 CtfTmfEventField curField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
6cfa0200
AM
107 fields.add(curField);
108 }
109
110 /* Add context information as CtfTmfEventField */
6cfa0200
AM
111 StructDefinition structContext = eventDef.getContext();
112 if (structContext != null) {
437bb7c0 113 for (Map.Entry<String, Definition> entry : structContext.getDefinitions().entrySet()) {
60fb38b8 114 /* Prefix field name */
437bb7c0
AM
115 String curContextName = CtfConstants.CONTEXT_FIELD_PREFIX + entry.getKey();
116 Definition curContextDef = entry.getValue();
117 CtfTmfEventField curContext = CtfTmfEventField.parseField(curContextDef, curContextName);
6cfa0200
AM
118 fields.add(curContext);
119 }
120 }
437bb7c0 121
6cfa0200
AM
122 return fields.toArray(new CtfTmfEventField[fields.size()]);
123 }
124}
This page took 0.041937 seconds and 5 git commands to generate.