1 /*******************************************************************************
2 * Copyright (c) 2016 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
8 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.lttng2
.kernel
.core
.tests
.perf
.analysis
.tid
;
12 import static org
.junit
.Assert
.fail
;
15 import java
.util
.List
;
16 import java
.util
.Random
;
18 import org
.eclipse
.jdt
.annotation
.NonNull
;
19 import org
.eclipse
.jdt
.annotation
.Nullable
;
20 import org
.eclipse
.test
.performance
.Dimension
;
21 import org
.eclipse
.test
.performance
.Performance
;
22 import org
.eclipse
.test
.performance
.PerformanceMeter
;
23 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.tid
.TidAnalysisModule
;
24 import org
.eclipse
.tracecompass
.lttng2
.kernel
.core
.tests
.perf
.analysis
.kernel
.KernelAnalysisBenchmark
;
25 import org
.eclipse
.tracecompass
.lttng2
.kernel
.core
.trace
.LttngKernelTrace
;
26 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
27 import org
.eclipse
.tracecompass
.testtraces
.ctf
.CtfTestTrace
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.tests
.shared
.TmfTestHelper
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
33 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.CtfTmfEvent
;
34 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.tests
.shared
.CtfTmfTestTraceUtils
;
35 import org
.junit
.Test
;
38 * Benchmarks some typical usages of the thread id analysis
40 * @author Matthew Khouzam
42 public class TidAnalysisUsageBenchmark
{
44 private static final String TEST_GET_RUNNING_THREAD
= "TID: Threads On CPU";
45 private static final int LOOP_COUNT
= 25;
46 private static final long SEED
= 65423897234L;
47 private static final int NUM_CPU_QUERIES
= 20000;
50 * Run the benchmark with "trace2"
53 public void testTrace2() {
54 runTest(CtfTestTrace
.TRACE2
, "Trace2");
58 * Run the benchmark with "many threads"
61 public void testManyThreads() {
62 runTest(CtfTestTrace
.MANY_THREADS
, "ManyThreads");
66 * Run the benchmark with "django httpd"
69 public void testDjangoHttpd() {
70 runTest(CtfTestTrace
.DJANGO_HTTPD
, "Django httpd");
73 private static TidAnalysisModule
getModule(@NonNull CtfTestTrace testTrace
, @NonNull LttngKernelTrace trace
) {
74 TidAnalysisModule module
= null;
75 String path
= CtfTmfTestTraceUtils
.getTrace(testTrace
).getPath();
78 /* Initialize the analysis module */
79 module
= new TidAnalysisModule();
81 trace
.initTrace(null, path
, CtfTmfEvent
.class);
82 module
.setTrace(trace
);
83 TmfTestHelper
.executeAnalysis(module
);
84 } catch (TmfAnalysisException
| TmfTraceException e
) {
90 private static void deleteSupplementaryFiles(ITmfTrace trace
) {
92 * Delete the supplementary files at the end of the benchmarks
94 File suppDir
= new File(TmfTraceManager
.getSupplementaryFileDir(trace
));
95 for (File file
: suppDir
.listFiles()) {
100 private static void runTest(@NonNull CtfTestTrace testTrace
, String testName
) {
102 /* First, complete the analysis */
103 LttngKernelTrace trace
= new LttngKernelTrace();
104 deleteSupplementaryFiles(trace
);
105 TidAnalysisModule module
= getModule(testTrace
, trace
);
107 /* Benchmark some query use cases */
108 benchmarkGetThreadOnCpu(testName
, module
);
110 deleteSupplementaryFiles(trace
);
114 CtfTmfTestTraceUtils
.dispose(testTrace
);
118 * Benchmarks getting a thread running on a random CPU from the kernel
119 * analysis at fixed intervals. This use case mimics an analysis that reads
120 * events and needs to get the currently running thread for those events.
122 private static void benchmarkGetThreadOnCpu(String testName
, TidAnalysisModule module
) {
124 Performance perf
= Performance
.getDefault();
125 PerformanceMeter pmRunningThread
= perf
.createPerformanceMeter(KernelAnalysisBenchmark
.TEST_ID
+ testName
+ ": " + TEST_GET_RUNNING_THREAD
);
126 perf
.tagAsSummary(pmRunningThread
, TEST_GET_RUNNING_THREAD
+ '(' + testName
+ ')', Dimension
.CPU_TIME
);
129 ITmfStateSystem ss
= module
.getStateSystem();
131 fail("The state system is null");
135 /* Get the number of CPUs */
137 @NonNull List
<@NonNull Integer
> cpus
= ss
.getSubAttributes(ITmfStateSystem
.ROOT_ATTRIBUTE
, false);
138 cpuCount
= cpus
.size();
140 fail("Impossible to get the number of CPUs");
143 /* Get the step and start time of the queries */
144 long startTime
= ss
.getStartTime();
145 long endTime
= ss
.getCurrentEndTime();
146 long step
= Math
.floorDiv(endTime
- startTime
, NUM_CPU_QUERIES
);
149 fail("Trace is too short to run the get thread on CPU benchmark");
152 /* Verify the query work by fetching a value at the end of the trace */
153 Integer threadOnCpu
= module
.getThreadOnCpuAtTime(0, endTime
);
154 if (threadOnCpu
== null) {
155 fail("null thread on CPU at the end of the trace. Something is not right with the state system");
158 for (int i
= 0; i
< LOOP_COUNT
; i
++) {
159 /* Get the thread running on a random CPU at fixed intervals */
160 Random randomGenerator
= new Random(SEED
);
161 pmRunningThread
.start();
162 for (long nextTime
= startTime
; nextTime
< endTime
; nextTime
+= step
) {
163 int cpu
= Math
.abs(randomGenerator
.nextInt()) % cpuCount
;
164 module
.getThreadOnCpuAtTime(cpu
, nextTime
);
166 pmRunningThread
.stop();
168 pmRunningThread
.commit();