diff --git a/integration/build.gradle.kts b/integration/build.gradle.kts index 867d4f0..ddcc7be 100644 --- a/integration/build.gradle.kts +++ b/integration/build.gradle.kts @@ -1,7 +1,6 @@ import org.gradle.process.internal.ExecException import java.io.ByteArrayOutputStream import java.lang.IllegalStateException -import java.nio.charset.StandardCharsets plugins { id("java") @@ -28,15 +27,20 @@ val hasXCtrace: Boolean by lazy { } val hasPMC: Boolean by lazy { - try { - val out = ByteArrayOutputStream() - val res = exec { - commandLine("sysctl", "-n", "hw.cpufamily") - standardOutput = out - } - res.exitValue == 0 && out.toByteArray().decodeToString().lineSequence().first() != "0" - } catch (e: ExecException) { + if (project.hasProperty("noPMU")) { false + } else { + try { + val out = ByteArrayOutputStream() + val res = exec { + commandLine("sysctl", "-n", "hw.cpufamily") + standardOutput = out + } + val value = out.toByteArray().decodeToString().lineSequence().first() + res.exitValue == 0 && value.toInt() != 0 + } catch (e: ExecException) { + false + } } } @@ -96,15 +100,17 @@ tasks { doLast { val out = standardOutput.toString() val predicate: (String) -> Boolean = { !it.contains("Unsupported profilers") } - val filter: (Sequence) -> Sequence = if (hasXCtrace) { - { it.takeWhile(predicate) } - } else { - { it.dropWhile(predicate) } + val filterFactory: (Boolean) -> ((Sequence) -> Sequence) = { isSupported -> + if (isSupported) { + { it.takeWhile(predicate) } + } else { + { it.dropWhile(predicate) } + } } val kind = if (hasXCtrace) "supported" else "unsupported" - checkHasXCTraceAsmProfiler(filter(out.lineSequence()), kind) + checkHasXCTraceAsmProfiler(filterFactory(hasXCtrace)(out.lineSequence()), kind) val normKind = if (hasPMC) "supported" else "unsupported" - checkHasXCTraceNormProfiler(filter(out.lineSequence()), normKind) + checkHasXCTraceNormProfiler(filterFactory(hasPMC)(out.lineSequence()), normKind) } } create("runNormProfiler", JavaExec::class.java) { @@ -121,7 +127,8 @@ tasks { args("listEvents=true") } check { - dependsOn.addAll(listOf( + dependsOn.addAll( + listOf( "runAsmProfWithDefaultArgs", "runAsmProfWithExplicitCpuProfiler", "runAsmProfWithExplicitTimeProfiler", @@ -129,6 +136,7 @@ tasks { "runNormProfiler", "listPmuEvents", "checkAvailableProfilers" - )) + ) + ) } } diff --git a/profiler/src/main/java/org/openjdk/jmh/profile/XCTraceNormProfiler.java b/profiler/src/main/java/org/openjdk/jmh/profile/XCTraceNormProfiler.java index 5de5dab..8088802 100644 --- a/profiler/src/main/java/org/openjdk/jmh/profile/XCTraceNormProfiler.java +++ b/profiler/src/main/java/org/openjdk/jmh/profile/XCTraceNormProfiler.java @@ -138,6 +138,7 @@ public XCTraceNormProfiler(String initLine) throws ProfilerException { OptionSet options = ProfilerUtils.parseInitLine(initLine, parser); XCTraceUtils.checkXCTraceWorks(); + skipForVM(); if (options.hasArgument(printAvailableEventsOpt)) { printAvailableEvents(); @@ -164,7 +165,6 @@ public XCTraceNormProfiler(String initLine) throws ProfilerException { if (rawPmuEvents.isEmpty()) { throw new ProfilerException("Either \"template\", or \"events\" should be specified"); } - skipForVM(); pmuEvents = remapPmuEvents(rawPmuEvents); samplingPackage = setupPmuEvents(); } else { @@ -174,12 +174,12 @@ public XCTraceNormProfiler(String initLine) throws ProfilerException { } private static void skipForVM() throws ProfilerException { - String prop = Utils.runWith("sysctl", "hw.cpufamily").stream() + Collection out = Utils.runWith("sysctl", "-n", "hw.cpufamily"); + String prop = out.stream() .flatMap(line -> Stream.of(line.split("\n"))) - .filter(line -> line.startsWith("hw.cpufamily: ")) .findFirst().orElseThrow(() -> new ProfilerException("Can't read hw.cpufamily sysctl property. Is is macOS?")); - if (prop.trim().equals("hw.cpufamily: 0")) { + if (Integer.parseInt(prop.trim()) == 0) { // TODO: is it really unavailable? throw new ProfilerException("hw.cpufamily is 0. Seems like it's a virtual machine, PMU is not available."); } diff --git a/profiler/src/test/java/org/openjdk/jmh/profile/KpepTest.java b/profiler/src/test/java/org/openjdk/jmh/profile/KpepTest.java index 27f71d0..68da209 100644 --- a/profiler/src/test/java/org/openjdk/jmh/profile/KpepTest.java +++ b/profiler/src/test/java/org/openjdk/jmh/profile/KpepTest.java @@ -21,14 +21,20 @@ import org.junit.Assert; import org.junit.Assume; import org.junit.Test; +import org.openjdk.jmh.util.Utils; import java.util.List; import java.util.Locale; public class KpepTest { + private static void skipOnUnknownCpuFamily() { + String line = Utils.runWith("sysctl", "-n", "hw.cpufamily").iterator().next(); + Assume.assumeFalse(line.split("\n")[0].equals("0")); + } @Test public void kpepTest() { Assume.assumeTrue(System.getProperty("os.name").toLowerCase(Locale.ROOT).equals("mac os x")); + skipOnUnknownCpuFamily(); KpepUtils.PerfEvents events = KpepUtils.supportedEvents(); Assert.assertFalse(events.aliases.isEmpty()); Assert.assertTrue(events.aliases.containsKey(KpepUtils.PerfEvents.CYCLES));