当前位置:网站首页>Chapter 7 of JVM series -- bytecode execution engine

Chapter 7 of JVM series -- bytecode execution engine

2022-04-23 14:35:00 InfoQ

After translation into local code , You can do a cache operation , Store in method area

[](() Machine code 、 Instructions 、 assembly language



Machine code :

  • All kinds of instructions expressed in binary code , be called
    machine instruction code
    . Start , People use it to write programs , This is machine language .
  • Machine language can be understood and accepted by computers , But it's so different from people's language , It's not easy to be understood and remembered by people , And programming with it is easy to make mistakes .
  • Once the program written with it is input into the computer ,CPU Read directly to run , So compared with programs written in other languages , Fastest execution .
  • Machine instructions and CPU Closely related , So different kinds of CPU The corresponding machine instructions are different .

Instructions :

  • Because the machine code has 0 and 1 A binary sequence of components , The readability is really poor , So people invented instructions .
  • An instruction is a machine code specific to 0 and 1 Sequence , Reduced to the corresponding instruction ( It is usually abbreviated in English , Such as mov,inc etc. ), It's a little more readable
  • Because of different hardware platforms , Perform the same operation , The corresponding machine code may be different , So the same instruction from different hardware platforms ( such as mov), The corresponding machine code may be different .

Instruction set :

  • Different hardware platforms , The instructions they support , There is a difference . So the instructions that each platform supports , Call it the instruction set of the corresponding platform .
  • As is common :x86 Instruction set , The corresponding is x86 Architecture platform 、ARM Instruction set , The corresponding is ARM Architecture platform

assembly language :

  • Because the readability of the instructions is still too poor , So people invented assembly language .
  • In assembly language , Use mnemonics (Mnemonics) An operation code that replaces a machine instruction , Using address symbols (Symbol) Or labelling (Label) The address of an instruction or operand . On different hardware platforms , Assembly languages correspond to different machine language instruction sets , Conversion to machine instructions by assembly process .
  • Because computers only know instruction codes , Therefore, the program written in assembly language must also be translated into machine instruction code , Computers can recognize and execute .

High-level language :

  • In order to make it easier for computer users to program , Later, various high-level computer languages appeared .
  • High level language is better than machine language 、 Assembly language is closer to human language when a computer executes a program written in a high-level language , Still need to interpret and compile the program into the machine's instruction code . The program that completes this process is called an interpreter or compiler .
  • High level languages are not directly translated into   Machine instructions , But translated into assembly language , As mentioned below C and C++

null
C、C++ Source program execution process :

  • The compilation process can be divided into two stages :
    Compile and assemble
    .
  • The build process :
      Is to read the source program ( Character stream ), Analyze the morphology and grammar of it , Convert high-level language instructions into functional equivalent assembly code
  • Assembly process :
      In fact, it refers to the process of translating assembly language code into target machine instructions .

null
Bytecode :

  • Bytecode is an intermediate state ( Middle ) The binary code of ( file ), It's more abstract than machine code , It needs to be translated by a literal translator before it can be machine code
  • Bytecode is mainly used to realize specific software operation and software environment 、 Nothing to do with the hardware environment .
  • Bytecode is implemented through compilers and virtual machines . The compiler compiles the source code into bytecode , Virtual machines on specific platforms translate bytecodes into instructions that can be executed directly .
  • The typical application of bytecode is :Java bytecode

[](() Interpreter



JVM The original intention of designers is simply to satisfy Java Program implementation cross platform features , Therefore, avoid using static compilation to generate local machine instructions directly , Thus, the idea of implementing interpreter executing program with line by line interpretation bytecode is born .

null
The interpreter's real role is a runtime “ translator ”, Put the contents of bytecode file “ translate ” Execute local machine instructions for the corresponding platform . When a bytecode instruction is interpreted and executed , Then, execute the interpretation operation according to the next bytecode instruction to be executed recorded in the program counter .

Interpreter classification :

stay Java In the development history of , There are two sets of interpretation actuators , It's the old
Bytecode interpreter
、 It's widely used now
Template interpreter
.

  • Bytecode interpreter
    The execution of bytecode is simulated by pure software code , Very inefficient .
  • Template interpreter
    Associate each bytecode with a template function , The template function directly generates the machine code when the bytecode is executed , This greatly improves the performance of the interpreter .

stay HotSpot VM in , The interpreter is mainly composed of Interpreter Module and Code Module composition :

  • Interpreter modular :
      The core function of the interpreter is realized
  • Code modular :
      Used to manage HotSpot VM Local machine instructions generated at runtime

Because the interpreter is very simple in design and Implementation , So in addition to Java Beyond language , Many high-level languages are also based on interpreter execution , such as Python、Perl、Ruby etc. . But today ,
Interpreter based execution has fallen into a synonym for inefficiency
.

To solve this problem ,JVM The platform supports a technique called just in time compilation . The purpose of just in time compilation is to avoid functions being interpreted and executed , It's going to be
The whole function body is compiled into machine code , Every time a function is executed , Just execute the compiled machine code
, In this way, the efficiency of execution can be greatly improved .

[](()JIT compiler



Java Code execution classification :

  • The first is
    Compile the source code into a bytecode file
    , And then at run time
    The bytecode file is converted into machine code through the interpreter to execute
  • The second is
    Compile implementation ( Directly compiled into machine code )
    . In order to improve the execution efficiency of modern virtual machine , Will use just in time compilation technology (JIT,Just In Time) Compile the method into machine code and then execute it

HotSpot VM It is one of the representative works of high performance virtual machine on the market . It uses
The coexistence of interpreter and real-time compiler
. stay Java When the virtual machine is running , Interpreters and just in time compilers can work together , Learn from each other , Try to choose the most appropriate way to balance the time it takes to compile native code and the time to directly interpret and execute code .

The problem is coming. :

Some developers will be surprised , since HotSpot VM Built in JIT Compiler , So why do you need to use the interpreter again to “ Drag on ” How about the execution performance of the program ? such as JRockit VM There's no interpreter inside , Bytecodes are all compiled and executed by just in time compilers .

JRockit Virtual machines cut down the interpreter , Just in time compilers . That's because JRockit Only deployed on the server , There is usually time for him to compile instructions ,
It's not very demanding for response
, After the completion of the compiler in time , Will provide better performance

First of all, clear :

When the program starts , The interpreter can work immediately , Save compilation time , Execute now .

The compiler wants to work , Compiling code into native code , It takes a certain amount of execution time . But after compiling it into local code , High execution efficiency .

therefore :

Even though JRockit VM The execution performance of the program in will be very efficient , But the program must take longer to compile when it starts . For server applications , Starting time is not the focus , But for those scenarios that are focused on startup time , Perhaps we need to use the interpreter and the instant compiler architecture to trade off a balance point .

In this mode , When Java When the virtual machine starts ,
The interpreter works first
, You don't have to wait for the instant compiler to complete its compilation before executing , This saves a lot of unnecessary compilation time .
as time goes on , Compilers work , Compile more and more code into local code , Get more execution efficiency .

meanwhile , Explain execution when radical optimization by the compiler doesn't work ,
As a compiler “ Escape door ”
.

HotSpot JVM Way of execution :

When the virtual machine starts ,
The interpreter works first
, You don't have to wait for the instant compiler to complete its compilation before executing , This saves a lot of unnecessary compilation time . also
As the program runs , Just in time compilers come into play , According to the hot spot detection function , Compile valuable bytecodes into local machine instructions , In exchange for higher program execution efficiency .

Different compilers :

  • Java  Linguistic “ Compile time ” It's actually a paragraph “ Not sure ” Operation process , Because it may refer to a
    Front end compiler
    ( Actually called “ The front end of the compiler ” More accurate ) hold .java The document is transformed into .class Documentation process ;
  • It may also refer to the of virtual machines
    Back end runtime compiler
    (JIT compiler ,Just In Time Compiler) The process of converting bytecode into machine code .
  • It may also refer to the use of
    Static precompiler
    (AOT compiler ,Ahead of Time Compiler) Put... Directly .java The process of Compiling Files into local machine code .

Front end compiler :
 Sun Of Javac、Eclipse JDT Incremental compiler in (ECJ).

JIT compiler :
 HotSpot VM Of C1、C2 compiler .

AOT  compiler :
 GNU Compiler for the Java(GCJ)、Excelsior JET.

Graal compiler :

since JDK10 rise ,HotSpot A new just in time compiler has been added :Graal compiler , The compilation effect has been evaluated in just a few years G2 compiler , Future period .

at present , With the lab status tag , You need to use the switch parameter to activate it :-XX:+UnlockExperimentalvMOptions -XX:+UseJVMCICompiler

AOT compiler :

jdk9 Introduced
AOT compiler ( Static precompiler ,Ahead of Time Compiler)
, So-called AOT compile , It's a concept opposite to just in time compilation . We know , Just in time compilation refers to
During the operation of the program
, Convert bytecode to machine code that can run directly on hardware , And deploy to a managed environment . and AOT Compilation refers to ,
Before the program runs
, The process of converting bytecode into machine code .



  • Java 9 The experimental method is introduced AOT Compiler tools aotc. It uses Graal compiler , Put in Java Class file to machine code , And stored in the generated dynamic shared library .>.java -> .class -> ( Use jaotc) -> .so

  • benefits :
     Java The virtual machine load has been precompiled into a binary Library , Can be executed directly . You don't have to wait for the compiler to warm up in time , Reduce Java Application brings benefits to people “ Slow for the first time ”  The bad experience of

  • shortcoming :
      Destroyed  java “  A compilation , Run anywhere ”, Must be for each different hardware ,OS Compile the corresponding distribution package .
    To reduce the Java The dynamics of linking process
    , The loaded code must be known in the compiler . We need to continue to optimize , At first, it only supported Linux X64 java base

Hot spot detection technology :

  • A method that is called many times , Or a method with more internal circulation can be called “ Hot code ”
    , Therefore, it can be passed JIT The compiler compiles into local machine instructions . Because this compilation occurs during the execution of a method , Therefore, it is called stack replacement , Or for short
    OSR(On Stack Replacement) compile .
  • How many times does a method have to be called , Or how many cycles does a loop body need to execute to reach this standard ? There must be a clear threshold ,JIT It's the compiler that will take this “ Hot code ” Compile for local machine instruction execution . It mainly depends on
    Hot spot detection function .
  • at present HotSpot VM The hot spot detection method is based on the counter .
  • Using counter based hotspot detection ,HotSpot V It will be built for every method 2 There are two different types of counters , Respectively
    Method call counter (Invocation Counter) It is used to count the number of calls of the method and the edge returning counter (Back Edge Counter) Used to count the number of cycles executed by the loop body .

Method call counter :

  • This counter is used to count the number of times a method is called , Its default threshold is Client In mode is 1500 Time , stay Server In mode is 10000 Time . Over this threshold , It will trigger JIT compile .
  • This threshold can be determined by virtual machine parameters  
    -XX:CompileThreshold
      To set .
  • When a method is called , We will first check whether the method has been JIT The compiled version , If there is , The compiled local code is preferred for execution . If there is no compiled version , Add... To the call counter value of this method 1, And then determine
    Whether the sum of the value of the method call counter and the back edge counter exceeds the threshold of the method call counter
    . If the threshold has been exceeded , A code compilation request for this method will be submitted to the just in time compiler .

null
Hotspot attenuation :

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231433113805.html