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