Skip to content

Commit

Permalink
Consider xctracenorm unsupported if there's no xctrace or PMC is unav…
Browse files Browse the repository at this point in the history
…ailable
  • Loading branch information
fzhinkin committed Mar 23, 2024
1 parent f62a152 commit e4f6fb2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
42 changes: 25 additions & 17 deletions integration/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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
}
}
}

Expand Down Expand Up @@ -96,15 +100,17 @@ tasks {
doLast {
val out = standardOutput.toString()
val predicate: (String) -> Boolean = { !it.contains("Unsupported profilers") }
val filter: (Sequence<String>) -> Sequence<String> = if (hasXCtrace) {
{ it.takeWhile(predicate) }
} else {
{ it.dropWhile(predicate) }
val filterFactory: (Boolean) -> ((Sequence<String>) -> Sequence<String>) = { 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) {
Expand All @@ -121,14 +127,16 @@ tasks {
args("listEvents=true")
}
check {
dependsOn.addAll(listOf(
dependsOn.addAll(
listOf(
"runAsmProfWithDefaultArgs",
"runAsmProfWithExplicitCpuProfiler",
"runAsmProfWithExplicitTimeProfiler",
"runTimestampFiltrationTest",
"runNormProfiler",
"listPmuEvents",
"checkAvailableProfilers"
))
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public XCTraceNormProfiler(String initLine) throws ProfilerException {
OptionSet options = ProfilerUtils.parseInitLine(initLine, parser);

XCTraceUtils.checkXCTraceWorks();
skipForVM();

if (options.hasArgument(printAvailableEventsOpt)) {
printAvailableEvents();
Expand All @@ -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 {
Expand All @@ -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<String> 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.");
}
Expand Down
6 changes: 6 additions & 0 deletions profiler/src/test/java/org/openjdk/jmh/profile/KpepTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit e4f6fb2

Please sign in to comment.