Is it possible to get an estimation of the number of instructions executed by Dalvik bytecode through analyzing Java bytecode

StackOverflow https://stackoverflow.com/questions/22579661

  •  19-06-2023
  •  | 
  •  

Question

I am building a static code analyzer that estimates the number of instructions executed by a Java method in an Android application. I want to know how resource-intensive is the method for the device. Thus, I am interested to know the number of instructions executed when running Dalvik bytecode. Currently, I am using the following command to get information about the instructions to be executed.

$ dexdump -d Class.dex | less

I know that the Dalvik bytecode is created by wrapping Java bytecode, like this

public class Foo{

  public static void main (String[] args){

  System.out.println("Here we do smt");

  }

}

$ javac Foo.java

$ dx --dex --output=foo.jar Foo.class

Since the input of my static analyzer is the Java source code of the application. I thought at first to convert each source file to dex, and then to execute the dexdump command to the information I need. However, I'd like to avoid converting to dex files, and instead to use the following command directly on the source code. Ideally, I am planning to use an existing tool for Java code analysis.

$ javap -c Classes

But, since Java VM is stack-based, and the Dalvik VM is register-based. I am bit skeptical about this strategy. I think that even though, I am comparing the same source code, Dalvik bytecode requires less number of instructions than Java bytecode. Thus, probably I'll lose some accuracy in my estimation. Am I right? or I am missing something here? Any thoughts on this or suggestions about any other method, I'll be appreciate it. Thanks

Was it helpful?

Solution

According to the answer at Does dx conversion to dex include verification of original class files?, some but not much optimization takes place, but I agree with the recommendation to check out the dx source for more information about the actual conversion.

If you find the conversion is mostly one-to-one, then your current strategy of counting Java bytecode instructions may give a decent estimate of Dalvik instruction count, but keep in mind there are many other factors beyond simply instruction count that affect performance (including, but definitely not limited to, values determined at run-time e.g. user input, cache performance, instruction execution time, pipeline characteristics, use of non-CPU resources, etc.) and this won't really give you a reasonable estimate of how "resource intensive" the code is (consider the simple example of a tight infinite while loop vs. a higher-instruction-count loop that contains a 1 second sleep on each iteration).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top