1 /*******************************************************************************
2 * Copyright (c) 2016 EfficiOS Inc., Alexandre Montplaisir
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.trace
;
12 import static org
.junit
.Assert
.assertEquals
;
13 import static org
.junit
.Assert
.assertNotNull
;
14 import static org
.junit
.Assert
.assertNull
;
15 import static org
.junit
.Assert
.assertNotEquals
;
17 import java
.util
.function
.Predicate
;
19 import org
.eclipse
.jdt
.annotation
.NonNull
;
20 import org
.eclipse
.tracecompass
.testtraces
.ctf
.CtfTestTrace
;
21 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
22 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
23 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
25 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTraceUtils
;
26 import org
.junit
.After
;
27 import org
.junit
.Before
;
28 import org
.junit
.Test
;
31 * Tests for the event-searching methods in {@link TmfTraceUtils}.
33 * @author Alexandre Montplaisir
35 public class TmfTraceUtilsSearchingTest
{
37 private static final @NonNull CtfTestTrace TEST_TRACE
= CtfTestTrace
.TRACE2
;
38 private static final long START_RANK
= 500L;
40 private ITmfTrace fTrace
;
41 private ITmfEvent fStartEvent
;
48 fTrace
= CtfTmfTestTraceUtils
.getTrace(TEST_TRACE
);
50 ITmfContext ctx
= fTrace
.seekEvent(START_RANK
);
51 fStartEvent
= fTrace
.getNext(ctx
);
52 assertEquals("softirq_raise", fStartEvent
.getName());
59 public void tearDown() {
66 * Test the {@link TmfTraceUtils#getNextEventMatching} method.
69 public void testNextMatchingEvent() {
70 ITmfTrace trace
= fTrace
;
73 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("sched_switch");
75 ITmfEvent actualEvent
= TmfTraceUtils
.getNextEventMatching(trace
, START_RANK
, predicate
);
77 ITmfContext ctx
= trace
.seekEvent(508L); // following sched_switch event
78 ITmfEvent expectedEvent
= trace
.getNext(ctx
);
80 assertEquals(expectedEvent
, actualEvent
);
84 * Test the {@link TmfTraceUtils#getNextEventMatching} method where no event
85 * matches the passed predicate. It should return null.
88 public void testNextMatchingEventNoMatch() {
89 ITmfTrace trace
= fTrace
;
92 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("non-existent-event");
94 ITmfEvent actualEvent
= TmfTraceUtils
.getNextEventMatching(trace
, START_RANK
, predicate
);
95 assertNull(actualEvent
);
99 * Test the {@link TmfTraceUtils#getNextEventMatching} method, where the
100 * event from which we start the search already matches the criterion (it
101 * should be correctly skipped so we can advance).
104 public void testNextMatchingEventStartMatches() {
105 ITmfTrace trace
= fTrace
;
106 assertNotNull(trace
);
108 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("softirq_raise");
109 ITmfEvent foundEvent
= TmfTraceUtils
.getNextEventMatching(trace
, START_RANK
, predicate
);
111 assertNotNull(foundEvent
);
112 assertNotEquals(fStartEvent
, foundEvent
);
116 * Test the {@link TmfTraceUtils#getPreviousEventMatching} method.
119 public void testPreviousMatchingEvent() {
120 ITmfTrace trace
= fTrace
;
121 assertNotNull(trace
);
123 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("sched_switch");
125 ITmfEvent actualEvent
= TmfTraceUtils
.getPreviousEventMatching(trace
, START_RANK
, predicate
);
127 ITmfContext ctx
= trace
.seekEvent(455L); // previous sched_switch event
128 ITmfEvent expectedEvent
= trace
.getNext(ctx
);
130 assertEquals(expectedEvent
, actualEvent
);
134 * Test the {@link TmfTraceUtils#getPreviousEventMatching} method where no event
135 * matches the passed predicate. It should return null.
138 public void testPreviousMatchingEventNoMatch() {
139 ITmfTrace trace
= fTrace
;
140 assertNotNull(trace
);
142 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("non-existent-event");
144 ITmfEvent actualEvent
= TmfTraceUtils
.getPreviousEventMatching(trace
, START_RANK
, predicate
);
145 assertNull(actualEvent
);
149 * Test the {@link TmfTraceUtils#getPreviousEventMatching} method, where the
150 * event from which we start the search already matches the criterion (it
151 * should be correctly skipped).
154 public void testPreviousMatchingEventStartMatches() {
155 ITmfTrace trace
= fTrace
;
156 assertNotNull(trace
);
158 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("softirq_raise");
159 ITmfEvent foundEvent
= TmfTraceUtils
.getPreviousEventMatching(trace
, START_RANK
, predicate
);
161 assertNotNull(foundEvent
);
162 assertNotEquals(fStartEvent
, foundEvent
);
166 * Test the {@link TmfTraceUtils#getPreviousEventMatching} method with an
167 * event that is expected to take more than one inner request.
170 public void testPreviousMatchingEventFar() {
171 ITmfTrace trace
= fTrace
;
172 assertNotNull(trace
);
174 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("sys_write");
175 ITmfEvent actualEvent
= TmfTraceUtils
.getPreviousEventMatching(trace
, START_RANK
, predicate
);
177 ITmfContext ctx
= trace
.seekEvent(387L); // previous sched_switch event
178 ITmfEvent expectedEvent
= trace
.getNext(ctx
);
180 assertEquals(expectedEvent
, actualEvent
);
184 * When doing a backwards search near the beginning of the trace (when
185 * startRank < step), make sure that we do not go beyond the start rank.
188 public void testPreviousMatchingBeginningOfTrace() {
189 ITmfTrace trace
= fTrace
;
190 assertNotNull(trace
);
192 final int startRank
= 3;
193 ITmfContext ctx
= fTrace
.seekEvent(startRank
);
194 ITmfEvent startEvent
= fTrace
.getNext(ctx
);
195 assertEquals("exit_syscall", startEvent
.getName());
197 /* There is a sys_mmap at rank 6, it should not be matched! */
198 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("sys_mmap");
199 ITmfEvent foundEvent
= TmfTraceUtils
.getPreviousEventMatching(trace
, startRank
, predicate
);
200 assertNull(foundEvent
);
202 /* Do not match the event itself either, or any later "exit_syscall" */
203 predicate
= event
-> event
.getName().equals("exit_syscall");
204 foundEvent
= TmfTraceUtils
.getPreviousEventMatching(trace
, startRank
, predicate
);
205 assertNull(foundEvent
);