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