1 /*******************************************************************************
2 * Copyright (c) 2011, 2015 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: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.ctf
.core
.trace
;
15 import java
.util
.HashMap
;
18 import org
.eclipse
.tracecompass
.ctf
.core
.CTFStrings
;
19 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.EnumDefinition
;
20 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.FloatDefinition
;
21 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDefinition
;
22 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IntegerDefinition
;
23 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StringDefinition
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDefinition
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.ICTFPacketDescriptor
;
28 * <b><u>StreamInputPacketIndexEntry</u></b>
30 * Represents an entry in the index of event packets.
32 public class StreamInputPacketIndexEntry
implements ICTFPacketDescriptor
{
34 private static final int UNKNOWN
= -1;
36 // ------------------------------------------------------------------------
38 // ------------------------------------------------------------------------
41 * Position of the start of the packet header in the file, in bits
43 private final long fOffsetBits
;
46 * Position of the start of the packet header in the file, in bytes
48 private final long fOffsetBytes
;
51 * Packet size, in bits
53 private final long fPacketSizeBits
;
56 * Content size, in bits
58 private final long fContentSizeBits
;
63 private final long fTimestampBegin
;
68 private final long fTimestampEnd
;
71 * How many lost events are there?
73 private final long fLostEvents
;
76 * Which target is being traced
78 private final String fTarget
;
79 private final long fTargetID
;
82 * Attributes of this index entry
84 private final Map
<String
, Object
> fAttributes
= new HashMap
<>();
86 // ------------------------------------------------------------------------
88 // ------------------------------------------------------------------------
91 * Constructs an index entry.
93 * @param dataOffsetBits
94 * offset in the file for the start of data in bits
95 * @param fileSizeBytes
96 * number of bytes in a file
99 public StreamInputPacketIndexEntry(long dataOffsetBits
, long fileSizeBytes
) {
100 fContentSizeBits
= (fileSizeBytes
* Byte
.SIZE
);
101 fPacketSizeBits
= (fileSizeBytes
* Byte
.SIZE
);
102 fOffsetBits
= dataOffsetBits
;
103 fOffsetBytes
= dataOffsetBits
/ Byte
.SIZE
;
105 fTarget
= ""; //$NON-NLS-1$
107 fTimestampBegin
= Long
.MIN_VALUE
;
108 fTimestampEnd
= Long
.MAX_VALUE
;
114 * @param dataOffsetBits
115 * offset in the file for the start of data in bits
116 * @param streamPacketContextDef
118 * @param fileSizeBytes
119 * number of bytes in a file
121 * number of lost events so far
123 public StreamInputPacketIndexEntry(long dataOffsetBits
, StructDefinition streamPacketContextDef
, long fileSizeBytes
, long lostSoFar
) {
124 for (String field
: streamPacketContextDef
.getDeclaration().getFieldsList()) {
125 IDefinition id
= streamPacketContextDef
.lookupDefinition(field
);
126 if (id
instanceof IntegerDefinition
) {
127 fAttributes
.put(field
, ((IntegerDefinition
) id
).getValue());
128 } else if (id
instanceof FloatDefinition
) {
129 fAttributes
.put(field
, ((FloatDefinition
) id
).getValue());
130 } else if (id
instanceof EnumDefinition
) {
131 fAttributes
.put(field
, ((EnumDefinition
) id
).getValue());
132 } else if (id
instanceof StringDefinition
) {
133 fAttributes
.put(field
, ((StringDefinition
) id
).getValue());
137 Long contentSize
= (Long
) fAttributes
.get(CTFStrings
.CONTENT_SIZE
);
138 Long packetSize
= (Long
) fAttributes
.get(CTFStrings
.PACKET_SIZE
);
139 Long tsBegin
= (Long
) fAttributes
.get(CTFStrings
.TIMESTAMP_BEGIN
);
140 Long tsEnd
= (Long
) fAttributes
.get(CTFStrings
.TIMESTAMP_END
);
141 String device
= (String
) fAttributes
.get(CTFStrings
.DEVICE
);
143 Long cpuId
= (Long
) fAttributes
.get(CTFStrings
.CPU_ID
);
144 Long lostEvents
= (Long
) fAttributes
.get(CTFStrings
.EVENTS_DISCARDED
);
146 /* Read the content size in bits */
147 if (contentSize
!= null) {
148 fContentSizeBits
= (contentSize
.longValue());
149 } else if (packetSize
!= null) {
150 fContentSizeBits
= (packetSize
.longValue());
152 fContentSizeBits
= (fileSizeBytes
* Byte
.SIZE
);
155 /* Read the packet size in bits */
156 if (packetSize
!= null) {
157 fPacketSizeBits
= (packetSize
.longValue());
158 } else if (this.getContentSizeBits() != 0) {
159 fPacketSizeBits
= fContentSizeBits
;
161 fPacketSizeBits
= (fileSizeBytes
* Byte
.SIZE
);
164 /* Read the begin timestamp */
165 if (tsBegin
!= null) {
166 fTimestampBegin
= (tsBegin
.longValue());
168 fTimestampBegin
= Long
.MIN_VALUE
;
171 /* Read the end timestamp */
173 // check if tsEnd == unsigned long max value
175 tsEnd
= Long
.MAX_VALUE
;
177 fTimestampEnd
= (tsEnd
.longValue());
179 fTimestampEnd
= Long
.MAX_VALUE
;
182 if (device
!= null) {
184 fTargetID
= Integer
.parseInt(device
.replaceAll("[\\D]", "")); //$NON-NLS-1$ //$NON-NLS-2$ // slow
185 } else if (cpuId
!= null) {
186 fTarget
= ("CPU" + cpuId
.toString()); //$NON-NLS-1$
193 if (lostEvents
!= null) {
194 fLostEvents
= (lostEvents
- lostSoFar
);
199 fOffsetBits
= dataOffsetBits
;
200 fOffsetBytes
= dataOffsetBits
/ Byte
.SIZE
;
203 // ------------------------------------------------------------------------
205 // ------------------------------------------------------------------------
208 public boolean includes(long ts
) {
209 return (ts
>= fTimestampBegin
) && (ts
<= fTimestampEnd
);
213 public String
toString() {
214 return "StreamInputPacketIndexEntry [offsetBits=" + fOffsetBits
//$NON-NLS-1$
215 + ", timestampBegin=" + fTimestampBegin
+ ", timestampEnd=" //$NON-NLS-1$ //$NON-NLS-2$
216 + fTimestampEnd
+ "]"; //$NON-NLS-1$
219 // ------------------------------------------------------------------------
220 // Getters and Setters
221 // ------------------------------------------------------------------------
224 public long getOffsetBits() {
229 public long getPacketSizeBits() {
230 return fPacketSizeBits
;
234 public long getContentSizeBits() {
235 return fContentSizeBits
;
239 public long getTimestampBegin() {
240 return fTimestampBegin
;
244 public long getTimestampEnd() {
245 return fTimestampEnd
;
249 public long getLostEvents() {
254 * Add an attribute to this index entry
257 * The name of the attribute
259 * The value to insert
261 public void addAttribute(String field
, Object value
) {
262 fAttributes
.put(field
, value
);
266 public Object
lookupAttribute(String field
) {
267 return fAttributes
.get(field
);
271 public String
getTarget() {
276 public long getTargetId() {
281 public long getOffsetBytes() {