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
;
17 import static org
.junit
.Assert
.fail
;
20 import java
.io
.IOException
;
21 import java
.io
.InputStream
;
23 import java
.util
.Collection
;
24 import java
.util
.HashSet
;
26 import javax
.xml
.XMLConstants
;
27 import javax
.xml
.transform
.Source
;
28 import javax
.xml
.transform
.stream
.StreamSource
;
29 import javax
.xml
.validation
.Schema
;
30 import javax
.xml
.validation
.SchemaFactory
;
31 import javax
.xml
.validation
.Validator
;
33 import org
.eclipse
.core
.resources
.IProject
;
34 import org
.eclipse
.core
.resources
.IResource
;
35 import org
.eclipse
.core
.runtime
.IPath
;
36 import org
.eclipse
.core
.runtime
.IStatus
;
37 import org
.eclipse
.core
.runtime
.Status
;
38 import org
.eclipse
.jdt
.annotation
.DefaultLocation
;
39 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
40 import org
.eclipse
.jdt
.annotation
.Nullable
;
41 import org
.eclipse
.osgi
.util
.NLS
;
42 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventType
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventType
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.TmfContentFieldAspect
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.TmfCpuAspect
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomEventContent
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlEvent
;
55 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlTrace
;
56 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlTraceDefinition
;
57 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
58 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
59 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
60 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
61 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfContext
;
62 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
63 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
64 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpointIndexer
;
65 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
66 import org
.xml
.sax
.SAXException
;
68 import com
.google
.common
.collect
.ImmutableList
;
71 * An XML development trace using a custom XML trace definition and schema.
73 * This class will typically be used to build custom traces to unit test more
74 * complex functionalities like analyzes or to develop and test data-driven
77 * This class wraps a custom XML trace and rewrites the returned events in the
78 * getNext() method so that event's fields are the ones defined in <field ... />
79 * elements instead of those defined in the custom XML parser. This way, each
80 * event can have a different set of fields. This class can, for example, mimic
83 * @author Geneviève Bastien
85 public class TmfXmlTraceStub
extends TmfTrace
{
87 private static final String DEVELOPMENT_TRACE_PARSER_PATH
= "TmfXmlDevelopmentTrace.xml"; //$NON-NLS-1$
88 private static final String DEVELOPMENT_TRACE_XSD
= "TmfXmlDevelopmentTrace.xsd"; //$NON-NLS-1$
89 private static final String EMPTY
= ""; //$NON-NLS-1$
91 /* XML elements and attributes names */
92 private static final String EVENT_NAME_FIELD
= "Message"; //$NON-NLS-1$
93 private static final String FIELD_NAMES_FIELD
= "fields"; //$NON-NLS-1$
94 private static final String VALUES_FIELD
= "values"; //$NON-NLS-1$
95 private static final String TYPES_FIELD
= "type"; //$NON-NLS-1$
96 private static final String VALUES_SEPARATOR
= " \\| "; //$NON-NLS-1$
97 private static final String TYPE_INTEGER
= "int"; //$NON-NLS-1$
98 private static final String TYPE_LONG
= "long"; //$NON-NLS-1$
99 private static final String ASPECT_CPU
= "cpu";
101 private static final Long SECONDS_TO_NS
= 1000000000L;
103 private final CustomXmlTraceDefinition fDefinition
;
104 private CustomXmlTrace fTrace
;
106 private Collection
<ITmfEventAspect
<?
>> fAspects
= TmfTrace
.BASE_ASPECTS
;
107 private final Collection
<ITmfEventAspect
<?
>> fAdditionalAspects
= new HashSet
<>();
110 * Validate and initialize a {@link TmfXmlTraceStub} object
112 * @param absolutePath
113 * The absolute file path of the trace file
116 public static TmfXmlTraceStub
setupTrace(IPath absolutePath
) {
117 TmfXmlTraceStub trace
= new TmfXmlTraceStub();
118 IStatus status
= trace
.validate(null, absolutePath
.toOSString());
119 if (!status
.isOK()) {
120 fail(status
.getException().getMessage());
123 trace
.initTrace(null, absolutePath
.toOSString(), TmfEvent
.class);
124 } catch (TmfTraceException e
) {
125 fail(e
.getMessage());
131 * Constructor. Constructs the custom XML trace with the appropriate
134 public TmfXmlTraceStub() {
136 /* Load custom XML definition */
137 try (InputStream in
= TmfXmlTraceStub
.class.getResourceAsStream(DEVELOPMENT_TRACE_PARSER_PATH
);) {
138 CustomXmlTraceDefinition
[] definitions
= CustomXmlTraceDefinition
.loadAll(in
);
139 if (definitions
.length
< 2) {
140 throw new IllegalStateException("The custom trace definition does not exist"); //$NON-NLS-1$
142 /* The first definition parses the 'set_aspects' event */
143 fTrace
= new CustomXmlTrace(definitions
[0]) {
145 protected ITmfTraceIndexer
createIndexer(int interval
) {
146 /* Use the in-memory checkpoint indexer */
147 return new TmfCheckpointIndexer(this, interval
);
150 /* The second definition parses 'event' trace events */
151 fDefinition
= checkNotNull(definitions
[1]);
152 } catch (IOException e
) {
153 throw new IllegalStateException("Cannot open the trace parser for development traces"); //$NON-NLS-1$
159 @NonNullByDefault({DefaultLocation
.TYPE_ARGUMENT
})
160 public void initTrace(@Nullable IResource resource
, @Nullable String path
, @Nullable Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
161 super.initTrace(resource
, path
, type
);
164 /* Initialize and read the trace with the 'set_aspects' definition */
165 TmfSignalManager
.deregister(fTrace
);
166 fTrace
.initTrace(resource
, path
, type
);
168 /* If a set_aspects event exists, getNext() will process it */
173 /* Initialize a new trace with the trace events definition */
174 fTrace
= new CustomXmlTrace(fDefinition
);
175 TmfSignalManager
.deregister(fTrace
);
176 fTrace
.initTrace(resource
, path
, type
);
177 /* Set the start and (current) end times for this trace */
182 ITmfEvent event
= getNext(ctx
);
184 final ITmfTimestamp curTime
= event
.getTimestamp();
185 this.setStartTime(curTime
);
186 this.setEndTime(curTime
);
192 public synchronized void dispose() {
198 public @Nullable ITmfEvent
parseEvent(@Nullable ITmfContext context
) {
199 return fTrace
.parseEvent(context
);
203 public @Nullable ITmfLocation
getCurrentLocation() {
204 return fTrace
.getCurrentLocation();
208 public double getLocationRatio(@Nullable ITmfLocation location
) {
209 return fTrace
.getLocationRatio(location
);
213 public @Nullable ITmfContext
seekEvent(@Nullable ITmfLocation location
) {
214 return fTrace
.seekEvent(location
);
218 public @Nullable ITmfContext
seekEvent(double ratio
) {
219 return fTrace
.seekEvent(ratio
);
223 public IStatus
validate(@Nullable IProject project
, @Nullable String path
) {
224 File xmlFile
= new File(path
);
225 if (!xmlFile
.exists() || !xmlFile
.isFile() || !xmlFile
.canRead()) {
226 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.Messages
.TmfDevelopmentTrace_FileNotFound
, path
));
228 /* Does the XML file validate with the XSD */
229 SchemaFactory schemaFactory
= SchemaFactory
.newInstance(XMLConstants
.W3C_XML_SCHEMA_NS_URI
);
230 Source xmlSource
= new StreamSource(xmlFile
);
233 URL url
= TmfXmlTraceStub
.class.getResource(DEVELOPMENT_TRACE_XSD
);
234 Schema schema
= schemaFactory
.newSchema(url
);
236 Validator validator
= schema
.newValidator();
237 validator
.validate(xmlSource
);
238 } catch (SAXException e
) {
239 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.Messages
.TmfDevelopmentTrace_ValidationError
, path
), e
);
240 } catch (IOException e
) {
241 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.Messages
.TmfDevelopmentTrace_IoError
, path
), e
);
243 return Status
.OK_STATUS
;
246 private static String
getStringValue(ITmfEventField content
, String fieldName
) {
247 ITmfEventField field
= content
.getField(fieldName
);
251 Object val
= field
.getValue();
252 if (!(val
instanceof String
)) {
259 public synchronized @Nullable ITmfEvent
getNext(@Nullable ITmfContext context
) {
260 if (context
== null) {
263 final ITmfContext savedContext
= new TmfContext(context
.getLocation(), context
.getRank());
264 CustomXmlEvent event
= fTrace
.getNext(context
);
269 /* Translate the content of the event */
270 /* The "fields" field contains a | separated list of field names */
271 /* The "values" field contains a | separated list of field values */
272 /* the "type" field contains a | separated list of field types */
273 ITmfEventField content
= event
.getContent();
274 if (content
== null) {
278 String fieldString
= getStringValue(content
, FIELD_NAMES_FIELD
);
279 String valueString
= getStringValue(content
, VALUES_FIELD
);
280 String typeString
= getStringValue(content
, TYPES_FIELD
);
282 String
[] fields
= fieldString
.split(VALUES_SEPARATOR
);
283 String
[] values
= valueString
.split(VALUES_SEPARATOR
);
284 String
[] types
= typeString
.split(VALUES_SEPARATOR
);
285 ITmfEventField
[] fieldsArray
= new TmfEventField
[fields
.length
];
287 for (int i
= 0; i
< fields
.length
; i
++) {
288 String value
= EMPTY
;
289 if (values
.length
> i
) {
293 if (types
.length
> i
) {
301 val
= Integer
.valueOf(value
);
302 } catch (NumberFormatException e
) {
303 Activator
.logError(String
.format("Get next XML event: cannot cast value %s to integer", value
), e
); //$NON-NLS-1$
310 val
= Long
.valueOf(value
);
311 } catch (NumberFormatException e
) {
312 Activator
.logError(String
.format("Get next XML event: cannot cast value %s to long", value
), e
); //$NON-NLS-1$
321 fieldsArray
[i
] = new TmfEventField(checkNotNull(fields
[i
]), val
, null);
325 * Generate the aspects for this trace if it is the 'set_aspects'
328 if (fTrace
.getDefinition() != fDefinition
) {
329 generateAspects(fieldsArray
);
333 /* Create a new event with new fields and name */
334 ITmfEventType customEventType
= event
.getType();
335 String eventName
= getStringValue(content
, EVENT_NAME_FIELD
);
336 TmfEventType eventType
= new TmfEventType(eventName
, customEventType
.getRootField());
337 ITmfEventField eventFields
= new CustomEventContent(content
.getName(), content
.getValue(), fieldsArray
);
339 * TODO: Timestamps for these traces are in nanos, but since the
340 * CustomXmlTrace does not support this format, the timestamp of the
341 * original is in second and we need to convert it. We should do that at
342 * the source when it is supported
344 ITmfTimestamp timestamp
= TmfTimestamp
.fromNanos(event
.getTimestamp().getValue() / SECONDS_TO_NS
);
345 TmfEvent newEvent
= new TmfEvent(this, ITmfContext
.UNKNOWN_RANK
, timestamp
, eventType
, eventFields
);
346 updateAttributes(savedContext
, event
);
350 private void generateAspects(ITmfEventField
[] fieldsArray
) {
351 ImmutableList
.Builder
<ITmfEventAspect
<?
>> builder
= new ImmutableList
.Builder
<>();
353 /* Initialize the first default trace aspects */
354 builder
.add(ITmfEventAspect
.BaseAspects
.TIMESTAMP
);
355 builder
.add(ITmfEventAspect
.BaseAspects
.EVENT_TYPE
);
357 /* Add custom aspects in between */
358 for (ITmfEventField field
: fieldsArray
) {
359 String name
= field
.getName();
360 final ITmfEventAspect
<?
> aspect
= new TmfContentFieldAspect(name
, name
);
361 if (name
.equals(ASPECT_CPU
)) {
362 builder
.add(new TmfCpuAspect() {
364 public @Nullable Integer
resolve(ITmfEvent event
) {
365 Object result
= aspect
.resolve(event
);
366 if (result
instanceof Number
) {
367 return ((Number
) result
).intValue();
377 /* Add the big content aspect */
378 builder
.add(ITmfEventAspect
.BaseAspects
.CONTENTS
);
379 /* Add the additional aspects */
380 builder
.addAll(fAdditionalAspects
);
381 fAspects
= builder
.build();
385 public Iterable
<ITmfEventAspect
<?
>> getEventAspects() {
390 * Adds a new event aspect to this type of trace. Since this trace type is
391 * used to build custom traces that mimic the behavior of real traces, the
392 * required aspects may be missing and this method allows to add them. This
393 * method should be called before calling
394 * {@link #initTrace(IResource, String, Class)} otherwise the additional
395 * aspects will not be picked up when generating the aspects.
400 public void addEventAspect(ITmfEventAspect
<?
> aspect
) {
401 fAdditionalAspects
.add(aspect
);