1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 Ericsson
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 * Matthew Khouzam - Initial API and implementation
11 * Patrick Tasse - Fix parsing of instance numbers
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.btf
.core
.trace
;
17 import java
.io
.FileNotFoundException
;
18 import java
.io
.IOException
;
19 import java
.io
.RandomAccessFile
;
20 import java
.nio
.ByteBuffer
;
21 import java
.text
.ParseException
;
22 import java
.text
.SimpleDateFormat
;
23 import java
.util
.Date
;
24 import java
.util
.HashMap
;
26 import java
.util
.TreeMap
;
28 import org
.eclipse
.core
.resources
.IProject
;
29 import org
.eclipse
.core
.resources
.IResource
;
30 import org
.eclipse
.core
.runtime
.IStatus
;
31 import org
.eclipse
.core
.runtime
.Status
;
32 import org
.eclipse
.tracecompass
.btf
.core
.Activator
;
33 import org
.eclipse
.tracecompass
.btf
.core
.event
.BtfEvent
;
34 import org
.eclipse
.tracecompass
.btf
.core
.event
.BtfEventType
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.io
.BufferedRandomAccessFile
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomTxtTraceContext
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTraceProperties
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfContext
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TraceValidationStatus
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfPersistentlyIndexable
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.TmfBTreeTraceIndexer
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpoint
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpoint
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.TmfLongLocation
;
55 import com
.google
.common
.collect
.ImmutableMap
;
58 * BTF reader. Reads Best Trace Format traces.
60 * @author Matthew Khouzam
62 public class BtfTrace
extends TmfTrace
implements ITmfPersistentlyIndexable
, ITmfTraceProperties
, AutoCloseable
{
64 private static final int MAX_FIELDS
= 7;
66 private static final long MICROSECONDS_IN_A_SECOND
= 1000000L;
68 private static final String VERSION
= "#version"; //$NON-NLS-1$
69 private static final String CREATOR
= "#creator"; //$NON-NLS-1$
70 private static final String CREATIONDATE
= "#creationDate"; //$NON-NLS-1$
71 private static final String INPUTFILE
= "#inputFile"; //$NON-NLS-1$
72 private static final String TIMESCALE
= "#timeScale"; //$NON-NLS-1$
73 private static final String ENTITYTYPE
= "#entityType"; //$NON-NLS-1$
74 private static final String ENTITYTABLE
= "#entityTable"; //$NON-NLS-1$
75 private static final String ENTITYTYPETABLE
= "#entityTypeTable"; //$NON-NLS-1$
78 private static final String lCREATIONDATE
= "#creationdate"; //$NON-NLS-1$
79 private static final String lINPUTFILE
= "#inputfile"; //$NON-NLS-1$
80 private static final String lTIMESCALE
= "#timescale"; //$NON-NLS-1$
81 private static final String lENTITYTYPE
= "#entitytype"; //$NON-NLS-1$
82 private static final String lENTITYTABLE
= "#entitytable"; //$NON-NLS-1$
83 private static final String lENTITYTYPETABLE
= "#entitytypetable"; //$NON-NLS-1$
85 private static final TmfLongLocation NULL_LOCATION
= new TmfLongLocation(-1L);
87 private static final int CACHE_SIZE
= 256;
88 private static final int MAX_CONFIDENCE
= 100;
89 private static final int MAX_LINES
= 100;
91 private static int fCheckpointSize
= -1;
93 private final Map
<String
, String
> fProperties
= new HashMap
<>();
95 private final Map
<Integer
, String
> fEntityTable
= new TreeMap
<>();
96 private final Map
<BtfEventType
, String
> fEntityTypeTable
= new HashMap
<>();
97 private final Map
<Integer
, BtfEventType
> fEntityTypes
= new TreeMap
<>();
99 private String fVersion
;
100 private String fCreator
;
101 private String fCreationDate
;
102 private String fInputFile
;
103 // default unit is ns
104 private BtfTimestampFormat fTsFormat
= BtfTimestampFormat
.NS
;
107 private RandomAccessFile fFileInput
;
108 private long fDataOffset
;
109 private long fTsOffset
= 0;
112 * Default constructor
116 setCacheSize(CACHE_SIZE
);
117 fProperties
.put(TIMESCALE
, fTsFormat
.toString());
120 private void parseHeader(RandomAccessFile input
) throws IOException
{
121 String line
= input
.readLine();
123 while (line
!= null && line
.startsWith("#")) { //$NON-NLS-1$
124 String
[] tokens
= line
.split(" ", 2); //$NON-NLS-1$
126 * please note that the examples we were given and the spec are NOT
127 * consistent, so we are ignoring the case to avoid issues
129 switch (tokens
[0].toLowerCase()) {
131 fVersion
= tokens
[1];
132 fProperties
.put(VERSION
, fVersion
);
135 fCreator
= tokens
[1];
136 fProperties
.put(CREATOR
, fCreator
);
139 fCreationDate
= tokens
[1];
140 fProperties
.put(CREATIONDATE
, fCreationDate
);
143 // DateFormats are inherently unsafe for multithreaded use so we can't make this a field. Just in case.
144 final SimpleDateFormat ISO8601DATEFORMAT
= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX"); //$NON-NLS-1$
145 Date dateTime
= ISO8601DATEFORMAT
.parse(fCreationDate
);
146 fTsOffset
= dateTime
.getTime() * MICROSECONDS_IN_A_SECOND
;
147 } catch (ParseException e
) {
148 Activator
.logWarning("Creation date error: " + e
.getMessage()); //$NON-NLS-1$
152 fInputFile
= tokens
[1];
153 fProperties
.put(INPUTFILE
, fInputFile
);
156 fTsFormat
= BtfTimestampFormat
.parse(tokens
[1]);
157 fProperties
.put(TIMESCALE
, fTsFormat
.toString());
160 pos
= fFileInput
.getFilePointer();
161 line
= fFileInput
.readLine();
162 while (line
.startsWith("#-")) { //$NON-NLS-1$
163 String tempLine
= line
.substring(1);
164 String
[] elements
= tempLine
.split(" ", 2); //$NON-NLS-1$
165 fEntityTypes
.put(Integer
.parseInt(elements
[0]), BtfEventTypeFactory
.parse(elements
[1]));
166 pos
= fFileInput
.getFilePointer();
167 line
= fFileInput
.readLine();
169 fFileInput
.seek(pos
);
170 fProperties
.put(ENTITYTYPE
, fEntityTypes
.toString());
173 pos
= fFileInput
.getFilePointer();
174 line
= fFileInput
.readLine();
175 while (line
.startsWith("#-")) { //$NON-NLS-1$
176 String tempLine
= line
.substring(1);
177 String
[] elements
= tempLine
.split(" ", 2); //$NON-NLS-1$
178 fEntityTable
.put(Integer
.parseInt(elements
[0]), elements
[1]);
179 pos
= fFileInput
.getFilePointer();
180 line
= fFileInput
.readLine();
182 fProperties
.put(ENTITYTABLE
, fEntityTable
.toString());
183 fFileInput
.seek(pos
);
185 case lENTITYTYPETABLE
:
186 pos
= fFileInput
.getFilePointer();
187 line
= fFileInput
.readLine();
188 while (line
.startsWith("#-")) { //$NON-NLS-1$
189 String tempLine
= line
.substring(1);
190 String
[] elements
= tempLine
.split(" ", 2); //$NON-NLS-1$
191 fEntityTypeTable
.put(BtfEventTypeFactory
.parse(elements
[0]), elements
[1]);
192 pos
= fFileInput
.getFilePointer();
193 line
= fFileInput
.readLine();
195 fFileInput
.seek(pos
);
196 fProperties
.put(ENTITYTYPETABLE
, fEntityTypeTable
.toString());
201 fDataOffset
= input
.getFilePointer();
202 line
= input
.readLine();
204 fTsOffset
= (long) (fTsOffset
* fTsFormat
.getScaleFactor());
208 public void initTrace(IResource resource
, String path
, Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
209 super.initTrace(resource
, path
, type
);
210 fFile
= new File(path
);
212 fFileInput
= new RandomAccessFile(fFile
, "r"); //$NON-NLS-1$
213 parseHeader(fFileInput
);
214 } catch (IOException e
) {
215 throw new TmfTraceException(e
.getMessage(), e
);
221 public IStatus
validate(IProject project
, String path
) {
222 File file
= new File(path
);
223 if (!file
.exists()) {
224 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, "File not found: " + path
); //$NON-NLS-1$
226 if (!file
.isFile()) {
227 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, "Not a file. It's a directory: " + path
); //$NON-NLS-1$
230 try (BufferedRandomAccessFile rafile
= new BufferedRandomAccessFile(path
, "r")) { //$NON-NLS-1$
233 String line
= rafile
.getNextLine();
234 while ((line
!= null) && line
.startsWith("#")) { //$NON-NLS-1$
235 line
= rafile
.getNextLine();
237 while ((line
!= null) && (lineCount
++ < MAX_LINES
)) {
239 ITmfEvent event
= parseLine(0, line
);
243 } catch (RuntimeException e
) {
244 confidence
= Integer
.MIN_VALUE
;
247 confidence
= MAX_CONFIDENCE
* matches
/ lineCount
;
248 line
= rafile
.getNextLine();
250 } catch (IOException e
) {
251 Activator
.logError("Error validating file: " + path
, e
); //$NON-NLS-1$
252 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, "IOException validating file: " + path
, e
); //$NON-NLS-1$
255 return new TraceValidationStatus(confidence
, Activator
.PLUGIN_ID
);
259 public ITmfLocation
getCurrentLocation() {
262 temp
= fFileInput
.getFilePointer();
263 } catch (IOException e
) {
265 return new TmfLongLocation(temp
);
269 public double getLocationRatio(ITmfLocation location
) {
270 long size
= fFile
.length() - fDataOffset
;
273 pos
= fFileInput
.getFilePointer() - fDataOffset
;
274 } catch (IOException e
) {
277 return 1.0 / size
* pos
;
281 public ITmfContext
seekEvent(ITmfLocation location
) {
282 final TmfContext context
= new TmfContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
283 if (NULL_LOCATION
.equals(location
) || fFile
== null) {
287 if (location
== null) {
288 fFileInput
.seek(fDataOffset
);
289 } else if (location
.getLocationInfo() instanceof Long
) {
290 fFileInput
.seek((Long
) location
.getLocationInfo());
292 context
.setLocation(new TmfLongLocation(fFileInput
.getFilePointer()));
294 } catch (final FileNotFoundException e
) {
295 Activator
.logError("Error seeking event. File not found: " + getPath(), e
); //$NON-NLS-1$
297 } catch (final IOException e
) {
298 Activator
.logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
304 public ITmfContext
seekEvent(double ratio
) {
306 return new TmfContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
309 long pos
= Math
.round(ratio
* fFile
.length()) - fDataOffset
;
311 fFileInput
.seek(pos
- 1);
312 if (fFileInput
.read() == '\n') {
317 final ITmfLocation location
= new TmfLongLocation(pos
);
318 final ITmfContext context
= seekEvent(location
);
319 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
321 } catch (final IOException e
) {
322 Activator
.logError("Error seeking event. File: " + getPath(), e
); //$NON-NLS-1$
323 return new CustomTxtTraceContext(NULL_LOCATION
, ITmfContext
.UNKNOWN_RANK
);
328 public ITmfEvent
parseEvent(ITmfContext tmfContext
) {
329 if (fFile
== null || (!(tmfContext
instanceof TmfContext
))) {
333 final TmfContext context
= (TmfContext
) tmfContext
;
334 if (context
.getLocation() == null
335 || !(context
.getLocation().getLocationInfo() instanceof Long
)
336 || NULL_LOCATION
.equals(context
.getLocation())) {
340 return parseLine(context
);
345 * Parse a line with a context
348 * the context, has a location
349 * @return the event from a given line
351 private ITmfEvent
parseLine(TmfContext context
) {
353 if (!context
.getLocation().getLocationInfo().equals(fFileInput
.getFilePointer())) {
354 seekEvent(context
.getLocation());
356 } catch (IOException e1
) {
357 seekEvent(context
.getLocation());
361 line
= fFileInput
.readLine();
362 return parseLine(context
.getRank(), line
);
364 } catch (IOException e
) {
371 * Parse a line of text and make an event using it.
374 * the rank of the event
376 * the raw string of the event
379 private ITmfEvent
parseLine(long rank
, String line
) {
383 String
[] tokens
= line
.split(",", MAX_FIELDS
); //$NON-NLS-1$
384 if (tokens
.length
< MAX_FIELDS
) {
388 long timestamp
= Long
.parseLong(tokens
[i
++]);
389 String source
= tokens
[i
++];
390 long sourceInstance
= -1;
392 sourceInstance
= Long
.parseLong(tokens
[i
++]);
393 } catch (NumberFormatException e
) {
394 // this field can be empty
396 BtfEventType type
= BtfEventTypeFactory
.parse(tokens
[i
++]);
397 String target
= tokens
[i
++];
398 long targetInstance
= -1;
400 targetInstance
= Long
.parseLong(tokens
[i
++]);
401 } catch (NumberFormatException e
) {
402 // this field can be empty
404 String event
= tokens
[i
++];
406 ITmfEventField content
= type
.generateContent(event
, sourceInstance
, targetInstance
);
408 return new BtfEvent(this, rank
,
409 getTimestampTransform().transform(fTsFormat
.createTimestamp(timestamp
+ fTsOffset
)),
412 type
.getDescription(),
418 public int getCheckpointSize() {
419 synchronized (BtfTrace
.class) {
420 if (fCheckpointSize
== -1) {
421 TmfCheckpoint c
= new TmfCheckpoint(TmfTimestamp
.ZERO
, new TmfLongLocation(0L), 0);
422 ByteBuffer b
= ByteBuffer
.allocate(ITmfCheckpoint
.MAX_SERIALIZE_SIZE
);
425 fCheckpointSize
= b
.position();
429 return fCheckpointSize
;
433 public ITmfLocation
restoreLocation(ByteBuffer bufferIn
) {
434 return new TmfLongLocation(bufferIn
);
438 protected ITmfTraceIndexer
createIndexer(int interval
) {
439 return new TmfBTreeTraceIndexer(this, interval
);
443 public Map
<String
, String
> getTraceProperties() {
444 return ImmutableMap
.copyOf(fProperties
);
448 public Iterable
<ITmfEventAspect
> getEventAspects() {
449 return BtfEventAspects
.getAspects();
453 public void close() throws IOException
{
454 if (fFileInput
!= null) {