1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 École Polytechnique de Montréal
4 * All rights reserved. This program and the accompanying materials are
5 * made 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 * Geneviève Bastien - Initial implementation
11 * Patrick Tasse - Dispose wrapped trace
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
;
16 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
19 import java
.io
.IOException
;
20 import java
.io
.InputStream
;
22 import java
.util
.Collection
;
23 import java
.util
.HashSet
;
25 import javax
.xml
.XMLConstants
;
26 import javax
.xml
.transform
.Source
;
27 import javax
.xml
.transform
.stream
.StreamSource
;
28 import javax
.xml
.validation
.Schema
;
29 import javax
.xml
.validation
.SchemaFactory
;
30 import javax
.xml
.validation
.Validator
;
32 import org
.eclipse
.core
.resources
.IProject
;
33 import org
.eclipse
.core
.resources
.IResource
;
34 import org
.eclipse
.core
.runtime
.IStatus
;
35 import org
.eclipse
.core
.runtime
.Status
;
36 import org
.eclipse
.jdt
.annotation
.Nullable
;
37 import org
.eclipse
.osgi
.util
.NLS
;
38 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventType
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventType
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.TmfContentFieldAspect
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.TmfCpuAspect
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomEventContent
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlEvent
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlTrace
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlTraceDefinition
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
55 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfNanoTimestamp
;
56 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
57 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfContext
;
58 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
59 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
60 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
61 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
62 import org
.xml
.sax
.SAXException
;
64 import com
.google
.common
.collect
.ImmutableList
;
67 * An XML development trace using a custom XML trace definition and schema.
69 * This class will typically be used to build custom traces to unit test more
70 * complex functionalities like analyzes or to develop and test data-driven
73 * This class wraps a custom XML trace and rewrites the returned events in the
74 * getNext() method so that event's fields are the ones defined in <field ... />
75 * elements instead of those defined in the custom XML parser. This way, each
76 * event can have a different set of fields. This class can, for example, mimic
79 * @author Geneviève Bastien
81 public class TmfXmlTraceStub
extends TmfTrace
{
83 private static final String DEVELOPMENT_TRACE_PARSER_PATH
= "TmfXmlDevelopmentTrace.xml"; //$NON-NLS-1$
84 private static final String DEVELOPMENT_TRACE_XSD
= "TmfXmlDevelopmentTrace.xsd"; //$NON-NLS-1$
85 private static final String EMPTY
= ""; //$NON-NLS-1$
87 /* XML elements and attributes names */
88 private static final String EVENT_NAME_FIELD
= "Message"; //$NON-NLS-1$
89 private static final String FIELD_NAMES_FIELD
= "fields"; //$NON-NLS-1$
90 private static final String VALUES_FIELD
= "values"; //$NON-NLS-1$
91 private static final String TYPES_FIELD
= "type"; //$NON-NLS-1$
92 private static final String VALUES_SEPARATOR
= " \\| "; //$NON-NLS-1$
93 private static final String TYPE_INTEGER
= "int"; //$NON-NLS-1$
94 private static final String TYPE_LONG
= "long"; //$NON-NLS-1$
95 private static final String ASPECT_CPU
= "cpu";
97 private static final Long SECONDS_TO_NS
= 1000000000L;
99 private final CustomXmlTraceDefinition fDefinition
;
100 private CustomXmlTrace fTrace
;
102 private Collection
<ITmfEventAspect
> fAspects
= TmfTrace
.BASE_ASPECTS
;
103 private final Collection
<ITmfEventAspect
> fAdditionalAspects
= new HashSet
<>();
106 * Constructor. Constructs the custom XML trace with the appropriate
109 public TmfXmlTraceStub() {
111 /* Load custom XML definition */
112 try (InputStream in
= TmfXmlTraceStub
.class.getResourceAsStream(DEVELOPMENT_TRACE_PARSER_PATH
);) {
113 CustomXmlTraceDefinition
[] definitions
= CustomXmlTraceDefinition
.loadAll(in
);
114 if (definitions
.length
< 2) {
115 throw new IllegalStateException("The custom trace definition does not exist"); //$NON-NLS-1$
117 /* The first definition parses the 'set_aspects' event */
118 fTrace
= new CustomXmlTrace(definitions
[0]) {
120 protected ITmfTraceIndexer
createIndexer(int interval
) {
121 /* Use the in-memory checkpoint indexer */
122 return new TmfCheckpointIndexer(this, interval
);
125 /* The second definition parses 'event' trace events */
126 fDefinition
= checkNotNull(definitions
[1]);
127 } catch (IOException e
) {
128 throw new IllegalStateException("Cannot open the trace parser for development traces"); //$NON-NLS-1$
134 public void initTrace(@Nullable IResource resource
, @Nullable String path
, @Nullable Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
135 super.initTrace(resource
, path
, type
);
138 /* Initialize and read the trace with the 'set_aspects' definition */
139 TmfSignalManager
.deregister(fTrace
);
140 fTrace
.initTrace(resource
, path
, type
);
142 /* If a set_aspects event exists, getNext() will process it */
147 /* Initialize a new trace with the trace events definition */
148 fTrace
= new CustomXmlTrace(fDefinition
);
149 TmfSignalManager
.deregister(fTrace
);
150 fTrace
.initTrace(resource
, path
, type
);
151 /* Set the start and (current) end times for this trace */
156 ITmfEvent event
= getNext(ctx
);
158 final ITmfTimestamp curTime
= event
.getTimestamp();
159 this.setStartTime(curTime
);
160 this.setEndTime(curTime
);
166 public synchronized void dispose() {
172 public @Nullable ITmfEvent
parseEvent(@Nullable ITmfContext context
) {
173 return fTrace
.parseEvent(context
);
177 public @Nullable ITmfLocation
getCurrentLocation() {
178 return fTrace
.getCurrentLocation();
182 public double getLocationRatio(@Nullable ITmfLocation location
) {
183 return fTrace
.getLocationRatio(location
);
187 public @Nullable ITmfContext
seekEvent(@Nullable ITmfLocation location
) {
188 return fTrace
.seekEvent(location
);
192 public @Nullable ITmfContext
seekEvent(double ratio
) {
193 return fTrace
.seekEvent(ratio
);
197 public IStatus
validate(@Nullable IProject project
, @Nullable String path
) {
198 File xmlFile
= new File(path
);
199 if (!xmlFile
.exists() || !xmlFile
.isFile() || !xmlFile
.canRead()) {
200 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.Messages
.TmfDevelopmentTrace_FileNotFound
, path
));
202 /* Does the XML file validate with the XSD */
203 SchemaFactory schemaFactory
= SchemaFactory
.newInstance(XMLConstants
.W3C_XML_SCHEMA_NS_URI
);
204 Source xmlSource
= new StreamSource(xmlFile
);
207 URL url
= TmfXmlTraceStub
.class.getResource(DEVELOPMENT_TRACE_XSD
);
208 Schema schema
= schemaFactory
.newSchema(url
);
210 Validator validator
= schema
.newValidator();
211 validator
.validate(xmlSource
);
212 } catch (SAXException e
) {
213 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.Messages
.TmfDevelopmentTrace_ValidationError
, path
), e
);
214 } catch (IOException e
) {
215 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.Messages
.TmfDevelopmentTrace_IoError
, path
), e
);
217 return Status
.OK_STATUS
;
220 private static String
getStringValue(ITmfEventField content
, String fieldName
) {
221 ITmfEventField field
= content
.getField(fieldName
);
225 Object val
= field
.getValue();
226 if (!(val
instanceof String
)) {
233 public synchronized @Nullable ITmfEvent
getNext(@Nullable ITmfContext context
) {
234 if (context
== null) {
237 final ITmfContext savedContext
= new TmfContext(context
.getLocation(), context
.getRank());
238 CustomXmlEvent event
= fTrace
.getNext(context
);
243 /* Translate the content of the event */
244 /* The "fields" field contains a | separated list of field names */
245 /* The "values" field contains a | separated list of field values */
246 /* the "type" field contains a | separated list of field types */
247 ITmfEventField content
= event
.getContent();
248 if (content
== null) {
252 String fieldString
= getStringValue(content
, FIELD_NAMES_FIELD
);
253 String valueString
= getStringValue(content
, VALUES_FIELD
);
254 String typeString
= getStringValue(content
, TYPES_FIELD
);
256 String
[] fields
= fieldString
.split(VALUES_SEPARATOR
);
257 String
[] values
= valueString
.split(VALUES_SEPARATOR
);
258 String
[] types
= typeString
.split(VALUES_SEPARATOR
);
259 ITmfEventField
[] fieldsArray
= new TmfEventField
[fields
.length
];
261 for (int i
= 0; i
< fields
.length
; i
++) {
262 String value
= EMPTY
;
263 if (values
.length
> i
) {
267 if (types
.length
> i
) {
275 val
= Integer
.valueOf(value
);
276 } catch (NumberFormatException e
) {
277 Activator
.logError(String
.format("Get next XML event: cannot cast value %s to integer", value
), e
); //$NON-NLS-1$
284 val
= Long
.valueOf(value
);
285 } catch (NumberFormatException e
) {
286 Activator
.logError(String
.format("Get next XML event: cannot cast value %s to long", value
), e
); //$NON-NLS-1$
295 fieldsArray
[i
] = new TmfEventField(checkNotNull(fields
[i
]), val
, null);
299 * Generate the aspects for this trace if it is the 'set_aspects'
302 if (fTrace
.getDefinition() != fDefinition
) {
303 generateAspects(fieldsArray
);
307 /* Create a new event with new fields and name */
308 ITmfEventType customEventType
= event
.getType();
309 String eventName
= getStringValue(content
, EVENT_NAME_FIELD
);
310 TmfEventType eventType
= new TmfEventType(eventName
, customEventType
.getRootField());
311 ITmfEventField eventFields
= new CustomEventContent(content
.getName(), content
.getValue(), fieldsArray
);
313 * TODO: Timestamps for these traces are in nanos, but since the
314 * CustomXmlTrace does not support this format, the timestamp of the
315 * original is in second and we need to convert it. We should do that at
316 * the source when it is supported
318 ITmfTimestamp timestamp
= new TmfNanoTimestamp(event
.getTimestamp().getValue() / SECONDS_TO_NS
);
319 TmfEvent newEvent
= new TmfEvent(this, ITmfContext
.UNKNOWN_RANK
, timestamp
, eventType
, eventFields
);
320 updateAttributes(savedContext
, event
);
324 private void generateAspects(ITmfEventField
[] fieldsArray
) {
325 ImmutableList
.Builder
<ITmfEventAspect
> builder
= new ImmutableList
.Builder
<>();
327 /* Initialize the first default trace aspects */
328 builder
.add(ITmfEventAspect
.BaseAspects
.TIMESTAMP
);
329 builder
.add(ITmfEventAspect
.BaseAspects
.EVENT_TYPE
);
331 /* Add custom aspects in between */
332 for (ITmfEventField field
: fieldsArray
) {
333 String name
= field
.getName();
334 final ITmfEventAspect aspect
= new TmfContentFieldAspect(name
, name
);
335 if (name
.equals(ASPECT_CPU
)) {
336 builder
.add(new TmfCpuAspect() {
338 public @Nullable Integer
resolve(ITmfEvent event
) {
339 Object result
= aspect
.resolve(event
);
340 if (result
instanceof Number
) {
341 return ((Number
) result
).intValue();
351 /* Add the big content aspect */
352 builder
.add(ITmfEventAspect
.BaseAspects
.CONTENTS
);
353 /* Add the additional aspects */
354 builder
.addAll(fAdditionalAspects
);
355 fAspects
= builder
.build();
359 public Iterable
<ITmfEventAspect
> getEventAspects() {
364 * Adds a new event aspect to this type of trace. Since this trace type is
365 * used to build custom traces that mimic the behavior of real traces, the
366 * required aspects may be missing and this method allows to add them. This
367 * method should be called before calling
368 * {@link #initTrace(IResource, String, Class)} otherwise the additional
369 * aspects will not be picked up when generating the aspects.
374 public void addEventAspect(ITmfEventAspect aspect
) {
375 fAdditionalAspects
.add(aspect
);