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 the
85 * event from which we start the search already matches the criterion (it
86 * should be correctly skipped so we can advance).
89 public void testNextMatchingEventStartMatches() {
90 ITmfTrace trace
= fTrace
;
93 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("softirq_raise");
94 ITmfEvent foundEvent
= TmfTraceUtils
.getNextEventMatching(trace
, START_RANK
, predicate
);
96 assertNotNull(foundEvent
);
97 assertNotEquals(fStartEvent
, foundEvent
);
101 * Test the {@link TmfTraceUtils#getPreviousEventMatching} method.
104 public void testPreviousMatchingEvent() {
105 ITmfTrace trace
= fTrace
;
106 assertNotNull(trace
);
108 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("sched_switch");
110 ITmfEvent actualEvent
= TmfTraceUtils
.getPreviousEventMatching(trace
, START_RANK
, predicate
);
112 ITmfContext ctx
= trace
.seekEvent(455L); // previous sched_switch event
113 ITmfEvent expectedEvent
= trace
.getNext(ctx
);
115 assertEquals(expectedEvent
, actualEvent
);
119 * Test the {@link TmfTraceUtils#getPreviousEventMatching} method, where the
120 * event from which we start the search already matches the criterion (it
121 * should be correctly skipped).
124 public void testPreviousMatchingEventStartMatches() {
125 ITmfTrace trace
= fTrace
;
126 assertNotNull(trace
);
128 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("softirq_raise");
129 ITmfEvent foundEvent
= TmfTraceUtils
.getPreviousEventMatching(trace
, START_RANK
, predicate
);
131 assertNotNull(foundEvent
);
132 assertNotEquals(fStartEvent
, foundEvent
);
136 * Test the {@link TmfTraceUtils#getPreviousEventMatching} method with an
137 * event that is expected to take more than one inner request.
140 public void testPreviousMatchingEventFar() {
141 ITmfTrace trace
= fTrace
;
142 assertNotNull(trace
);
144 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("sys_write");
145 ITmfEvent actualEvent
= TmfTraceUtils
.getPreviousEventMatching(trace
, START_RANK
, predicate
);
147 ITmfContext ctx
= trace
.seekEvent(387L); // previous sched_switch event
148 ITmfEvent expectedEvent
= trace
.getNext(ctx
);
150 assertEquals(expectedEvent
, actualEvent
);
154 * When doing a backwards search near the beginning of the trace (when
155 * startRank < step), make sure that we do not go beyond the start rank.
158 public void testPreviousMatchingBeginningOfTrace() {
159 ITmfTrace trace
= fTrace
;
160 assertNotNull(trace
);
162 final int startRank
= 3;
163 ITmfContext ctx
= fTrace
.seekEvent(startRank
);
164 ITmfEvent startEvent
= fTrace
.getNext(ctx
);
165 assertEquals("exit_syscall", startEvent
.getName());
167 /* There is a sys_mmap at rank 6, it should not be matched! */
168 Predicate
<@NonNull ITmfEvent
> predicate
= event
-> event
.getName().equals("sys_mmap");
169 ITmfEvent foundEvent
= TmfTraceUtils
.getPreviousEventMatching(trace
, startRank
, predicate
);
170 assertNull(foundEvent
);
172 /* Do not match the event itself either, or any later "exit_syscall" */
173 predicate
= event
-> event
.getName().equals("exit_syscall");
174 foundEvent
= TmfTraceUtils
.getPreviousEventMatching(trace
, startRank
, predicate
);
175 assertNull(foundEvent
);