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