Skip to content

Commit

Permalink
Add interfaces for PerfMap and JitDump to getMemorySegment() in AMD64…
Browse files Browse the repository at this point in the history
…AsmBuilder
  • Loading branch information
YaSuenag committed Dec 21, 2024
1 parent 01cb758 commit f3d720d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
7 changes: 3 additions & 4 deletions src/main/java/com/yasuenag/ffmasm/CodeSegment.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class CodeSegment implements AutoCloseable{
/**
* Holder for method information. This is used for perfmap dumping.
*/
public static record MethodInfo(MethodHandle methodHandle, String name, long address, int size){
public static record MethodInfo(String name, long address, int size){
@Override
public String toString(){
return String.format("0x%x 0x%x %s", address, size, name);
Expand Down Expand Up @@ -189,18 +189,17 @@ public MemorySegment getAddr(){

/**
* Add method info. It will be dumped to perf map as related method of this CodeSegment.
* @param mh MethodHandle of the method
* @param name Method name
* @param address Address of the method
* @param size Size of the method (machine code)
* @return MethodInfo of the method info.
* @throws IllegalArgumentException if the address is out of range from this CodeSegment.
*/
public MethodInfo addMethodInfo(MethodHandle mh, String name, long address, int size){
public MethodInfo addMethodInfo(String name, long address, int size){
if((address < addr.address()) || ((addr.address() + this.size) < (address + size))){
throw new IllegalArgumentException("Address is out of range from CodeSegment.");
}
var methodInfo = new MethodInfo(mh, name, address, size);
var methodInfo = new MethodInfo(name, address, size);
methods.add(methodInfo);
return methodInfo;
}
Expand Down
45 changes: 36 additions & 9 deletions src/main/java/com/yasuenag/ffmasm/amd64/AMD64AsmBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,15 @@ public MethodHandle build(String name, Linker.Option... options){
return build(name, null, options);
}

private void storeMethodInfo(String name, JitDump jitdump){
var top = mem.address();
var size = byteBuf.position();
var methodInfo = seg.addMethodInfo(name, top, size);
if(jitdump != null){
jitdump.writeFunction(methodInfo);
}
}

/**
* Build as a MethodHandle
*
Expand All @@ -997,24 +1006,42 @@ public MethodHandle build(String name, Linker.Option... options){
*/
public MethodHandle build(String name, JitDump jitdump, Linker.Option... options){
updateTail();
var top = mem.address();
var size = byteBuf.position();
var mh = Linker.nativeLinker().downcallHandle(mem, desc, options);

var methodInfo = seg.addMethodInfo(mh, name, top, size);
if(jitdump != null){
jitdump.writeFunction(methodInfo);
}
return mh;
storeMethodInfo(name, jitdump);
return Linker.nativeLinker().downcallHandle(mem, desc, options);
}

/**
* Get MemorySegment which is associated with this builder.
*
* @return MemorySegment of this builder
* @throws IllegalStateException when label(s) are not defined even if they are used
*/
public MemorySegment getMemorySegment(){
return getMemorySegment("<unnamed>");
}

/**
* Get MemorySegment which is associated with this builder.
*
* @param name Method name
* @return MemorySegment of this builder
* @throws IllegalStateException when label(s) are not defined even if they are used
*/
public MemorySegment getMemorySegment(String name){
return getMemorySegment(name, null);
}

/**
* Get MemorySegment which is associated with this builder.
*
* @param name Method name
* @param jitdump JitDump instance which should be written.
* @return MemorySegment of this builder
* @throws IllegalStateException when label(s) are not defined even if they are used
*/
public MemorySegment getMemorySegment(String name, JitDump jitdump){
updateTail();
storeMethodInfo(name, jitdump);
return mem;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void testAlignment(){

@Test
public void testMethodInfoString(){
var info = new CodeSegment.MethodInfo(null, "func", 0x1234, 0xff);
var info = new CodeSegment.MethodInfo("func", 0x1234, 0xff);
Assertions.assertEquals("0x1234 0xff func", info.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void testJitDumpHeader(){
@EnabledOnOs(OS.LINUX)
public void testJitDumpFunctionEntry(){
var dumpfile = getDumpFileInstance();
var info = new CodeSegment.MethodInfo(null, "func", 0x1234, 0);
var info = new CodeSegment.MethodInfo("func", 0x1234, 0);

try(var jitdump = JitDump.getInstance(Path.of("/tmp"))){
jitdump.writeFunction(info);
Expand Down

0 comments on commit f3d720d

Please sign in to comment.