rcp: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ctf.core.tests / src / org / eclipse / tracecompass / tmf / ctf / core / tests / event / EventContextTest.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2015 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.tracecompass.tmf.ctf.core.tests.event;
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.tracecompass.tmf.core.event.ITmfEvent;
20 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
21 import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest;
22 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
23 import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp;
24 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
25 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
26 import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEvent;
27 import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTrace;
28 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
29 import org.junit.After;
30 import org.junit.Before;
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 CtfTmfTestTrace testTrace = CtfTmfTestTrace.KERNEL_VM;
46
47 private CtfTmfTrace fixture;
48 private long startTime;
49 private long endTime;
50
51 // ------------------------------------------------------------------------
52 // Class methods
53 // ------------------------------------------------------------------------
54
55 /**
56 * Perform pre-class initialization.
57 *
58 * @throws TmfTraceException
59 * If the test trace is not found
60 */
61 @Before
62 public void setUp() throws TmfTraceException {
63 assumeTrue(testTrace.exists());
64 fixture = new CtfTmfTrace();
65 fixture.initTrace((IResource) null, testTrace.getPath(), CtfTmfEvent.class);
66 fixture.indexTrace(true);
67
68 startTime = fixture.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
69 endTime = fixture.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue();
70 }
71
72 /**
73 * Perform post-class clean-up.
74 */
75 @After
76 public void tearDown() {
77 if (fixture != null) {
78 fixture.dispose();
79 }
80 }
81
82 // ------------------------------------------------------------------------
83 // Test methods
84 // ------------------------------------------------------------------------
85
86 /**
87 * Make sure the trace is the correct one, and its timestamps are read
88 * correctly.
89 */
90 @Test
91 public void testTrace() {
92 assertEquals(1363700740555978750L, startTime);
93 assertEquals(1363700770550261288L, endTime);
94 }
95
96 /**
97 * Test the context of the very first event of the trace.
98 */
99 @Test
100 public void testContextStart() {
101 CtfTmfEvent firstEvent = getEventAt(startTime);
102 long perfPageFault = (Long) firstEvent.getContent().getField("context._perf_page_fault").getValue();
103 String procname = (String) firstEvent.getContent().getField("context._procname").getValue();
104 long tid = (Long) firstEvent.getContent().getField("context._tid").getValue();
105
106 assertEquals(613, perfPageFault);
107 assertEquals("lttng-sessiond", procname);
108 assertEquals(1230, tid);
109 }
110
111 /**
112 * Test the context of the event at 1363700745.559739078.
113 */
114 @Test
115 public void testContext1() {
116 long time = startTime + 5000000000L; // 1363700745.559739078
117 CtfTmfEvent event = getEventAt(time);
118 long perfPageFault = (Long) event.getContent().getField("context._perf_page_fault").getValue();
119 String procname = (String) event.getContent().getField("context._procname").getValue();
120 long tid = (Long) event.getContent().getField("context._tid").getValue();
121
122 assertEquals(6048, perfPageFault);
123 assertEquals("swapper/0", procname);
124 assertEquals(0, tid);
125 }
126
127 /**
128 * Test the context of the event at 1363700750.559707062.
129 */
130 @Test
131 public void testContext2() {
132 long time = startTime + 2 * 5000000000L; // 1363700750.559707062
133 CtfTmfEvent event = getEventAt(time);
134 long perfPageFault = (Long) event.getContent().getField("context._perf_page_fault").getValue();
135 String procname = (String) event.getContent().getField("context._procname").getValue();
136 long tid = (Long) event.getContent().getField("context._tid").getValue();
137
138 assertEquals(13258, perfPageFault);
139 assertEquals("swapper/0", procname);
140 assertEquals(0, tid);
141 }
142
143 /**
144 * Test the context of the event at 1363700755.555723128, which is roughly
145 * mid-way through the trace.
146 */
147 @Test
148 public void testContextMiddle() {
149 long midTime = startTime + (endTime - startTime) / 2L; // 1363700755.555723128
150 CtfTmfEvent midEvent = getEventAt(midTime);
151 long perfPageFault = (Long) midEvent.getContent().getField("context._perf_page_fault").getValue();
152 String procname = (String) midEvent.getContent().getField("context._procname").getValue();
153 long tid = (Long) midEvent.getContent().getField("context._tid").getValue();
154
155 assertEquals(19438, perfPageFault);
156 assertEquals("swapper/0", procname);
157 assertEquals(0, tid);
158 }
159
160 /**
161 * Test the context of the event at 1363700760.559719724.
162 */
163 @Test
164 public void testContext3() {
165 long time = startTime + 4 * 5000000000L; // 1363700760.559719724
166 CtfTmfEvent event = getEventAt(time);
167 long perfPageFault = (Long) event.getContent().getField("context._perf_page_fault").getValue();
168 String procname = (String) event.getContent().getField("context._procname").getValue();
169 long tid = (Long) event.getContent().getField("context._tid").getValue();
170
171 assertEquals(21507, perfPageFault);
172 assertEquals("swapper/0", procname);
173 assertEquals(0, tid);
174 }
175
176 /**
177 * Test the context of the event at 1363700765.559714634.
178 */
179 @Test
180 public void testContext4() {
181 long time = startTime + 5 * 5000000000L; // 1363700765.559714634
182 CtfTmfEvent event = getEventAt(time);
183 long perfPageFault = (Long) event.getContent().getField("context._perf_page_fault").getValue();
184 String procname = (String) event.getContent().getField("context._procname").getValue();
185 long tid = (Long) event.getContent().getField("context._tid").getValue();
186
187 assertEquals(21507, perfPageFault);
188 assertEquals("swapper/0", procname);
189 assertEquals(0, tid);
190 }
191
192 /**
193 * Test the context of the last event of the trace.
194 */
195 @Test
196 public void testContextEnd() {
197 CtfTmfEvent lastEvent = getEventAt(endTime);
198 long perfPageFault = (Long) lastEvent.getContent().getField("context._perf_page_fault").getValue();
199 String procname = (String) lastEvent.getContent().getField("context._procname").getValue();
200 long tid = (Long) lastEvent.getContent().getField("context._tid").getValue();
201
202 assertEquals(22117, perfPageFault);
203 assertEquals("lttng-sessiond", procname);
204 assertEquals(1230, tid);
205 }
206
207 // ------------------------------------------------------------------------
208 // Private stuff
209 // ------------------------------------------------------------------------
210
211 private synchronized CtfTmfEvent getEventAt(long timestamp) {
212 EventContextTestRequest req = new EventContextTestRequest(timestamp);
213 fixture.sendRequest(req);
214 try {
215 req.waitForCompletion();
216 } catch (InterruptedException e) {
217 return null;
218 }
219 return req.getEvent();
220 }
221
222 private class EventContextTestRequest extends TmfEventRequest {
223
224 private CtfTmfEvent retEvent = null;
225
226 public EventContextTestRequest(long timestamp) {
227 super(CtfTmfEvent.class,
228 new TmfTimeRange(new TmfNanoTimestamp(timestamp), TmfTimestamp.BIG_CRUNCH),
229 0, 1, ExecutionType.FOREGROUND);
230 }
231
232 @Override
233 public void handleData(ITmfEvent event) {
234 retEvent = (CtfTmfEvent) event;
235 }
236
237 public CtfTmfEvent getEvent() {
238 return retEvent;
239 }
240 }
241 }
This page took 0.053642 seconds and 5 git commands to generate.