| 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 | |
| 13 | package org.eclipse.linuxtools.tmf.core.ctfadaptor; |
| 14 | |
| 15 | import java.util.ArrayList; |
| 16 | import java.util.List; |
| 17 | import java.util.Map; |
| 18 | |
| 19 | import org.eclipse.linuxtools.ctf.core.CTFStrings; |
| 20 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; |
| 21 | import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration; |
| 22 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; |
| 23 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; |
| 24 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; |
| 25 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; |
| 26 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField; |
| 27 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; |
| 28 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
| 29 | |
| 30 | /** |
| 31 | * Factory for CtfTmfEvent's. |
| 32 | * |
| 33 | * This code was moved out of CtfTmfEvent to provide better separation between |
| 34 | * the parsing/instantiation of events, and the usual TMF API implementations. |
| 35 | * |
| 36 | * @author Alexandre Montplaisir |
| 37 | * @since 2.0 |
| 38 | */ |
| 39 | public final class CtfTmfEventFactory { |
| 40 | |
| 41 | /** |
| 42 | * Don't let anyone instantiate this class. |
| 43 | */ |
| 44 | private CtfTmfEventFactory() {} |
| 45 | |
| 46 | /** |
| 47 | * Factory method to instantiate new {@link CtfTmfEvent}'s. |
| 48 | * |
| 49 | * @param eventDef |
| 50 | * CTF EventDefinition object corresponding to this trace event |
| 51 | * @param fileName |
| 52 | * The path to the trace file |
| 53 | * @param originTrace |
| 54 | * The trace from which this event originates |
| 55 | * @return The newly-built CtfTmfEvent |
| 56 | */ |
| 57 | public static CtfTmfEvent createEvent(EventDefinition eventDef, |
| 58 | String fileName, CtfTmfTrace originTrace) { |
| 59 | |
| 60 | /* Prepare what to pass to CtfTmfEvent's constructor */ |
| 61 | final IEventDeclaration eventDecl = eventDef.getDeclaration(); |
| 62 | final long ts = eventDef.getTimestamp(); |
| 63 | final CtfTmfTimestamp timestamp = originTrace.createTimestamp( |
| 64 | originTrace.getCTFTrace().timestampCyclesToNanos(ts)); |
| 65 | |
| 66 | int sourceCPU = eventDef.getCPU(); |
| 67 | |
| 68 | ITmfEventField content = new TmfEventField( |
| 69 | ITmfEventField.ROOT_FIELD_ID, null, parseFields(eventDef)); |
| 70 | |
| 71 | String reference = fileName == null ? CtfTmfEvent.NO_STREAM : fileName; |
| 72 | |
| 73 | /* Handle the special case of lost events */ |
| 74 | if (eventDecl.getName().equals(CTFStrings.LOST_EVENT_NAME)) { |
| 75 | Definition nbLostEventsDef = eventDef.getFields().getDefinitions().get(CTFStrings.LOST_EVENTS_FIELD); |
| 76 | Definition durationDef = eventDef.getFields().getDefinitions().get(CTFStrings.LOST_EVENTS_DURATION); |
| 77 | if (!(nbLostEventsDef instanceof IntegerDefinition) || !(durationDef instanceof IntegerDefinition)) { |
| 78 | /* |
| 79 | * One or both of these fields doesn't exist, or is not of the |
| 80 | * right type. The event claims to be a "lost event", but is |
| 81 | * malformed. Log it and return a null event instead. |
| 82 | */ |
| 83 | return getNullEvent(); |
| 84 | } |
| 85 | long nbLostEvents = ((IntegerDefinition) nbLostEventsDef).getValue(); |
| 86 | long duration = ((IntegerDefinition) durationDef).getValue(); |
| 87 | CtfTmfTimestamp timestampEnd = new CtfTmfTimestamp( |
| 88 | originTrace.getCTFTrace().timestampCyclesToNanos(ts) + duration); |
| 89 | |
| 90 | CtfTmfLostEvent lostEvent = new CtfTmfLostEvent(originTrace, |
| 91 | ITmfContext.UNKNOWN_RANK, |
| 92 | content, |
| 93 | reference, // filename |
| 94 | sourceCPU, |
| 95 | eventDecl, |
| 96 | new TmfTimeRange(timestamp, timestampEnd), |
| 97 | nbLostEvents); |
| 98 | return lostEvent; |
| 99 | } |
| 100 | |
| 101 | /* Handle standard event types */ |
| 102 | CtfTmfEvent event = new CtfTmfEvent( |
| 103 | originTrace, |
| 104 | ITmfContext.UNKNOWN_RANK, |
| 105 | timestamp, |
| 106 | content, |
| 107 | reference, // filename |
| 108 | sourceCPU, |
| 109 | eventDecl); |
| 110 | return event; |
| 111 | } |
| 112 | |
| 113 | /* Singleton instance of a null event */ |
| 114 | private static CtfTmfEvent nullEvent = null; |
| 115 | |
| 116 | /** |
| 117 | * Get an instance of a null event. |
| 118 | * |
| 119 | * @return An empty event |
| 120 | */ |
| 121 | public static CtfTmfEvent getNullEvent() { |
| 122 | if (nullEvent == null) { |
| 123 | nullEvent = new CtfTmfEvent(); |
| 124 | } |
| 125 | return nullEvent; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Extract the field information from the structDefinition haze-inducing |
| 130 | * mess, and put them into something ITmfEventField can cope with. |
| 131 | */ |
| 132 | private static CtfTmfEventField[] parseFields(EventDefinition eventDef) { |
| 133 | List<CtfTmfEventField> fields = new ArrayList<>(); |
| 134 | |
| 135 | StructDefinition structFields = eventDef.getFields(); |
| 136 | for (Map.Entry<String, Definition> entry : structFields.getDefinitions().entrySet()) { |
| 137 | String curFieldName = entry.getKey(); |
| 138 | Definition curFieldDef = entry.getValue(); |
| 139 | CtfTmfEventField curField = CtfTmfEventField.parseField(curFieldDef, curFieldName); |
| 140 | fields.add(curField); |
| 141 | } |
| 142 | |
| 143 | /* Add context information as CtfTmfEventField */ |
| 144 | StructDefinition structContext = eventDef.getContext(); |
| 145 | if (structContext != null) { |
| 146 | for (Map.Entry<String, Definition> entry : structContext.getDefinitions().entrySet()) { |
| 147 | /* Prefix field name */ |
| 148 | String curContextName = CtfConstants.CONTEXT_FIELD_PREFIX + entry.getKey(); |
| 149 | Definition curContextDef = entry.getValue(); |
| 150 | CtfTmfEventField curContext = CtfTmfEventField.parseField(curContextDef, curContextName); |
| 151 | fields.add(curContext); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return fields.toArray(new CtfTmfEventField[fields.size()]); |
| 156 | } |
| 157 | } |