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