1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
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
10 * Matthew Khouzam - Initial API and implementation
11 * Alexandre Montplaisir - Initial API and implementation
12 * Simon Delisle - Replace LinkedList by TreeSet in callsitesByName attribute
13 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.ctf
.core
.trace
;
18 import java
.io
.FileFilter
;
19 import java
.io
.FileInputStream
;
20 import java
.io
.IOException
;
21 import java
.io
.Serializable
;
22 import java
.nio
.ByteOrder
;
23 import java
.nio
.MappedByteBuffer
;
24 import java
.nio
.channels
.FileChannel
;
25 import java
.nio
.channels
.FileChannel
.MapMode
;
26 import java
.util
.Arrays
;
27 import java
.util
.Collections
;
28 import java
.util
.Comparator
;
29 import java
.util
.HashMap
;
30 import java
.util
.Iterator
;
32 import java
.util
.Map
.Entry
;
34 import java
.util
.TreeSet
;
35 import java
.util
.UUID
;
37 import org
.eclipse
.linuxtools
.ctf
.core
.event
.CTFCallsite
;
38 import org
.eclipse
.linuxtools
.ctf
.core
.event
.CTFClock
;
39 import org
.eclipse
.linuxtools
.ctf
.core
.event
.IEventDeclaration
;
40 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
41 import org
.eclipse
.linuxtools
.ctf
.core
.event
.scope
.IDefinitionScope
;
42 import org
.eclipse
.linuxtools
.ctf
.core
.event
.scope
.LexicalScope
;
43 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
44 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
45 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
46 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
47 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.CTFCallsiteComparator
;
48 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.metadata
.exceptions
.ParseException
;
49 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.types
.ArrayDefinition
;
52 * A CTF trace on the file system.
54 * Represents a trace on the filesystem. It is responsible of parsing the
55 * metadata, creating declarations data structures, indexing the event packets
56 * (in other words, all the work that can be shared between readers), but the
57 * actual reading of events is left to TraceReader.
59 * @author Matthew Khouzam
60 * @version $Revision: 1.0 $
62 public class CTFTrace
implements IDefinitionScope
, AutoCloseable
{
65 public String
toString() {
66 /* Only for debugging, shouldn't be externalized */
67 return "CTFTrace [path=" + fPath
+ ", major=" + fMajor
+ ", minor=" //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
68 + fMinor
+ ", uuid=" + fUuid
+ "]"; //$NON-NLS-1$ //$NON-NLS-2$
72 * The trace directory on the filesystem.
74 private final File fPath
;
77 * Major CTF version number
82 * Minor CTF version number
94 private ByteOrder fByteOrder
;
97 * Packet header structure declaration
99 private StructDeclaration fPacketHeaderDecl
= null;
102 * The clock of the trace
104 private CTFClock fSingleClock
;
107 * Packet header structure definition
109 * This is only used when opening the trace files, to read the first packet
110 * header and see if they are valid trace files.
112 private StructDefinition fPacketHeaderDef
;
115 * Collection of streams contained in the trace.
117 private final Map
<Long
, CTFStream
> fStreams
= new HashMap
<>();
120 * Collection of environment variables set by the tracer
122 private final Map
<String
, String
> fEnvironment
= new HashMap
<>();
125 * Collection of all the clocks in a system.
127 private final Map
<String
, CTFClock
> fClocks
= new HashMap
<>();
129 /** Handlers for the metadata files */
130 private static final FileFilter METADATA_FILE_FILTER
= new MetadataFileFilter();
131 private static final Comparator
<File
> METADATA_COMPARATOR
= new MetadataComparator();
133 /** Callsite helpers */
134 private CTFCallsiteComparator fCtfCallsiteComparator
= new CTFCallsiteComparator();
136 private Map
<String
, TreeSet
<CTFCallsite
>> fCallsitesByName
= new HashMap
<>();
138 /** Callsite helpers */
139 private TreeSet
<CTFCallsite
> fCallsitesByIP
= new TreeSet
<>();
141 // ------------------------------------------------------------------------
143 // ------------------------------------------------------------------------
149 * Filesystem path of the trace directory
150 * @throws CTFReaderException
151 * If no CTF trace was found at the path
153 public CTFTrace(String path
) throws CTFReaderException
{
154 this(new File(path
));
162 * Filesystem path of the trace directory.
163 * @throws CTFReaderException
164 * If no CTF trace was found at the path
166 public CTFTrace(File path
) throws CTFReaderException
{
168 final Metadata metadata
= new Metadata(this);
170 /* Set up the internal containers for this trace */
171 if (!fPath
.exists()) {
172 throw new CTFReaderException("Trace (" + path
.getPath() + ") doesn't exist. Deleted or moved?"); //$NON-NLS-1$ //$NON-NLS-2$
175 if (!fPath
.isDirectory()) {
176 throw new CTFReaderException("Path must be a valid directory"); //$NON-NLS-1$
179 /* Open and parse the metadata file */
180 metadata
.parseFile();
186 * Streamed constructor
194 private void init(File path
) throws CTFReaderException
{
196 /* Open all the trace files */
198 /* List files not called metadata and not hidden. */
199 File
[] files
= path
.listFiles(METADATA_FILE_FILTER
);
200 Arrays
.sort(files
, METADATA_COMPARATOR
);
202 /* Try to open each file */
203 for (File streamFile
: files
) {
204 openStreamInput(streamFile
);
207 /* Create their index */
208 for (CTFStream stream
: getStreams()) {
209 Set
<CTFStreamInput
> inputs
= stream
.getStreamInputs();
210 for (CTFStreamInput s
: inputs
) {
219 * FIXME Not needed anymore, class doesn't need to be AutoCloseable.
224 public void close() {
227 // ------------------------------------------------------------------------
228 // Getters/Setters/Predicates
229 // ------------------------------------------------------------------------
232 * Gets an event declaration hash map for a given streamID
235 * The ID of the stream from which to read
236 * @return The Hash map with the event declarations
239 public Map
<Long
, IEventDeclaration
> getEvents(Long streamId
) {
240 return fStreams
.get(streamId
).getEvents();
244 * Get an event by it's ID
247 * The ID of the stream from which to read
249 * the ID of the event
250 * @return the event declaration
253 public IEventDeclaration
getEventType(long streamId
, long id
) {
254 return getEvents(streamId
).get(id
);
258 * Method getStream gets the stream for a given id
261 * Long the id of the stream
262 * @return Stream the stream that we need
265 public CTFStream
getStream(Long id
) {
266 return fStreams
.get(id
);
270 * Method nbStreams gets the number of available streams
272 * @return int the number of streams
274 public int nbStreams() {
275 return fStreams
.size();
279 * Method setMajor sets the major version of the trace (DO NOT USE)
282 * long the major version
284 public void setMajor(long major
) {
289 * Method setMinor sets the minor version of the trace (DO NOT USE)
292 * long the minor version
294 public void setMinor(long minor
) {
299 * Method setUUID sets the UUID of a trace
304 public void setUUID(UUID uuid
) {
309 * Method setByteOrder sets the byte order
312 * ByteOrder of the trace, can be little-endian or big-endian
314 public void setByteOrder(ByteOrder byteOrder
) {
315 fByteOrder
= byteOrder
;
319 * Method setPacketHeader sets the packet header of a trace (DO NOT USE)
321 * @param packetHeader
322 * StructDeclaration the header in structdeclaration form
324 public void setPacketHeader(StructDeclaration packetHeader
) {
325 fPacketHeaderDecl
= packetHeader
;
329 * Method majorIsSet is the major version number set?
331 * @return boolean is the major set?
334 public boolean majorIsSet() {
335 return fMajor
!= null;
339 * Method minorIsSet. is the minor version number set?
341 * @return boolean is the minor set?
343 public boolean minorIsSet() {
344 return fMinor
!= null;
348 * Method UUIDIsSet is the UUID set?
350 * @return boolean is the UUID set?
353 public boolean uuidIsSet() {
354 return fUuid
!= null;
358 * Method byteOrderIsSet is the byteorder set?
360 * @return boolean is the byteorder set?
362 public boolean byteOrderIsSet() {
363 return fByteOrder
!= null;
367 * Method packetHeaderIsSet is the packet header set?
369 * @return boolean is the packet header set?
371 public boolean packetHeaderIsSet() {
372 return fPacketHeaderDecl
!= null;
376 * Method getUUID gets the trace UUID
378 * @return UUID gets the trace UUID
380 public UUID
getUUID() {
385 * Method getMajor gets the trace major version
387 * @return long gets the trace major version
389 public long getMajor() {
394 * Method getMinor gets the trace minor version
396 * @return long gets the trace minor version
398 public long getMinor() {
403 * Method getByteOrder gets the trace byte order
405 * @return ByteOrder gets the trace byte order
407 public final ByteOrder
getByteOrder() {
412 * Method getPacketHeader gets the trace packet header
414 * @return StructDeclaration gets the trace packet header
416 public StructDeclaration
getPacketHeader() {
417 return fPacketHeaderDecl
;
421 * Method getTraceDirectory gets the trace directory
423 * @return File the path in "File" format.
425 public File
getTraceDirectory() {
430 * Get all the streams as an iterable.
432 * @return Iterable<Stream> an iterable over streams.
435 public Iterable
<CTFStream
> getStreams() {
436 return fStreams
.values();
440 * Method getPath gets the path of the trace directory
442 * @return String the path of the trace directory, in string format.
443 * @see java.io.File#getPath()
445 public String
getPath() {
446 return (fPath
!= null) ? fPath
.getPath() : ""; //$NON-NLS-1$
453 public LexicalScope
getScopePath() {
454 return LexicalScope
.TRACE
;
457 // ------------------------------------------------------------------------
459 // ------------------------------------------------------------------------
461 private void addStream(CTFStreamInput s
) {
466 Iterator
<Entry
<Long
, IEventDeclaration
>> it
= s
.getStream()
467 .getEvents().entrySet().iterator();
468 while (it
.hasNext()) {
469 Entry
<Long
, IEventDeclaration
> pairs
= it
.next();
470 Long eventNum
= pairs
.getKey();
471 IEventDeclaration eventDec
= pairs
.getValue();
472 getEvents(s
.getStream().getId()).put(eventNum
, eventDec
);
482 * Tries to open the given file, reads the first packet header of the file
483 * and check its validity. This will add a file to a stream as a streaminput
486 * A trace file in the trace directory.
488 * Which index in the class' streamFileChannel array this file
490 * @throws CTFReaderException
491 * if there is a file error
493 private CTFStream
openStreamInput(File streamFile
) throws CTFReaderException
{
494 MappedByteBuffer byteBuffer
;
495 BitBuffer streamBitBuffer
;
498 if (!streamFile
.canRead()) {
499 throw new CTFReaderException("Unreadable file : " //$NON-NLS-1$
500 + streamFile
.getPath());
503 try (FileInputStream fis
= new FileInputStream(streamFile
);
504 FileChannel fc
= fis
.getChannel()) {
505 /* Map one memory page of 4 kiB */
506 byteBuffer
= fc
.map(MapMode
.READ_ONLY
, 0, (int) Math
.min(fc
.size(), 4096L));
507 } catch (IOException e
) {
508 /* Shouldn't happen at this stage if every other check passed */
509 throw new CTFReaderException(e
);
512 /* Create a BitBuffer with this mapping and the trace byte order */
513 streamBitBuffer
= new BitBuffer(byteBuffer
, this.getByteOrder());
515 if (fPacketHeaderDecl
!= null) {
516 /* Read the packet header */
517 fPacketHeaderDef
= fPacketHeaderDecl
.createDefinition(null, LexicalScope
.PACKET_HEADER
.getName(), streamBitBuffer
);
519 /* Check the magic number */
520 IntegerDefinition magicDef
= (IntegerDefinition
) fPacketHeaderDef
.lookupDefinition("magic"); //$NON-NLS-1$
521 int magic
= (int) magicDef
.getValue();
522 if (magic
!= Utils
.CTF_MAGIC
) {
523 throw new CTFReaderException("CTF magic mismatch"); //$NON-NLS-1$
527 Definition lookupDefinition
= fPacketHeaderDef
.lookupDefinition("uuid"); //$NON-NLS-1$
528 ArrayDefinition uuidDef
= (ArrayDefinition
) lookupDefinition
;
529 if (uuidDef
!= null) {
530 UUID otheruuid
= Utils
.getUUIDfromDefinition(uuidDef
);
532 if (!fUuid
.equals(otheruuid
)) {
533 throw new CTFReaderException("UUID mismatch"); //$NON-NLS-1$
537 /* Read the stream ID */
538 Definition streamIDDef
= fPacketHeaderDef
.lookupDefinition("stream_id"); //$NON-NLS-1$
540 if (streamIDDef
instanceof IntegerDefinition
) { // this doubles as a
542 long streamID
= ((IntegerDefinition
) streamIDDef
).getValue();
543 stream
= fStreams
.get(streamID
);
545 /* No stream_id in the packet header */
546 stream
= fStreams
.get(null);
550 /* No packet header, we suppose there is only one stream */
551 stream
= fStreams
.get(null);
554 if (stream
== null) {
555 throw new CTFReaderException("Unexpected end of stream"); //$NON-NLS-1$
559 * Create the stream input and add a reference to the streamInput in the
562 stream
.addInput(new CTFStreamInput(stream
, streamFile
));
568 * Looks up a definition from packet
573 * @see org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope#lookupDefinition(String)
576 public Definition
lookupDefinition(String lookupPath
) {
577 if (lookupPath
.equals("trace.packet.header")) { //$NON-NLS-1$
578 return fPacketHeaderDef
;
584 * Add a new stream file to support new streams while the trace is being
588 * the file of the stream
589 * @throws CTFReaderException
590 * A stream had an issue being read
593 public void addStreamFile(File streamFile
) throws CTFReaderException
{
594 openStreamInput(streamFile
);
598 * Registers a new stream to the trace.
602 * @throws ParseException
603 * If there was some problem reading the metadata
606 public void addStream(CTFStream stream
) throws ParseException
{
608 * If there is already a stream without id (the null key), it must be
611 if (fStreams
.get(null) != null) {
612 throw new ParseException("Stream without id with multiple streams"); //$NON-NLS-1$
616 * If the stream we try to add has the null key, it must be the only
617 * one. Thus, if the streams container is not empty, it is not valid.
619 if ((stream
.getId() == null) && (fStreams
.size() != 0)) {
620 throw new ParseException("Stream without id with multiple streams"); //$NON-NLS-1$
624 * If a stream with the same ID already exists, it is not valid.
626 CTFStream existingStream
= fStreams
.get(stream
.getId());
627 if (existingStream
!= null) {
628 throw new ParseException("Stream id already exists"); //$NON-NLS-1$
631 /* This stream is valid and has a unique id. */
632 fStreams
.put(stream
.getId(), stream
);
636 * Gets the Environment variables from the trace metadata (See CTF spec)
638 * @return The environment variables in the form of an unmodifiable map
642 public Map
<String
, String
> getEnvironment() {
643 return Collections
.unmodifiableMap(fEnvironment
);
647 * Add a variable to the environment variables
650 * the name of the variable
652 * the value of the variable
654 public void addEnvironmentVar(String varName
, String varValue
) {
655 fEnvironment
.put(varName
, varValue
);
659 * Add a clock to the clock list
662 * the name of the clock (full name with scope)
666 public void addClock(String nameValue
, CTFClock ctfClock
) {
667 fClocks
.put(nameValue
, ctfClock
);
671 * gets the clock with a specific name
674 * the name of the clock.
677 public CTFClock
getClock(String name
) {
678 return fClocks
.get(name
);
682 * gets the clock if there is only one. (this is 100% of the use cases as of
687 public final CTFClock
getClock() {
688 if (fClocks
.size() == 1) {
689 fSingleClock
= fClocks
.get(fClocks
.keySet().iterator().next());
696 * gets the time offset of a clock with respect to UTC in nanoseconds
698 * @return the time offset of a clock with respect to UTC in nanoseconds
700 public final long getOffset() {
701 if (getClock() == null) {
704 return fSingleClock
.getClockOffset();
708 * gets the time offset of a clock with respect to UTC in nanoseconds
710 * @return the time offset of a clock with respect to UTC in nanoseconds
712 private double getTimeScale() {
713 if (getClock() == null) {
716 return fSingleClock
.getClockScale();
720 * Gets the current first packet start time
721 * @return the current start time
724 public long getCurrentStartTime() {
725 long currentStart
= Long
.MAX_VALUE
;
726 for (CTFStream stream
: fStreams
.values()) {
727 for (CTFStreamInput si
: stream
.getStreamInputs()) {
728 currentStart
= Math
.min(currentStart
, si
.getIndex().getEntries().get(0).getTimestampBegin());
731 return timestampCyclesToNanos(currentStart
);
735 * Gets the current last packet end time
736 * @return the current end time
739 public long getCurrentEndTime() {
740 long currentEnd
= Long
.MIN_VALUE
;
741 for (CTFStream stream
: fStreams
.values()) {
742 for (CTFStreamInput si
: stream
.getStreamInputs()) {
743 currentEnd
= Math
.max(currentEnd
, si
.getTimestampEnd());
746 return timestampCyclesToNanos(currentEnd
);
750 * Does the trace need to time scale?
752 * @return if the trace is in ns or cycles.
754 private boolean clockNeedsScale() {
755 if (getClock() == null) {
758 return fSingleClock
.isClockScaled();
762 * the inverse clock for returning to a scale.
764 * @return 1.0 / scale
766 private double getInverseTimeScale() {
767 if (getClock() == null) {
770 return fSingleClock
.getClockAntiScale();
775 * clock cycles since boot
776 * @return time in nanoseconds UTC offset
779 public long timestampCyclesToNanos(long cycles
) {
780 long retVal
= cycles
+ getOffset();
782 * this fix is since quite often the offset will be > than 53 bits and
783 * therefore the conversion will be lossy
785 if (clockNeedsScale()) {
786 retVal
= (long) (retVal
* getTimeScale());
793 * time in nanoseconds UTC offset
794 * @return clock cycles since boot.
797 public long timestampNanoToCycles(long nanos
) {
800 * this fix is since quite often the offset will be > than 53 bits and
801 * therefore the conversion will be lossy
803 if (clockNeedsScale()) {
804 retVal
= (long) (nanos
* getInverseTimeScale());
808 return retVal
- getOffset();
815 * the event name of the callsite
817 * the name of the callsite function
819 * the ip of the callsite
821 * the filename of the callsite
823 * the line number of the callsite
825 public void addCallsite(String eventName
, String funcName
, long ip
,
826 String fileName
, long lineNumber
) {
827 final CTFCallsite cs
= new CTFCallsite(eventName
, funcName
, ip
,
828 fileName
, lineNumber
);
829 TreeSet
<CTFCallsite
> csl
= fCallsitesByName
.get(eventName
);
831 csl
= new TreeSet
<>(fCtfCallsiteComparator
);
832 fCallsitesByName
.put(eventName
, csl
);
837 fCallsitesByIP
.add(cs
);
841 * Gets the set of callsites associated to an event name. O(1)
845 * @return the callsite set can be empty
848 public TreeSet
<CTFCallsite
> getCallsiteCandidates(String eventName
) {
849 TreeSet
<CTFCallsite
> retVal
= fCallsitesByName
.get(eventName
);
850 if (retVal
== null) {
851 retVal
= new TreeSet
<>(fCtfCallsiteComparator
);
857 * The I'm feeling lucky of getCallsiteCandidates O(1)
861 * @return the first callsite that has that event name, can be null
864 public CTFCallsite
getCallsite(String eventName
) {
865 TreeSet
<CTFCallsite
> callsites
= fCallsitesByName
.get(eventName
);
866 if (callsites
!= null) {
867 return callsites
.first();
873 * Gets a callsite from the instruction pointer O(log(n))
876 * the instruction pointer to lookup
877 * @return the callsite just before that IP in the list remember the IP is
878 * backwards on X86, can be null if no callsite is before the IP.
881 public CTFCallsite
getCallsite(long ip
) {
882 CTFCallsite cs
= new CTFCallsite(null, null, ip
, null, 0L);
883 return fCallsitesByIP
.ceiling(cs
);
887 * Gets a callsite using the event name and instruction pointer O(log(n))
890 * the name of the event
892 * the instruction pointer
893 * @return the closest matching callsite, can be null
895 public CTFCallsite
getCallsite(String eventName
, long ip
) {
896 final TreeSet
<CTFCallsite
> candidates
= fCallsitesByName
.get(eventName
);
897 if (candidates
== null) {
900 final CTFCallsite dummyCs
= new CTFCallsite(null, null, ip
, null, -1);
901 final CTFCallsite callsite
= candidates
.ceiling(dummyCs
);
902 if (callsite
== null) {
903 return candidates
.floor(dummyCs
);
912 * the ID of the stream
914 * new file in the stream
915 * @throws CTFReaderException
916 * The file must exist
919 public void addStream(long id
, File streamFile
) throws CTFReaderException
{
920 CTFStream stream
= null;
921 if (fStreams
.containsKey(id
)) {
922 stream
= fStreams
.get(id
);
924 stream
= new CTFStream(this);
925 fStreams
.put(id
, stream
);
927 stream
.addInput(new CTFStreamInput(stream
, streamFile
));
931 class MetadataFileFilter
implements FileFilter
{
934 public boolean accept(File pathname
) {
935 if (pathname
.isDirectory()) {
938 if (pathname
.isHidden()) {
941 if (pathname
.getName().equals("metadata")) { //$NON-NLS-1$
949 class MetadataComparator
implements Comparator
<File
>, Serializable
{
951 private static final long serialVersionUID
= 1L;
954 public int compare(File o1
, File o2
) {
955 return o1
.getName().compareTo(o2
.getName());