fff07a060392b3df77b44756bc5acaced04ca678
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / internal / ctf / core / trace / StreamInputPacketIndexEntry.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2015 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: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.internal.ctf.core.trace;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
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;
26
27 /**
28 * <b><u>StreamInputPacketIndexEntry</u></b>
29 * <p>
30 * Represents an entry in the index of event packets.
31 */
32 public class StreamInputPacketIndexEntry implements ICTFPacketDescriptor {
33
34 private static final int UNKNOWN = -1;
35
36 // ------------------------------------------------------------------------
37 // Attributes
38 // ------------------------------------------------------------------------
39
40 /**
41 * Position of the start of the packet header in the file, in bits
42 */
43 private final long fOffsetBits;
44
45 /**
46 * Position of the start of the packet header in the file, in bytes
47 */
48 private final long fOffsetBytes;
49
50 /**
51 * Packet size, in bits
52 */
53 private final long fPacketSizeBits;
54
55 /**
56 * Content size, in bits
57 */
58 private final long fContentSizeBits;
59
60 /**
61 * Begin timestamp
62 */
63 private final long fTimestampBegin;
64
65 /**
66 * End timestamp
67 */
68 private final long fTimestampEnd;
69
70 /**
71 * How many lost events are there?
72 */
73 private final long fLostEvents;
74
75 /**
76 * Which target is being traced
77 */
78 private final String fTarget;
79 private final long fTargetID;
80
81 /**
82 * Attributes of this index entry
83 */
84 private final Map<String, Object> fAttributes = new HashMap<>();
85
86 // ------------------------------------------------------------------------
87 // Constructors
88 // ------------------------------------------------------------------------
89
90 /**
91 * Constructs an index entry.
92 *
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
97 */
98
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;
104 fLostEvents = 0;
105 fTarget = ""; //$NON-NLS-1$
106 fTargetID = 0;
107 fTimestampBegin = Long.MIN_VALUE;
108 fTimestampEnd = Long.MAX_VALUE;
109 }
110
111 /**
112 * full Constructor
113 *
114 * @param dataOffsetBits
115 * offset in the file for the start of data in bits
116 * @param streamPacketContextDef
117 * packet context
118 * @param fileSizeBytes
119 * number of bytes in a file
120 * @param lostSoFar
121 * number of lost events so far
122 */
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());
134 }
135 }
136
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);
142 // LTTng Specific
143 Long cpuId = (Long) fAttributes.get(CTFStrings.CPU_ID);
144 Long lostEvents = (Long) fAttributes.get(CTFStrings.EVENTS_DISCARDED);
145
146 /* Read the content size in bits */
147 if (contentSize != null) {
148 fContentSizeBits = (contentSize.longValue());
149 } else if (packetSize != null) {
150 fContentSizeBits = (packetSize.longValue());
151 } else {
152 fContentSizeBits = (fileSizeBytes * Byte.SIZE);
153 }
154
155 /* Read the packet size in bits */
156 if (packetSize != null) {
157 fPacketSizeBits = (packetSize.longValue());
158 } else if (this.getContentSizeBits() != 0) {
159 fPacketSizeBits = fContentSizeBits;
160 } else {
161 fPacketSizeBits = (fileSizeBytes * Byte.SIZE);
162 }
163
164 /* Read the begin timestamp */
165 if (tsBegin != null) {
166 fTimestampBegin = (tsBegin.longValue());
167 } else {
168 fTimestampBegin = Long.MIN_VALUE;
169 }
170
171 /* Read the end timestamp */
172 if (tsEnd != null) {
173 // check if tsEnd == unsigned long max value
174 if (tsEnd == -1) {
175 tsEnd = Long.MAX_VALUE;
176 }
177 fTimestampEnd = (tsEnd.longValue());
178 } else {
179 fTimestampEnd = Long.MAX_VALUE;
180 }
181
182 if (device != null) {
183 fTarget = device;
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$
187 fTargetID = cpuId;
188 } else {
189 fTarget = null;
190 fTargetID = UNKNOWN;
191 }
192
193 if (lostEvents != null) {
194 fLostEvents = (lostEvents - lostSoFar);
195 } else {
196 fLostEvents = 0;
197 }
198
199 fOffsetBits = dataOffsetBits;
200 fOffsetBytes = dataOffsetBits / Byte.SIZE;
201 }
202
203 // ------------------------------------------------------------------------
204 // Operations
205 // ------------------------------------------------------------------------
206
207 @Override
208 public boolean includes(long ts) {
209 return (ts >= fTimestampBegin) && (ts <= fTimestampEnd);
210 }
211
212 @Override
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$
217 }
218
219 // ------------------------------------------------------------------------
220 // Getters and Setters
221 // ------------------------------------------------------------------------
222
223 @Override
224 public long getOffsetBits() {
225 return fOffsetBits;
226 }
227
228 @Override
229 public long getPacketSizeBits() {
230 return fPacketSizeBits;
231 }
232
233 @Override
234 public long getContentSizeBits() {
235 return fContentSizeBits;
236 }
237
238 @Override
239 public long getTimestampBegin() {
240 return fTimestampBegin;
241 }
242
243 @Override
244 public long getTimestampEnd() {
245 return fTimestampEnd;
246 }
247
248 @Override
249 public long getLostEvents() {
250 return fLostEvents;
251 }
252
253 /**
254 * Add an attribute to this index entry
255 *
256 * @param field
257 * The name of the attribute
258 * @param value
259 * The value to insert
260 */
261 public void addAttribute(String field, Object value) {
262 fAttributes.put(field, value);
263 }
264
265 @Override
266 public Object lookupAttribute(String field) {
267 return fAttributes.get(field);
268 }
269
270 @Override
271 public String getTarget() {
272 return fTarget;
273 }
274
275 @Override
276 public long getTargetId() {
277 return fTargetID;
278 }
279
280 @Override
281 public long getOffsetBytes() {
282 return fOffsetBytes;
283 }
284 }
This page took 0.03687 seconds and 4 git commands to generate.