rcp: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core.tests / src / org / eclipse / tracecompass / ctf / core / tests / trace / IOstructgenTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Matthew Khouzam - Initial API and implementation
10 * Simon Delisle - Generate dummy trace
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.ctf.core.tests.trace;
14
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertNotNull;
17
18 import java.io.File;
19 import java.io.FileOutputStream;
20 import java.io.FileWriter;
21 import java.io.IOException;
22 import java.nio.ByteBuffer;
23 import java.nio.ByteOrder;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.eclipse.tracecompass.ctf.core.CTFException;
28 import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
29 import org.eclipse.tracecompass.ctf.core.tests.CtfCoreTestPlugin;
30 import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
31 import org.eclipse.tracecompass.internal.ctf.core.event.EventDeclaration;
32 import org.eclipse.tracecompass.internal.ctf.core.event.metadata.IOStructGen;
33 import org.junit.Test;
34
35 /**
36 * Unit tests for {@link IOStructGen}
37 *
38 * @author Matthew Khouzam
39 */
40 public class IOstructgenTest {
41
42 private static final String metadataDecs = "typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
43 + "typealias integer { size = 16; align = 8; signed = false; } := uint16_t;\n"
44 + "typealias integer { size = 32; align = 8; signed = false; } := uint32_t;\n"
45 + "typealias integer { size = 64; align = 8; signed = false; } := uint64_t;\n"
46 + "typealias integer { size = 64; align = 8; signed = false; } := unsigned long;\n"
47 + "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
48 + "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
49 + "typealias integer { size = 32; align = 1; signed = true; base = decimal; } := int32_t;\n"
50 + "typealias integer { size = 31; align = 1; signed = true; base = dec; } := int31_t;\n"
51 + "typealias integer { size = 30; align = 1; signed = true; base = d; } := int30_t;\n"
52 + "typealias integer { size = 29; align = 1; signed = true; base = i; } := int29_t;\n"
53 + "typealias integer { size = 28; align = 1; signed = true; base = u; } := int28_t;\n"
54 + "typealias integer { size = 27; align = 1; signed = true; base = hexadecimal; } := int27_t;\n"
55 + "typealias integer { size = 26; align = 1; signed = true; base = hex; } := int26_t;\n"
56 + "typealias integer { size = 25; align = 1; signed = true; base = x; } := int25_t;\n"
57 + "typealias integer { size = 24; align = 1; signed = true; base = X; } := int24_t;\n"
58 + "typealias integer { size = 23; align = 1; signed = true; base = p; } := int23_t;\n"
59 + "typealias integer { size = 22; align = 1; signed = true; base = 16; } := int22_t;\n"
60 + "typealias integer { size = 21; align = 1; signed = true; base = oct; } := int21_t;\n"
61 + "typealias integer { size = 20; align = 1; signed = true; base = b; } := int20_t;\n"
62 + "typealias integer { size = 19; align = 1; signed = true; base = octal; } := int19_t;\n"
63 + "typealias integer { size = 18; align = 1; signed = true; base = o; } := int18_t;\n"
64 + "typealias integer { size = 17; align = 1; signed = true; base = binary; } := int17_t;\n"
65 + "\n"
66 + "trace {\n"
67 + " major = 1;\n"
68 + " minor = 8;\n"
69 + " uuid = \"b04d391b-e736-44c1-8d89-4bb438857f8d\";\n"
70 + " byte_order = le;\n"
71 + " packet.header := struct {\n"
72 + " uint32_t magic;\n"
73 + " uint8_t uuid[16];\n"
74 + " uint32_t stream_id;\n" + " };\n" + "};\n" + "\n";
75 private static final String environmentMD = "env {\n"
76 + " hostname = \"DemoSystem\";\n"
77 + " vpid = 1337;\n"
78 + " procname = \"demo\";\n"
79 + " domain = \"autogenerated\";\n"
80 + " tracer_name = \"tmf\";\n"
81 + " tracer_major = 2;\n"
82 + " tracer_minor = 0x01;\n"
83 + " tracer_patchlevel = 0;\n"
84 + "};\n" + "\n";
85 private static final String clockMD = "clock {\n" + " name = monotonic;\n"
86 + " uuid = \"cbf9f42e-9be7-4798-a96f-11db556e2ebb\";\n"
87 + " description = \"Monotonic Clock\";\n"
88 + " freq = 1000000000; /* Frequency, in Hz */\n"
89 + " /* clock value offset from Epoch is: offset * (1/freq) */\n"
90 + " offset = 1350310657466295832;\n" + "};\n"
91 + "\n";
92
93 private static final String ctfStart =
94 "typealias integer {\n"
95 + " size = 27; align = 1; signed = false;\n"
96 + " map = clock.monotonic.value;\n"
97 + "} := uint27_clock_monotonic_t;\n"
98 + "\n"
99 + "typealias integer {\n"
100 + " size = 32; align = 8; signed = false;\n"
101 + " map = clock.monotonic.value;\n"
102 + "} := uint32_clock_monotonic_t;\n"
103 + "\n"
104 + "typealias integer {\n"
105 + " size = 64; align = 8; signed = false;\n"
106 + " map = clock.monotonic.value;\n"
107 + "} := uint64_clock_monotonic_t;\n"
108 + "\n";
109
110 private static final String ctfHeaders =
111 "struct packet_context {\n"
112 + " uint64_clock_monotonic_t timestamp_begin;\n"
113 + " uint64_clock_monotonic_t timestamp_end;\n"
114 + " uint64_t content_size;\n"
115 + " uint64_t packet_size;\n"
116 + " unsigned long events_discarded;\n"
117 + " uint32_t cpu_id;\n"
118 + "};\n"
119 + "\n"
120 + "struct event_header_compact {\n"
121 + " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
122 + " variant <id> {\n"
123 + " struct {\n"
124 + " uint27_clock_monotonic_t timestamp;\n"
125 + " } compact;\n"
126 + " struct {\n"
127 + " uint32_t id;\n"
128 + " uint64_clock_monotonic_t timestamp;\n"
129 + " } extended;\n"
130 + " } v;\n"
131 + "} align(8);\n"
132 + "\n"
133 + "struct event_header_large {\n"
134 + " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
135 + " variant <id> {\n" + " struct {\n"
136 + " uint32_clock_monotonic_t timestamp;\n"
137 + " } compact;\n" + " struct {\n"
138 + " uint32_t id;\n"
139 + " uint64_clock_monotonic_t timestamp;\n"
140 + " } extended;\n" + " } v;\n" + "} align(8);\n" + "\n";
141
142 private static final String ctfBody = "stream {\n"
143 + " id = 0;\n"
144 + " event.header := struct event_header_compact;\n"
145 + " packet.context := struct packet_context;\n"
146 + " event.context := struct {\n"
147 + " integer { size = 64; align = 8; signed = 0; encoding = none; base = 16; } _ip;\n"
148 + " };\n"
149 + "};\n"
150 + "\n"
151 + "event {\n"
152 + " name = \"ust_tests_demo3:done\";\n"
153 + " id = 0;\n"
154 + " stream_id = 0;\n"
155 + " loglevel = 4;\n"
156 + " fields := struct {\n"
157 + " integer { size = 32; align = 8; signed = 1; encoding = none; base = 10; } _value;\n"
158 + " };\n"
159 + "};\n"
160 + "\n"
161 + "event {\n"
162 + " name = \"ust_tests_demo:starting\";\n"
163 + " id = 1;\n"
164 + " stream_id = 0;\n"
165 + " loglevel = 2;\n"
166 + " model.emf.uri = \"http://example.com/path_to_model?q=ust_tests_demo:starting\";\n"
167 + " fields := struct {\n"
168 + " integer { size = 32; align = 8; signed = 1; encoding = none; base = 10; } _value;\n"
169 + " };\n"
170 + "};\n"
171 + "\n"
172 + "event {\n"
173 + " name = \"ust_tests_demo:done\";\n"
174 + " id = 2;\n"
175 + " stream_id = 0;\n"
176 + " loglevel = 2;\n"
177 + " model.emf.uri = \"http://example.com/path_to_model?q=ust_tests_demo:done\";\n"
178 + " fields := struct {\n"
179 + " integer { size = 32; align = 8; signed = 1; encoding = none; base = 10; } _value;\n"
180 + " };\n"
181 + "};\n"
182 + "\n"
183 + "event {\n"
184 + " name = \"ust_tests_demo2:loop\";\n"
185 + " id = 3;\n"
186 + " stream_id = 0;\n"
187 + " loglevel = 4;\n"
188 + " fields := struct {\n"
189 + " integer { size = 32; align = 8; signed = 1; encoding = none; base = 10; } _intfield;\n"
190 + " integer { size = 32; align = 8; signed = 1; encoding = none; base = 16; } _intfield2;\n"
191 + " integer { size = 64; align = 8; signed = 1; encoding = none; base = 10; } _longfield;\n"
192 + " integer { size = 32; align = 8; signed = 1; encoding = none; base = 10; byte_order = be; } _netintfield;\n"
193 + " integer { size = 32; align = 8; signed = 1; encoding = none; base = 16; byte_order = be; } _netintfieldhex;\n"
194 + " integer { size = 64; align = 8; signed = 1; encoding = none; base = 10; } _arrfield1[3];\n"
195 + " integer { size = 8; align = 8; signed = 1; encoding = UTF8; base = 10; } _arrfield2[10];\n"
196 + " integer { size = 64; align = 8; signed = 0; encoding = none; base = 10; } __seqfield1_length;\n"
197 + " integer { size = 8; align = 8; signed = 1; encoding = none; base = 10; } _seqfield1[ __seqfield1_length ];\n"
198 + " integer { size = 64; align = 8; signed = 0; encoding = none; base = 10; } __seqfield2_length;\n"
199 + " integer { size = 8; align = 8; signed = 1; encoding = UTF8; base = 10; } _seqfield2[ __seqfield2_length ];\n"
200 + " string _stringfield;\n"
201 + " floating_point { exp_dig = 8; mant_dig = 24; align = 8; } _floatfield;\n"
202 + " floating_point { exp_dig = 11; mant_dig = 53; align = 8; } _doublefield;\n"
203 + " };\n"
204 + "};\n"
205 + "\n";
206
207 private static final String enumMd =
208 "typealias integer { size = 32; align = 8; signed = false; } := int;\n"
209 + "typealias enum { ONE = 0, a,b,c=10, d} := useless_enum;\n"
210 + "struct useless{ \n"
211 + " enum : uint8_t { A=0, \"B\",} enum3;\n"
212 + " useless_enum enum2;"
213 + " enum { C, D, E } enum4;\n"
214 + " uint16_t val;\n"
215 + "} ;\n"
216 + "\n"
217 + "event {\n"
218 + " name = \"enumEvent\";\n"
219 + " id = 6;\n"
220 + " stream_id = 0;\n"
221 + " loglevel = 5;\n"
222 + " fields := struct{\n"
223 + " uint16_t _some_field;\n"
224 // + " useless junk;\n"
225 // + " bad_enum a;\n"
226 + " enum {A, B, C = 3 , } _other_enum;\n"
227 + " };\n"
228 + "};\n"
229 + "\n";
230
231 private final static String contextMD =
232 "event {\n" +
233 " name = \"someOtherEvent\";\n" +
234 " id = 5;\n" +
235 " stream_id = 0;\n" +
236 " loglevel = 5;\n" +
237 " context := struct{\n" +
238 " uint16_t _someContext;\n" +
239 " };\n" +
240 " fields := struct{\n" +
241 " uint16_t _somefield;\n" +
242 " };\n" +
243 "};\n " +
244 "\n";
245
246 private static final String callsiteMD =
247 "callsite {\n"
248 + " name = \"ust_tests_demo2:loop\";\n"
249 + " func = \"main\";\n" + " ip = 0x400a29;\n"
250 + " file = \"demo.c\";\n" + " line = 59;\n" + "};\n" + "\n"
251 + "callsite {\n" + " name = \"ust_tests_demo3:done\";\n"
252 + " func = \"main\";\n" + " ip = 0x400a6c;\n"
253 + " file = \"demo.c\";\n" + " line = 62;\n" + "};\n" + "\n"
254 + "callsite {\n" + " name = \"ust_tests_demo:done\";\n"
255 + " func = \"main\";\n" + " ip = 0x400aaf;\n"
256 + " file = \"demo.c\";\n" + " line = 61;\n" + "};\n" + "\n"
257 + "callsite {\n" + " name = \"ust_tests_demo:starting\";\n"
258 + " func = \"main\";\n" + " ip = 0x400af2;\n"
259 + " file = \"demo.c\";\n" + " line = 55;\n" + "};\n";
260
261 private static final String simpleTSDL = metadataDecs + ctfStart + ctfHeaders
262 + ctfBody;
263 private static final String enumTSDL = metadataDecs + ctfStart + ctfHeaders
264 + ctfBody + enumMd;
265 private static final String clockTSDL = metadataDecs + clockMD + ctfStart
266 + ctfHeaders + ctfBody;
267 private static final String envTSDL = metadataDecs + environmentMD + ctfStart
268 + ctfHeaders + ctfBody;
269 private static final String contextTSDL = metadataDecs + environmentMD + ctfStart
270 + ctfHeaders + ctfBody + contextMD;
271 private static final String callsiteTSDL = metadataDecs + ctfStart + ctfHeaders
272 + ctfBody + callsiteMD;
273 private static final String allDressedTSDL = metadataDecs + environmentMD + clockMD
274 + ctfStart + ctfHeaders + ctfBody + enumMd + callsiteMD;
275
276 static final String tempTraceDir = CtfCoreTestPlugin.getTemporaryDirPath()
277 + File.separator + "tempTrace";
278
279 private static final int DATA_SIZE = 4096;
280
281 private static final int HEADER_SIZE = 68;
282
283 private static final int PACKET_SIZE = DATA_SIZE + HEADER_SIZE + 512;
284
285 private CTFTrace trace;
286
287 private static class Event {
288 private static final int EVENT_SIZE = 16;
289 private int eventId;
290 private int eventTimestamp;
291 private int eventContent;
292
293 public Event(int id, int content) {
294 eventId = id;
295 eventTimestamp = 0;
296 eventContent = content;
297 }
298
299 public void setEventTimestamp(int eventTimestamp) {
300 this.eventTimestamp = eventTimestamp;
301 }
302
303 public void setEventContent(int eventContent) {
304 this.eventContent = eventContent;
305 }
306
307 public void writeEvent(ByteBuffer data) {
308 // Id and Timestamp
309 int timeId = eventTimestamp << 5;
310 timeId |= eventId & 0x1f;
311 data.putInt(timeId);
312
313 // Context
314 long ip = 0x0000facedecafe00L + ((data.position() /
315 getSize()) & 0x0F);
316 data.putLong(ip);
317
318 // Content
319 data.putInt(eventContent);
320
321 }
322
323 public int getSize() {
324 return EVENT_SIZE;
325 }
326
327 }
328
329 private static void deltree(File f) {
330 for (File elem : f.listFiles()) {
331 if (elem.isDirectory()) {
332 deltree(elem);
333 }
334 elem.delete();
335 }
336 f.delete();
337 }
338
339 private static void createDummyTrace(String metadata) {
340 File dir = new File(tempTraceDir);
341 if (dir.exists()) {
342 deltree(dir);
343 }
344 dir.mkdirs();
345
346 File metadataFile = new File(tempTraceDir + "/metadata");
347 try (FileWriter fw = new FileWriter(metadataFile);) {
348 fw.write(metadata);
349 } catch (IOException e) {
350 e.printStackTrace();
351 }
352
353 byte magicLE[] = { (byte) 0xC1, (byte) 0x1F, (byte) 0xFC,
354 (byte) 0xC1 };
355 byte uuid[] = { (byte) 0xb0, 0x4d, 0x39, 0x1b, (byte) 0xe7,
356 0x36, 0x44, (byte) 0xc1, (byte) 0x8d, (byte) 0x89, 0x4b,
357 (byte) 0xb4, 0x38, (byte) 0x85, 0x7f, (byte) 0x8d };
358
359 Event ev = new Event(2, 2);
360
361 final int nbEvents = (DATA_SIZE / ev.getSize()) - 1;
362 final int contentSize = (nbEvents * ev.getSize() +
363 HEADER_SIZE) * 8;
364
365 ByteBuffer data = ByteBuffer.allocate(PACKET_SIZE);
366 data.order(ByteOrder.LITTLE_ENDIAN);
367 data.clear();
368
369 // packet header
370 // magic number 4
371 data.put(magicLE);
372 // uuid 16
373 data.put(uuid);
374 // stream ID 4
375 data.putInt(0);
376
377 // packet context
378 // timestamp_begin 8
379 data.putLong(0xa500);
380
381 // timestamp_end 8
382 data.putLong(nbEvents * 0x10000 + 0xa5a6);
383
384 // content_size 8
385 data.putLong(contentSize);
386
387 // packet_size 8
388 data.putLong(PACKET_SIZE * 8);
389
390 // events_discarded 8
391 data.putLong(0);
392
393 // cpu_id 4
394 data.putInt(0);
395
396 // fill me
397 for (int i = 0; i < nbEvents; i++) {
398 ev.setEventTimestamp(i * 0x10000 + 0xa5a5);
399 ev.setEventContent(i);
400 ev.writeEvent(data);
401 }
402
403 // The byteBuffer needs to be flipped in file writing mode
404 data.flip();
405
406 File dummyFile = new File(tempTraceDir + "/dummyChan");
407 try (FileOutputStream fos = new FileOutputStream(dummyFile);) {
408 fos.getChannel().write(data);
409 } catch (IOException e) {
410 e.printStackTrace();
411 }
412
413 }
414
415 /**
416 * Simple test (only the minimum)
417 *
418 * @throws CTFException
419 * something wrong happened
420 */
421 @Test
422 public void TSDLSimpleTest() throws CTFException {
423 createDummyTrace(simpleTSDL);
424 trace = new CTFTrace(tempTraceDir);
425 assertNotNull(trace);
426 }
427
428 /**
429 * Test with environment variables
430 *
431 * @throws CTFException
432 * something wrong happened
433 */
434 @Test
435 public void TSDLEnvironmentTest() throws CTFException {
436 createDummyTrace(envTSDL);
437 trace = new CTFTrace(tempTraceDir);
438 assertNotNull(trace);
439 }
440
441 /**
442 * Test with Clocks
443 *
444 * @throws CTFException
445 * something wrong happened
446 */
447 @Test
448 public void TSDLEnumTest() throws CTFException {
449 createDummyTrace(enumTSDL);
450 trace = new CTFTrace(tempTraceDir);
451 assertNotNull(trace);
452 }
453
454 /**
455 * Test with Clocks
456 *
457 * @throws CTFException
458 * something wrong happened
459 */
460 @Test
461 public void TSDLClockTest() throws CTFException {
462 createDummyTrace(clockTSDL);
463 trace = new CTFTrace(tempTraceDir);
464 assertNotNull(trace);
465 }
466
467 /**
468 * Test with Contexts
469 *
470 * @throws CTFException
471 * something wrong happened
472 */
473 @Test
474 public void TSDLContextTest() throws CTFException {
475 createDummyTrace(contextTSDL);
476 trace = new CTFTrace(tempTraceDir);
477 assertNotNull(trace);
478 }
479
480 /**
481 * Test with Callsites
482 *
483 * @throws CTFException
484 * something wrong happened
485 */
486 @Test
487 public void TSDLCallsiteTest() throws CTFException {
488 createDummyTrace(callsiteTSDL);
489 trace = new CTFTrace(tempTraceDir);
490 assertNotNull(trace);
491 }
492
493 /**
494 * Test everything
495 *
496 * @throws CTFException
497 * something wrong happened
498 */
499 @Test
500 public void TSDLAllTest() throws CTFException {
501 createDummyTrace(allDressedTSDL);
502 trace = new CTFTrace(tempTraceDir);
503 assertNotNull(trace);
504
505 final List<IEventDeclaration> eventDeclarations = new ArrayList<>(trace.getEventDeclarations(0L));
506 final EventDeclaration eventDeclaration = (EventDeclaration) eventDeclarations.get(2);
507 assertEquals("http://example.com/path_to_model?q=ust_tests_demo:done",
508 eventDeclaration.getCustomAttribute("model.emf.uri"));
509 }
510
511 }
This page took 0.145177 seconds and 5 git commands to generate.