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
.core
.runtime
.IProgressMonitor
;
20 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
21 import org
.eclipse
.jdt
.annotation
.NonNull
;
22 import org
.eclipse
.tracecompass
.testtraces
.ctf
.CtfTestTrace
;
23 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
27 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTraceUtils
;
28 import org
.junit
.After
;
29 import org
.junit
.Before
;
30 import org
.junit
.Test
;
33 * Tests for the event-searching methods in {@link TmfTraceUtils}.
35 * @author Alexandre Montplaisir
37 public class TmfTraceUtilsSearchingTest
{
39 private static final @NonNull CtfTestTrace TEST_TRACE
= CtfTestTrace
.TRACE2
;
40 private static final long START_RANK
= 500L;
42 private ITmfTrace fTrace
;
43 private ITmfEvent fStartEvent
;
50 fTrace
= CtfTmfTestTraceUtils
.getTrace(TEST_TRACE
);
52 ITmfContext ctx
= fTrace
.seekEvent(START_RANK
);
53 fStartEvent
= fTrace
.getNext(ctx
);
54 assertEquals("softirq_raise", fStartEvent
.getName());
61 public void tearDown() {
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
72 * Test the {@link TmfTraceUtils#getNextEventMatching} method.
75 public void testNextMatchingEvent() {
76 ITmfTrace trace
= fTrace
;
79 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("sched_switch");
81 ITmfEvent actualEvent
= TmfTraceUtils
.getNextEventMatching(trace
, START_RANK
, predicate
, null);
83 ITmfContext ctx
= trace
.seekEvent(508L); // following sched_switch event
84 ITmfEvent expectedEvent
= trace
.getNext(ctx
);
86 assertEquals(expectedEvent
, actualEvent
);
90 * Test that the presence of progress monitor does not affect the results.
93 public void testNextMatchingEventProgressMonitor() {
94 ITmfTrace trace
= fTrace
;
97 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("sched_switch");
99 IProgressMonitor monitor
= new NullProgressMonitor();
100 ITmfEvent actualEvent
= TmfTraceUtils
.getNextEventMatching(trace
, START_RANK
, predicate
, monitor
);
102 ITmfContext ctx
= trace
.seekEvent(508L); // following sched_switch event
103 ITmfEvent expectedEvent
= trace
.getNext(ctx
);
105 assertEquals(expectedEvent
, actualEvent
);
109 * Test the {@link TmfTraceUtils#getNextEventMatching} method where no event
110 * matches the passed predicate. It should return null.
113 public void testNextMatchingEventNoMatch() {
114 ITmfTrace trace
= fTrace
;
115 assertNotNull(trace
);
117 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("non-existent-event");
119 ITmfEvent actualEvent
= TmfTraceUtils
.getNextEventMatching(trace
, START_RANK
, predicate
, null);
120 assertNull(actualEvent
);
124 * Test the {@link TmfTraceUtils#getNextEventMatching} method, where the
125 * event from which we start the search already matches the criterion (it
126 * should be correctly skipped so we can advance).
129 public void testNextMatchingEventStartMatches() {
130 ITmfTrace trace
= fTrace
;
131 assertNotNull(trace
);
133 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("softirq_raise");
134 ITmfEvent foundEvent
= TmfTraceUtils
.getNextEventMatching(trace
, START_RANK
, predicate
, null);
136 assertNotNull(foundEvent
);
137 assertNotEquals(fStartEvent
, foundEvent
);
141 * Test with a progress monitor that cancels the job.
144 public void testNextMatchingEventCancelled() {
145 ITmfTrace trace
= fTrace
;
146 assertNotNull(trace
);
148 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("sched_switch");
150 IProgressMonitor monitor
= new NullProgressMonitor();
151 monitor
.setCanceled(true);
152 ITmfEvent actualEvent
= TmfTraceUtils
.getNextEventMatching(trace
, START_RANK
, predicate
, monitor
);
154 /* No event should be returend */
155 assertNull(actualEvent
);
158 // ------------------------------------------------------------------------
159 // Backwards searches
160 // ------------------------------------------------------------------------
163 * Test the {@link TmfTraceUtils#getPreviousEventMatching} method.
166 public void testPreviousMatchingEvent() {
167 ITmfTrace trace
= fTrace
;
168 assertNotNull(trace
);
170 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("sched_switch");
172 ITmfEvent actualEvent
= TmfTraceUtils
.getPreviousEventMatching(trace
, START_RANK
, predicate
, null);
174 ITmfContext ctx
= trace
.seekEvent(455L); // previous sched_switch event
175 ITmfEvent expectedEvent
= trace
.getNext(ctx
);
177 assertEquals(expectedEvent
, actualEvent
);
181 * Test that the presence of progress monitor does not affect the results.
184 public void testPreviousMatchingEventProgressMonitor() {
185 ITmfTrace trace
= fTrace
;
186 assertNotNull(trace
);
188 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("sched_switch");
190 IProgressMonitor monitor
= new NullProgressMonitor();
191 ITmfEvent actualEvent
= TmfTraceUtils
.getPreviousEventMatching(trace
, START_RANK
, predicate
, monitor
);
193 ITmfContext ctx
= trace
.seekEvent(455L); // previous sched_switch event
194 ITmfEvent expectedEvent
= trace
.getNext(ctx
);
196 assertEquals(expectedEvent
, actualEvent
);
200 * Test the {@link TmfTraceUtils#getPreviousEventMatching} method where no event
201 * matches the passed predicate. It should return null.
204 public void testPreviousMatchingEventNoMatch() {
205 ITmfTrace trace
= fTrace
;
206 assertNotNull(trace
);
208 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("non-existent-event");
210 ITmfEvent actualEvent
= TmfTraceUtils
.getPreviousEventMatching(trace
, START_RANK
, predicate
, null);
211 assertNull(actualEvent
);
215 * Test the {@link TmfTraceUtils#getPreviousEventMatching} method, where the
216 * event from which we start the search already matches the criterion (it
217 * should be correctly skipped).
220 public void testPreviousMatchingEventStartMatches() {
221 ITmfTrace trace
= fTrace
;
222 assertNotNull(trace
);
224 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("softirq_raise");
225 ITmfEvent foundEvent
= TmfTraceUtils
.getPreviousEventMatching(trace
, START_RANK
, predicate
, null);
227 assertNotNull(foundEvent
);
228 assertNotEquals(fStartEvent
, foundEvent
);
232 * Test the {@link TmfTraceUtils#getPreviousEventMatching} method with an
233 * event that is expected to take more than one inner request.
236 public void testPreviousMatchingEventFar() {
237 ITmfTrace trace
= fTrace
;
238 assertNotNull(trace
);
240 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("sys_write");
241 ITmfEvent actualEvent
= TmfTraceUtils
.getPreviousEventMatching(trace
, START_RANK
, predicate
, null);
243 ITmfContext ctx
= trace
.seekEvent(387L); // previous sched_switch event
244 ITmfEvent expectedEvent
= trace
.getNext(ctx
);
246 assertEquals(expectedEvent
, actualEvent
);
250 * When doing a backwards search near the beginning of the trace (when
251 * startRank < step), make sure that we do not go beyond the start rank.
254 public void testPreviousMatchingBeginningOfTrace() {
255 ITmfTrace trace
= fTrace
;
256 assertNotNull(trace
);
258 final int startRank
= 3;
259 ITmfContext ctx
= fTrace
.seekEvent(startRank
);
260 ITmfEvent startEvent
= fTrace
.getNext(ctx
);
261 assertEquals("exit_syscall", startEvent
.getName());
263 /* There is a sys_mmap at rank 6, it should not be matched! */
264 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("sys_mmap");
265 ITmfEvent foundEvent
= TmfTraceUtils
.getPreviousEventMatching(trace
, startRank
, predicate
, null);
266 assertNull(foundEvent
);
268 /* Do not match the event itself either, or any later "exit_syscall" */
269 predicate
= event
-> event
.getName().equals("exit_syscall");
270 foundEvent
= TmfTraceUtils
.getPreviousEventMatching(trace
, startRank
, predicate
, null);
271 assertNull(foundEvent
);
275 * Test with a progress monitor that cancels the job.
278 public void testPreviousMatchingEventCancelled() {
279 ITmfTrace trace
= fTrace
;
280 assertNotNull(trace
);
282 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("sched_switch");
284 IProgressMonitor monitor
= new NullProgressMonitor();
285 monitor
.setCanceled(true);
286 ITmfEvent actualEvent
= TmfTraceUtils
.getPreviousEventMatching(trace
, START_RANK
, predicate
, monitor
);
288 /* No event should be returend */
289 assertNull(actualEvent
);