Commit | Line | Data |
---|---|---|
866e5b51 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: Matthew Khouzam - Initial API and implementation | |
10 | * Contributors: Alexandre Montplaisir - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.ctf.core.trace; | |
14 | ||
15 | import java.io.File; | |
16 | import java.io.FileFilter; | |
17 | import java.io.FileInputStream; | |
18 | import java.io.IOException; | |
debcffff | 19 | import java.io.Serializable; |
866e5b51 FC |
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; | |
aa572e22 | 27 | import java.util.Iterator; |
d0d3aa1b AM |
28 | import java.util.LinkedList; |
29 | import java.util.List; | |
866e5b51 | 30 | import java.util.Map; |
aa572e22 | 31 | import java.util.Map.Entry; |
866e5b51 FC |
32 | import java.util.Set; |
33 | import java.util.UUID; | |
34 | ||
866e5b51 | 35 | import org.eclipse.linuxtools.ctf.core.event.CTFClock; |
aa572e22 | 36 | import org.eclipse.linuxtools.ctf.core.event.EventDeclaration; |
788ddcbc | 37 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; |
866e5b51 FC |
38 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition; |
39 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; | |
40 | import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope; | |
41 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; | |
42 | import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; | |
43 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; | |
a9d52b8f | 44 | import org.eclipse.linuxtools.internal.ctf.core.Activator; |
ce2388e0 FC |
45 | import org.eclipse.linuxtools.internal.ctf.core.event.io.BitBuffer; |
46 | import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseException; | |
47 | import org.eclipse.linuxtools.internal.ctf.core.trace.Stream; | |
48 | import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInput; | |
788ddcbc | 49 | import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndex; |
866e5b51 FC |
50 | |
51 | /** | |
52 | * <b><u>CTFTrace</u></b> | |
53 | * <p> | |
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. | |
debcffff | 58 | * |
866e5b51 FC |
59 | * @author Matthew Khouzam |
60 | * @version $Revision: 1.0 $ | |
61 | */ | |
62 | public class CTFTrace implements IDefinitionScope { | |
63 | ||
64 | // ------------------------------------------------------------------------ | |
65 | // Attributes | |
66 | // ------------------------------------------------------------------------ | |
67 | ||
68 | /* | |
69 | * (non-Javadoc) | |
debcffff | 70 | * |
866e5b51 FC |
71 | * @see java.lang.Object#toString() |
72 | */ | |
73 | @SuppressWarnings("nls") | |
74 | @Override | |
75 | public String toString() { | |
76 | /* Only for debugging, shouldn't be externalized */ | |
77 | return "CTFTrace [path=" + path + ", major=" + major + ", minor=" | |
78 | + minor + ", uuid=" + uuid + "]"; | |
79 | } | |
80 | ||
81 | /** | |
82 | * The trace directory on the filesystem. | |
83 | */ | |
84 | private final File path; | |
85 | ||
86 | /** | |
87 | * The metadata parsing object. | |
88 | */ | |
89 | private final Metadata metadata; | |
90 | ||
91 | /** | |
92 | * Major CTF version number | |
93 | */ | |
94 | private Long major; | |
95 | ||
96 | /** | |
97 | * Minor CTF version number | |
98 | */ | |
99 | private Long minor; | |
100 | ||
101 | /** | |
102 | * Trace UUID | |
103 | */ | |
104 | private UUID uuid; | |
105 | ||
106 | /** | |
107 | * Trace byte order | |
108 | */ | |
109 | private ByteOrder byteOrder; | |
110 | ||
111 | /** | |
112 | * Packet header structure declaration | |
113 | */ | |
debcffff | 114 | private StructDeclaration packetHeaderDecl = null; |
866e5b51 FC |
115 | |
116 | /** | |
117 | * Packet header structure definition | |
debcffff | 118 | * |
866e5b51 FC |
119 | * This is only used when opening the trace files, to read the first packet |
120 | * header and see if they are valid trace files. | |
121 | */ | |
122 | private StructDefinition packetHeaderDef; | |
123 | ||
124 | /** | |
125 | * Collection of streams contained in the trace. | |
126 | */ | |
c88e827d | 127 | private final HashMap<Long, Stream> streams; |
866e5b51 FC |
128 | |
129 | /** | |
130 | * Collection of environment variables set by the tracer | |
131 | */ | |
c88e827d | 132 | private final HashMap<String, String> environment; |
866e5b51 FC |
133 | |
134 | /** | |
135 | * Collection of all the clocks in a system. | |
136 | */ | |
c88e827d | 137 | private final HashMap<String, CTFClock> clocks; |
866e5b51 | 138 | |
26ea03d2 | 139 | /** FileChannels to the streams */ |
d0d3aa1b | 140 | private final List<FileChannel> streamFileChannels; |
26ea03d2 | 141 | |
c88e827d AM |
142 | /** Handlers for the metadata files */ |
143 | private final static FileFilter metadataFileFilter = new MetadataFileFilter(); | |
8ecc80f3 MK |
144 | private final static Comparator<File> metadataComparator = new MetadataComparator(); // $codepro.audit.disable |
145 | // fieldJavadoc | |
866e5b51 | 146 | |
aa572e22 | 147 | /** map of all the event types */ |
788ddcbc MK |
148 | private final HashMap<Long,HashMap<Long, EventDeclaration>> eventDecs; |
149 | /** map of all the event types */ | |
150 | private final HashMap<StreamInput,HashMap<Long, EventDefinition>> eventDefs; | |
151 | /** map of all the indexes */ | |
152 | private final HashMap<StreamInput, StreamInputPacketIndex> indexes; | |
153 | ||
154 | ||
aa572e22 | 155 | |
866e5b51 FC |
156 | // ------------------------------------------------------------------------ |
157 | // Constructors | |
158 | // ------------------------------------------------------------------------ | |
159 | ||
160 | /** | |
161 | * Trace constructor. | |
debcffff | 162 | * |
866e5b51 | 163 | * @param path |
be6df2d8 AM |
164 | * Filesystem path of the trace directory |
165 | * @throws CTFReaderException | |
166 | * If no CTF trace was found at the path | |
866e5b51 FC |
167 | */ |
168 | public CTFTrace(String path) throws CTFReaderException { | |
169 | this(new File(path)); | |
aa572e22 | 170 | |
866e5b51 FC |
171 | } |
172 | ||
173 | /** | |
174 | * Trace constructor. | |
debcffff | 175 | * |
866e5b51 FC |
176 | * @param path |
177 | * Filesystem path of the trace directory. | |
178 | * @throws CTFReaderException | |
be6df2d8 | 179 | * If no CTF trace was found at the path |
866e5b51 | 180 | */ |
866e5b51 FC |
181 | public CTFTrace(File path) throws CTFReaderException { |
182 | this.path = path; | |
c88e827d | 183 | this.metadata = new Metadata(this); |
866e5b51 | 184 | |
c88e827d AM |
185 | /* Set up the internal containers for this trace */ |
186 | streams = new HashMap<Long, Stream>(); | |
187 | environment = new HashMap<String, String>(); | |
188 | clocks = new HashMap<String, CTFClock>(); | |
d0d3aa1b | 189 | streamFileChannels = new LinkedList<FileChannel>(); |
788ddcbc MK |
190 | eventDecs = new HashMap<Long, HashMap<Long, EventDeclaration>>(); |
191 | eventDefs = new HashMap<StreamInput, HashMap<Long, EventDefinition>>(); | |
d0d3aa1b AM |
192 | |
193 | if (!this.path.isDirectory()) { | |
194 | throw new CTFReaderException("Path must be a valid directory"); //$NON-NLS-1$ | |
195 | } | |
c88e827d AM |
196 | |
197 | /* Open and parse the metadata file */ | |
198 | metadata.parse(); | |
199 | ||
200 | if (Activator.getDefault() != null) { | |
201 | Activator.getDefault().log(metadata.toString()); | |
202 | } | |
866e5b51 | 203 | |
c88e827d AM |
204 | /* Open all the trace files */ |
205 | /* Create the definitions needed to read things from the files */ | |
206 | if (packetHeaderDecl != null) { | |
207 | packetHeaderDef = packetHeaderDecl.createDefinition(this, | |
208 | "packet.header"); //$NON-NLS-1$ | |
209 | } | |
210 | ||
211 | /* List files not called metadata and not hidden. */ | |
212 | File[] files = path.listFiles(metadataFileFilter); | |
213 | Arrays.sort(files, metadataComparator); | |
788ddcbc | 214 | indexes = new HashMap<StreamInput, StreamInputPacketIndex>(); |
c88e827d | 215 | /* Try to open each file */ |
d0d3aa1b AM |
216 | for (File streamFile : files) { |
217 | openStreamInput(streamFile); | |
c88e827d | 218 | } |
788ddcbc | 219 | |
c88e827d AM |
220 | /* Create their index */ |
221 | for (Map.Entry<Long, Stream> stream : streams.entrySet()) { | |
222 | Set<StreamInput> inputs = stream.getValue().getStreamInputs(); | |
223 | for (StreamInput s : inputs) { | |
aa572e22 MK |
224 | /* |
225 | * Copy the events | |
226 | */ | |
227 | Iterator<Entry<Long, EventDeclaration>> it = s.getStream() | |
228 | .getEvents().entrySet().iterator(); | |
229 | while (it.hasNext()) { | |
230 | Map.Entry<Long, EventDeclaration> pairs = it.next(); | |
231 | Long eventNum = pairs.getKey(); | |
232 | EventDeclaration eventDec = pairs.getValue(); | |
788ddcbc | 233 | getEvents(s.getStream().getId()).put(eventNum, eventDec); |
aa572e22 MK |
234 | } |
235 | ||
236 | /* | |
237 | * index the trace | |
238 | */ | |
bfe038ff | 239 | s.setupIndex(); |
c88e827d AM |
240 | } |
241 | } | |
866e5b51 FC |
242 | } |
243 | ||
26ea03d2 | 244 | @Override |
8ecc80f3 | 245 | protected void finalize() throws Throwable { |
26ea03d2 AM |
246 | /* If this trace gets closed, release the descriptors to the streams */ |
247 | for (FileChannel fc : streamFileChannels) { | |
248 | if (fc != null) { | |
249 | try { | |
250 | fc.close(); | |
251 | } catch (IOException e) { | |
aa572e22 | 252 | // do nothing it's ok, we tried to close it. |
26ea03d2 AM |
253 | } |
254 | } | |
255 | } | |
787bc247 MK |
256 | super.finalize(); |
257 | ||
26ea03d2 AM |
258 | } |
259 | ||
866e5b51 FC |
260 | // ------------------------------------------------------------------------ |
261 | // Getters/Setters/Predicates | |
262 | // ------------------------------------------------------------------------ | |
263 | ||
aa572e22 | 264 | /** |
be6df2d8 AM |
265 | * Gets an event declaration hash map for a given streamID |
266 | * | |
267 | * @param streamId | |
268 | * The ID of the stream from which to read | |
269 | * @return The Hash map with the event declarations | |
788ddcbc | 270 | */ |
be6df2d8 AM |
271 | public HashMap<Long, EventDeclaration> getEvents(Long streamId) { |
272 | return eventDecs.get(streamId); | |
788ddcbc MK |
273 | } |
274 | ||
275 | /** | |
276 | * Gets an index for a given StreamInput | |
277 | * @param id the StreamInput | |
278 | * @return The index | |
aa572e22 | 279 | */ |
788ddcbc MK |
280 | public StreamInputPacketIndex getIndex(StreamInput id){ |
281 | if(! indexes.containsKey(id)){ | |
282 | indexes.put(id, new StreamInputPacketIndex()); | |
283 | } | |
284 | return indexes.get(id); | |
aa572e22 MK |
285 | } |
286 | ||
287 | /** | |
788ddcbc MK |
288 | * Gets an event Declaration hashmap for a given StreamInput |
289 | * @param id the StreamInput | |
290 | * @return the hashmap with the event definitions | |
291 | */ | |
292 | public HashMap<Long, EventDefinition> getEventDefs(StreamInput id) { | |
293 | if(! eventDefs.containsKey(id)){ | |
294 | eventDefs.put(id, new HashMap<Long, EventDefinition>()); | |
295 | } | |
296 | return eventDefs.get(id); | |
297 | } | |
298 | ||
299 | /** | |
300 | * Get an event by it's ID | |
aa572e22 | 301 | * |
be6df2d8 AM |
302 | * @param streamId |
303 | * The ID of the stream from which to read | |
788ddcbc MK |
304 | * @param id |
305 | * the ID of the event | |
306 | * @return the event declaration | |
aa572e22 | 307 | */ |
788ddcbc MK |
308 | public EventDeclaration getEventType(long streamId, long id) { |
309 | return getEvents(streamId).get(id); | |
aa572e22 MK |
310 | } |
311 | ||
866e5b51 FC |
312 | /** |
313 | * Method getStream gets the stream for a given id | |
debcffff | 314 | * |
866e5b51 FC |
315 | * @param id |
316 | * Long the id of the stream | |
317 | * @return Stream the stream that we need | |
318 | */ | |
319 | public Stream getStream(Long id) { | |
320 | return streams.get(id); | |
321 | } | |
322 | ||
323 | /** | |
324 | * Method nbStreams gets the number of available streams | |
debcffff | 325 | * |
866e5b51 FC |
326 | * @return int the number of streams |
327 | */ | |
328 | public int nbStreams() { | |
329 | return streams.size(); | |
330 | } | |
331 | ||
332 | /** | |
333 | * Method setMajor sets the major version of the trace (DO NOT USE) | |
debcffff | 334 | * |
866e5b51 FC |
335 | * @param major |
336 | * long the major version | |
337 | */ | |
338 | public void setMajor(long major) { | |
339 | this.major = major; | |
340 | } | |
341 | ||
342 | /** | |
343 | * Method setMinor sets the minor version of the trace (DO NOT USE) | |
debcffff | 344 | * |
866e5b51 FC |
345 | * @param minor |
346 | * long the minor version | |
347 | */ | |
348 | public void setMinor(long minor) { | |
349 | this.minor = minor; | |
350 | } | |
351 | ||
352 | /** | |
353 | * Method setUUID sets the UUID of a trace | |
debcffff | 354 | * |
866e5b51 FC |
355 | * @param uuid |
356 | * UUID | |
357 | */ | |
358 | public void setUUID(UUID uuid) { | |
359 | this.uuid = uuid; | |
360 | } | |
361 | ||
362 | /** | |
363 | * Method setByteOrder sets the byte order | |
debcffff | 364 | * |
866e5b51 FC |
365 | * @param byteOrder |
366 | * ByteOrder of the trace, can be little-endian or big-endian | |
367 | */ | |
368 | public void setByteOrder(ByteOrder byteOrder) { | |
369 | this.byteOrder = byteOrder; | |
370 | } | |
371 | ||
372 | /** | |
373 | * Method setPacketHeader sets the packet header of a trace (DO NOT USE) | |
debcffff | 374 | * |
866e5b51 FC |
375 | * @param packetHeader |
376 | * StructDeclaration the header in structdeclaration form | |
377 | */ | |
378 | public void setPacketHeader(StructDeclaration packetHeader) { | |
379 | this.packetHeaderDecl = packetHeader; | |
380 | } | |
381 | ||
382 | /** | |
383 | * Method majortIsSet is the major version number set? | |
debcffff | 384 | * |
866e5b51 FC |
385 | * @return boolean is the major set? |
386 | */ | |
387 | public boolean majortIsSet() { | |
388 | return major != null; | |
389 | } | |
390 | ||
391 | /** | |
392 | * Method minorIsSet. is the minor version number set? | |
debcffff | 393 | * |
866e5b51 FC |
394 | * @return boolean is the minor set? |
395 | */ | |
396 | public boolean minorIsSet() { | |
397 | return minor != null; | |
398 | } | |
399 | ||
400 | /** | |
401 | * Method UUIDIsSet is the UUID set? | |
debcffff | 402 | * |
866e5b51 FC |
403 | * @return boolean is the UUID set? |
404 | */ | |
405 | public boolean UUIDIsSet() { | |
406 | return uuid != null; | |
407 | } | |
408 | ||
409 | /** | |
410 | * Method byteOrderIsSet is the byteorder set? | |
debcffff | 411 | * |
866e5b51 FC |
412 | * @return boolean is the byteorder set? |
413 | */ | |
414 | public boolean byteOrderIsSet() { | |
415 | return byteOrder != null; | |
416 | } | |
417 | ||
418 | /** | |
419 | * Method packetHeaderIsSet is the packet header set? | |
debcffff | 420 | * |
866e5b51 FC |
421 | * @return boolean is the packet header set? |
422 | */ | |
423 | public boolean packetHeaderIsSet() { | |
424 | return packetHeaderDecl != null; | |
425 | } | |
426 | ||
427 | /** | |
428 | * Method getUUID gets the trace UUID | |
debcffff | 429 | * |
866e5b51 FC |
430 | * @return UUID gets the trace UUID |
431 | */ | |
432 | public UUID getUUID() { | |
433 | return uuid; | |
434 | } | |
435 | ||
436 | /** | |
437 | * Method getMajor gets the trace major version | |
debcffff | 438 | * |
866e5b51 FC |
439 | * @return long gets the trace major version |
440 | */ | |
441 | public long getMajor() { | |
442 | return major; | |
443 | } | |
444 | ||
445 | /** | |
446 | * Method getMinor gets the trace minor version | |
debcffff | 447 | * |
866e5b51 FC |
448 | * @return long gets the trace minor version |
449 | */ | |
450 | public long getMinor() { | |
451 | return minor; | |
452 | } | |
453 | ||
454 | /** | |
455 | * Method getByteOrder gets the trace byte order | |
debcffff | 456 | * |
866e5b51 FC |
457 | * @return ByteOrder gets the trace byte order |
458 | */ | |
459 | public ByteOrder getByteOrder() { | |
460 | return byteOrder; | |
461 | } | |
462 | ||
463 | /** | |
464 | * Method getPacketHeader gets the trace packet header | |
debcffff | 465 | * |
866e5b51 FC |
466 | * @return StructDeclaration gets the trace packet header |
467 | */ | |
468 | public StructDeclaration getPacketHeader() { | |
469 | return packetHeaderDecl; | |
470 | } | |
471 | ||
472 | /** | |
473 | * Method getTraceDirectory gets the trace directory | |
debcffff | 474 | * |
866e5b51 FC |
475 | * @return File the path in "File" format. |
476 | */ | |
477 | public File getTraceDirectory() { | |
478 | return path; | |
479 | } | |
480 | ||
481 | /** | |
482 | * Method getStreams get all the streams in a map format. | |
debcffff | 483 | * |
866e5b51 FC |
484 | * @return Map<Long,Stream> a map of all the streams. |
485 | */ | |
486 | public Map<Long, Stream> getStreams() { | |
487 | return streams; | |
488 | } | |
489 | ||
490 | /** | |
491 | * Method getPath gets the path of the trace directory | |
debcffff | 492 | * |
866e5b51 FC |
493 | * @return String the path of the trace directory, in string format. |
494 | * @see java.io.File#getPath() | |
495 | */ | |
496 | @Override | |
497 | public String getPath() { | |
498 | return path.getPath(); | |
499 | } | |
500 | ||
501 | // ------------------------------------------------------------------------ | |
502 | // Operations | |
503 | // ------------------------------------------------------------------------ | |
504 | ||
866e5b51 FC |
505 | /** |
506 | * Tries to open the given file, reads the first packet header of the file | |
507 | * and check its validity. | |
debcffff | 508 | * |
866e5b51 FC |
509 | * @param streamFile |
510 | * A trace file in the trace directory. | |
26ea03d2 AM |
511 | * @param index |
512 | * Which index in the class' streamFileChannel array this file | |
513 | * must use | |
866e5b51 FC |
514 | * @throws CTFReaderException |
515 | */ | |
aa572e22 | 516 | private void openStreamInput(File streamFile) throws CTFReaderException { |
866e5b51 FC |
517 | MappedByteBuffer byteBuffer; |
518 | BitBuffer streamBitBuffer; | |
d0d3aa1b AM |
519 | Stream stream; |
520 | FileChannel fc; | |
866e5b51 FC |
521 | |
522 | if (!streamFile.canRead()) { | |
523 | throw new CTFReaderException("Unreadable file : " //$NON-NLS-1$ | |
524 | + streamFile.getPath()); | |
525 | } | |
526 | ||
527 | try { | |
528 | /* Open the file and get the FileChannel */ | |
d0d3aa1b AM |
529 | fc = new FileInputStream(streamFile).getChannel(); |
530 | streamFileChannels.add(fc); | |
866e5b51 FC |
531 | |
532 | /* Map one memory page of 4 kiB */ | |
d0d3aa1b | 533 | byteBuffer = fc.map(MapMode.READ_ONLY, 0, 4096); |
866e5b51 FC |
534 | } catch (IOException e) { |
535 | /* Shouldn't happen at this stage if every other check passed */ | |
536 | throw new CTFReaderException(); | |
537 | } | |
538 | ||
539 | /* Create a BitBuffer with this mapping and the trace byte order */ | |
540 | streamBitBuffer = new BitBuffer(byteBuffer, this.getByteOrder()); | |
541 | ||
542 | if (packetHeaderDef != null) { | |
543 | /* Read the packet header */ | |
544 | packetHeaderDef.read(streamBitBuffer); | |
545 | ||
546 | /* Check the magic number */ | |
aa572e22 MK |
547 | IntegerDefinition magicDef = (IntegerDefinition) packetHeaderDef |
548 | .lookupDefinition("magic"); //$NON-NLS-1$ | |
866e5b51 FC |
549 | int magic = (int) magicDef.getValue(); |
550 | if (magic != Utils.CTF_MAGIC) { | |
551 | throw new CTFReaderException("CTF magic mismatch"); //$NON-NLS-1$ | |
552 | } | |
553 | ||
554 | /* Check UUID */ | |
aa572e22 MK |
555 | ArrayDefinition uuidDef = (ArrayDefinition) packetHeaderDef |
556 | .lookupDefinition("uuid"); //$NON-NLS-1$ | |
866e5b51 FC |
557 | if (uuidDef != null) { |
558 | byte[] uuidArray = new byte[Utils.UUID_LEN]; | |
559 | ||
560 | for (int i = 0; i < Utils.UUID_LEN; i++) { | |
aa572e22 MK |
561 | IntegerDefinition uuidByteDef = (IntegerDefinition) uuidDef |
562 | .getElem(i); | |
866e5b51 FC |
563 | uuidArray[i] = (byte) uuidByteDef.getValue(); |
564 | } | |
565 | ||
566 | UUID otheruuid = Utils.makeUUID(uuidArray); | |
567 | ||
568 | if (!this.uuid.equals(otheruuid)) { | |
569 | throw new CTFReaderException("UUID mismatch"); //$NON-NLS-1$ | |
570 | } | |
571 | } | |
572 | ||
573 | /* Read stream ID */ | |
574 | // TODO: it hasn't been checked that the stream_id field exists and | |
575 | // is an unsigned | |
576 | // integer | |
aa572e22 MK |
577 | IntegerDefinition streamIDDef = (IntegerDefinition) packetHeaderDef |
578 | .lookupDefinition("stream_id"); //$NON-NLS-1$ | |
866e5b51 FC |
579 | assert (streamIDDef != null); |
580 | ||
581 | long streamID = streamIDDef.getValue(); | |
582 | ||
583 | /* Get the stream to which this trace file belongs to */ | |
d0d3aa1b | 584 | stream = streams.get(streamID); |
866e5b51 FC |
585 | } else { |
586 | /* No packet header, we suppose there is only one stream */ | |
d0d3aa1b AM |
587 | stream = streams.get(null); |
588 | } | |
866e5b51 | 589 | |
d0d3aa1b AM |
590 | /* Create the stream input */ |
591 | StreamInput streamInput = new StreamInput(stream, fc, streamFile); | |
866e5b51 | 592 | |
d0d3aa1b AM |
593 | /* Add a reference to the streamInput in the stream */ |
594 | stream.addInput(streamInput); | |
866e5b51 FC |
595 | } |
596 | ||
597 | /** | |
598 | * Looks up a definition from packet | |
debcffff | 599 | * |
866e5b51 FC |
600 | * @param lookupPath |
601 | * String | |
602 | * @return Definition | |
603 | * @see org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope#lookupDefinition(String) | |
604 | */ | |
605 | @Override | |
606 | public Definition lookupDefinition(String lookupPath) { | |
607 | if (lookupPath.equals("trace.packet.header")) { //$NON-NLS-1$ | |
608 | return packetHeaderDef; | |
609 | } | |
610 | return null; | |
611 | } | |
612 | ||
613 | /** | |
614 | * Adds a new stream to the trace. | |
debcffff | 615 | * |
866e5b51 FC |
616 | * @param stream |
617 | * A stream object. | |
866e5b51 | 618 | * @throws ParseException |
be6df2d8 | 619 | * If there was some problem reading the metadata |
866e5b51 FC |
620 | */ |
621 | public void addStream(Stream stream) throws ParseException { | |
622 | ||
623 | /* | |
624 | * If there is already a stream without id (the null key), it must be | |
625 | * the only one | |
626 | */ | |
627 | if (streams.get(null) != null) { | |
628 | throw new ParseException("Stream without id with multiple streams"); //$NON-NLS-1$ | |
629 | } | |
630 | ||
631 | /* | |
788ddcbc | 632 | * If the stream we try to add has the null key, it must be the onl * one. Thus, if the streams container is not empty, it is not valid. |
866e5b51 FC |
633 | */ |
634 | if ((stream.getId() == null) && (streams.size() != 0)) { | |
635 | throw new ParseException("Stream without id with multiple streams"); //$NON-NLS-1$ | |
636 | } | |
637 | ||
638 | /* If a stream with the same ID already exists, it is not valid. */ | |
639 | if (streams.get(stream.getId()) != null) { | |
640 | throw new ParseException("Stream id already exists"); //$NON-NLS-1$ | |
641 | } | |
642 | ||
643 | /* It should be ok now. */ | |
644 | streams.put(stream.getId(), stream); | |
788ddcbc | 645 | eventDecs.put(stream.getId(), new HashMap<Long,EventDeclaration>()); |
866e5b51 FC |
646 | } |
647 | ||
9ac2eb62 MK |
648 | /** |
649 | * gets the Environment variables from the trace metadata (See CTF spec) | |
650 | * @return the environment variables in a hashmap form (key value) | |
651 | */ | |
866e5b51 FC |
652 | public HashMap<String, String> getEnvironment() { |
653 | return environment; | |
654 | } | |
655 | ||
9ac2eb62 MK |
656 | /** |
657 | * Look up a specific environment variable | |
658 | * @param key the key to look for | |
659 | * @return the value of the variable, can be null. | |
660 | */ | |
c88e827d | 661 | public String lookupEnvironment(String key) { |
866e5b51 FC |
662 | return environment.get(key); |
663 | } | |
664 | ||
9ac2eb62 MK |
665 | /** |
666 | * Add a variable to the environment variables | |
667 | * @param varName the name of the variable | |
668 | * @param varValue the value of the variable | |
669 | */ | |
c88e827d | 670 | public void addEnvironmentVar(String varName, String varValue) { |
866e5b51 FC |
671 | environment.put(varName, varValue); |
672 | } | |
673 | ||
9ac2eb62 MK |
674 | /** |
675 | * Add a clock to the clock list | |
676 | * @param nameValue the name of the clock (full name with scope) | |
677 | * @param ctfClock the clock | |
678 | */ | |
866e5b51 | 679 | public void addClock(String nameValue, CTFClock ctfClock) { |
c88e827d | 680 | clocks.put(nameValue, ctfClock); |
866e5b51 FC |
681 | } |
682 | ||
9ac2eb62 MK |
683 | /** |
684 | * gets the clock with a specific name | |
685 | * @param name the name of the clock. | |
686 | * @return the clock | |
687 | */ | |
c88e827d | 688 | public CTFClock getClock(String name) { |
866e5b51 FC |
689 | return clocks.get(name); |
690 | } | |
691 | ||
8ecc80f3 MK |
692 | private CTFClock singleClock; |
693 | private long singleOffset; | |
694 | ||
9ac2eb62 MK |
695 | /** |
696 | * gets the clock if there is only one. (this is 100% of the use cases as of June 2012) | |
697 | * @return the clock | |
698 | */ | |
8ecc80f3 | 699 | public final CTFClock getClock() { |
c88e827d | 700 | if (clocks.size() == 1) { |
8ecc80f3 MK |
701 | if (singleClock == null) { |
702 | singleClock = clocks.get(clocks.keySet().toArray()[0]); | |
703 | singleOffset = (Long) getClock().getProperty("offset"); //$NON-NLS-1$ | |
704 | } | |
705 | return singleClock; | |
866e5b51 FC |
706 | } |
707 | return null; | |
708 | } | |
709 | ||
9ac2eb62 MK |
710 | /** |
711 | * gets the time offset of a clock with respect to UTC in nanoseconds | |
712 | * @return the time offset of a clock with respect to UTC in nanoseconds | |
713 | */ | |
8ecc80f3 | 714 | public final long getOffset() { |
c88e827d | 715 | if (getClock() == null) { |
ce2388e0 FC |
716 | return 0; |
717 | } | |
8ecc80f3 | 718 | return singleOffset; |
ce2388e0 FC |
719 | } |
720 | ||
9ac2eb62 MK |
721 | /** |
722 | * Does a given stream contain any events? | |
723 | * @param id the stream ID | |
724 | * @return true if the stream has events. | |
725 | */ | |
788ddcbc MK |
726 | public boolean hasEvents(Long id){ |
727 | return eventDecs.containsKey(id); | |
728 | } | |
9ac2eb62 MK |
729 | |
730 | /** | |
731 | * Add an event declaration map to the events map. | |
732 | * @param id the id of a stream | |
733 | * @return the hashmap containing events. | |
734 | */ | |
788ddcbc MK |
735 | public HashMap<Long, EventDeclaration> createEvents(Long id){ |
736 | HashMap<Long, EventDeclaration> value = eventDecs.get(id); | |
737 | if( value == null ) { | |
738 | value = new HashMap<Long, EventDeclaration>(); | |
739 | eventDecs.put(id, value); | |
740 | } | |
741 | return value; | |
742 | } | |
743 | ||
866e5b51 | 744 | } |
c88e827d AM |
745 | |
746 | class MetadataFileFilter implements FileFilter { | |
747 | ||
748 | @Override | |
749 | public boolean accept(File pathname) { | |
750 | if (pathname.isDirectory()) { | |
751 | return false; | |
752 | } | |
753 | if (pathname.isHidden()) { | |
754 | return false; | |
755 | } | |
756 | if (pathname.getName().equals("metadata")) { //$NON-NLS-1$ | |
757 | return false; | |
758 | } | |
759 | return true; | |
760 | } | |
761 | ||
762 | } | |
763 | ||
debcffff | 764 | class MetadataComparator implements Comparator<File>, Serializable { |
c88e827d | 765 | |
8fd82db5 FC |
766 | private static final long serialVersionUID = 1L; |
767 | ||
c88e827d AM |
768 | @Override |
769 | public int compare(File o1, File o2) { | |
770 | return o1.getName().compareTo(o2.getName()); | |
771 | } | |
772 | } |