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