Commit | Line | Data |
---|---|---|
9824473b AM |
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 | * Alexandre Montplaisir - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.core.tests.ctfadaptor; | |
14 | ||
15 | import static org.junit.Assert.assertEquals; | |
16 | import static org.junit.Assume.assumeTrue; | |
17 | ||
18 | import org.eclipse.core.resources.IResource; | |
19 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent; | |
20 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTimestamp; | |
21 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace; | |
22 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; | |
23 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; | |
24 | import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest; | |
25 | import org.eclipse.linuxtools.tmf.core.tests.shared.CtfTmfTestTraces; | |
26 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; | |
27 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; | |
28 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; | |
29 | import org.junit.AfterClass; | |
30 | import org.junit.BeforeClass; | |
31 | import org.junit.Test; | |
32 | ||
33 | /** | |
34 | * Tests for reading event contexts from a CtfTmfTrace. | |
35 | * | |
36 | * @author Alexandre Montplaisir | |
37 | */ | |
38 | public class EventContextTest { | |
39 | ||
40 | // ------------------------------------------------------------------------ | |
41 | // Attributes | |
42 | // ------------------------------------------------------------------------ | |
43 | ||
44 | /* We use test trace #2, kernel_vm, which has event contexts */ | |
45 | private static final int TRACE_INDEX = 2; | |
46 | private static final String PATH = CtfTmfTestTraces.getTestTracePath(TRACE_INDEX); | |
47 | ||
48 | private static CtfTmfTrace fixture; | |
49 | private static long startTime; | |
50 | private static long endTime; | |
51 | ||
52 | // ------------------------------------------------------------------------ | |
53 | // Class methods | |
54 | // ------------------------------------------------------------------------ | |
55 | ||
56 | /** | |
57 | * Perform pre-class initialization. | |
58 | * | |
59 | * @throws TmfTraceException | |
60 | * If the test trace is not found | |
61 | */ | |
62 | @BeforeClass | |
63 | public static void setUp() throws TmfTraceException { | |
64 | assumeTrue(CtfTmfTestTraces.tracesExist()); | |
65 | fixture = new CtfTmfTrace(); | |
66 | fixture.initTrace((IResource) null, PATH, CtfTmfEvent.class); | |
67 | fixture.indexTrace(true); | |
68 | ||
69 | startTime = fixture.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(); | |
70 | endTime = fixture.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(); | |
71 | } | |
72 | ||
73 | /** | |
74 | * Perform post-class clean-up. | |
75 | */ | |
76 | @AfterClass | |
77 | public static void tearDown() { | |
78 | if (fixture != null) { | |
79 | fixture.dispose(); | |
80 | } | |
81 | } | |
82 | ||
83 | // ------------------------------------------------------------------------ | |
84 | // Test methods | |
85 | // ------------------------------------------------------------------------ | |
86 | ||
87 | /** | |
88 | * Make sure the trace is the correct one, and its timestamps are read | |
89 | * correctly. | |
90 | */ | |
91 | @Test | |
92 | public void testTrace() { | |
93 | assertEquals(1363700740555978750L, startTime); | |
94 | assertEquals(1363700770550261288L, endTime); | |
95 | } | |
96 | ||
97 | /** | |
98 | * Test the context of the very first event of the trace. | |
99 | */ | |
100 | @Test | |
101 | public void testContextStart() { | |
102 | CtfTmfEvent firstEvent = getEventAt(startTime); | |
103 | long perfPageFault = (Long) firstEvent.getContent().getField("context._perf_page_fault").getValue(); | |
104 | String procname = (String) firstEvent.getContent().getField("context._procname").getValue(); | |
105 | long tid = (Long) firstEvent.getContent().getField("context._tid").getValue(); | |
106 | ||
107 | assertEquals(613, perfPageFault); | |
108 | assertEquals("lttng-sessiond", procname); | |
109 | assertEquals(1230, tid); | |
110 | } | |
111 | ||
112 | /** | |
113 | * Test the context of the event at 1363700745.559739078. | |
114 | */ | |
115 | @Test | |
116 | public void testContext1() { | |
117 | long time = startTime + 5000000000L; // 1363700745.559739078 | |
118 | CtfTmfEvent event = getEventAt(time); | |
119 | long perfPageFault = (Long) event.getContent().getField("context._perf_page_fault").getValue(); | |
120 | String procname = (String) event.getContent().getField("context._procname").getValue(); | |
121 | long tid = (Long) event.getContent().getField("context._tid").getValue(); | |
122 | ||
123 | assertEquals(6048, perfPageFault); | |
124 | assertEquals("swapper/0", procname); | |
125 | assertEquals(0, tid); | |
126 | } | |
127 | ||
128 | /** | |
129 | * Test the context of the event at 1363700750.559707062. | |
130 | */ | |
131 | @Test | |
132 | public void testContext2() { | |
133 | long time = startTime + 2 * 5000000000L; // 1363700750.559707062 | |
134 | CtfTmfEvent event = getEventAt(time); | |
135 | long perfPageFault = (Long) event.getContent().getField("context._perf_page_fault").getValue(); | |
136 | String procname = (String) event.getContent().getField("context._procname").getValue(); | |
137 | long tid = (Long) event.getContent().getField("context._tid").getValue(); | |
138 | ||
139 | assertEquals(13258, perfPageFault); | |
140 | assertEquals("swapper/0", procname); | |
141 | assertEquals(0, tid); | |
142 | } | |
143 | ||
144 | /** | |
145 | * Test the context of the event at 1363700755.555723128, which is roughly | |
146 | * mid-way through the trace. | |
147 | */ | |
148 | @Test | |
149 | public void testContextMiddle() { | |
150 | long midTime = startTime + (endTime - startTime) / 2L; // 1363700755.555723128 | |
151 | CtfTmfEvent midEvent = getEventAt(midTime); | |
152 | long perfPageFault = (Long) midEvent.getContent().getField("context._perf_page_fault").getValue(); | |
153 | String procname = (String) midEvent.getContent().getField("context._procname").getValue(); | |
154 | long tid = (Long) midEvent.getContent().getField("context._tid").getValue(); | |
155 | ||
156 | assertEquals(19438, perfPageFault); | |
157 | assertEquals("swapper/0", procname); | |
158 | assertEquals(0, tid); | |
159 | } | |
160 | ||
161 | /** | |
162 | * Test the context of the event at 1363700760.559719724. | |
163 | */ | |
164 | @Test | |
165 | public void testContext3() { | |
166 | long time = startTime + 4 * 5000000000L; // 1363700760.559719724 | |
167 | CtfTmfEvent event = getEventAt(time); | |
168 | long perfPageFault = (Long) event.getContent().getField("context._perf_page_fault").getValue(); | |
169 | String procname = (String) event.getContent().getField("context._procname").getValue(); | |
170 | long tid = (Long) event.getContent().getField("context._tid").getValue(); | |
171 | ||
172 | assertEquals(21507, perfPageFault); | |
173 | assertEquals("swapper/0", procname); | |
174 | assertEquals(0, tid); | |
175 | } | |
176 | ||
177 | /** | |
178 | * Test the context of the event at 1363700765.559714634. | |
179 | */ | |
180 | @Test | |
181 | public void testContext4() { | |
182 | long time = startTime + 5 * 5000000000L; // 1363700765.559714634 | |
183 | CtfTmfEvent event = getEventAt(time); | |
184 | long perfPageFault = (Long) event.getContent().getField("context._perf_page_fault").getValue(); | |
185 | String procname = (String) event.getContent().getField("context._procname").getValue(); | |
186 | long tid = (Long) event.getContent().getField("context._tid").getValue(); | |
187 | ||
188 | assertEquals(21507, perfPageFault); | |
189 | assertEquals("swapper/0", procname); | |
190 | assertEquals(0, tid); | |
191 | } | |
192 | ||
193 | /** | |
194 | * Test the context of the last event of the trace. | |
195 | */ | |
196 | @Test | |
197 | public void testContextEnd() { | |
198 | CtfTmfEvent lastEvent = getEventAt(endTime); | |
199 | long perfPageFault = (Long) lastEvent.getContent().getField("context._perf_page_fault").getValue(); | |
200 | String procname = (String) lastEvent.getContent().getField("context._procname").getValue(); | |
201 | long tid = (Long) lastEvent.getContent().getField("context._tid").getValue(); | |
202 | ||
203 | assertEquals(22117, perfPageFault); | |
204 | assertEquals("lttng-sessiond", procname); | |
205 | assertEquals(1230, tid); | |
206 | } | |
207 | ||
208 | // ------------------------------------------------------------------------ | |
209 | // Private stuff | |
210 | // ------------------------------------------------------------------------ | |
211 | ||
212 | private synchronized CtfTmfEvent getEventAt(long timestamp) { | |
213 | EventContextTestRequest req = new EventContextTestRequest(timestamp); | |
214 | fixture.sendRequest(req); | |
215 | try { | |
216 | req.waitForCompletion(); | |
217 | } catch (InterruptedException e) { | |
218 | return null; | |
219 | } | |
220 | return req.getEvent(); | |
221 | } | |
222 | ||
223 | private class EventContextTestRequest extends TmfEventRequest { | |
224 | ||
225 | private CtfTmfEvent retEvent = null; | |
226 | ||
227 | public EventContextTestRequest(long timestamp) { | |
228 | super(CtfTmfEvent.class, | |
229 | new TmfTimeRange(new CtfTmfTimestamp(timestamp), TmfTimestamp.BIG_CRUNCH), | |
230 | 0, 1, 1, ExecutionType.FOREGROUND); | |
231 | } | |
232 | ||
233 | @Override | |
234 | public void handleData(ITmfEvent event) { | |
235 | retEvent = (CtfTmfEvent) event; | |
236 | } | |
237 | ||
238 | public CtfTmfEvent getEvent() { | |
239 | return retEvent; | |
240 | } | |
241 | } | |
242 | } |