| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2016 Ericsson |
| 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 | |
| 10 | package org.eclipse.tracecompass.lttng2.kernel.core.tests.perf.analysis.tid; |
| 11 | |
| 12 | import static org.junit.Assert.fail; |
| 13 | |
| 14 | import java.io.File; |
| 15 | import java.util.List; |
| 16 | import java.util.Random; |
| 17 | |
| 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; |
| 36 | |
| 37 | /** |
| 38 | * Benchmarks some typical usages of the thread id analysis |
| 39 | * |
| 40 | * @author Matthew Khouzam |
| 41 | */ |
| 42 | public class TidAnalysisUsageBenchmark { |
| 43 | |
| 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; |
| 48 | |
| 49 | /** |
| 50 | * Run the benchmark with "trace2" |
| 51 | */ |
| 52 | @Test |
| 53 | public void testTrace2() { |
| 54 | runTest(CtfTestTrace.TRACE2, "Trace2"); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Run the benchmark with "many threads" |
| 59 | */ |
| 60 | @Test |
| 61 | public void testManyThreads() { |
| 62 | runTest(CtfTestTrace.MANY_THREADS, "ManyThreads"); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Run the benchmark with "django httpd" |
| 67 | */ |
| 68 | @Test |
| 69 | public void testDjangoHttpd() { |
| 70 | runTest(CtfTestTrace.DJANGO_HTTPD, "Django httpd"); |
| 71 | } |
| 72 | |
| 73 | private static TidAnalysisModule getModule(@NonNull CtfTestTrace testTrace, @NonNull LttngKernelTrace trace) { |
| 74 | TidAnalysisModule module = null; |
| 75 | String path = CtfTmfTestTraceUtils.getTrace(testTrace).getPath(); |
| 76 | |
| 77 | try { |
| 78 | /* Initialize the analysis module */ |
| 79 | module = new TidAnalysisModule(); |
| 80 | module.setId("test"); |
| 81 | trace.initTrace(null, path, CtfTmfEvent.class); |
| 82 | module.setTrace(trace); |
| 83 | TmfTestHelper.executeAnalysis(module); |
| 84 | } catch (TmfAnalysisException | TmfTraceException e) { |
| 85 | fail(e.getMessage()); |
| 86 | } |
| 87 | return module; |
| 88 | } |
| 89 | |
| 90 | private static void deleteSupplementaryFiles(ITmfTrace trace) { |
| 91 | /* |
| 92 | * Delete the supplementary files at the end of the benchmarks |
| 93 | */ |
| 94 | File suppDir = new File(TmfTraceManager.getSupplementaryFileDir(trace)); |
| 95 | for (File file : suppDir.listFiles()) { |
| 96 | file.delete(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | private static void runTest(@NonNull CtfTestTrace testTrace, String testName) { |
| 101 | |
| 102 | /* First, complete the analysis */ |
| 103 | LttngKernelTrace trace = new LttngKernelTrace(); |
| 104 | deleteSupplementaryFiles(trace); |
| 105 | TidAnalysisModule module = getModule(testTrace, trace); |
| 106 | |
| 107 | /* Benchmark some query use cases */ |
| 108 | benchmarkGetThreadOnCpu(testName, module); |
| 109 | |
| 110 | deleteSupplementaryFiles(trace); |
| 111 | module.dispose(); |
| 112 | trace.dispose(); |
| 113 | |
| 114 | CtfTmfTestTraceUtils.dispose(testTrace); |
| 115 | } |
| 116 | |
| 117 | /** |
| 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. |
| 121 | */ |
| 122 | private static void benchmarkGetThreadOnCpu(String testName, TidAnalysisModule module) { |
| 123 | |
| 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); |
| 127 | |
| 128 | @Nullable |
| 129 | ITmfStateSystem ss = module.getStateSystem(); |
| 130 | if (ss == null) { |
| 131 | fail("The state system is null"); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | /* Get the number of CPUs */ |
| 136 | int cpuCount = -1; |
| 137 | @NonNull List<@NonNull Integer> cpus = ss.getSubAttributes(ITmfStateSystem.ROOT_ATTRIBUTE, false); |
| 138 | cpuCount = cpus.size(); |
| 139 | if (cpuCount < 1) { |
| 140 | fail("Impossible to get the number of CPUs"); |
| 141 | } |
| 142 | |
| 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); |
| 147 | |
| 148 | if (step < 1) { |
| 149 | fail("Trace is too short to run the get thread on CPU benchmark"); |
| 150 | } |
| 151 | |
| 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"); |
| 156 | } |
| 157 | |
| 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); |
| 165 | } |
| 166 | pmRunningThread.stop(); |
| 167 | } |
| 168 | pmRunningThread.commit(); |
| 169 | } |
| 170 | } |