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
;
20 import java
.util
.ArrayList
;
21 import java
.util
.LinkedList
;
22 import java
.util
.List
;
23 import java
.util
.concurrent
.TimeUnit
;
25 import org
.eclipse
.jdt
.annotation
.NonNull
;
26 import org
.eclipse
.tracecompass
.testtraces
.ctf
.CtfTestTrace
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
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
.TmfSelectionRangeUpdatedSignal
;
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
.tests
.shared
.CtfTmfTestTraceUtils
;
35 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.CtfTmfTrace
;
36 import org
.junit
.After
;
37 import org
.junit
.Before
;
38 import org
.junit
.Ignore
;
39 import org
.junit
.Rule
;
40 import org
.junit
.Test
;
41 import org
.junit
.rules
.TestRule
;
42 import org
.junit
.rules
.Timeout
;
45 * Test suite for the scheduler.
47 public class TmfSchedulerTest
{
49 /** Time-out tests after 60 seconds */
51 public TestRule globalTimeout
= new Timeout(1, TimeUnit
.MINUTES
);
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
57 private static final @NonNull CtfTestTrace testTrace
= CtfTestTrace
.KERNEL
;
58 private static final int NB_EVENTS_TRACE
= 695319;
59 private static final int NB_EVENTS_TIME_RANGE
= 155133;
61 // ------------------------------------------------------------------------
63 // ------------------------------------------------------------------------
65 private CtfTmfTrace fixture
;
67 private long fStartTime
;
68 private long fEndTime
;
69 private TmfTimeRange fForegroundTimeRange
;
71 private final List
<String
> fOrderList
= new ArrayList
<>();
72 private int fForegroundId
= 0;
73 private int fBackgroundId
= 0;
76 * Perform pre-test initialization.
80 fixture
= CtfTmfTestTraceUtils
.getTrace(testTrace
);
81 fixture
.indexTrace(true);
82 fStartTime
= fixture
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
83 fEndTime
= fixture
.getEndTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
85 long foregroundStartTime
= fStartTime
+ ((fEndTime
- fStartTime
) / 4);
86 long foregroundEndTime
= fStartTime
+ ((fEndTime
- fStartTime
) / 2);
87 fForegroundTimeRange
= new TmfTimeRange(new TmfTimestamp(foregroundStartTime
, ITmfTimestamp
.NANOSECOND_SCALE
),
88 new TmfTimestamp(foregroundEndTime
, ITmfTimestamp
.NANOSECOND_SCALE
));
92 * Perform post-test clean-up.
95 public void tearDown() {
96 if (fixture
!= null) {
101 // ------------------------------------------------------------------------
103 // ------------------------------------------------------------------------
106 * Test one background request
109 public void backgroundRequest() {
110 BackgroundRequest background
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
111 fixture
.sendRequest(background
);
113 background
.waitForCompletion();
114 } catch (InterruptedException e
) {
117 assertEquals(NB_EVENTS_TRACE
, background
.getNbEvents());
121 * Test one foreground request
124 public void foregroundRequest() {
125 ForegroundRequest foreground
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
126 fixture
.sendRequest(foreground
);
128 foreground
.waitForCompletion();
129 } catch (InterruptedException e
) {
132 assertEquals(NB_EVENTS_TRACE
, foreground
.getNbEvents());
136 * Test one foreground and one background request for the entire trace at
140 public void TestMultiRequest1() {
141 BackgroundRequest background
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
142 ForegroundRequest foreground
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
144 fixture
.sendRequest(background
);
145 fixture
.sendRequest(foreground
);
147 background
.waitForCompletion();
148 foreground
.waitForCompletion();
149 } catch (InterruptedException e
) {
153 assertEquals(NB_EVENTS_TRACE
, background
.getNbEvents());
154 assertEquals(NB_EVENTS_TRACE
, foreground
.getNbEvents());
158 * Test one background request for the entire trace and one foreground
159 * request for smaller time range
162 public void TestMultiRequest2() {
163 BackgroundRequest background2
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
164 ForegroundRequest foreground2
= new ForegroundRequest(fForegroundTimeRange
);
166 fixture
.sendRequest(background2
);
167 fixture
.sendRequest(foreground2
);
169 background2
.waitForCompletion();
170 foreground2
.waitForCompletion();
171 } catch (InterruptedException e
) {
175 assertEquals(NB_EVENTS_TRACE
, background2
.getNbEvents());
176 assertEquals(NB_EVENTS_TIME_RANGE
, foreground2
.getNbEvents());
180 * Test two foreground request, one to select a time range and one to select
181 * an event in this time range
184 public void TestMultiRequest3() {
185 ForegroundRequest foreground3
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
186 fixture
.sendRequest(foreground3
);
188 TmfSelectionRangeUpdatedSignal signal3
= new TmfSelectionRangeUpdatedSignal(this, new TmfTimestamp(fForegroundTimeRange
.getStartTime()));
189 fixture
.broadcast(signal3
);
192 foreground3
.waitForCompletion();
193 } catch (InterruptedException e
) {
197 assertEquals(NB_EVENTS_TRACE
, foreground3
.getNbEvents());
201 * Test two foreground request, one to select a time range and one to select
202 * an event before this time range
205 public void TestMultiRequest4() {
206 ForegroundRequest foreground4
= new ForegroundRequest(fForegroundTimeRange
);
207 fixture
.sendRequest(foreground4
);
208 TmfSelectionRangeUpdatedSignal signal4
= new TmfSelectionRangeUpdatedSignal(this, new TmfTimestamp(fStartTime
+ ((fEndTime
- fStartTime
) / 8)));
209 fixture
.broadcast(signal4
);
212 foreground4
.waitForCompletion();
213 } catch (InterruptedException e
) {
217 assertEquals(NB_EVENTS_TIME_RANGE
, foreground4
.getNbEvents());
221 * Test two foreground request, one to select a time range and one to select
222 * an event after this time range
225 public void TestMultiRequest5() {
226 ForegroundRequest foreground5
= new ForegroundRequest(fForegroundTimeRange
);
227 fixture
.sendRequest(foreground5
);
228 TmfSelectionRangeUpdatedSignal signal5
= new TmfSelectionRangeUpdatedSignal(this, new TmfTimestamp(fEndTime
- ((fEndTime
- fStartTime
) / 4)));
229 fixture
.broadcast(signal5
);
232 foreground5
.waitForCompletion();
233 } catch (InterruptedException e
) {
237 assertEquals(NB_EVENTS_TIME_RANGE
, foreground5
.getNbEvents());
241 * Test one background and one foreground request for the entire trace and
242 * one foreground request to select an event
245 public void TestMultiRequest6() {
246 BackgroundRequest background6
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
247 ForegroundRequest foreground6
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
249 fixture
.sendRequest(background6
);
250 fixture
.sendRequest(foreground6
);
252 TmfSelectionRangeUpdatedSignal signal6
= new TmfSelectionRangeUpdatedSignal(this, new TmfTimestamp(fStartTime
+ ((fEndTime
- fStartTime
) / 8)));
253 fixture
.broadcast(signal6
);
256 background6
.waitForCompletion();
257 foreground6
.waitForCompletion();
258 } catch (InterruptedException e
) {
262 assertEquals(NB_EVENTS_TRACE
, background6
.getNbEvents());
263 assertEquals(NB_EVENTS_TRACE
, foreground6
.getNbEvents());
267 * Four request, two foreground and two background
270 public void TestMultiRequest7() {
271 ForegroundRequest foreground7
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
272 ForegroundRequest foreground8
= new ForegroundRequest(fForegroundTimeRange
);
273 BackgroundRequest background7
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
274 BackgroundRequest background8
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
275 fixture
.sendRequest(foreground7
);
276 fixture
.sendRequest(foreground8
);
277 fixture
.sendRequest(background7
);
278 fixture
.sendRequest(background8
);
280 foreground7
.waitForCompletion();
281 foreground8
.waitForCompletion();
282 background7
.waitForCompletion();
283 background8
.waitForCompletion();
284 } catch (InterruptedException e
) {
287 assertEquals(NB_EVENTS_TRACE
, foreground7
.getNbEvents());
288 assertEquals(NB_EVENTS_TIME_RANGE
, foreground8
.getNbEvents());
289 assertEquals(NB_EVENTS_TRACE
, background7
.getNbEvents());
290 assertEquals(NB_EVENTS_TRACE
, background8
.getNbEvents());
294 * One long foreground request and one short foreground request, the short
295 * one should finish first
298 public void preemptedForegroundRequest() {
299 ForegroundRequest foreground9
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
300 TmfTimeRange shortTimeRange
= new TmfTimeRange(new TmfTimestamp(fStartTime
, ITmfTimestamp
.NANOSECOND_SCALE
),
301 new TmfTimestamp(fStartTime
+ ((fEndTime
- fStartTime
) / 16), ITmfTimestamp
.NANOSECOND_SCALE
));
302 ForegroundRequest shortForeground
= new ForegroundRequest(shortTimeRange
);
303 fixture
.sendRequest(foreground9
);
305 foreground9
.waitForStart();
306 } catch (InterruptedException e
) {
309 fixture
.sendRequest(shortForeground
);
311 shortForeground
.waitForCompletion();
312 } catch (InterruptedException e
) {
315 assertFalse(foreground9
.isCompleted());
319 * One long background request and one short foreground request, the
320 * foreground request should finish first
323 public void preemptedBackgroundRequest() {
324 BackgroundRequest background9
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
325 ForegroundRequest foreground10
= new ForegroundRequest(fForegroundTimeRange
);
326 fixture
.sendRequest(background9
);
327 fixture
.sendRequest(foreground10
);
329 foreground10
.waitForCompletion();
330 } catch (InterruptedException e
) {
333 assertTrue(foreground10
.isCompleted());
334 assertFalse(background9
.isCompleted());
338 * Test if the scheduler is working as expected
342 public void executionOrder() {
343 List
<String
> expectedOrder
= new LinkedList
<>();
344 expectedOrder
.add("FOREGROUND1");
345 expectedOrder
.add("FOREGROUND2");
346 expectedOrder
.add("FOREGROUND3");
347 expectedOrder
.add("FOREGROUND4");
348 expectedOrder
.add("BACKGROUND1");
349 expectedOrder
.add("FOREGROUND1");
350 expectedOrder
.add("FOREGROUND2");
351 expectedOrder
.add("FOREGROUND3");
352 expectedOrder
.add("FOREGROUND4");
353 expectedOrder
.add("BACKGROUND2");
359 BackgroundRequest background1
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
360 BackgroundRequest background2
= new BackgroundRequest(TmfTimeRange
.ETERNITY
);
362 ForegroundRequest foreground1
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
363 ForegroundRequest foreground2
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
364 ForegroundRequest foreground3
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
365 ForegroundRequest foreground4
= new ForegroundRequest(TmfTimeRange
.ETERNITY
);
367 fixture
.sendRequest(foreground1
);
368 fixture
.sendRequest(foreground2
);
369 fixture
.sendRequest(foreground3
);
370 fixture
.sendRequest(foreground4
);
371 fixture
.sendRequest(background1
);
372 fixture
.sendRequest(background2
);
374 foreground1
.waitForCompletion();
375 foreground2
.waitForCompletion();
376 foreground3
.waitForCompletion();
377 foreground4
.waitForCompletion();
378 background1
.waitForCompletion();
379 background2
.waitForCompletion();
380 } catch (InterruptedException e
) {
383 assertEquals(expectedOrder
, fOrderList
.subList(0, expectedOrder
.size()));
386 // ------------------------------------------------------------------------
388 // ------------------------------------------------------------------------
390 private class BackgroundRequest
extends TmfEventRequest
{
391 private int nbEvents
= 0;
392 private String backgroundName
;
394 BackgroundRequest(TmfTimeRange timeRange
) {
395 super(fixture
.getEventType(),
398 ITmfEventRequest
.ALL_DATA
,
399 ExecutionType
.BACKGROUND
);
400 backgroundName
= getExecType().toString() + ++fBackgroundId
;
404 public void handleData(final ITmfEvent event
) {
405 super.handleData(event
);
406 synchronized (fOrderList
) {
407 if (fOrderList
.isEmpty() || !fOrderList
.get(fOrderList
.size() - 1).equals(backgroundName
)) {
408 fOrderList
.add(backgroundName
);
414 public int getNbEvents() {
419 private class ForegroundRequest
extends TmfEventRequest
{
420 private int nbEvents
= 0;
421 private String foregroundName
;
423 ForegroundRequest(TmfTimeRange timeRange
) {
424 super(fixture
.getEventType(),
427 ITmfEventRequest
.ALL_DATA
,
428 ExecutionType
.FOREGROUND
);
429 foregroundName
= getExecType().toString() + ++fForegroundId
;
433 public void handleData(final ITmfEvent event
) {
434 super.handleData(event
);
435 synchronized (fOrderList
) {
436 if (fOrderList
.isEmpty() || !fOrderList
.get(fOrderList
.size() - 1).equals(foregroundName
)) {
437 fOrderList
.add(foregroundName
);
443 public int getNbEvents() {