gpt4 book ai didi

java - ASM 字节码操作 : Measuring method execution speed

转载 作者:行者123 更新时间:2023-11-30 11:11:36 28 4
gpt4 key购买 nike

我对 ASM 和字节码操作还很陌生。我的任务相当简单:我告诉我的代理要访问什么类和方法,它会测量该方法的执行时间。测量是用 Guava 库的 Stopwatch 类完成的。 ASM 基本上通过在开头启动 Stopwatch 并在方法主体结束时停止它并打印出执行时间来包围方法主体。这对某些方法有效,但对大多数方法都失败了。

这是 ASM 访问目标方法开头的部分。

package com.agent.agentclasses;


import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.commons.LocalVariablesSorter;
import org.objectweb.asm.Type;

public class ModifierMethodWriter extends LocalVariablesSorter /*MethodVisitor*/ {
private int time;
protected ModifierMethodWriter(int api, int access, String desc, MethodVisitor mv) {
super(api, access, desc, mv);
}


@Override
public void visitCode() {
System.out.println("I am @ModifierMethodWriter!");

/*
* Guava - beginning of the method !
*/
time = newLocal(Type.getObjectType("stopwatch"));


super.visitMethodInsn(Opcodes.INVOKESTATIC, "com/google/common/base/Stopwatch", "createStarted", "()Lcom/google/common/base/Stopwatch;", false);

super.visitVarInsn(Opcodes.ASTORE, time);
super.visitCode();




}

在最后添加代码!

package com.agent.agentclasses;

import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.commons.AdviceAdapter;

public class AddCodeBeforeReturn extends AdviceAdapter {
public AddCodeBeforeReturn(int api, MethodVisitor mv,
int acc, String name, String desc){

super(api, mv, acc, name, desc);

}
@Override
protected void onMethodExit(int opcode) {
/*
* Guava - Before return !
*/
super.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
super.visitIntInsn(Opcodes.ALOAD, 2);
super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/google/common/base/Stopwatch", "stop", "()Lcom/google/common/base/Stopwatch;", false);
super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/google/common/base/Stopwatch", "toString", "()Ljava/lang/String;", false);
super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
}
}

这是我要测试我的代理的测试类:它是 HSQL 数据库类,我想测量 selectAll() 方法的执行时间。

package com.agent.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import com.agent.testclasses.*;
public class Database {

private Connection connection = null;
private ResultSet resultSet = null;
private Statement statement = null;
private String createtablestr = " CREATE TABLE IF NOT EXISTS Students(Id int,Name varchar(255));";
private static int id = 1;

public String connect() {
System.out.println("I am in connect!");
try{
Class.forName("org.hsqldb.jdbcDriver");
connection = DriverManager.getConnection("jdbc:hsqldb:file:C:/hsqldb/studentdb", "sa", "");
if (connection == null)
{

return "Error: Connection failed.";
}
statement = connection.createStatement();
statement.executeUpdate(createtablestr);
}catch(Throwable ex){
ex.printStackTrace();
}

return "Connection succesful!";
}

public String insert(Student student){
connect();
try {

String insertStr = "INSERT INTO Students VALUES ('"+ id +"','"+ student.getName() + "')";
statement.executeUpdate(insertStr);
id++;
}catch(Throwable ex){ex.printStackTrace(); return "Error: Data was not written!";}

return "Data written succesfully!";
}

public String selectAll() {


connect();
String outputHTML="";
try{


resultSet = statement.executeQuery("SELECT * FROM Students");


while (resultSet.next())
{
outputHTML+= "<tr>" + "<td>" + resultSet.getString("Id") + "</td>" + "<td>" + resultSet.getString("Name") + "</td>" + "</tr>";
}



resultSet.close();
connection.commit();
connection.close();


}
catch (Throwable e)
{
e.printStackTrace();
}

return outputHTML;
}
}

最后,jvm thorws 的异常:

Exception in thread "main" java.lang.VerifyError: Stack map does not match the one at exception handler 85
Exception Details:
Location:
com/agent/database/Database.connect()Ljava/lang/String; @85: astore_2
Reason:
Type top (current frame, locals[1]) is not assignable to 'stopwatch' (stack map, locals[1])
Current Frame:
bci: @12
flags: { }
locals: { 'com/agent/database/Database', top, 'com/google/common/base/Stopwatch' }
stack: { 'java/lang/Throwable' }
Stackmap Frame:
bci: @85
flags: { }
locals: { 'com/agent/database/Database', 'stopwatch' }
stack: { 'java/lang/Throwable' }
Bytecode:
0x0000000: b800 2a4d b200 3212 34b6 003a 123c b800
0x0000010: 4257 2a12 4412 4612 48b8 004e b500 182a
0x0000020: b400 18c7 0014 1250 b200 3219 02b6 0053
0x0000030: b600 56b6 003a b02a 2ab4 0018 b900 5e01
0x0000040: 00b5 001c 2ab4 001c 2ab4 0020 b900 6402
0x0000050: 0057 a700 084d 2cb6 0067 1269 b200 3219
0x0000060: 02b6 0053 b600 56b6 003a b0
Exception Handler Table:
bci [12, 38] => handler: 85
bci [55, 82] => handler: 85
Stackmap Table:
append_frame(@55,Object[#88])
same_locals_1_stack_item_frame(@85,Object[#44])
same_frame(@90)

at com.agent.testers.tester1.main(tester1.java:15)

在我看来,ASM 在秒表的局部变量表中创建新条目后,jvm 会尝试用我尝试定位的方法(在本例中为 selectAll())的局部变量覆盖它。因此:

Type top (current frame, locals[1]) is not assignable to 'stopwatch' (stack map, locals[1])

我不确定这是否是实际问题,但我需要解决它!最后我的代理应该可以用任何方法插入。您的所有帮助将不胜感激!谢谢。

最佳答案

您正在新分配的“time”局部变量中创建“StopWatch”实例,然后从局部变量槽 2 加载它的实例,您应该在“super.visitCode()”之后添加代码。

另见 ASM FAQ Why do I get the xxx verifier error有关如何使用的更多详细信息 CheckClassAdapter调试您的字节码转换。

关于java - ASM 字节码操作 : Measuring method execution speed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27342044/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com