Commit | Line | Data |
---|---|---|
306586b1 | 1 | /******************************************************************************* |
8967c8c0 | 2 | * Copyright (c) 2013, 2014 Ericsson |
306586b1 SD |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made 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: | |
10 | * Simon Delisle - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.ctf.core.tests.request; |
306586b1 | 14 | |
ab346a2b BH |
15 | import static org.junit.Assert.assertEquals; |
16 | import static org.junit.Assert.assertFalse; | |
17 | import static org.junit.Assert.assertTrue; | |
18 | import static org.junit.Assert.fail; | |
306586b1 SD |
19 | import static org.junit.Assume.assumeTrue; |
20 | ||
21 | import java.util.ArrayList; | |
306586b1 SD |
22 | import java.util.LinkedList; |
23 | import java.util.List; | |
24 | ||
25 | import org.eclipse.core.resources.IResource; | |
2bdf0193 AM |
26 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
27 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; | |
28 | import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest; | |
29 | import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest; | |
30 | import org.eclipse.tracecompass.tmf.core.signal.TmfTimeSynchSignal; | |
31 | import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp; | |
32 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange; | |
33 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp; | |
34 | import org.eclipse.tracecompass.tmf.ctf.core.CtfTmfEvent; | |
35 | import org.eclipse.tracecompass.tmf.ctf.core.CtfTmfTrace; | |
36 | import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTrace; | |
306586b1 SD |
37 | import org.junit.After; |
38 | import org.junit.Before; | |
b1b999ea | 39 | import org.junit.Ignore; |
50d6b86f | 40 | import org.junit.Rule; |
306586b1 | 41 | import org.junit.Test; |
50d6b86f AM |
42 | import org.junit.rules.TestRule; |
43 | import org.junit.rules.Timeout; | |
306586b1 SD |
44 | |
45 | /** | |
46 | * Test suite for the scheduler. | |
47 | */ | |
48 | public class TmfSchedulerTest { | |
49 | ||
50d6b86f AM |
50 | /** Time-out tests after 60 seconds */ |
51 | @Rule | |
52 | public TestRule globalTimeout= new Timeout(60000); | |
53 | ||
306586b1 SD |
54 | // ------------------------------------------------------------------------ |
55 | // Constants | |
56 | // ------------------------------------------------------------------------ | |
57 | ||
9ac63b5b | 58 | private static final CtfTmfTestTrace testTrace = CtfTmfTestTrace.KERNEL; |
306586b1 SD |
59 | private static final int NB_EVENTS_TRACE = 695319; |
60 | private static final int NB_EVENTS_TIME_RANGE = 155133; | |
61 | ||
62 | // ------------------------------------------------------------------------ | |
63 | // Attributes | |
64 | // ------------------------------------------------------------------------ | |
65 | ||
66 | private CtfTmfTrace fixture; | |
67 | ||
68 | private long fStartTime; | |
69 | private long fEndTime; | |
70 | private TmfTimeRange fForegroundTimeRange; | |
71 | ||
ccf2bbb4 | 72 | private final List<String> fOrderList = new ArrayList<>(); |
306586b1 SD |
73 | private int fForegroundId = 0; |
74 | private int fBackgroundId = 0; | |
75 | ||
76 | /** | |
77 | * Perform pre-test initialization. | |
78 | * | |
79 | * @throws TmfTraceException | |
80 | * If the test trace is not found | |
81 | */ | |
82 | @Before | |
83 | public void setUp() throws TmfTraceException { | |
9ac63b5b | 84 | assumeTrue(testTrace.exists()); |
306586b1 | 85 | fixture = new CtfTmfTrace(); |
9ac63b5b | 86 | fixture.initTrace((IResource) null, testTrace.getPath(), CtfTmfEvent.class); |
306586b1 SD |
87 | fixture.indexTrace(true); |
88 | fStartTime = fixture.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(); | |
89 | fEndTime = fixture.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(); | |
90 | ||
91 | long foregroundStartTime = fStartTime + ((fEndTime - fStartTime) / 4); | |
92 | long foregroundEndTime = fStartTime + ((fEndTime - fStartTime) / 2); | |
065cc19b AM |
93 | fForegroundTimeRange = new TmfTimeRange(new TmfTimestamp(foregroundStartTime, ITmfTimestamp.NANOSECOND_SCALE), |
94 | new TmfTimestamp(foregroundEndTime, ITmfTimestamp.NANOSECOND_SCALE)); | |
306586b1 SD |
95 | } |
96 | ||
97 | /** | |
98 | * Perform post-test clean-up. | |
99 | */ | |
100 | @After | |
101 | public void tearDown() { | |
102 | if (fixture != null) { | |
103 | fixture.dispose(); | |
104 | } | |
105 | } | |
106 | ||
107 | // ------------------------------------------------------------------------ | |
108 | // Tests cases | |
109 | // ------------------------------------------------------------------------ | |
110 | ||
111 | /** | |
112 | * Test one background request | |
113 | */ | |
114 | @Test | |
115 | public void backgroundRequest() { | |
116 | BackgroundRequest background = new BackgroundRequest(TmfTimeRange.ETERNITY); | |
117 | fixture.sendRequest(background); | |
118 | try { | |
119 | background.waitForCompletion(); | |
120 | } catch (InterruptedException e) { | |
121 | fail(); | |
122 | } | |
123 | assertEquals(NB_EVENTS_TRACE, background.getNbEvents()); | |
124 | } | |
125 | ||
126 | /** | |
127 | * Test one foreground request | |
128 | */ | |
129 | @Test | |
130 | public void foregroundRequest() { | |
131 | ForegroundRequest foreground = new ForegroundRequest(TmfTimeRange.ETERNITY); | |
132 | fixture.sendRequest(foreground); | |
133 | try { | |
134 | foreground.waitForCompletion(); | |
135 | } catch (InterruptedException e) { | |
136 | fail(); | |
137 | } | |
138 | assertEquals(NB_EVENTS_TRACE, foreground.getNbEvents()); | |
139 | } | |
140 | ||
141 | /** | |
142 | * Test one foreground and one background request for the entire trace at | |
143 | * the same time | |
144 | */ | |
145 | @Test | |
146 | public void TestMultiRequest1() { | |
147 | BackgroundRequest background = new BackgroundRequest(TmfTimeRange.ETERNITY); | |
148 | ForegroundRequest foreground = new ForegroundRequest(TmfTimeRange.ETERNITY); | |
149 | ||
150 | fixture.sendRequest(background); | |
151 | fixture.sendRequest(foreground); | |
152 | try { | |
153 | background.waitForCompletion(); | |
154 | foreground.waitForCompletion(); | |
155 | } catch (InterruptedException e) { | |
156 | fail(); | |
157 | } | |
158 | ||
159 | assertEquals(NB_EVENTS_TRACE, background.getNbEvents()); | |
160 | assertEquals(NB_EVENTS_TRACE, foreground.getNbEvents()); | |
161 | } | |
162 | ||
163 | /** | |
164 | * Test one background request for the entire trace and one foreground | |
165 | * request for smaller time range | |
166 | */ | |
167 | @Test | |
168 | public void TestMultiRequest2() { | |
169 | BackgroundRequest background2 = new BackgroundRequest(TmfTimeRange.ETERNITY); | |
170 | ForegroundRequest foreground2 = new ForegroundRequest(fForegroundTimeRange); | |
171 | ||
172 | fixture.sendRequest(background2); | |
173 | fixture.sendRequest(foreground2); | |
174 | try { | |
175 | background2.waitForCompletion(); | |
176 | foreground2.waitForCompletion(); | |
177 | } catch (InterruptedException e) { | |
178 | fail(); | |
179 | } | |
180 | ||
181 | assertEquals(NB_EVENTS_TRACE, background2.getNbEvents()); | |
182 | assertEquals(NB_EVENTS_TIME_RANGE, foreground2.getNbEvents()); | |
183 | } | |
184 | ||
185 | /** | |
186 | * Test two foreground request, one to select a time range and one to select | |
187 | * an event in this time range | |
188 | */ | |
189 | @Test | |
190 | public void TestMultiRequest3() { | |
191 | ForegroundRequest foreground3 = new ForegroundRequest(TmfTimeRange.ETERNITY); | |
192 | fixture.sendRequest(foreground3); | |
193 | ||
194 | TmfTimeSynchSignal signal3 = new TmfTimeSynchSignal(this, new TmfTimestamp(fForegroundTimeRange.getStartTime())); | |
195 | fixture.broadcast(signal3); | |
196 | ||
197 | try { | |
198 | foreground3.waitForCompletion(); | |
199 | } catch (InterruptedException e) { | |
200 | fail(); | |
201 | } | |
202 | ||
203 | assertEquals(NB_EVENTS_TRACE, foreground3.getNbEvents()); | |
204 | } | |
205 | ||
206 | /** | |
207 | * Test two foreground request, one to select a time range and one to select | |
208 | * an event before this time range | |
209 | */ | |
210 | @Test | |
211 | public void TestMultiRequest4() { | |
212 | ForegroundRequest foreground4 = new ForegroundRequest(fForegroundTimeRange); | |
213 | fixture.sendRequest(foreground4); | |
214 | TmfTimeSynchSignal signal4 = new TmfTimeSynchSignal(this, new TmfTimestamp(fStartTime + ((fEndTime - fStartTime) / 8))); | |
215 | fixture.broadcast(signal4); | |
216 | ||
217 | try { | |
218 | foreground4.waitForCompletion(); | |
219 | } catch (InterruptedException e) { | |
220 | fail(); | |
221 | } | |
222 | ||
223 | assertEquals(NB_EVENTS_TIME_RANGE, foreground4.getNbEvents()); | |
224 | } | |
225 | ||
226 | /** | |
227 | * Test two foreground request, one to select a time range and one to select | |
228 | * an event after this time range | |
229 | */ | |
230 | @Test | |
231 | public void TestMultiRequest5() { | |
232 | ForegroundRequest foreground5 = new ForegroundRequest(fForegroundTimeRange); | |
233 | fixture.sendRequest(foreground5); | |
234 | TmfTimeSynchSignal signal5 = new TmfTimeSynchSignal(this, new TmfTimestamp(fEndTime - ((fEndTime - fStartTime) / 4))); | |
235 | fixture.broadcast(signal5); | |
236 | ||
237 | try { | |
238 | foreground5.waitForCompletion(); | |
239 | } catch (InterruptedException e) { | |
240 | fail(); | |
241 | } | |
242 | ||
243 | assertEquals(NB_EVENTS_TIME_RANGE, foreground5.getNbEvents()); | |
244 | } | |
245 | ||
246 | /** | |
247 | * Test one background and one foreground request for the entire trace and | |
248 | * one foreground request to select an event | |
249 | */ | |
250 | @Test | |
251 | public void TestMultiRequest6() { | |
252 | BackgroundRequest background6 = new BackgroundRequest(TmfTimeRange.ETERNITY); | |
253 | ForegroundRequest foreground6 = new ForegroundRequest(TmfTimeRange.ETERNITY); | |
254 | ||
255 | fixture.sendRequest(background6); | |
256 | fixture.sendRequest(foreground6); | |
257 | ||
258 | TmfTimeSynchSignal signal6 = new TmfTimeSynchSignal(this, new TmfTimestamp(fStartTime + ((fEndTime - fStartTime) / 8))); | |
259 | fixture.broadcast(signal6); | |
260 | ||
261 | try { | |
262 | background6.waitForCompletion(); | |
263 | foreground6.waitForCompletion(); | |
264 | } catch (InterruptedException e) { | |
265 | fail(); | |
266 | } | |
267 | ||
268 | assertEquals(NB_EVENTS_TRACE, background6.getNbEvents()); | |
269 | assertEquals(NB_EVENTS_TRACE, foreground6.getNbEvents()); | |
270 | } | |
271 | ||
272 | /** | |
273 | * Four request, two foreground and two background | |
274 | */ | |
275 | @Test | |
276 | public void TestMultiRequest7() { | |
277 | ForegroundRequest foreground7 = new ForegroundRequest(TmfTimeRange.ETERNITY); | |
278 | ForegroundRequest foreground8 = new ForegroundRequest(fForegroundTimeRange); | |
279 | BackgroundRequest background7 = new BackgroundRequest(TmfTimeRange.ETERNITY); | |
280 | BackgroundRequest background8 = new BackgroundRequest(TmfTimeRange.ETERNITY); | |
281 | fixture.sendRequest(foreground7); | |
282 | fixture.sendRequest(foreground8); | |
283 | fixture.sendRequest(background7); | |
284 | fixture.sendRequest(background8); | |
285 | try { | |
286 | foreground7.waitForCompletion(); | |
287 | foreground8.waitForCompletion(); | |
288 | background7.waitForCompletion(); | |
289 | background8.waitForCompletion(); | |
290 | } catch (InterruptedException e) { | |
291 | fail(); | |
292 | } | |
293 | assertEquals(NB_EVENTS_TRACE, foreground7.getNbEvents()); | |
294 | assertEquals(NB_EVENTS_TIME_RANGE, foreground8.getNbEvents()); | |
295 | assertEquals(NB_EVENTS_TRACE, background7.getNbEvents()); | |
296 | assertEquals(NB_EVENTS_TRACE, background8.getNbEvents()); | |
297 | } | |
298 | ||
299 | /** | |
300 | * One long foreground request and one short foreground request, the short | |
301 | * one should finish first | |
302 | */ | |
303 | @Test | |
304 | public void preemptedForegroundRequest() { | |
305 | ForegroundRequest foreground9 = new ForegroundRequest(TmfTimeRange.ETERNITY); | |
065cc19b AM |
306 | TmfTimeRange shortTimeRange = new TmfTimeRange(new TmfTimestamp(fStartTime, ITmfTimestamp.NANOSECOND_SCALE), |
307 | new TmfTimestamp(fStartTime + ((fEndTime - fStartTime) / 16), ITmfTimestamp.NANOSECOND_SCALE)); | |
306586b1 SD |
308 | ForegroundRequest shortForeground = new ForegroundRequest(shortTimeRange); |
309 | fixture.sendRequest(foreground9); | |
8967c8c0 BH |
310 | try { |
311 | foreground9.waitForStart(); | |
312 | } catch (InterruptedException e) { | |
313 | fail(); | |
314 | } | |
306586b1 SD |
315 | fixture.sendRequest(shortForeground); |
316 | try { | |
317 | shortForeground.waitForCompletion(); | |
318 | } catch (InterruptedException e) { | |
319 | fail(); | |
320 | } | |
321 | assertFalse(foreground9.isCompleted()); | |
322 | } | |
323 | ||
324 | /** | |
325 | * One long background request and one short foreground request, the | |
326 | * foreground request should finish first | |
327 | */ | |
328 | @Test | |
329 | public void preemptedBackgroundRequest() { | |
330 | BackgroundRequest background9 = new BackgroundRequest(TmfTimeRange.ETERNITY); | |
331 | ForegroundRequest foreground10 = new ForegroundRequest(fForegroundTimeRange); | |
332 | fixture.sendRequest(background9); | |
333 | fixture.sendRequest(foreground10); | |
334 | try { | |
335 | foreground10.waitForCompletion(); | |
336 | } catch (InterruptedException e) { | |
337 | fail(); | |
338 | } | |
339 | assertTrue(foreground10.isCompleted()); | |
340 | assertFalse(background9.isCompleted()); | |
341 | } | |
342 | ||
343 | /** | |
344 | * Test if the scheduler is working as expected | |
345 | */ | |
b1b999ea | 346 | @Ignore |
306586b1 SD |
347 | @Test |
348 | public void executionOrder() { | |
ccf2bbb4 | 349 | List<String> expectedOrder = new LinkedList<>(); |
306586b1 SD |
350 | expectedOrder.add("FOREGROUND1"); |
351 | expectedOrder.add("FOREGROUND2"); | |
352 | expectedOrder.add("FOREGROUND3"); | |
353 | expectedOrder.add("FOREGROUND4"); | |
354 | expectedOrder.add("BACKGROUND1"); | |
355 | expectedOrder.add("FOREGROUND1"); | |
356 | expectedOrder.add("FOREGROUND2"); | |
357 | expectedOrder.add("FOREGROUND3"); | |
358 | expectedOrder.add("FOREGROUND4"); | |
359 | expectedOrder.add("BACKGROUND2"); | |
360 | ||
361 | fOrderList.clear(); | |
362 | fForegroundId = 0; | |
363 | fBackgroundId = 0; | |
364 | ||
365 | BackgroundRequest background1 = new BackgroundRequest(TmfTimeRange.ETERNITY); | |
366 | BackgroundRequest background2 = new BackgroundRequest(TmfTimeRange.ETERNITY); | |
367 | ||
368 | ForegroundRequest foreground1 = new ForegroundRequest(TmfTimeRange.ETERNITY); | |
369 | ForegroundRequest foreground2 = new ForegroundRequest(TmfTimeRange.ETERNITY); | |
370 | ForegroundRequest foreground3 = new ForegroundRequest(TmfTimeRange.ETERNITY); | |
371 | ForegroundRequest foreground4 = new ForegroundRequest(TmfTimeRange.ETERNITY); | |
372 | ||
373 | fixture.sendRequest(foreground1); | |
374 | fixture.sendRequest(foreground2); | |
375 | fixture.sendRequest(foreground3); | |
376 | fixture.sendRequest(foreground4); | |
377 | fixture.sendRequest(background1); | |
378 | fixture.sendRequest(background2); | |
379 | try { | |
380 | foreground1.waitForCompletion(); | |
381 | foreground2.waitForCompletion(); | |
382 | foreground3.waitForCompletion(); | |
383 | foreground4.waitForCompletion(); | |
384 | background1.waitForCompletion(); | |
385 | background2.waitForCompletion(); | |
386 | } catch (InterruptedException e) { | |
387 | fail(); | |
388 | } | |
389 | assertEquals(expectedOrder, fOrderList.subList(0, expectedOrder.size())); | |
390 | } | |
391 | ||
392 | // ------------------------------------------------------------------------ | |
393 | // Helper methods | |
394 | // ------------------------------------------------------------------------ | |
395 | ||
396 | private class BackgroundRequest extends TmfEventRequest { | |
306586b1 SD |
397 | private int nbEvents = 0; |
398 | private String backgroundName; | |
399 | ||
400 | BackgroundRequest(TmfTimeRange timeRange) { | |
7184fc40 AM |
401 | super(fixture.getEventType(), |
402 | timeRange, | |
403 | 0, | |
2740e05c | 404 | ITmfEventRequest.ALL_DATA, |
306586b1 SD |
405 | ExecutionType.BACKGROUND); |
406 | backgroundName = getExecType().toString() + ++fBackgroundId; | |
407 | } | |
408 | ||
409 | @Override | |
410 | public void handleData(final ITmfEvent event) { | |
411 | super.handleData(event); | |
ab346a2b BH |
412 | synchronized (fOrderList) { |
413 | if (fOrderList.isEmpty() || !fOrderList.get(fOrderList.size() - 1).equals(backgroundName)) { | |
414 | fOrderList.add(backgroundName); | |
415 | } | |
306586b1 SD |
416 | } |
417 | ++nbEvents; | |
418 | } | |
419 | ||
420 | public int getNbEvents() { | |
421 | return nbEvents; | |
422 | } | |
423 | } | |
424 | ||
425 | private class ForegroundRequest extends TmfEventRequest { | |
306586b1 SD |
426 | private int nbEvents = 0; |
427 | private String foregroundName; | |
428 | ||
429 | ForegroundRequest(TmfTimeRange timeRange) { | |
7184fc40 AM |
430 | super(fixture.getEventType(), |
431 | timeRange, | |
432 | 0, | |
2740e05c | 433 | ITmfEventRequest.ALL_DATA, |
306586b1 SD |
434 | ExecutionType.FOREGROUND); |
435 | foregroundName = getExecType().toString() + ++fForegroundId; | |
436 | } | |
437 | ||
438 | @Override | |
439 | public void handleData(final ITmfEvent event) { | |
440 | super.handleData(event); | |
ab346a2b BH |
441 | synchronized (fOrderList) { |
442 | if (fOrderList.isEmpty() || !fOrderList.get(fOrderList.size() - 1).equals(foregroundName)) { | |
443 | fOrderList.add(foregroundName); | |
444 | } | |
306586b1 SD |
445 | } |
446 | ++nbEvents; | |
447 | } | |
448 | ||
449 | public int getNbEvents() { | |
450 | return nbEvents; | |
451 | } | |
452 | } | |
9d63098b | 453 | } |