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();
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() {
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.
245 // ------------------------------------------------------------------------
246 // Getters/Setters/Predicates
247 // ------------------------------------------------------------------------
250 * Get an event by it's ID
253 * the ID of the event
254 * @return the event declaration
256 public EventDeclaration
getEventType(long id
) {
257 return events
.get(id
);
261 * Get the number of events in the trace so far.
263 * @return the number of events in the trace
265 public int getNbEventTypes() {
266 return events
.size();
270 * Method getStream gets the stream for a given id
273 * Long the id of the stream
274 * @return Stream the stream that we need
276 public Stream
getStream(Long id
) {
277 return streams
.get(id
);
281 * Method nbStreams gets the number of available streams
283 * @return int the number of streams
285 public int nbStreams() {
286 return streams
.size();
290 * Method setMajor sets the major version of the trace (DO NOT USE)
293 * long the major version
295 public void setMajor(long major
) {
300 * Method setMinor sets the minor version of the trace (DO NOT USE)
303 * long the minor version
305 public void setMinor(long minor
) {
310 * Method setUUID sets the UUID of a trace
315 public void setUUID(UUID uuid
) {
320 * Method setByteOrder sets the byte order
323 * ByteOrder of the trace, can be little-endian or big-endian
325 public void setByteOrder(ByteOrder byteOrder
) {
326 this.byteOrder
= byteOrder
;
330 * Method setPacketHeader sets the packet header of a trace (DO NOT USE)
332 * @param packetHeader
333 * StructDeclaration the header in structdeclaration form
335 public void setPacketHeader(StructDeclaration packetHeader
) {
336 this.packetHeaderDecl
= packetHeader
;
340 * Method majortIsSet is the major version number set?
342 * @return boolean is the major set?
344 public boolean majortIsSet() {
345 return major
!= null;
349 * Method minorIsSet. is the minor version number set?
351 * @return boolean is the minor set?
353 public boolean minorIsSet() {
354 return minor
!= null;
358 * Method UUIDIsSet is the UUID set?
360 * @return boolean is the UUID set?
362 public boolean UUIDIsSet() {
367 * Method byteOrderIsSet is the byteorder set?
369 * @return boolean is the byteorder set?
371 public boolean byteOrderIsSet() {
372 return byteOrder
!= null;
376 * Method packetHeaderIsSet is the packet header set?
378 * @return boolean is the packet header set?
380 public boolean packetHeaderIsSet() {
381 return packetHeaderDecl
!= null;
385 * Method getUUID gets the trace UUID
387 * @return UUID gets the trace UUID
389 public UUID
getUUID() {
394 * Method getMajor gets the trace major version
396 * @return long gets the trace major version
398 public long getMajor() {
403 * Method getMinor gets the trace minor version
405 * @return long gets the trace minor version
407 public long getMinor() {
412 * Method getByteOrder gets the trace byte order
414 * @return ByteOrder gets the trace byte order
416 public ByteOrder
getByteOrder() {
421 * Method getPacketHeader gets the trace packet header
423 * @return StructDeclaration gets the trace packet header
425 public StructDeclaration
getPacketHeader() {
426 return packetHeaderDecl
;
430 * Method getTraceDirectory gets the trace directory
432 * @return File the path in "File" format.
434 public File
getTraceDirectory() {
439 * Method getStreams get all the streams in a map format.
441 * @return Map<Long,Stream> a map of all the streams.
443 public Map
<Long
, Stream
> getStreams() {
448 * Method getPath gets the path of the trace directory
450 * @return String the path of the trace directory, in string format.
451 * @see java.io.File#getPath()
454 public String
getPath() {
455 return path
.getPath();
458 // ------------------------------------------------------------------------
460 // ------------------------------------------------------------------------
463 * Tries to open the given file, reads the first packet header of the file
464 * and check its validity.
467 * A trace file in the trace directory.
469 * Which index in the class' streamFileChannel array this file
471 * @throws CTFReaderException
473 private void openStreamInput(File streamFile
) throws CTFReaderException
{
474 MappedByteBuffer byteBuffer
;
475 BitBuffer streamBitBuffer
;
479 if (!streamFile
.canRead()) {
480 throw new CTFReaderException("Unreadable file : " //$NON-NLS-1$
481 + streamFile
.getPath());
485 /* Open the file and get the FileChannel */
486 fc
= new FileInputStream(streamFile
).getChannel();
487 streamFileChannels
.add(fc
);
489 /* Map one memory page of 4 kiB */
490 byteBuffer
= fc
.map(MapMode
.READ_ONLY
, 0, 4096);
491 } catch (IOException e
) {
492 /* Shouldn't happen at this stage if every other check passed */
493 throw new CTFReaderException();
496 /* Create a BitBuffer with this mapping and the trace byte order */
497 streamBitBuffer
= new BitBuffer(byteBuffer
, this.getByteOrder());
499 if (packetHeaderDef
!= null) {
500 /* Read the packet header */
501 packetHeaderDef
.read(streamBitBuffer
);
503 /* Check the magic number */
504 IntegerDefinition magicDef
= (IntegerDefinition
) packetHeaderDef
505 .lookupDefinition("magic"); //$NON-NLS-1$
506 int magic
= (int) magicDef
.getValue();
507 if (magic
!= Utils
.CTF_MAGIC
) {
508 throw new CTFReaderException("CTF magic mismatch"); //$NON-NLS-1$
512 ArrayDefinition uuidDef
= (ArrayDefinition
) packetHeaderDef
513 .lookupDefinition("uuid"); //$NON-NLS-1$
514 assert ((uuidDef
!= null) && (uuidDef
.getDeclaration().getLength() == Utils
.UUID_LEN
));
515 if (uuidDef
!= null) {
516 byte[] uuidArray
= new byte[Utils
.UUID_LEN
];
518 for (int i
= 0; i
< Utils
.UUID_LEN
; i
++) {
519 IntegerDefinition uuidByteDef
= (IntegerDefinition
) uuidDef
521 uuidArray
[i
] = (byte) uuidByteDef
.getValue();
524 UUID otheruuid
= Utils
.makeUUID(uuidArray
);
526 if (!this.uuid
.equals(otheruuid
)) {
527 throw new CTFReaderException("UUID mismatch"); //$NON-NLS-1$
532 // TODO: it hasn't been checked that the stream_id field exists and
535 IntegerDefinition streamIDDef
= (IntegerDefinition
) packetHeaderDef
536 .lookupDefinition("stream_id"); //$NON-NLS-1$
537 assert (streamIDDef
!= null);
539 long streamID
= streamIDDef
.getValue();
541 /* Get the stream to which this trace file belongs to */
542 stream
= streams
.get(streamID
);
544 /* No packet header, we suppose there is only one stream */
545 stream
= streams
.get(null);
548 /* Create the stream input */
549 StreamInput streamInput
= new StreamInput(stream
, fc
, streamFile
);
551 /* Add a reference to the streamInput in the stream */
552 stream
.addInput(streamInput
);
556 * Looks up a definition from packet
561 * @see org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope#lookupDefinition(String)
564 public Definition
lookupDefinition(String lookupPath
) {
565 if (lookupPath
.equals("trace.packet.header")) { //$NON-NLS-1$
566 return packetHeaderDef
;
572 * Adds a new stream to the trace.
577 * @throws ParseException
579 public void addStream(Stream stream
) throws ParseException
{
582 * If there is already a stream without id (the null key), it must be
585 if (streams
.get(null) != null) {
586 throw new ParseException("Stream without id with multiple streams"); //$NON-NLS-1$
590 * If the stream we try to add has the null key, it must be the only
591 * one. Thus, if the streams container is not empty, it is not valid.
593 if ((stream
.getId() == null) && (streams
.size() != 0)) {
594 throw new ParseException("Stream without id with multiple streams"); //$NON-NLS-1$
597 /* If a stream with the same ID already exists, it is not valid. */
598 if (streams
.get(stream
.getId()) != null) {
599 throw new ParseException("Stream id already exists"); //$NON-NLS-1$
602 /* It should be ok now. */
603 streams
.put(stream
.getId(), stream
);
606 public HashMap
<String
, String
> getEnvironment() {
610 public String
lookupEnvironment(String key
) {
611 return environment
.get(key
);
614 public void addEnvironmentVar(String varName
, String varValue
) {
615 environment
.put(varName
, varValue
);
618 public void addClock(String nameValue
, CTFClock ctfClock
) {
619 clocks
.put(nameValue
, ctfClock
);
622 public CTFClock
getClock(String name
) {
623 return clocks
.get(name
);
626 public CTFClock
getClock() {
627 if (clocks
.size() == 1) {
628 String key
= (String
) clocks
.keySet().toArray()[0];
629 return clocks
.get(key
);
634 public long getOffset() {
635 if (getClock() == null) {
638 return (Long
) getClock().getProperty("offset"); //$NON-NLS-1$
643 class MetadataFileFilter
implements FileFilter
{
646 public boolean accept(File pathname
) {
647 if (pathname
.isDirectory()) {
650 if (pathname
.isHidden()) {
653 if (pathname
.getName().equals("metadata")) { //$NON-NLS-1$
661 class MetadataComparator
implements Comparator
<File
>, Serializable
{
664 public int compare(File o1
, File o2
) {
665 return o1
.getName().compareTo(o2
.getName());