Commit | Line | Data |
---|---|---|
791072b0 MK |
1 | package org.eclipse.linuxtools.ctf.core.tests.trace; |
2 | ||
3 | import static org.junit.Assert.*; | |
4 | ||
5 | import java.io.File; | |
6 | import java.io.FileOutputStream; | |
7 | import java.io.FileWriter; | |
8 | import java.io.IOException; | |
9 | ||
10 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; | |
11 | import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; | |
12 | import org.eclipse.linuxtools.internal.ctf.core.event.metadata.IOStructGen; | |
13 | import org.junit.Test; | |
14 | ||
15 | /** | |
16 | * Unit tests for {@link IOStructGen} | |
17 | * @author Matthew Khouzam | |
18 | * | |
19 | */ | |
20 | @SuppressWarnings("nls") | |
21 | public class IOstructgenTest { | |
22 | ||
23 | ||
24 | private static final String metadataDecs = "typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n" | |
25 | + "typealias integer { size = 16; align = 8; signed = false; } := uint16_t;\n" | |
26 | + "typealias integer { size = 32; align = 8; signed = false; } := uint32_t;\n" | |
27 | + "typealias integer { size = 64; align = 8; signed = false; } := uint64_t;\n" | |
28 | + "typealias integer { size = 64; align = 8; signed = false; } := unsigned long;\n" | |
29 | + "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n" | |
30 | + "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n" | |
31 | + "typealias integer { size = 32; align = 1; signed = true; base = decimal; } := int32_t;\n" | |
32 | + "typealias integer { size = 31; align = 1; signed = true; base = dec; } := int31_t;\n" | |
33 | + "typealias integer { size = 30; align = 1; signed = true; base = d; } := int30_t;\n" | |
34 | + "typealias integer { size = 29; align = 1; signed = true; base = i; } := int29_t;\n" | |
35 | + "typealias integer { size = 28; align = 1; signed = true; base = u; } := int28_t;\n" | |
36 | + "typealias integer { size = 27; align = 1; signed = true; base = hexadecimal; } := int27_t;\n" | |
37 | + "typealias integer { size = 26; align = 1; signed = true; base = hex; } := int26_t;\n" | |
38 | + "typealias integer { size = 25; align = 1; signed = true; base = x; } := int25_t;\n" | |
39 | + "typealias integer { size = 24; align = 1; signed = true; base = X; } := int24_t;\n" | |
40 | + "typealias integer { size = 23; align = 1; signed = true; base = p; } := int23_t;\n" | |
41 | + "typealias integer { size = 22; align = 1; signed = true; base = 16; } := int22_t;\n" | |
42 | + "typealias integer { size = 21; align = 1; signed = true; base = oct; } := int21_t;\n" | |
43 | + "typealias integer { size = 20; align = 1; signed = true; base = b; } := int20_t;\n" | |
44 | + "typealias integer { size = 19; align = 1; signed = true; base = octal; } := int19_t;\n" | |
45 | + "typealias integer { size = 18; align = 1; signed = true; base = o; } := int18_t;\n" | |
46 | + "typealias integer { size = 17; align = 1; signed = true; base = binary; } := int17_t;\n" | |
47 | + "\n" | |
48 | + "trace {\n" | |
49 | + " major = 1;\n" | |
50 | + " minor = 8;\n" | |
51 | + " uuid = \"b04d391b-e736-44c1-8d89-4bb438857f8d\";\n" | |
52 | + " byte_order = le;\n" | |
53 | + " packet.header := struct {\n" | |
54 | + " uint32_t magic;\n" | |
55 | + " uint8_t uuid[16];\n" | |
56 | + " uint32_t stream_id;\n" + " };\n" + "};\n" + "\n"; | |
57 | private static final String environmentMD = "env {\n" | |
58 | + " hostname = \"DemoSystem\";\n" | |
59 | + " vpid = 1337;\n" | |
60 | + " procname = \"demo\";\n" | |
61 | + " domain = \"autogenerated\";\n" | |
62 | + " tracer_name = \"tmf\";\n" | |
63 | + " tracer_major = 2;\n" | |
64 | + " tracer_minor = 0x01;\n" | |
65 | + " tracer_patchlevel = 0;\n" | |
66 | + "};\n" + "\n"; | |
67 | private static final String clockMD = "clock {\n" + " name = monotonic;\n" | |
68 | + " uuid = \"cbf9f42e-9be7-4798-a96f-11db556e2ebb\";\n" | |
69 | + " description = \"Monotonic Clock\";\n" | |
70 | + " freq = 1000000000; /* Frequency, in Hz */\n" | |
71 | + " /* clock value offset from Epoch is: offset * (1/freq) */\n" | |
72 | + " offset = 1350310657466295832;\n" + "};\n" | |
73 | + "\n"; | |
74 | ||
75 | private static final String ctfStart = | |
76 | "typealias integer {\n" | |
77 | + " size = 27; align = 1; signed = false;\n" | |
78 | + " map = clock.monotonic.value;\n" | |
79 | + "} := uint27_clock_monotonic_t;\n" | |
80 | + "\n" | |
81 | + "typealias integer {\n" | |
82 | + " size = 32; align = 8; signed = false;\n" | |
83 | + " map = clock.monotonic.value;\n" | |
84 | + "} := uint32_clock_monotonic_t;\n" | |
85 | + "\n" | |
86 | + "typealias integer {\n" | |
87 | + " size = 64; align = 8; signed = false;\n" | |
88 | + " map = clock.monotonic.value;\n" | |
89 | + "} := uint64_clock_monotonic_t;\n" | |
90 | + "\n"; | |
91 | ||
92 | ||
93 | private static final String ctfHeaders = | |
94 | "struct packet_context {\n" | |
95 | + " uint64_clock_monotonic_t timestamp_begin;\n" | |
96 | + " uint64_clock_monotonic_t timestamp_end;\n" | |
97 | + " uint64_t content_size;\n" | |
98 | + " uint64_t packet_size;\n" | |
99 | + " unsigned long events_discarded;\n" | |
100 | + " uint32_t cpu_id;\n" | |
101 | + "};\n" | |
102 | + "\n" | |
103 | + "struct event_header_compact {\n" | |
104 | + " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n" | |
105 | + " variant <id> {\n" | |
106 | + " struct {\n" | |
107 | + " uint27_clock_monotonic_t timestamp;\n" | |
108 | + " } compact;\n" | |
109 | + " struct {\n" | |
110 | + " uint32_t id;\n" | |
111 | + " uint64_clock_monotonic_t timestamp;\n" | |
112 | + " } extended;\n" | |
113 | + " } v;\n" | |
114 | + "} align(8);\n" | |
115 | + "\n" | |
116 | + "struct event_header_large {\n" | |
117 | + " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n" | |
118 | + " variant <id> {\n" + " struct {\n" | |
119 | + " uint32_clock_monotonic_t timestamp;\n" | |
120 | + " } compact;\n" + " struct {\n" | |
121 | + " uint32_t id;\n" | |
122 | + " uint64_clock_monotonic_t timestamp;\n" | |
123 | + " } extended;\n" + " } v;\n" + "} align(8);\n" + "\n"; | |
124 | ||
125 | private static final String ctfBody = "stream {\n" | |
126 | + " id = 0;\n" | |
127 | + " event.header := struct event_header_compact;\n" | |
128 | + " packet.context := struct packet_context;\n" | |
129 | + " event.context := struct {\n" | |
130 | + " integer { size = 64; align = 8; signed = 0; encoding = none; base = 16; } _ip;\n" | |
131 | + " };\n" | |
132 | + "};\n" | |
133 | + "\n" | |
134 | + "event {\n" | |
135 | + " name = \"ust_tests_demo3:done\";\n" | |
136 | + " id = 0;\n" | |
137 | + " stream_id = 0;\n" | |
138 | + " loglevel = 4;\n" | |
139 | + " fields := struct {\n" | |
140 | + " integer { size = 32; align = 8; signed = 1; encoding = none; base = 10; } _value;\n" | |
141 | + " };\n" | |
142 | + "};\n" | |
143 | + "\n" | |
144 | + "event {\n" | |
145 | + " name = \"ust_tests_demo:starting\";\n" | |
146 | + " id = 1;\n" | |
147 | + " stream_id = 0;\n" | |
148 | + " loglevel = 2;\n" | |
149 | + " model.emf.uri = \"http://example.com/path_to_model?q=ust_tests_demo:starting\";\n" | |
150 | + " fields := struct {\n" | |
151 | + " integer { size = 32; align = 8; signed = 1; encoding = none; base = 10; } _value;\n" | |
152 | + " };\n" | |
153 | + "};\n" | |
154 | + "\n" | |
155 | + "event {\n" | |
156 | + " name = \"ust_tests_demo:done\";\n" | |
157 | + " id = 2;\n" | |
158 | + " stream_id = 0;\n" | |
159 | + " loglevel = 2;\n" | |
160 | + " model.emf.uri = \"http://example.com/path_to_model?q=ust_tests_demo:done\";\n" | |
161 | + " fields := struct {\n" | |
162 | + " integer { size = 32; align = 8; signed = 1; encoding = none; base = 10; } _value;\n" | |
163 | + " };\n" | |
164 | + "};\n" | |
165 | + "\n" | |
166 | + "event {\n" | |
167 | + " name = \"ust_tests_demo2:loop\";\n" | |
168 | + " id = 3;\n" | |
169 | + " stream_id = 0;\n" | |
170 | + " loglevel = 4;\n" | |
171 | + " fields := struct {\n" | |
172 | + " integer { size = 32; align = 8; signed = 1; encoding = none; base = 10; } _intfield;\n" | |
173 | + " integer { size = 32; align = 8; signed = 1; encoding = none; base = 16; } _intfield2;\n" | |
174 | + " integer { size = 64; align = 8; signed = 1; encoding = none; base = 10; } _longfield;\n" | |
175 | + " integer { size = 32; align = 8; signed = 1; encoding = none; base = 10; byte_order = be; } _netintfield;\n" | |
176 | + " integer { size = 32; align = 8; signed = 1; encoding = none; base = 16; byte_order = be; } _netintfieldhex;\n" | |
177 | + " integer { size = 64; align = 8; signed = 1; encoding = none; base = 10; } _arrfield1[3];\n" | |
178 | + " integer { size = 8; align = 8; signed = 1; encoding = UTF8; base = 10; } _arrfield2[10];\n" | |
179 | + " integer { size = 64; align = 8; signed = 0; encoding = none; base = 10; } __seqfield1_length;\n" | |
180 | + " integer { size = 8; align = 8; signed = 1; encoding = none; base = 10; } _seqfield1[ __seqfield1_length ];\n" | |
181 | + " integer { size = 64; align = 8; signed = 0; encoding = none; base = 10; } __seqfield2_length;\n" | |
182 | + " integer { size = 8; align = 8; signed = 1; encoding = UTF8; base = 10; } _seqfield2[ __seqfield2_length ];\n" | |
183 | + " string _stringfield;\n" | |
184 | + " floating_point { exp_dig = 8; mant_dig = 24; align = 8; } _floatfield;\n" | |
185 | + " floating_point { exp_dig = 11; mant_dig = 53; align = 8; } _doublefield;\n" | |
186 | + " };\n" | |
187 | + "};\n" | |
188 | + "\n"; | |
189 | ||
190 | private static final String enumMd = | |
191 | "typealias integer { size = 32; align = 8; signed = false; } := int;\n" | |
192 | + "typealias enum { ONE = 0, a,b,c=10, d} := useless_enum;\n" | |
193 | + "struct useless{ \n" | |
194 | + " enum : uint8_t { A=0, \"B\",} enum3;\n" | |
195 | + " useless_enum enum2;" | |
196 | + " enum { C, D, E } enum4;\n" | |
197 | + " uint16_t val;\n" | |
198 | + "} ;\n" | |
199 | + "\n" | |
200 | + "event {\n" | |
201 | + " name = \"enumEvent\";\n" | |
202 | + " id = 6;\n" | |
203 | + " stream_id = 0;\n" | |
204 | + " loglevel = 5;\n" | |
205 | + " fields := struct{\n" | |
206 | + " uint16_t _some_field;\n" | |
207 | // + " useless junk;\n" | |
208 | // + " bad_enum a;\n" | |
209 | + " enum {A, B, C = 3 , } _other_enum;\n" | |
210 | + " };\n" | |
211 | + "};\n" | |
212 | + "\n"; | |
213 | ||
214 | private final static String contextMD = | |
215 | "event {\n" + | |
216 | " name = \"someOtherEvent\";\n" + | |
217 | " id = 5;\n" + | |
218 | " stream_id = 0;\n" + | |
219 | " loglevel = 5;\n" + | |
220 | " context := struct{\n" + | |
221 | " uint16_t _someContext;\n" + | |
222 | " };\n" + | |
223 | " fields := struct{\n" + | |
224 | " uint16_t _somefield;\n" + | |
225 | " };\n" + | |
226 | "};\n " + | |
227 | "\n" ; | |
228 | ||
229 | private static final String callsiteMD = | |
230 | "callsite {\n" | |
231 | + " name = \"ust_tests_demo2:loop\";\n" | |
232 | + " func = \"main\";\n" + " ip = 0x400a29;\n" | |
233 | + " file = \"demo.c\";\n" + " line = 59;\n" + "};\n" + "\n" | |
234 | + "callsite {\n" + " name = \"ust_tests_demo3:done\";\n" | |
235 | + " func = \"main\";\n" + " ip = 0x400a6c;\n" | |
236 | + " file = \"demo.c\";\n" + " line = 62;\n" + "};\n" + "\n" | |
237 | + "callsite {\n" + " name = \"ust_tests_demo:done\";\n" | |
238 | + " func = \"main\";\n" + " ip = 0x400aaf;\n" | |
239 | + " file = \"demo.c\";\n" + " line = 61;\n" + "};\n" + "\n" | |
240 | + "callsite {\n" + " name = \"ust_tests_demo:starting\";\n" | |
241 | + " func = \"main\";\n" + " ip = 0x400af2;\n" | |
242 | + " file = \"demo.c\";\n" + " line = 55;\n" + "};\n"; | |
243 | ||
244 | private static final String simpleTSDL = metadataDecs + ctfStart + ctfHeaders | |
245 | + ctfBody; | |
246 | private static final String enumTSDL = metadataDecs + ctfStart + ctfHeaders | |
247 | + ctfBody + enumMd; | |
248 | private static final String clockTSDL = metadataDecs + clockMD + ctfStart | |
249 | + ctfHeaders + ctfBody; | |
250 | private static final String envTSDL = metadataDecs + environmentMD + ctfStart | |
251 | + ctfHeaders + ctfBody; | |
252 | private static final String contextTSDL = metadataDecs + environmentMD + ctfStart | |
253 | + ctfHeaders + ctfBody +contextMD; | |
254 | private static final String callsiteTSDL = metadataDecs + ctfStart + ctfHeaders | |
255 | + ctfBody + callsiteMD; | |
256 | private static final String allDressedTSDL = metadataDecs + environmentMD + clockMD | |
257 | + ctfStart + ctfHeaders + ctfBody +enumMd+ callsiteMD; | |
258 | ||
259 | static final String tempTraceDir = System.getProperty("java.io.tmpdir") | |
260 | + "/" + "tempTrace"; | |
261 | ||
262 | private static void deltree(File f) { | |
263 | for (File elem : f.listFiles()) { | |
264 | if (elem.isDirectory()) { | |
265 | deltree(elem); | |
266 | } | |
267 | elem.delete(); | |
268 | } | |
269 | f.delete(); | |
270 | } | |
271 | ||
272 | private static void createDummyTrace(String metadata) { | |
273 | ||
274 | File f; | |
275 | try { | |
276 | f = new File(tempTraceDir); | |
277 | if (f.exists()) { | |
278 | deltree(f); | |
279 | } | |
280 | f.mkdirs(); | |
281 | f = new File(tempTraceDir + "/metadata"); //$NON-NLS-1$ | |
282 | FileWriter fw = new FileWriter(f); | |
283 | fw.write(metadata); | |
284 | fw.close(); | |
285 | f.createNewFile(); | |
286 | ||
287 | byte magicLE[] = { (byte) 0xC1, (byte) 0x1F, (byte) 0xFC, (byte) 0xC1 }; | |
288 | byte uuid[] = { (byte) 0xb0, 0x4d, 0x39, 0x1b, (byte) 0xe7, 0x36, | |
289 | 0x44, (byte) 0xc1, (byte) 0x8d, (byte) 0x89, 0x4b, | |
290 | (byte) 0xb4, 0x38, (byte) 0x85, 0x7f, (byte) 0x8d }; | |
291 | final int size = 4096; | |
292 | byte[] data = new byte[size]; | |
293 | for (int i = 0; i < size; i++) { | |
294 | data[i] = 0x00; | |
295 | } | |
296 | f = new File(tempTraceDir + "/dummyChan"); //$NON-NLS-1$ | |
297 | fw = new FileWriter(f); | |
298 | FileOutputStream fos = new FileOutputStream(f); | |
299 | fos.write(magicLE); | |
300 | fos.write(uuid); | |
301 | fos.write(data); | |
302 | fos.close(); | |
303 | fw.close(); | |
304 | f.createNewFile(); | |
305 | } catch (IOException e) { | |
306 | e.printStackTrace(); | |
307 | } finally { | |
308 | f = null; | |
309 | } | |
310 | ||
311 | } | |
312 | ||
313 | /** | |
314 | * Simple test (only the minimum) | |
315 | * @throws CTFReaderException something wrong happened | |
316 | */ | |
317 | @Test | |
318 | public void TSDLSimpleTest() throws CTFReaderException { | |
319 | createDummyTrace(simpleTSDL); | |
320 | CTFTrace trace = new CTFTrace(tempTraceDir); | |
321 | assertNotNull(trace); | |
322 | ||
323 | } | |
324 | ||
325 | /** | |
326 | * Test with environment variables | |
327 | * @throws CTFReaderException something wrong happened | |
328 | */ | |
329 | @Test | |
330 | public void TSDLEnvironmentTest() throws CTFReaderException { | |
331 | createDummyTrace(envTSDL); | |
332 | CTFTrace trace = new CTFTrace(tempTraceDir); | |
333 | assertNotNull(trace); | |
334 | } | |
335 | ||
336 | /** | |
337 | * Test with Clocks | |
338 | * @throws CTFReaderException something wrong happened | |
339 | */ | |
340 | @Test | |
341 | public void TSDLEnumTest() throws CTFReaderException { | |
342 | createDummyTrace(enumTSDL); | |
343 | CTFTrace trace = new CTFTrace(tempTraceDir); | |
344 | assertNotNull(trace); | |
345 | } | |
346 | ||
347 | /** | |
348 | * Test with Clocks | |
349 | * @throws CTFReaderException something wrong happened | |
350 | */ | |
351 | @Test | |
352 | public void TSDLClockTest() throws CTFReaderException { | |
353 | createDummyTrace(clockTSDL); | |
354 | CTFTrace trace = new CTFTrace(tempTraceDir); | |
355 | assertNotNull(trace); | |
356 | } | |
357 | ||
358 | /** | |
359 | * Test with Contexts | |
360 | * @throws CTFReaderException something wrong happened | |
361 | */ | |
362 | @Test | |
363 | public void TSDLContextTest() throws CTFReaderException { | |
364 | createDummyTrace(contextTSDL); | |
365 | CTFTrace trace = new CTFTrace(tempTraceDir); | |
366 | assertNotNull(trace); | |
367 | } | |
368 | ||
369 | /** | |
370 | * Test with Callsites | |
371 | * @throws CTFReaderException something wrong happened | |
372 | */ | |
373 | @Test | |
374 | public void TSDLCallsiteTest() throws CTFReaderException { | |
375 | createDummyTrace(callsiteTSDL); | |
376 | CTFTrace trace = new CTFTrace(tempTraceDir); | |
377 | assertNotNull(trace); | |
378 | } | |
379 | ||
380 | /** | |
381 | * Test everything | |
382 | * @throws CTFReaderException something wrong happened | |
383 | */ | |
384 | @Test | |
385 | public void TSDLAllTest() throws CTFReaderException { | |
386 | createDummyTrace(allDressedTSDL); | |
387 | CTFTrace trace = new CTFTrace(tempTraceDir); | |
388 | assertNotNull(trace); | |
389 | } | |
390 | ||
391 | } |