Commit | Line | Data |
---|---|---|
312f397a GB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2015 École Polytechnique de Montréal | |
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 | * Geneviève Bastien - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.tracecompass.analysis.os.linux.core.tests.kernelanalysis; | |
14 | ||
15 | import static org.junit.Assert.assertEquals; | |
16 | import static org.junit.Assert.assertNotNull; | |
17 | import static org.junit.Assert.assertNull; | |
18 | import static org.junit.Assert.fail; | |
19 | ||
20 | import java.io.File; | |
21 | ||
22 | import org.eclipse.core.runtime.IPath; | |
23 | import org.eclipse.core.runtime.IStatus; | |
24 | import org.eclipse.jdt.annotation.NonNull; | |
25 | import org.eclipse.tracecompass.analysis.os.linux.core.kernelanalysis.KernelAnalysis; | |
26 | import org.eclipse.tracecompass.analysis.os.linux.core.kernelanalysis.KernelTidAspect; | |
27 | import org.eclipse.tracecompass.analysis.os.linux.core.tests.Activator; | |
28 | import org.eclipse.tracecompass.common.core.NonNullUtils; | |
29 | import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule; | |
30 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; | |
31 | import org.eclipse.tracecompass.tmf.core.event.TmfEvent; | |
32 | import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect; | |
33 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; | |
34 | import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal; | |
35 | import org.eclipse.tracecompass.tmf.core.trace.ITmfContext; | |
36 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
37 | import org.eclipse.tracecompass.tmf.core.trace.TmfTrace; | |
38 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager; | |
39 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils; | |
40 | import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub; | |
41 | import org.junit.After; | |
42 | import org.junit.Before; | |
43 | import org.junit.Test; | |
44 | ||
45 | import com.google.common.collect.ImmutableSet; | |
46 | ||
47 | /** | |
48 | * Test the {@link KernelTidAspect} class | |
49 | * | |
50 | * @author Geneviève Bastien | |
51 | */ | |
52 | public class KernelTidAspectTest { | |
53 | ||
54 | private static final @NonNull String LTTNG_KERNEL_FILE = "testfiles/lttng_kernel_analysis.xml"; | |
55 | ||
56 | // ------------------------------------------------------------------------ | |
57 | // Test trace class definition | |
58 | // ------------------------------------------------------------------------ | |
59 | ||
60 | private static class TmfXmlTraceStubWithTidAspects extends TmfXmlTraceStub { | |
61 | ||
62 | public TmfXmlTraceStubWithTidAspects() { | |
63 | super(); | |
64 | } | |
65 | ||
66 | @Override | |
67 | public Iterable<ITmfEventAspect> getEventAspects() { | |
68 | ImmutableSet.Builder<ITmfEventAspect> builder = ImmutableSet.builder(); | |
69 | builder.addAll(super.getEventAspects()); | |
70 | builder.add(new KernelTidAspect()); | |
71 | return NonNullUtils.checkNotNull(builder.build()); | |
72 | } | |
73 | ||
74 | } | |
75 | ||
76 | private ITmfTrace fTrace; | |
77 | ||
78 | private static void deleteSuppFiles(ITmfTrace trace) { | |
79 | /* Remove supplementary files */ | |
80 | File suppDir = new File(TmfTraceManager.getSupplementaryFileDir(trace)); | |
81 | for (File file : suppDir.listFiles()) { | |
82 | file.delete(); | |
83 | } | |
84 | } | |
85 | ||
86 | /** | |
87 | * Test setup | |
88 | */ | |
89 | @Before | |
90 | public void setUp() { | |
91 | fTrace = new TmfXmlTraceStubWithTidAspects(); | |
92 | IPath filePath = Activator.getAbsoluteFilePath(LTTNG_KERNEL_FILE); | |
93 | IStatus status = fTrace.validate(null, filePath.toOSString()); | |
94 | if (!status.isOK()) { | |
95 | fail(status.getException().getMessage()); | |
96 | } | |
97 | try { | |
98 | fTrace.initTrace(null, filePath.toOSString(), TmfEvent.class); | |
99 | } catch (TmfTraceException e) { | |
100 | fail(e.getMessage()); | |
101 | } | |
102 | deleteSuppFiles(fTrace); | |
103 | /* Make sure the Kernel analysis has run */ | |
104 | ((TmfTrace) fTrace).traceOpened(new TmfTraceOpenedSignal(this, fTrace, null)); | |
105 | IAnalysisModule module = null; | |
106 | for (IAnalysisModule mod : TmfTraceUtils.getAnalysisModulesOfClass(fTrace, KernelAnalysis.class)) { | |
107 | module = mod; | |
108 | } | |
109 | assertNotNull(module); | |
110 | module.schedule(); | |
111 | module.waitForCompletion(); | |
112 | } | |
113 | ||
114 | /** | |
115 | * Test clean up | |
116 | */ | |
117 | @After | |
118 | public void tearDown() { | |
119 | fTrace.dispose(); | |
120 | } | |
121 | ||
122 | private void resolveNextEvent(ITmfContext context, Integer tid) { | |
123 | ||
124 | ITmfEvent event = fTrace.getNext(context); | |
125 | assertNotNull(event); | |
126 | ||
127 | Object tidObj = TmfTraceUtils.resolveEventAspectOfClassForEvent(fTrace, KernelTidAspect.class, event); | |
128 | if (tid == null) { | |
129 | assertNull(tidObj); | |
130 | } else { | |
131 | assertNotNull(tidObj); | |
132 | assertEquals(tid, tidObj); | |
133 | } | |
134 | } | |
135 | ||
136 | /** | |
137 | * Test the {@link KernelTidAspect#resolve(ITmfEvent)} method method | |
138 | */ | |
139 | @Test | |
140 | public void testResolveTidAspect() { | |
141 | ||
142 | ITmfContext context = fTrace.seekEvent(0L); | |
143 | resolveNextEvent(context, null); | |
144 | resolveNextEvent(context, null); | |
145 | resolveNextEvent(context, null); | |
146 | resolveNextEvent(context, 11); | |
147 | resolveNextEvent(context, null); | |
148 | resolveNextEvent(context, null); | |
149 | resolveNextEvent(context, 20); | |
150 | resolveNextEvent(context, 20); | |
151 | resolveNextEvent(context, 21); | |
152 | resolveNextEvent(context, 11); | |
153 | resolveNextEvent(context, 30); | |
154 | resolveNextEvent(context, 21); | |
155 | resolveNextEvent(context, 20); | |
156 | } | |
157 | ||
158 | } |