Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see JIT-compiled code in JVM?

Is there some way to see the native code produces by the JIT in a JVM?

like image 904
alsor.net Avatar asked Oct 01 '09 11:10

alsor.net


People also ask

Where is JIT compiled code stored?

Answer 2: The JVM keeps the JITCed code for each method in C heap, linked to via tables in the internal representation of the class object. The code is only deleted when the JVM ends or in the rare case that the class is "unloaded".

Is JIT present in JVM?

JIT is one of the components of JVM. JVM compiles complete byte code to machine code. JIT compiles only the reusable byte code to machine code.

How do I find my JVM code?

If you use the OpenJDK JVM, then you can get the source code from here. If you use the Kaffe JVM, you can get the source from here. If you use the Sun JVM version 6 or later, then you can get the source from here. If you use a Sun JVM earlier than 6, then you can often get the source under an academic license.

How do you read a JIT?

The JIT compiler is enabled by default, and is activated when a Java method is called. The JIT compiler compiles the bytecode of that method into native machine code, compiling it "just in time" to run. When a method has been compiled, the JVM calls the compiled code of that method directly instead of interpreting it.


2 Answers

General usage

As explained by other answers, you can run with the following JVM options:

-XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly 

Filter on a specific method

You can also filter on a specific method with the following syntax:

-XX:+UnlockDiagnosticVMOptions -XX:CompileCommand=print,*MyClass.myMethod 

Notes:

  • you might need to put the second argument within quotes depending on OS etc.
  • if the method gets inlined, you could miss some optimisations

How to: Install the required libraries on Windows

If you are running Windows, this page has instructions on how to build and install hsdis-amd64.dll and hsdis-i386.dll which are required to make it work. We copy below and extend the content of that page* for reference:


Where to get prebuilt binaries

You can download prebuilt binaries for Windows from the fcml project

  • hsdis-amd64.dll
  • hsdis-i386.dll

How to build hsdis-amd64.dll and hsdis-i386.dll on Windows

This version of the guide was prepared on Windows 8.1 64bit using 64-bit Cygwin and producing hsdis-amd64.dll

  1. Install Cygwin. At the Select Packages screen, add the following packages (by expanding the Devel category, then clicking once on the Skip label next to each package name):

    • make
    • mingw64-x86_64-gcc-core (only needed for hsdis-amd64.dll)
    • mingw64-i686-gcc-core (only needed for hsdis-i386.dll)
    • diffutils (in Utils category)
  2. Run the Cygwin Terminal. This can be done using the Desktop or Start Menu icon created by the installer, and will create your Cygwin home directory (C:\cygwin\home\<username>\ or C:\cygwin64\home\<username>\ by default).

  3. Download the latest GNU binutils source package and extract its contents to your Cygwin home directory. At the time of writing, the latest package is binutils-2.25.tar.bz2. This should result in a directory named binutils-2.25 (or whatever the latest version is) in your Cygwin home directory.
  4. Download the OpenJDK source by going to the JDK 8 Updates repository, selecting the tag corresponding to your installed JRE version, and clicking bz2. Extract the hsdis directory (found in src\share\tools) to your Cygwin home directory.
  5. In the Cygwin Terminal, enter cd ~/hsdis.
  6. To build hsdis-amd64.dll, enter

    make OS=Linux MINGW=x86_64-w64-mingw32 'AR=$(MINGW)-ar' BINUTILS=~/binutils-2.25

    To build hsdis-i386.dll, enter

    make OS=Linux MINGW=i686-w64-mingw32 'AR=$(MINGW)-ar' BINUTILS=~/binutils-2.25

    In either case, replace 2.25 with the binutils version you downloaded. OS=Linux is necessary because, although Cygwin is a Linux-like environment, the hsdis makefile fails to recognize it as such.

  7. The build will fail with messages ./chew: No such file or directory and gcc: command not found. Edit <Cygwin home directory>\hsdis\build\Linux-amd64\bfd\Makefile in a text editor like Wordpad or Notepad++ to change SUBDIRS = doc po (line 342, if using binutils 2.25) to SUBDIRS = po. Re-run the previous command.

The DLL can now be installed by copying it from hsdis\build\Linux-amd64 or hsdis\build\Linux-i586 to your JRE's bin\server or bin\client directory. You can find all such directories on your system by searching for java.dll.

Bonus tip: if you prefer Intel ASM syntax to AT&T, specify -XX:PrintAssemblyOptions=intel alongside any other PrintAssembly options you use.

*page license is Creative Commons

like image 134
assylias Avatar answered Sep 18 '22 11:09

assylias


Assuming you're using the Sun Hotspot JVM (i.e. the one provided on java.com by Oracle), you can add the flag

-XX:+PrintOptoAssembly

when running your code. This will print out the optimized code generated by the JIT compiler and leaves out the rest.

If you want see the entire bytecode, including the unoptimized parts, add

-XX:CompileThreshold=#

when you're running your code.

You can read more about this command and the functionality of JIT in general here.

like image 41
Falaina Avatar answered Sep 21 '22 11:09

Falaina