-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We should be able to use RDTSC as a timestamp counter with `JITDUMP_FLAGS_ARCH_TIMESTAMP` flag, but it does not work with perf-6.10.6-200.fc40.x86_64 at least. libperf-jvmti.so provided by perf tool is same behavior. So I suspect it is a bug in perf tool, and I use monotonic clock in this feature. See a237173 for RDTSC version.
- Loading branch information
Showing
12 changed files
with
815 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
target/ | ||
jit-*.dump | ||
jitted-*.so | ||
perf.*data* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
Example of profiling with perf tool | ||
=================== | ||
|
||
This example shows how to use `JitDump` to profile your assembly code generated by ffmasm. [PerfJit.java](src/main/java/com/yasuenag/ffmasm/examples/perf/PerfJit.java) issues `RDRAND` instruction in 10,000,000 times. | ||
|
||
# How to build | ||
|
||
``` | ||
cd /path/to/ffasm | ||
mvn install | ||
cd examples/perf | ||
mvn package | ||
``` | ||
|
||
# Run test | ||
|
||
[perf.sh](perf.sh) runs the example with `perf` and injects jitted code (it means assembly code generated by ffmasm) with `perf inject`. | ||
|
||
``` | ||
./perf.sh | ||
``` | ||
|
||
You can see `jit-<PID>.dump`, `jitted-*.so`, and `perf.*data*` on working directory of `perf.sh`. Please keep them until you run `perf report`. | ||
|
||
# Profiling with `perf report` | ||
|
||
You need to specify `perf.jit.data` with `-i` option to load injected data. | ||
|
||
``` | ||
perf report -i perf.jit.data | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/bash | ||
|
||
BASEDIR=$(dirname $0) | ||
|
||
perf record -k 1 $JAVA_HOME/bin/java -jar $BASEDIR/target/perf-example-0.1.0.jar && \ | ||
perf inject --jit -i perf.data -o perf.jit.data && \ | ||
echo 'Completed. run "perf report -i perf.jit.data"' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.yasuenag</groupId> | ||
<artifactId>perf-example</artifactId> | ||
<packaging>jar</packaging> | ||
<version>0.1.0</version> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>22</maven.compiler.source> | ||
<maven.compiler.target>22</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.yasuenag</groupId> | ||
<artifactId>ffmasm</artifactId> | ||
<version>0.4.0-SNAPSHOT</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.13.0</version> | ||
<configuration> | ||
<compilerArgs> | ||
<arg>-Xlint:all</arg> | ||
</compilerArgs> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<version>3.4.2</version> | ||
<configuration> | ||
<archive> | ||
<manifestEntries> | ||
<Enable-Native-Access>ALL-UNNAMED</Enable-Native-Access> | ||
</manifestEntries> | ||
</archive> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
<version>3.6.0</version> | ||
<configuration> | ||
<createDependencyReducedPom>false</createDependencyReducedPom> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>shade</goal> | ||
</goals> | ||
<configuration> | ||
<transformers> | ||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> | ||
<mainClass>com.yasuenag.ffmasm.examples.perf.PerfJit</mainClass> | ||
</transformer> | ||
</transformers> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
46 changes: 46 additions & 0 deletions
46
examples/perf/src/main/java/com/yasuenag/ffmasm/examples/perf/PerfJit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.yasuenag.ffmasm.examples.perf; | ||
|
||
import java.lang.foreign.*; | ||
import java.lang.invoke.*; | ||
import java.nio.file.*; | ||
import java.util.*; | ||
|
||
import com.yasuenag.ffmasm.*; | ||
import com.yasuenag.ffmasm.amd64.*; | ||
|
||
|
||
public class PerfJit{ | ||
|
||
private static final CodeSegment seg; | ||
|
||
private static final MethodHandle rdtsc; | ||
|
||
private static final JitDump jitdump; | ||
|
||
static{ | ||
try{ | ||
seg = new CodeSegment(); | ||
jitdump = JitDump.getInstance(Path.of(".")); | ||
var desc = FunctionDescriptor.of(ValueLayout.JAVA_LONG); | ||
rdtsc = AMD64AsmBuilder.create(AMD64AsmBuilder.class, seg, desc) | ||
/* .align 16 */ .alignTo16BytesWithNOP() | ||
/* retry: */ .label("retry") | ||
/* rdrand %rax */ .rdrand(Register.RAX) | ||
/* jae retry */ .jae("retry") | ||
/* ret */ .ret() | ||
.build("ffm_rdtsc", jitdump, Linker.Option.critical(false)); | ||
} | ||
catch(Throwable t){ | ||
throw new RuntimeException(t); | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws Throwable{ | ||
try(jitdump; seg){ | ||
for(int i = 0; i < 10_000_000; i++){ | ||
long _ = (long)rdtsc.invokeExact(); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (C) 2024, Yasumasa Suenaga | ||
* | ||
* This file is part of ffmasm. | ||
* | ||
* ffmasm is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* ffmasm 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 Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with ffmasm. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.yasuenag.ffmasm; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
|
||
import com.yasuenag.ffmasm.internal.linux.PerfJitDump; | ||
|
||
|
||
/** | ||
* Interface of jitdump for perf command on Linux. | ||
* | ||
* @author Yasumasa Suenaga | ||
*/ | ||
public interface JitDump extends AutoCloseable{ | ||
|
||
/** | ||
* Get instance of JitDump. | ||
* | ||
* @param dir Base directory which jitdump is generated. | ||
* @throws UnsupportedPlatformException if the call happens on unsupported platform. | ||
*/ | ||
public static JitDump getInstance(Path dir) throws UnsupportedPlatformException, PlatformException, IOException{ | ||
var osName = System.getProperty("os.name"); | ||
if(osName.equals("Linux")){ | ||
return new PerfJitDump(dir); | ||
} | ||
|
||
throw new UnsupportedPlatformException("This platform is not supported in JitDump"); | ||
} | ||
|
||
/** | ||
* Write method info to jitdump. | ||
* | ||
* @param method MethodInfo should be written. | ||
*/ | ||
public void writeFunction(CodeSegment.MethodInfo method); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.