1 /*******************************************************************************
2 * Copyright (c) 2011-2012 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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.ctf
.core
.trace
;
16 import java
.io
.FileFilter
;
17 import java
.io
.FileInputStream
;
18 import java
.io
.IOException
;
19 import java
.io
.Serializable
;
20 import java
.nio
.ByteOrder
;
21 import java
.nio
.MappedByteBuffer
;
22 import java
.nio
.channels
.FileChannel
;
23 import java
.nio
.channels
.FileChannel
.MapMode
;
24 import java
.util
.Arrays
;
25 import java
.util
.Comparator
;
26 import java
.util
.HashMap
;
27 import java
.util
.Iterator
;
28 import java
.util
.LinkedList
;
29 import java
.util
.List
;
31 import java
.util
.Map
.Entry
;
33 import java
.util
.UUID
;
35 import org
.eclipse
.linuxtools
.ctf
.core
.event
.CTFClock
;
36 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDeclaration
;
37 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDefinition
;
38 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
39 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDefinitionScope
;
40 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
41 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
42 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
43 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.Activator
;
44 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.io
.BitBuffer
;
45 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.metadata
.exceptions
.ParseException
;
46 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.Stream
;
47 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.StreamInput
;
50 * <b><u>CTFTrace</u></b>
52 * Represents a trace on the filesystem. It is responsible of parsing the
53 * metadata, creating declarations data structures, indexing the event packets
54 * (in other words, all the work that can be shared between readers), but the
55 * actual reading of events is left to TraceReader.
57 * @author Matthew Khouzam
58 * @version $Revision: 1.0 $
60 public class CTFTrace
implements IDefinitionScope
{
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
69 * @see java.lang.Object#toString()
71 @SuppressWarnings("nls")
73 public String
toString() {
74 /* Only for debugging, shouldn't be externalized */
75 return "CTFTrace [path=" + path
+ ", major=" + major
+ ", minor="
76 + minor
+ ", uuid=" + uuid
+ "]";
80 * The trace directory on the filesystem.
82 private final File path
;
85 * The metadata parsing object.
87 private final Metadata metadata
;
90 * Major CTF version number
95 * Minor CTF version number
107 private ByteOrder byteOrder
;
110 * Packet header structure declaration
112 private StructDeclaration packetHeaderDecl
= null;
115 * Packet header structure definition
117 * This is only used when opening the trace files, to read the first packet
118 * header and see if they are valid trace files.
120 private StructDefinition packetHeaderDef
;
123 * Collection of streams contained in the trace.
125 private final HashMap
<Long
, Stream
> streams
;
128 * Collection of environment variables set by the tracer
130 private final HashMap
<String
, String
> environment
;
133 * Collection of all the clocks in a system.
135 private final HashMap
<String
, CTFClock
> clocks
;
137 /** FileChannels to the streams */
138 private final List
<FileChannel
> streamFileChannels
;
140 /** Handlers for the metadata files */
141 private final static FileFilter metadataFileFilter
= new MetadataFileFilter();
142 private final static Comparator
<File
> metadataComparator
= new MetadataComparator(); // $codepro.audit.disable fieldJavadoc
144 /** map of all the event types */
145 private final HashMap
<Long
, EventDeclaration
> events
;
147 // ------------------------------------------------------------------------
149 // ------------------------------------------------------------------------
155 * Filesystem path of the trace directory.
156 * @throws IOException
158 public CTFTrace(String path
) throws CTFReaderException
{
159 this(new File(path
));
167 * Filesystem path of the trace directory.
168 * @throws CTFReaderException
170 public CTFTrace(File path
) throws CTFReaderException
{
172 this.metadata
= new Metadata(this);
174 /* Set up the internal containers for this trace */
175 streams
= new HashMap
<Long
, Stream
>();
176 environment
= new HashMap
<String
, String
>();
177 clocks
= new HashMap
<String
, CTFClock
>();
178 streamFileChannels
= new LinkedList
<FileChannel
>();
180 if (!this.path
.isDirectory()) {
181 throw new CTFReaderException("Path must be a valid directory"); //$NON-NLS-1$
184 /* Open and parse the metadata file */
187 if (Activator
.getDefault() != null) {
188 Activator
.getDefault().log(metadata
.toString());
191 /* Open all the trace files */
192 /* Create the definitions needed to read things from the files */
193 if (packetHeaderDecl
!= null) {
194 packetHeaderDef
= packetHeaderDecl
.createDefinition(this,
195 "packet.header"); //$NON-NLS-1$
198 /* List files not called metadata and not hidden. */
199 File
[] files
= path
.listFiles(metadataFileFilter
);
200 Arrays
.sort(files
, metadataComparator
);
202 /* Try to open each file */
203 for (File streamFile
: files
) {
204 openStreamInput(streamFile
);
206 events
= new HashMap
<Long
, EventDeclaration
>();
207 /* Create their index */
208 for (Map
.Entry
<Long
, Stream
> stream
: streams
.entrySet()) {
209 Set
<StreamInput
> inputs
= stream
.getValue().getStreamInputs();
210 for (StreamInput s
: inputs
) {
214 Iterator
<Entry
<Long
, EventDeclaration
>> it
= s
.getStream()
215 .getEvents().entrySet().iterator();
216 while (it
.hasNext()) {
217 Map
.Entry
<Long
, EventDeclaration
> pairs
= it
.next();
218 Long eventNum
= pairs
.getKey();
219 EventDeclaration eventDec
= pairs
.getValue();
220 events
.put(eventNum
, eventDec
);
232 protected void finalize() throws Throwable
{
233 /* If this trace gets closed, release the descriptors to the streams */
234 for (FileChannel fc
: streamFileChannels
) {
238 } catch (IOException e
) {
239 // do nothing it's ok, we tried to close it.
247 // ------------------------------------------------------------------------
248 // Getters/Setters/Predicates
249 // ------------------------------------------------------------------------
252 * Get an event by it's ID
255 * the ID of the event
256 * @return the event declaration
258 public EventDeclaration
getEventType(long id
) {
259 return events
.get(id
);
263 * Get the number of events in the trace so far.
265 * @return the number of events in the trace
267 public int getNbEventTypes() {
268 return events
.size();
272 * Method getStream gets the stream for a given id
275 * Long the id of the stream
276 * @return Stream the stream that we need
278 public Stream
getStream(Long id
) {
279 return streams
.get(id
);
283 * Method nbStreams gets the number of available streams
285 * @return int the number of streams
287 public int nbStreams() {
288 return streams
.size();
292 * Method setMajor sets the major version of the trace (DO NOT USE)
295 * long the major version
297 public void setMajor(long major
) {
302 * Method setMinor sets the minor version of the trace (DO NOT USE)
305 * long the minor version
307 public void setMinor(long minor
) {
312 * Method setUUID sets the UUID of a trace
317 public void setUUID(UUID uuid
) {
322 * Method setByteOrder sets the byte order
325 * ByteOrder of the trace, can be little-endian or big-endian
327 public void setByteOrder(ByteOrder byteOrder
) {
328 this.byteOrder
= byteOrder
;
332 * Method setPacketHeader sets the packet header of a trace (DO NOT USE)
334 * @param packetHeader
335 * StructDeclaration the header in structdeclaration form
337 public void setPacketHeader(StructDeclaration packetHeader
) {
338 this.packetHeaderDecl
= packetHeader
;
342 * Method majortIsSet is the major version number set?
344 * @return boolean is the major set?
346 public boolean majortIsSet() {
347 return major
!= null;
351 * Method minorIsSet. is the minor version number set?
353 * @return boolean is the minor set?
355 public boolean minorIsSet() {
356 return minor
!= null;
360 * Method UUIDIsSet is the UUID set?
362 * @return boolean is the UUID set?
364 public boolean UUIDIsSet() {
369 * Method byteOrderIsSet is the byteorder set?
371 * @return boolean is the byteorder set?
373 public boolean byteOrderIsSet() {
374 return byteOrder
!= null;
378 * Method packetHeaderIsSet is the packet header set?
380 * @return boolean is the packet header set?
382 public boolean packetHeaderIsSet() {
383 return packetHeaderDecl
!= null;
387 * Method getUUID gets the trace UUID
389 * @return UUID gets the trace UUID
391 public UUID
getUUID() {
396 * Method getMajor gets the trace major version
398 * @return long gets the trace major version
400 public long getMajor() {
405 * Method getMinor gets the trace minor version
407 * @return long gets the trace minor version
409 public long getMinor() {
414 * Method getByteOrder gets the trace byte order
416 * @return ByteOrder gets the trace byte order
418 public ByteOrder
getByteOrder() {
423 * Method getPacketHeader gets the trace packet header
425 * @return StructDeclaration gets the trace packet header
427 public StructDeclaration
getPacketHeader() {
428 return packetHeaderDecl
;
432 * Method getTraceDirectory gets the trace directory
434 * @return File the path in "File" format.
436 public File
getTraceDirectory() {
441 * Method getStreams get all the streams in a map format.
443 * @return Map<Long,Stream> a map of all the streams.
445 public Map
<Long
, Stream
> getStreams() {
450 * Method getPath gets the path of the trace directory
452 * @return String the path of the trace directory, in string format.
453 * @see java.io.File#getPath()
456 public String
getPath() {
457 return path
.getPath();
460 // ------------------------------------------------------------------------
462 // ------------------------------------------------------------------------
465 * Tries to open the given file, reads the first packet header of the file
466 * and check its validity.
469 * A trace file in the trace directory.
471 * Which index in the class' streamFileChannel array this file
473 * @throws CTFReaderException
475 private void openStreamInput(File streamFile
) throws CTFReaderException
{
476 MappedByteBuffer byteBuffer
;
477 BitBuffer streamBitBuffer
;
481 if (!streamFile
.canRead()) {
482 throw new CTFReaderException("Unreadable file : " //$NON-NLS-1$
483 + streamFile
.getPath());
487 /* Open the file and get the FileChannel */
488 fc
= new FileInputStream(streamFile
).getChannel();
489 streamFileChannels
.add(fc
);
491 /* Map one memory page of 4 kiB */
492 byteBuffer
= fc
.map(MapMode
.READ_ONLY
, 0, 4096);
493 } catch (IOException e
) {
494 /* Shouldn't happen at this stage if every other check passed */
495 throw new CTFReaderException();
498 /* Create a BitBuffer with this mapping and the trace byte order */
499 streamBitBuffer
= new BitBuffer(byteBuffer
, this.getByteOrder());
501 if (packetHeaderDef
!= null) {
502 /* Read the packet header */
503 packetHeaderDef
.read(streamBitBuffer
);
505 /* Check the magic number */
506 IntegerDefinition magicDef
= (IntegerDefinition
) packetHeaderDef
507 .lookupDefinition("magic"); //$NON-NLS-1$
508 int magic
= (int) magicDef
.getValue();
509 if (magic
!= Utils
.CTF_MAGIC
) {
510 throw new CTFReaderException("CTF magic mismatch"); //$NON-NLS-1$
514 ArrayDefinition uuidDef
= (ArrayDefinition
) packetHeaderDef
515 .lookupDefinition("uuid"); //$NON-NLS-1$
516 if (uuidDef
!= null) {
517 byte[] uuidArray
= new byte[Utils
.UUID_LEN
];
519 for (int i
= 0; i
< Utils
.UUID_LEN
; i
++) {
520 IntegerDefinition uuidByteDef
= (IntegerDefinition
) uuidDef
522 uuidArray
[i
] = (byte) uuidByteDef
.getValue();
525 UUID otheruuid
= Utils
.makeUUID(uuidArray
);
527 if (!this.uuid
.equals(otheruuid
)) {
528 throw new CTFReaderException("UUID mismatch"); //$NON-NLS-1$
533 // TODO: it hasn't been checked that the stream_id field exists and
536 IntegerDefinition streamIDDef
= (IntegerDefinition
) packetHeaderDef
537 .lookupDefinition("stream_id"); //$NON-NLS-1$
538 assert (streamIDDef
!= null);
540 long streamID
= streamIDDef
.getValue();
542 /* Get the stream to which this trace file belongs to */
543 stream
= streams
.get(streamID
);
545 /* No packet header, we suppose there is only one stream */
546 stream
= streams
.get(null);
549 /* Create the stream input */
550 StreamInput streamInput
= new StreamInput(stream
, fc
, streamFile
);
552 /* Add a reference to the streamInput in the stream */
553 stream
.addInput(streamInput
);
557 * Looks up a definition from packet
562 * @see org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope#lookupDefinition(String)
565 public Definition
lookupDefinition(String lookupPath
) {
566 if (lookupPath
.equals("trace.packet.header")) { //$NON-NLS-1$
567 return packetHeaderDef
;
573 * Adds a new stream to the trace.
578 * @throws ParseException
580 public void addStream(Stream stream
) throws ParseException
{
583 * If there is already a stream without id (the null key), it must be
586 if (streams
.get(null) != null) {
587 throw new ParseException("Stream without id with multiple streams"); //$NON-NLS-1$
591 * If the stream we try to add has the null key, it must be the only
592 * one. Thus, if the streams container is not empty, it is not valid.
594 if ((stream
.getId() == null) && (streams
.size() != 0)) {
595 throw new ParseException("Stream without id with multiple streams"); //$NON-NLS-1$
598 /* If a stream with the same ID already exists, it is not valid. */
599 if (streams
.get(stream
.getId()) != null) {
600 throw new ParseException("Stream id already exists"); //$NON-NLS-1$
603 /* It should be ok now. */
604 streams
.put(stream
.getId(), stream
);
607 public HashMap
<String
, String
> getEnvironment() {
611 public String
lookupEnvironment(String key
) {
612 return environment
.get(key
);
615 public void addEnvironmentVar(String varName
, String varValue
) {
616 environment
.put(varName
, varValue
);
619 public void addClock(String nameValue
, CTFClock ctfClock
) {
620 clocks
.put(nameValue
, ctfClock
);
623 public CTFClock
getClock(String name
) {
624 return clocks
.get(name
);
627 public CTFClock
getClock() {
628 if (clocks
.size() == 1) {
629 String key
= (String
) clocks
.keySet().toArray()[0];
630 return clocks
.get(key
);
635 public long getOffset() {
636 if (getClock() == null) {
639 return (Long
) getClock().getProperty("offset"); //$NON-NLS-1$
644 class MetadataFileFilter
implements FileFilter
{
647 public boolean accept(File pathname
) {
648 if (pathname
.isDirectory()) {
651 if (pathname
.isHidden()) {
654 if (pathname
.getName().equals("metadata")) { //$NON-NLS-1$
662 class MetadataComparator
implements Comparator
<File
>, Serializable
{
664 private static final long serialVersionUID
= 1L;
667 public int compare(File o1
, File o2
) {
668 return o1
.getName().compareTo(o2
.getName());