1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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
.Comparator
;
28 import java
.util
.HashMap
;
29 import java
.util
.Iterator
;
30 import java
.util
.LinkedList
;
31 import java
.util
.List
;
33 import java
.util
.Map
.Entry
;
35 import java
.util
.TreeSet
;
36 import java
.util
.UUID
;
38 import org
.eclipse
.linuxtools
.ctf
.core
.event
.CTFCallsite
;
39 import org
.eclipse
.linuxtools
.ctf
.core
.event
.CTFClock
;
40 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
41 import org
.eclipse
.linuxtools
.ctf
.core
.event
.IEventDeclaration
;
42 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
43 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDefinition
;
44 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
45 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDefinitionScope
;
46 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
47 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
48 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
49 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.CTFCallsiteComparator
;
50 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.metadata
.exceptions
.ParseException
;
53 * A CTF trace on the file system.
55 * Represents a trace on the filesystem. It is responsible of parsing the
56 * metadata, creating declarations data structures, indexing the event packets
57 * (in other words, all the work that can be shared between readers), but the
58 * actual reading of events is left to TraceReader.
60 * @author Matthew Khouzam
61 * @version $Revision: 1.0 $
63 public class CTFTrace
implements IDefinitionScope
{
65 @SuppressWarnings("nls")
67 public String
toString() {
68 /* Only for debugging, shouldn't be externalized */
69 return "CTFTrace [path=" + path
+ ", major=" + major
+ ", minor="
70 + minor
+ ", uuid=" + uuid
+ "]";
74 * The trace directory on the filesystem.
76 private final File path
;
79 * Major CTF version number
84 * Minor CTF version number
96 private ByteOrder byteOrder
;
99 * Packet header structure declaration
101 private StructDeclaration packetHeaderDecl
= null;
104 * The clock of the trace
106 private CTFClock singleClock
;
109 * Packet header structure definition
111 * This is only used when opening the trace files, to read the first packet
112 * header and see if they are valid trace files.
114 private StructDefinition packetHeaderDef
;
117 * Collection of streams contained in the trace.
119 private final Map
<Long
, Stream
> streams
= new HashMap
<Long
, Stream
>();
122 * Collection of environment variables set by the tracer
124 private final Map
<String
, String
> environment
= new HashMap
<String
, String
>();
127 * Collection of all the clocks in a system.
129 private final Map
<String
, CTFClock
> clocks
= new HashMap
<String
, CTFClock
>();
131 /** FileInputStreams to the streams */
132 private final List
<FileInputStream
> fileInputStreams
= new LinkedList
<FileInputStream
>();
134 /** Handlers for the metadata files */
135 private static final FileFilter METADATA_FILE_FILTER
= new MetadataFileFilter();
136 private static final Comparator
<File
> METADATA_COMPARATOR
= new MetadataComparator();
138 /** map of all the event types */
139 private final Map
<Long
, HashMap
<Long
, IEventDeclaration
>> eventDecs
= new HashMap
<Long
, HashMap
<Long
, IEventDeclaration
>>();
141 /** Callsite helpers */
142 private CTFCallsiteComparator ctfCallsiteComparator
= new CTFCallsiteComparator();
144 private Map
<String
, TreeSet
<CTFCallsite
>> callsitesByName
= new HashMap
<String
, TreeSet
<CTFCallsite
>>();
146 /** Callsite helpers */
147 private TreeSet
<CTFCallsite
> callsitesByIP
= new TreeSet
<CTFCallsite
>();
149 // ------------------------------------------------------------------------
151 // ------------------------------------------------------------------------
157 * Filesystem path of the trace directory
158 * @throws CTFReaderException
159 * If no CTF trace was found at the path
161 public CTFTrace(String path
) throws CTFReaderException
{
162 this(new File(path
));
170 * Filesystem path of the trace directory.
171 * @throws CTFReaderException
172 * If no CTF trace was found at the path
174 public CTFTrace(File path
) throws CTFReaderException
{
176 final Metadata metadata
= new Metadata(this);
178 /* Set up the internal containers for this trace */
179 if (!this.path
.exists()) {
180 throw new CTFReaderException("Trace (" + path
.getPath() + ") doesn't exist. Deleted or moved?"); //$NON-NLS-1$ //$NON-NLS-2$
183 if (!this.path
.isDirectory()) {
184 throw new CTFReaderException("Path must be a valid directory"); //$NON-NLS-1$
187 /* Open and parse the metadata file */
190 /* Open all the trace files */
191 /* Create the definitions needed to read things from the files */
192 if (packetHeaderDecl
!= null) {
193 packetHeaderDef
= packetHeaderDecl
.createDefinition(this, "packet.header"); //$NON-NLS-1$
196 /* List files not called metadata and not hidden. */
197 File
[] files
= path
.listFiles(METADATA_FILE_FILTER
);
198 Arrays
.sort(files
, METADATA_COMPARATOR
);
199 /* Try to open each file */
200 for (File streamFile
: files
) {
201 openStreamInput(streamFile
);
204 /* Create their index */
205 for (Map
.Entry
<Long
, Stream
> stream
: streams
.entrySet()) {
206 Set
<StreamInput
> inputs
= stream
.getValue().getStreamInputs();
207 for (StreamInput s
: inputs
) {
211 Iterator
<Entry
<Long
, IEventDeclaration
>> it
= s
.getStream()
212 .getEvents().entrySet().iterator();
213 while (it
.hasNext()) {
214 Entry
<Long
, IEventDeclaration
> pairs
= it
.next();
215 Long eventNum
= pairs
.getKey();
216 IEventDeclaration eventDec
= pairs
.getValue();
217 getEvents(s
.getStream().getId()).put(eventNum
, eventDec
);
233 public void dispose() {
234 for (FileInputStream fis
: fileInputStreams
) {
238 } catch (IOException e
) {
239 // do nothing it's ok, we tried to close it.
243 // Invoke GC to release MappedByteBuffer objects (Java bug JDK-4724038)
247 // ------------------------------------------------------------------------
248 // Getters/Setters/Predicates
249 // ------------------------------------------------------------------------
252 * Gets an event declaration hash map for a given streamID
255 * The ID of the stream from which to read
256 * @return The Hash map with the event declarations
259 public Map
<Long
, IEventDeclaration
> getEvents(Long streamId
) {
260 return eventDecs
.get(streamId
);
264 * Gets an event Declaration hashmap for a given StreamInput
268 * @return an empty hashmap, please see deprecated
270 * @deprecated You should be using
271 * {@link StreamInputReader#getEventDefinitions()} instead.
274 public Map
<Long
, EventDefinition
> getEventDefs(StreamInput id
) {
275 return new HashMap
<Long
, EventDefinition
>();
279 * Get an event by it's ID
282 * The ID of the stream from which to read
284 * the ID of the event
285 * @return the event declaration
288 public IEventDeclaration
getEventType(long streamId
, long id
) {
289 return getEvents(streamId
).get(id
);
293 * Method getStream gets the stream for a given id
296 * Long the id of the stream
297 * @return Stream the stream that we need
300 public Stream
getStream(Long id
) {
301 return streams
.get(id
);
305 * Method nbStreams gets the number of available streams
307 * @return int the number of streams
309 public int nbStreams() {
310 return streams
.size();
314 * Method setMajor sets the major version of the trace (DO NOT USE)
317 * long the major version
319 public void setMajor(long major
) {
324 * Method setMinor sets the minor version of the trace (DO NOT USE)
327 * long the minor version
329 public void setMinor(long minor
) {
334 * Method setUUID sets the UUID of a trace
339 public void setUUID(UUID uuid
) {
344 * Method setByteOrder sets the byte order
347 * ByteOrder of the trace, can be little-endian or big-endian
349 public void setByteOrder(ByteOrder byteOrder
) {
350 this.byteOrder
= byteOrder
;
354 * Method setPacketHeader sets the packet header of a trace (DO NOT USE)
356 * @param packetHeader
357 * StructDeclaration the header in structdeclaration form
359 public void setPacketHeader(StructDeclaration packetHeader
) {
360 this.packetHeaderDecl
= packetHeader
;
364 * Method majorIsSet is the major version number set?
366 * @return boolean is the major set?
368 public boolean majorIsSet() {
369 return major
!= null;
373 * Method minorIsSet. is the minor version number set?
375 * @return boolean is the minor set?
377 public boolean minorIsSet() {
378 return minor
!= null;
382 * Method UUIDIsSet is the UUID set?
384 * @return boolean is the UUID set?
387 public boolean uuidIsSet() {
392 * Method byteOrderIsSet is the byteorder set?
394 * @return boolean is the byteorder set?
396 public boolean byteOrderIsSet() {
397 return byteOrder
!= null;
401 * Method packetHeaderIsSet is the packet header set?
403 * @return boolean is the packet header set?
405 public boolean packetHeaderIsSet() {
406 return packetHeaderDecl
!= null;
410 * Method getUUID gets the trace UUID
412 * @return UUID gets the trace UUID
414 public UUID
getUUID() {
419 * Method getMajor gets the trace major version
421 * @return long gets the trace major version
423 public long getMajor() {
428 * Method getMinor gets the trace minor version
430 * @return long gets the trace minor version
432 public long getMinor() {
437 * Method getByteOrder gets the trace byte order
439 * @return ByteOrder gets the trace byte order
441 public final ByteOrder
getByteOrder() {
446 * Method getPacketHeader gets the trace packet header
448 * @return StructDeclaration gets the trace packet header
450 public StructDeclaration
getPacketHeader() {
451 return packetHeaderDecl
;
455 * Method getTraceDirectory gets the trace directory
457 * @return File the path in "File" format.
459 public File
getTraceDirectory() {
464 * Method getStreams get all the streams in a map format.
466 * @return Map<Long,Stream> a map of all the streams.
468 public Map
<Long
, Stream
> getStreams() {
473 * Method getPath gets the path of the trace directory
475 * @return String the path of the trace directory, in string format.
476 * @see java.io.File#getPath()
479 public String
getPath() {
480 return path
.getPath();
483 // ------------------------------------------------------------------------
485 // ------------------------------------------------------------------------
488 * Tries to open the given file, reads the first packet header of the file
489 * and check its validity.
492 * A trace file in the trace directory.
494 * Which index in the class' streamFileChannel array this file
496 * @throws CTFReaderException
498 private void openStreamInput(File streamFile
) throws CTFReaderException
{
499 MappedByteBuffer byteBuffer
;
500 BitBuffer streamBitBuffer
;
504 if (!streamFile
.canRead()) {
505 throw new CTFReaderException("Unreadable file : " //$NON-NLS-1$
506 + streamFile
.getPath());
510 /* Open the file and get the FileChannel */
511 FileInputStream fis
= new FileInputStream(streamFile
);
512 fileInputStreams
.add(fis
);
513 fc
= fis
.getChannel();
515 /* Map one memory page of 4 kiB */
516 byteBuffer
= fc
.map(MapMode
.READ_ONLY
, 0, (int) Math
.min(fc
.size(), 4096L));
517 } catch (IOException e
) {
518 /* Shouldn't happen at this stage if every other check passed */
519 throw new CTFReaderException(e
);
522 /* Create a BitBuffer with this mapping and the trace byte order */
523 streamBitBuffer
= new BitBuffer(byteBuffer
, this.getByteOrder());
525 if (packetHeaderDef
!= null) {
526 /* Read the packet header */
527 packetHeaderDef
.read(streamBitBuffer
);
529 /* Check the magic number */
530 IntegerDefinition magicDef
= (IntegerDefinition
) packetHeaderDef
531 .lookupDefinition("magic"); //$NON-NLS-1$
532 int magic
= (int) magicDef
.getValue();
533 if (magic
!= Utils
.CTF_MAGIC
) {
534 throw new CTFReaderException("CTF magic mismatch"); //$NON-NLS-1$
538 ArrayDefinition uuidDef
= (ArrayDefinition
) packetHeaderDef
539 .lookupDefinition("uuid"); //$NON-NLS-1$
540 if (uuidDef
!= null) {
541 byte[] uuidArray
= new byte[Utils
.UUID_LEN
];
543 for (int i
= 0; i
< Utils
.UUID_LEN
; i
++) {
544 IntegerDefinition uuidByteDef
= (IntegerDefinition
) uuidDef
546 uuidArray
[i
] = (byte) uuidByteDef
.getValue();
549 UUID otheruuid
= Utils
.makeUUID(uuidArray
);
551 if (!this.uuid
.equals(otheruuid
)) {
552 throw new CTFReaderException("UUID mismatch"); //$NON-NLS-1$
556 /* Read the stream ID */
557 Definition streamIDDef
= packetHeaderDef
.lookupDefinition("stream_id"); //$NON-NLS-1$
559 if (streamIDDef
instanceof IntegerDefinition
) { // this doubles as a
561 long streamID
= ((IntegerDefinition
) streamIDDef
).getValue();
562 stream
= streams
.get(streamID
);
564 /* No stream_id in the packet header */
565 stream
= streams
.get(null);
569 /* No packet header, we suppose there is only one stream */
570 stream
= streams
.get(null);
573 if (stream
== null) {
574 throw new CTFReaderException("Unexpected end of stream"); //$NON-NLS-1$
577 /* Create the stream input */
578 StreamInput streamInput
= new StreamInput(stream
, fc
, streamFile
);
580 /* Add a reference to the streamInput in the stream */
581 stream
.addInput(streamInput
);
585 * Looks up a definition from packet
590 * @see org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope#lookupDefinition(String)
593 public Definition
lookupDefinition(String lookupPath
) {
594 if (lookupPath
.equals("trace.packet.header")) { //$NON-NLS-1$
595 return packetHeaderDef
;
601 * Adds a new stream to the trace.
605 * @throws ParseException
606 * If there was some problem reading the metadata
609 public void addStream(Stream stream
) throws ParseException
{
612 * If there is already a stream without id (the null key), it must be
615 if (streams
.get(null) != null) {
616 throw new ParseException("Stream without id with multiple streams"); //$NON-NLS-1$
620 * If the stream we try to add has the null key, it must be the only
621 * one. Thus, if the streams container is not empty, it is not valid.
623 if ((stream
.getId() == null) && (streams
.size() != 0)) {
624 throw new ParseException("Stream without id with multiple streams"); //$NON-NLS-1$
627 /* If a stream with the same ID already exists, it is not valid. */
628 if (streams
.get(stream
.getId()) != null) {
629 throw new ParseException("Stream id already exists"); //$NON-NLS-1$
632 /* It should be ok now. */
633 streams
.put(stream
.getId(), stream
);
634 eventDecs
.put(stream
.getId(), new HashMap
<Long
, IEventDeclaration
>());
638 * gets the Environment variables from the trace metadata (See CTF spec)
640 * @return the environment variables in a map form (key value)
643 public Map
<String
, String
> getEnvironment() {
648 * Look up a specific environment variable
651 * the key to look for
652 * @return the value of the variable, can be null.
654 public String
lookupEnvironment(String key
) {
655 return environment
.get(key
);
659 * Add a variable to the environment variables
662 * the name of the variable
664 * the value of the variable
666 public void addEnvironmentVar(String varName
, String varValue
) {
667 environment
.put(varName
, varValue
);
671 * Add a clock to the clock list
674 * the name of the clock (full name with scope)
678 public void addClock(String nameValue
, CTFClock ctfClock
) {
679 clocks
.put(nameValue
, ctfClock
);
683 * gets the clock with a specific name
686 * the name of the clock.
689 public CTFClock
getClock(String name
) {
690 return clocks
.get(name
);
694 * gets the clock if there is only one. (this is 100% of the use cases as of
699 public final CTFClock
getClock() {
700 if (clocks
.size() == 1) {
701 singleClock
= clocks
.get(clocks
.keySet().iterator().next());
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 public final long getOffset() {
713 if (getClock() == null) {
716 return singleClock
.getClockOffset();
720 * gets the time offset of a clock with respect to UTC in nanoseconds
722 * @return the time offset of a clock with respect to UTC in nanoseconds
724 private double getTimeScale() {
725 if (getClock() == null) {
728 return singleClock
.getClockScale();
732 * Does the trace need to time scale?
734 * @return if the trace is in ns or cycles.
736 private boolean clockNeedsScale() {
737 if (getClock() == null) {
740 return singleClock
.isClockScaled();
744 * the inverse clock for returning to a scale.
746 * @return 1.0 / scale
748 private double getInverseTimeScale() {
749 if (getClock() == null) {
752 return singleClock
.getClockAntiScale();
757 * clock cycles since boot
758 * @return time in nanoseconds UTC offset
761 public long timestampCyclesToNanos(long cycles
) {
762 long retVal
= cycles
+ getOffset();
764 * this fix is since quite often the offset will be > than 53 bits and
765 * therefore the conversion will be lossy
767 if (clockNeedsScale()) {
768 retVal
= (long) (retVal
* getTimeScale());
775 * time in nanoseconds UTC offset
776 * @return clock cycles since boot.
779 public long timestampNanoToCycles(long nanos
) {
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) (nanos
* getInverseTimeScale());
790 return retVal
- getOffset();
794 * Does a given stream contain any events?
798 * @return true if the stream has events.
800 public boolean hasEvents(Long id
) {
801 return eventDecs
.containsKey(id
);
805 * Add an event declaration map to the events map.
809 * @return the hashmap containing events.
812 public Map
<Long
, IEventDeclaration
> createEvents(Long id
) {
813 HashMap
<Long
, IEventDeclaration
> value
= eventDecs
.get(id
);
815 value
= new HashMap
<Long
, IEventDeclaration
>();
816 eventDecs
.put(id
, value
);
825 * the event name of the callsite
827 * the name of the callsite function
829 * the ip of the callsite
831 * the filename of the callsite
833 * the line number of the callsite
835 public void addCallsite(String eventName
, String funcName
, long ip
,
836 String fileName
, long lineNumber
) {
837 final CTFCallsite cs
= new CTFCallsite(eventName
, funcName
, ip
,
838 fileName
, lineNumber
);
839 TreeSet
<CTFCallsite
> csl
= callsitesByName
.get(eventName
);
841 csl
= new TreeSet
<CTFCallsite
>(ctfCallsiteComparator
);
842 callsitesByName
.put(eventName
, csl
);
847 callsitesByIP
.add(cs
);
851 * Gets the set of callsites associated to an event name. O(1)
855 * @return the callsite set can be empty
858 public TreeSet
<CTFCallsite
> getCallsiteCandidates(String eventName
) {
859 TreeSet
<CTFCallsite
> retVal
= callsitesByName
.get(eventName
);
860 if (retVal
== null) {
861 retVal
= new TreeSet
<CTFCallsite
>(ctfCallsiteComparator
);
867 * The I'm feeling lucky of getCallsiteCandidates O(1)
871 * @return the first callsite that has that event name, can be null
874 public CTFCallsite
getCallsite(String eventName
) {
875 TreeSet
<CTFCallsite
> callsites
= callsitesByName
.get(eventName
);
876 if (callsites
!= null) {
877 return callsites
.first();
883 * Gets a callsite from the instruction pointer O(log(n))
886 * the instruction pointer to lookup
887 * @return the callsite just before that IP in the list remember the IP is
888 * backwards on X86, can be null if no callsite is before the IP.
891 public CTFCallsite
getCallsite(long ip
) {
892 CTFCallsite cs
= new CTFCallsite(null, null, ip
, null, 0L);
893 return callsitesByIP
.ceiling(cs
);
897 * Gets a callsite using the event name and instruction pointer O(log(n))
900 * the name of the event
902 * the instruction pointer
903 * @return the closest matching callsite, can be null
905 public CTFCallsite
getCallsite(String eventName
, long ip
) {
906 final TreeSet
<CTFCallsite
> candidates
= callsitesByName
.get(eventName
);
907 final CTFCallsite dummyCs
= new CTFCallsite(null, null, ip
, null, -1);
908 final CTFCallsite callsite
= candidates
.ceiling(dummyCs
);
909 if (callsite
== null) {
910 return candidates
.floor(dummyCs
);
916 class MetadataFileFilter
implements FileFilter
{
919 public boolean accept(File pathname
) {
920 if (pathname
.isDirectory()) {
923 if (pathname
.isHidden()) {
926 if (pathname
.getName().equals("metadata")) { //$NON-NLS-1$
934 class MetadataComparator
implements Comparator
<File
>, Serializable
{
936 private static final long serialVersionUID
= 1L;
939 public int compare(File o1
, File o2
) {
940 return o1
.getName().compareTo(o2
.getName());