1 /*******************************************************************************
2 * Copyright (c) 2013 Ericsson
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
10 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.ctfadaptor
;
15 import static org
.junit
.Assert
.assertEquals
;
16 import static org
.junit
.Assume
.assumeTrue
;
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
.CtfTmfTestTrace
;
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
;
34 * Tests for reading event contexts from a CtfTmfTrace.
36 * @author Alexandre Montplaisir
38 public class EventContextTest
{
40 // ------------------------------------------------------------------------
42 // ------------------------------------------------------------------------
44 /* We use test trace #2, kernel_vm, which has event contexts */
45 private static final CtfTmfTestTrace testTrace
= CtfTmfTestTrace
.KERNEL_VM
;
47 private static CtfTmfTrace fixture
;
48 private static long startTime
;
49 private static long endTime
;
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
56 * Perform pre-class initialization.
58 * @throws TmfTraceException
59 * If the test trace is not found
62 public static 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);
68 startTime
= fixture
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
69 endTime
= fixture
.getEndTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
73 * Perform post-class clean-up.
76 public static void tearDown() {
77 if (fixture
!= null) {
82 // ------------------------------------------------------------------------
84 // ------------------------------------------------------------------------
87 * Make sure the trace is the correct one, and its timestamps are read
91 public void testTrace() {
92 assertEquals(1363700740555978750L, startTime
);
93 assertEquals(1363700770550261288L, endTime
);
97 * Test the context of the very first event of the trace.
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();
106 assertEquals(613, perfPageFault
);
107 assertEquals("lttng-sessiond", procname
);
108 assertEquals(1230, tid
);
112 * Test the context of the event at 1363700745.559739078.
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();
122 assertEquals(6048, perfPageFault
);
123 assertEquals("swapper/0", procname
);
124 assertEquals(0, tid
);
128 * Test the context of the event at 1363700750.559707062.
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();
138 assertEquals(13258, perfPageFault
);
139 assertEquals("swapper/0", procname
);
140 assertEquals(0, tid
);
144 * Test the context of the event at 1363700755.555723128, which is roughly
145 * mid-way through the trace.
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();
155 assertEquals(19438, perfPageFault
);
156 assertEquals("swapper/0", procname
);
157 assertEquals(0, tid
);
161 * Test the context of the event at 1363700760.559719724.
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();
171 assertEquals(21507, perfPageFault
);
172 assertEquals("swapper/0", procname
);
173 assertEquals(0, tid
);
177 * Test the context of the event at 1363700765.559714634.
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();
187 assertEquals(21507, perfPageFault
);
188 assertEquals("swapper/0", procname
);
189 assertEquals(0, tid
);
193 * Test the context of the last event of the trace.
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();
202 assertEquals(22117, perfPageFault
);
203 assertEquals("lttng-sessiond", procname
);
204 assertEquals(1230, tid
);
207 // ------------------------------------------------------------------------
209 // ------------------------------------------------------------------------
211 private synchronized CtfTmfEvent
getEventAt(long timestamp
) {
212 EventContextTestRequest req
= new EventContextTestRequest(timestamp
);
213 fixture
.sendRequest(req
);
215 req
.waitForCompletion();
216 } catch (InterruptedException e
) {
219 return req
.getEvent();
222 private class EventContextTestRequest
extends TmfEventRequest
{
224 private CtfTmfEvent retEvent
= null;
226 public EventContextTestRequest(long timestamp
) {
227 super(CtfTmfEvent
.class,
228 new TmfTimeRange(new CtfTmfTimestamp(timestamp
), TmfTimestamp
.BIG_CRUNCH
),
229 0, 1, ExecutionType
.FOREGROUND
);
233 public void handleData(ITmfEvent event
) {
234 retEvent
= (CtfTmfEvent
) event
;
237 public CtfTmfEvent
getEvent() {