Skip to content

Commit

Permalink
7903696: JMH: Add xctrace-based perfasm profiler for macOS
Browse files Browse the repository at this point in the history
Reviewed-by: shade
  • Loading branch information
fzhinkin authored and shipilev committed Apr 23, 2024
1 parent 641f048 commit 6d6ce63
Show file tree
Hide file tree
Showing 29 changed files with 4,366 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.jmh.it.profilers;

import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
import org.openjdk.jmh.it.Fixtures;
import org.openjdk.jmh.profile.ProfilerException;
import org.openjdk.jmh.profile.XCTraceAsmProfiler;
import org.openjdk.jmh.results.Result;
import org.openjdk.jmh.results.RunResult;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import org.openjdk.jmh.util.Utils;

import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;

public class XCTraceAsmProfilerTest extends AbstractAsmProfilerTest {
private static final File TMP_DIR = new File(System.getProperty("java.io.tmpdir"));

private static boolean xctraceExists() {
Collection<String> out = Utils.tryWith("xcode-select", "-p");
if (!out.isEmpty()) {
return false;
}
Optional<String> path = Utils.runWith("xcode-select", "-p").stream()
.flatMap(line -> Arrays.stream(line.split("\n")))
.findFirst();
return path.map(p -> new File(p, "usr/bin/xctrace").exists()).orElse(false);
}

private static void skipIfProfilerNotSupport() {
Assume.assumeTrue(xctraceExists());
}

private void checkProfiling(int numForks) throws RunnerException {
Options opts = new OptionsBuilder()
.include(Fixtures.getTestMask(this.getClass()))
.addProfiler(XCTraceAsmProfiler.class)
.forks(numForks)
.build();

RunResult rr = new Runner(opts).runSingle();

Map<String, Result> sr = rr.getSecondaryResults();
String out = ProfilerTestUtils.checkedGet(sr, "asm").extendedInfo();
if (!checkDisassembly(out)) {
throw new IllegalStateException("Profile does not contain the required frame: " + out);
}
}

@Test
public void testProfiling() throws RunnerException {
skipIfProfilerNotSupport();

checkProfiling(1);
}
@Test
public void testProfilingWithMultipleForks() throws RunnerException {
skipIfProfilerNotSupport();

checkProfiling(2);
}

@Test
public void testTemplateOption() throws RunnerException {
skipIfProfilerNotSupport();

Options opts = new OptionsBuilder()
.include(Fixtures.getTestMask(this.getClass()))
.addProfiler(XCTraceAsmProfiler.class, "template=Time Profiler")
.build();

RunResult rr = new Runner(opts).runSingle();

Map<String, Result> sr = rr.getSecondaryResults();
String out = ProfilerTestUtils.checkedGet(sr, "asm").extendedInfo();
if (!checkDisassembly(out)) {
throw new IllegalStateException("Profile does not contain the required frame: " + out);
}
}

@Test
public void testSavePerfBinHandling() throws RunnerException {
skipIfProfilerNotSupport();
File expectedFile = new File(TMP_DIR, "results.trace");
try {
Options opts = new OptionsBuilder()
.include(Fixtures.getTestMask(this.getClass()))
.addProfiler(XCTraceAsmProfiler.class,
"savePerfBin=true;savePerfBinToFile=" + expectedFile.getAbsolutePath())
.build();
new Runner(opts).runSingle();
Assert.assertTrue("Results were not copied", expectedFile.isDirectory());
} finally {
expectedFile.delete();
}
}

@Test
public void testConstructorThrowsWhenXCTraceDoesNotExist() {
Assume.assumeFalse(xctraceExists());
Assert.assertThrows(ProfilerException.class, () -> new XCTraceAsmProfiler(""));
}
}
3 changes: 3 additions & 0 deletions jmh-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ questions.
<mapping>
<java>PHP</java>
</mapping>
<excludes>
<exclude>src/test/resources/org.openjdk.jmh.profile.xctrace/*</exclude>
</excludes>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -59,8 +59,8 @@ public abstract class AbstractPerfAsmProfiler implements ExternalProfiler {
private final String savePerfOutputToFile;

private final boolean savePerfBin;
private final String savePerfBinTo;
private final String savePerfBinFile;
protected final String savePerfBinTo;
protected final String savePerfBinFile;

private final boolean saveLog;
private final String saveLogTo;
Expand Down Expand Up @@ -333,6 +333,24 @@ protected List<String> stripEventNames(List<String> src) {
*/
protected abstract String perfBinaryExtension();

/**
* Copy perf binary output file generated for a benchmark run.
*
* @param runParams BenchmarkParams for a run perf results were generated for.
* @param logger Logger to print debug messages.
*/
protected void savePerfBinaryOutput(BenchmarkParams runParams, PrintWriter logger) {
String target = (savePerfBinFile == null) ?
savePerfBinTo + "/" + runParams.id() + perfBinaryExtension() :
savePerfBinFile;
try {
FileUtils.copy(perfBinData.getAbsolutePath(), target);
logger.println("Perf binary output saved to " + target);
} catch (IOException e) {
logger.println("Unable to save perf binary output to " + target);
}
}

private TextResult processAssembly(BenchmarkResult br) {
/**
* 1. Parse binary events.
Expand Down Expand Up @@ -679,15 +697,7 @@ private TextResult processAssembly(BenchmarkResult br) {
* Print binary perf output, if needed:
*/
if (savePerfBin) {
String target = (savePerfBinFile == null) ?
savePerfBinTo + "/" + br.getParams().id() + perfBinaryExtension() :
savePerfBinFile;
try {
FileUtils.copy(perfBinData.getAbsolutePath(), target);
pw.println("Perf binary output saved to " + target);
} catch (IOException e) {
pw.println("Unable to save perf binary output to " + target);
}
savePerfBinaryOutput(br.getParams(), pw);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -176,6 +176,7 @@ public static void listProfilers(PrintStream out) {
BUILT_IN.put("mempool", MemPoolProfiler.class);
BUILT_IN.put("xperfasm", WinPerfAsmProfiler.class);
BUILT_IN.put("dtraceasm", DTraceAsmProfiler.class);
BUILT_IN.put("xctraceasm", XCTraceAsmProfiler.class);
BUILT_IN.put("pauses", PausesProfiler.class);
BUILT_IN.put("safepoints", SafepointsProfiler.class);
BUILT_IN.put("perfc2c", LinuxPerfC2CProfiler.class);
Expand Down
Loading

0 comments on commit 6d6ce63

Please sign in to comment.