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