tmf: Add utility method to search for events matching a Predicate
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core.tests / src / org / eclipse / tracecompass / tmf / ctf / core / tests / trace / TmfTraceUtilsSearchingTest.java
CommitLineData
e4bc4695
AM
1/*******************************************************************************
2 * Copyright (c) 2016 EfficiOS Inc., Alexandre Montplaisir
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
10package org.eclipse.tracecompass.tmf.ctf.core.tests.trace;
11
12import static org.junit.Assert.assertEquals;
13import static org.junit.Assert.assertNotNull;
14import static org.junit.Assert.assertNull;
15import static org.junit.Assert.assertNotEquals;
16
17import java.util.function.Predicate;
18
19import org.eclipse.jdt.annotation.NonNull;
20import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
21import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
22import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
23import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
24import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
25import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTraceUtils;
26import org.junit.After;
27import org.junit.Before;
28import org.junit.Test;
29
30/**
31 * Tests for the event-searching methods in {@link TmfTraceUtils}.
32 *
33 * @author Alexandre Montplaisir
34 */
35public class TmfTraceUtilsSearchingTest {
36
37 private static final @NonNull CtfTestTrace TEST_TRACE = CtfTestTrace.TRACE2;
38 private static final long START_RANK = 500L;
39
40 private ITmfTrace fTrace;
41 private ITmfEvent fStartEvent;
42
43 /**
44 * Test setup
45 */
46 @Before
47 public void setUp() {
48 fTrace = CtfTmfTestTraceUtils.getTrace(TEST_TRACE);
49
50 ITmfContext ctx = fTrace.seekEvent(START_RANK);
51 fStartEvent = fTrace.getNext(ctx);
52 assertEquals("softirq_raise", fStartEvent.getName());
53 }
54
55 /**
56 * Test cleanup
57 */
58 @After
59 public void tearDown() {
60 if (fTrace != null) {
61 fTrace.dispose();
62 }
63 }
64
65 /**
66 * Test the {@link TmfTraceUtils#getNextEventMatching} method.
67 */
68 @Test
69 public void testNextMatchingEvent() {
70 ITmfTrace trace = fTrace;
71 assertNotNull(trace);
72
73 Predicate<@NonNull ITmfEvent> predicate = event -> event.getName().equals("sched_switch");
74
75 ITmfEvent actualEvent = TmfTraceUtils.getNextEventMatching(trace, START_RANK, predicate);
76
77 ITmfContext ctx = trace.seekEvent(508L); // following sched_switch event
78 ITmfEvent expectedEvent = trace.getNext(ctx);
79
80 assertEquals(expectedEvent, actualEvent);
81 }
82
83 /**
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).
87 */
88 @Test
89 public void testNextMatchingEventStartMatches() {
90 ITmfTrace trace = fTrace;
91 assertNotNull(trace);
92
93 Predicate<@NonNull ITmfEvent> predicate = event -> event.getName().equals("softirq_raise");
94 ITmfEvent foundEvent = TmfTraceUtils.getNextEventMatching(trace, START_RANK, predicate);
95
96 assertNotNull(foundEvent);
97 assertNotEquals(fStartEvent, foundEvent);
98 }
99
100 /**
101 * Test the {@link TmfTraceUtils#getPreviousEventMatching} method.
102 */
103 @Test
104 public void testPreviousMatchingEvent() {
105 ITmfTrace trace = fTrace;
106 assertNotNull(trace);
107
108 Predicate<@NonNull ITmfEvent> predicate = event -> event.getName().equals("sched_switch");
109
110 ITmfEvent actualEvent = TmfTraceUtils.getPreviousEventMatching(trace, START_RANK, predicate);
111
112 ITmfContext ctx = trace.seekEvent(455L); // previous sched_switch event
113 ITmfEvent expectedEvent = trace.getNext(ctx);
114
115 assertEquals(expectedEvent, actualEvent);
116 }
117
118 /**
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).
122 */
123 @Test
124 public void testPreviousMatchingEventStartMatches() {
125 ITmfTrace trace = fTrace;
126 assertNotNull(trace);
127
128 Predicate<@NonNull ITmfEvent> predicate = event -> event.getName().equals("softirq_raise");
129 ITmfEvent foundEvent = TmfTraceUtils.getPreviousEventMatching(trace, START_RANK, predicate);
130
131 assertNotNull(foundEvent);
132 assertNotEquals(fStartEvent, foundEvent);
133 }
134
135 /**
136 * Test the {@link TmfTraceUtils#getPreviousEventMatching} method with an
137 * event that is expected to take more than one inner request.
138 */
139 @Test
140 public void testPreviousMatchingEventFar() {
141 ITmfTrace trace = fTrace;
142 assertNotNull(trace);
143
144 Predicate<@NonNull ITmfEvent> predicate = event -> event.getName().equals("sys_write");
145 ITmfEvent actualEvent = TmfTraceUtils.getPreviousEventMatching(trace, START_RANK, predicate);
146
147 ITmfContext ctx = trace.seekEvent(387L); // previous sched_switch event
148 ITmfEvent expectedEvent = trace.getNext(ctx);
149
150 assertEquals(expectedEvent, actualEvent);
151 }
152
153 /**
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.
156 */
157 @Test
158 public void testPreviousMatchingBeginningOfTrace() {
159 ITmfTrace trace = fTrace;
160 assertNotNull(trace);
161
162 final int startRank = 3;
163 ITmfContext ctx = fTrace.seekEvent(startRank);
164 ITmfEvent startEvent = fTrace.getNext(ctx);
165 assertEquals("exit_syscall", startEvent.getName());
166
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);
171
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);
176 }
177}
This page took 0.031222 seconds and 5 git commands to generate.