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
.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
;
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 int TRACE_INDEX
= 2;
46 private static final String PATH
= CtfTmfTestTraces
.getTestTracePath(TRACE_INDEX
);
48 private static CtfTmfTrace fixture
;
49 private static long startTime
;
50 private static long endTime
;
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
57 * Perform pre-class initialization.
59 * @throws TmfTraceException
60 * If the test trace is not found
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);
69 startTime
= fixture
.getStartTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
70 endTime
= fixture
.getEndTime().normalize(0, ITmfTimestamp
.NANOSECOND_SCALE
).getValue();
74 * Perform post-class clean-up.
77 public static void tearDown() {
78 if (fixture
!= null) {
83 // ------------------------------------------------------------------------
85 // ------------------------------------------------------------------------
88 * Make sure the trace is the correct one, and its timestamps are read
92 public void testTrace() {
93 assertEquals(1363700740555978750L, startTime
);
94 assertEquals(1363700770550261288L, endTime
);
98 * Test the context of the very first event of the trace.
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();
107 assertEquals(613, perfPageFault
);
108 assertEquals("lttng-sessiond", procname
);
109 assertEquals(1230, tid
);
113 * Test the context of the event at 1363700745.559739078.
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();
123 assertEquals(6048, perfPageFault
);
124 assertEquals("swapper/0", procname
);
125 assertEquals(0, tid
);
129 * Test the context of the event at 1363700750.559707062.
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();
139 assertEquals(13258, perfPageFault
);
140 assertEquals("swapper/0", procname
);
141 assertEquals(0, tid
);
145 * Test the context of the event at 1363700755.555723128, which is roughly
146 * mid-way through the trace.
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();
156 assertEquals(19438, perfPageFault
);
157 assertEquals("swapper/0", procname
);
158 assertEquals(0, tid
);
162 * Test the context of the event at 1363700760.559719724.
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();
172 assertEquals(21507, perfPageFault
);
173 assertEquals("swapper/0", procname
);
174 assertEquals(0, tid
);
178 * Test the context of the event at 1363700765.559714634.
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();
188 assertEquals(21507, perfPageFault
);
189 assertEquals("swapper/0", procname
);
190 assertEquals(0, tid
);
194 * Test the context of the last event of the trace.
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();
203 assertEquals(22117, perfPageFault
);
204 assertEquals("lttng-sessiond", procname
);
205 assertEquals(1230, tid
);
208 // ------------------------------------------------------------------------
210 // ------------------------------------------------------------------------
212 private synchronized CtfTmfEvent
getEventAt(long timestamp
) {
213 EventContextTestRequest req
= new EventContextTestRequest(timestamp
);
214 fixture
.sendRequest(req
);
216 req
.waitForCompletion();
217 } catch (InterruptedException e
) {
220 return req
.getEvent();
223 private class EventContextTestRequest
extends TmfEventRequest
{
225 private CtfTmfEvent retEvent
= null;
227 public EventContextTestRequest(long timestamp
) {
228 super(CtfTmfEvent
.class,
229 new TmfTimeRange(new CtfTmfTimestamp(timestamp
), TmfTimestamp
.BIG_CRUNCH
),
230 0, 1, ExecutionType
.FOREGROUND
);
234 public void handleData(ITmfEvent event
) {
235 retEvent
= (CtfTmfEvent
) event
;
238 public CtfTmfEvent
getEvent() {