1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
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
10 * Simon Delisle - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.temp
.request
;
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
;
19 import static org
.junit
.Assume
.assumeTrue
;
21 import java
.util
.ArrayList
;
22 import java
.util
.LinkedList
;
23 import java
.util
.List
;
25 import org
.eclipse
.core
.resources
.IResource
;
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
.event
.CtfTmfEvent
;
35 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTrace
;
36 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.CtfTmfTrace
;
37 import org
.junit
.After
;
38 import org
.junit
.Before
;
39 import org
.junit
.Ignore
;
40 import org
.junit
.Rule
;
41 import org
.junit
.Test
;
42 import org
.junit
.rules
.TestRule
;
43 import org
.junit
.rules
.Timeout
;
46 * Test suite for the scheduler.
48 public class TmfSchedulerTest
{
50 /** Time-out tests after 60 seconds */
52 public TestRule globalTimeout
= new Timeout(60000);
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
58 private static final CtfTmfTestTrace testTrace
= CtfTmfTestTrace
.KERNEL
;
59 private static final int NB_EVENTS_TRACE
= 695319;
60 private static final int NB_EVENTS_TIME_RANGE
= 155133;
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
66 private CtfTmfTrace fixture
;
68 private long fStartTime
;
69 private long fEndTime
;
70 private TmfTimeRange fForegroundTimeRange
;
72 private final List
<String
> fOrderList
= new ArrayList
<>();
73 private int fForegroundId
= 0;
74 private int fBackgroundId
= 0;
77 * Perform pre-test initialization.
79 * @throws TmfTraceException
80 * If the test trace is not found
83 public void setUp() throws TmfTraceException
{
84 assumeTrue(testTrace
.exists());
85 fixture
= new CtfTmfTrace();
86 fixture
.initTrace((IResource
) null, testTrace
.getPath(), CtfTmfEvent
.class);
87 fixture
.indexTrace(true);
88 fStartTime
= fixture
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
89 fEndTime
= fixture
.getEndTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
91 long foregroundStartTime
= fStartTime
+ ((fEndTime
- fStartTime
) / 4);
92 long foregroundEndTime
= fStartTime
+ ((fEndTime
- fStartTime
) / 2);
93 fForegroundTimeRange
= new TmfTimeRange(new TmfTimestamp(foregroundStartTime
, ITmfTimestamp
.NANOSECOND_SCALE
),
94 new TmfTimestamp(foregroundEndTime
, ITmfTimestamp
.NANOSECOND_SCALE
));
98 * Perform post-test clean-up.
101 public void tearDown() {
102 if (fixture
!= null) {
107 // ------------------------------------------------------------------------
109 // ------------------------------------------------------------------------
112 * Test one background request
115 public void backgroundRequest() {
116 BackgroundRequest background
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
117 fixture
.sendRequest(background
);
119 background
.waitForCompletion();
120 } catch (InterruptedException e
) {
123 assertEquals(NB_EVENTS_TRACE
, background
.getNbEvents());
127 * Test one foreground request
130 public void foregroundRequest() {
131 ForegroundRequest foreground
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
132 fixture
.sendRequest(foreground
);
134 foreground
.waitForCompletion();
135 } catch (InterruptedException e
) {
138 assertEquals(NB_EVENTS_TRACE
, foreground
.getNbEvents());
142 * Test one foreground and one background request for the entire trace at
146 public void TestMultiRequest1() {
147 BackgroundRequest background
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
148 ForegroundRequest foreground
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
150 fixture
.sendRequest(background
);
151 fixture
.sendRequest(foreground
);
153 background
.waitForCompletion();
154 foreground
.waitForCompletion();
155 } catch (InterruptedException e
) {
159 assertEquals(NB_EVENTS_TRACE
, background
.getNbEvents());
160 assertEquals(NB_EVENTS_TRACE
, foreground
.getNbEvents());
164 * Test one background request for the entire trace and one foreground
165 * request for smaller time range
168 public void TestMultiRequest2() {
169 BackgroundRequest background2
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
170 ForegroundRequest foreground2
= new ForegroundRequest(fForegroundTimeRange
);
172 fixture
.sendRequest(background2
);
173 fixture
.sendRequest(foreground2
);
175 background2
.waitForCompletion();
176 foreground2
.waitForCompletion();
177 } catch (InterruptedException e
) {
181 assertEquals(NB_EVENTS_TRACE
, background2
.getNbEvents());
182 assertEquals(NB_EVENTS_TIME_RANGE
, foreground2
.getNbEvents());
186 * Test two foreground request, one to select a time range and one to select
187 * an event in this time range
190 public void TestMultiRequest3() {
191 ForegroundRequest foreground3
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
192 fixture
.sendRequest(foreground3
);
194 TmfTimeSynchSignal signal3
= new TmfTimeSynchSignal(this, new TmfTimestamp(fForegroundTimeRange
.getStartTime()));
195 fixture
.broadcast(signal3
);
198 foreground3
.waitForCompletion();
199 } catch (InterruptedException e
) {
203 assertEquals(NB_EVENTS_TRACE
, foreground3
.getNbEvents());
207 * Test two foreground request, one to select a time range and one to select
208 * an event before this time range
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
);
218 foreground4
.waitForCompletion();
219 } catch (InterruptedException e
) {
223 assertEquals(NB_EVENTS_TIME_RANGE
, foreground4
.getNbEvents());
227 * Test two foreground request, one to select a time range and one to select
228 * an event after this time range
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
);
238 foreground5
.waitForCompletion();
239 } catch (InterruptedException e
) {
243 assertEquals(NB_EVENTS_TIME_RANGE
, foreground5
.getNbEvents());
247 * Test one background and one foreground request for the entire trace and
248 * one foreground request to select an event
251 public void TestMultiRequest6() {
252 BackgroundRequest background6
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
253 ForegroundRequest foreground6
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
255 fixture
.sendRequest(background6
);
256 fixture
.sendRequest(foreground6
);
258 TmfTimeSynchSignal signal6
= new TmfTimeSynchSignal(this, new TmfTimestamp(fStartTime
+ ((fEndTime
- fStartTime
) / 8)));
259 fixture
.broadcast(signal6
);
262 background6
.waitForCompletion();
263 foreground6
.waitForCompletion();
264 } catch (InterruptedException e
) {
268 assertEquals(NB_EVENTS_TRACE
, background6
.getNbEvents());
269 assertEquals(NB_EVENTS_TRACE
, foreground6
.getNbEvents());
273 * Four request, two foreground and two background
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
);
286 foreground7
.waitForCompletion();
287 foreground8
.waitForCompletion();
288 background7
.waitForCompletion();
289 background8
.waitForCompletion();
290 } catch (InterruptedException e
) {
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());
300 * One long foreground request and one short foreground request, the short
301 * one should finish first
304 public void preemptedForegroundRequest() {
305 ForegroundRequest foreground9
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
306 TmfTimeRange shortTimeRange
= new TmfTimeRange(new TmfTimestamp(fStartTime
, ITmfTimestamp
.NANOSECOND_SCALE
),
307 new TmfTimestamp(fStartTime
+ ((fEndTime
- fStartTime
) / 16), ITmfTimestamp
.NANOSECOND_SCALE
));
308 ForegroundRequest shortForeground
= new ForegroundRequest(shortTimeRange
);
309 fixture
.sendRequest(foreground9
);
311 foreground9
.waitForStart();
312 } catch (InterruptedException e
) {
315 fixture
.sendRequest(shortForeground
);
317 shortForeground
.waitForCompletion();
318 } catch (InterruptedException e
) {
321 assertFalse(foreground9
.isCompleted());
325 * One long background request and one short foreground request, the
326 * foreground request should finish first
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
);
335 foreground10
.waitForCompletion();
336 } catch (InterruptedException e
) {
339 assertTrue(foreground10
.isCompleted());
340 assertFalse(background9
.isCompleted());
344 * Test if the scheduler is working as expected
348 public void executionOrder() {
349 List
<String
> expectedOrder
= new LinkedList
<>();
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");
365 BackgroundRequest background1
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
366 BackgroundRequest background2
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
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
);
373 fixture
.sendRequest(foreground1
);
374 fixture
.sendRequest(foreground2
);
375 fixture
.sendRequest(foreground3
);
376 fixture
.sendRequest(foreground4
);
377 fixture
.sendRequest(background1
);
378 fixture
.sendRequest(background2
);
380 foreground1
.waitForCompletion();
381 foreground2
.waitForCompletion();
382 foreground3
.waitForCompletion();
383 foreground4
.waitForCompletion();
384 background1
.waitForCompletion();
385 background2
.waitForCompletion();
386 } catch (InterruptedException e
) {
389 assertEquals(expectedOrder
, fOrderList
.subList(0, expectedOrder
.size()));
392 // ------------------------------------------------------------------------
394 // ------------------------------------------------------------------------
396 private class BackgroundRequest
extends TmfEventRequest
{
397 private int nbEvents
= 0;
398 private String backgroundName
;
400 BackgroundRequest(TmfTimeRange timeRange
) {
401 super(fixture
.getEventType(),
404 ITmfEventRequest
.ALL_DATA
,
405 ExecutionType
.BACKGROUND
);
406 backgroundName
= getExecType().toString() + ++fBackgroundId
;
410 public void handleData(final ITmfEvent event
) {
411 super.handleData(event
);
412 synchronized (fOrderList
) {
413 if (fOrderList
.isEmpty() || !fOrderList
.get(fOrderList
.size() - 1).equals(backgroundName
)) {
414 fOrderList
.add(backgroundName
);
420 public int getNbEvents() {
425 private class ForegroundRequest
extends TmfEventRequest
{
426 private int nbEvents
= 0;
427 private String foregroundName
;
429 ForegroundRequest(TmfTimeRange timeRange
) {
430 super(fixture
.getEventType(),
433 ITmfEventRequest
.ALL_DATA
,
434 ExecutionType
.FOREGROUND
);
435 foregroundName
= getExecType().toString() + ++fForegroundId
;
439 public void handleData(final ITmfEvent event
) {
440 super.handleData(event
);
441 synchronized (fOrderList
) {
442 if (fOrderList
.isEmpty() || !fOrderList
.get(fOrderList
.size() - 1).equals(foregroundName
)) {
443 fOrderList
.add(foregroundName
);
449 public int getNbEvents() {