当前位置：网站首页>11. Execution engine
11. Execution engine
2020-12-07 17:18:50 【Irving the procedural ape】
1. Executive engine overview and basic execution process
In the previous post , Have been to Class loading , And the runtime data area of the loaded class , however our java The program is running in jvm Medium , Our bytecode instructions are not directly recognized by the operating system , This is the time The executive engine is on the scene .
- The execution engine is Java One of the components of the virtual machine core .“ virtual machine ” It's a comparison with “ The physical machine ” The concept of , Both machines have code execution capabilities , The difference is that the execution engine of the physical machine is built directly on the processor 、 cache 、 Instruction set and operating system level , The execution engine of virtual machine is realized by software itself , Therefore, the architecture of instruction set and execution engine can be customized without physical constraints , Be able to execute instruction set formats that are not directly supported by hardware .
- JVM The main task of bytecode is to load bytecode into it , But bytecode can't run directly on the operating system , Because bytecode instructions are not equivalent to local machine instructions , It contains only a few things that can be JVM Bytecode instruction identified 、 The symbol table , And other supporting information .
- that , If you want to make a Java The program runs , Execution engine （Execution Engine） The task is to interpret bytecode instructions / It can only be compiled to the local machine instructions on the corresponding platform . Simply speaking ,JVM The execution engine in acts as a translator to translate a high-level language into a machine language .
1.2 The basic execution process
We learned earlier that Unique to threads Program counter , Write down a line Address of instruction , Provided to the execution engine to execute ,
- What kind of bytecode instructions the execution engine needs to execute depends entirely on PC register .
- Every time after an instruction operation has been executed ,PC The register will update the address of the next instruction to be executed .
- Of course, the method is in the process of execution , It is possible for the execution engine to accurately locate the object reference stored in the local variable table Java Object instance information in the heap , And locate the type information of the target object through the metadata pointer in the object header .
- From the outside , be-all Java The execution engine input of the virtual machine 、 Handle 、 The output is consistent ： The input is a bytecode binary stream , The processing is bytecode parsing execution 、 The equivalent of just in time compilation , The output is the execution process .
Sketch Map :
2. java The process of code compilation and execution
2.1 Java Code interpretation and execution and Compile implementation
Before most of the program code is converted into the object code of the physical machine or the instruction set executed by the virtual machine , You need to go through the steps in the figure below ：
- The orange part in the front is the process of compiling and generating bytecode file （javac Instructions java File transfer class file ）, and JVM irrelevant
- Back green （ Explain to perform ） And blue （ Just in time compilation ） It's just JVM The process of execution , Corresponding to the interpreter and compiler in the execution engine
What is an interpreter ？ What is? JIT compiler ？
- ** Interpreter : **
When Java The virtual machine will start according to Predefined specifications The bytecode is executed line by line , Put the contents of each bytecode file “ translate ” Execute local machine instructions for the corresponding platform .
- JIT（Just In Time Compiler） compiler
The virtual machine will source code directly compile Become a machine language related to the local machine platform .
In the beginning java when , We call it java The language is Semi compiled and semi interpreted language , This is because jvm in That's what the execution engine does ,
- JDK1.0 Time , take Java Language orientation is “ Explain to perform ” It's more accurate . later ,Java We also developed compilers that can generate native code directly .
- Now? JVM In execution Java Code time , Usually, interpretation execution and compilation execution are combined .
- After translating bytecode into local code , You can do a cache operation , Stored in the method area JIT In the code cache
jvm Execution process summary chart :
2.2 Machine code , Instructions , assembly language
- All kinds of instructions expressed in binary code , It's called machine code ( It's just a bunch of 010101, Can be directly cpu perform ). 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 .
Because the machine code is made by 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
An instruction may represent a large series of machine codes with continuous functions
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 .
Different hardware platforms , The instructions they support , There is a difference . So the instructions that each platform supports , Call it platform specific Instruction set .
- x86 Instruction set , The corresponding is x86 Architecture platform
- ARM Instruction set , The corresponding is ARM Architecture platform
- 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 machine code , So the program written in assembly language must be translated （ assembly ） Machine code , Computers can recognize and execute .
- In order to make it easier for computer users to program , Later, various high-level computer languages appeared .(C,C++,JAVA)
- 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
- First, you need to go through the compilation phase , Translate high level language into assembly language
- And then through the assembly phase , Translate the generated assembly language into machine instructions
C The flow of language :
- 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 .
JAVA The language is similar , The first thing that will be written is JAVA The file is compiled into a bytecode file ( Also known as front-end compilation , Half of the compilation is done , reduce jvm The workload of compiling ), This is the bytecode instruction that we often see before , Here's a description of bytecode :
- Bytecode is an intermediate state （ Middle ） The binary code of （ file ）, It's more abstract than machine code , You need to perform engine translation to become 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 .
3.1 Why there's a bytecode middle code
I said before , jvm The execution engine in can put Bytecode Instructions for interpretation and execution , That's the same thing , Why? The interpreter cannot execute directly java Source code ,
Theoretically, it can
The picture above shows , java Language is coming jvm The first step , Will also java The source code has been compiled , Become class Bytecode , This step will be greatly saved jvm The job of ,javac take java The language is compiled into a lower level instruction form , And it is optimized in the process of compiling ( character string , Constant optimization ),
3.2 The function of the 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 .
- 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 , And then according to PC The next bytecode instruction recorded in the register that needs to be executed performs an interpretation operation .
3.3 The classification of interpreters
- stay Java In the development history of , There are two sets of interpretation actuators , That is, the old bytecode interpreter and the template interpreter now widely used .
- Bytecode interpreter simulates bytecode execution through pure software code , Very inefficient .
- The template interpreter associates 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 .(Map Key value pair ?)
Now in the 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
3.4 The status quo of the interpreter
- 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 , And often by some C/C++ What programmers make fun of .
- 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 the whole machine , Every time a function is executed , Just execute the compiled machine code , In this way, the efficiency of execution can be greatly improved .
- But anyway , Interpreter based execution mode still makes an indelible contribution to the development of intermediate language .
Now , The interpreter is also an integral part of the execution engine , Even though it compiles instantly There are many advantages , But it also has its disadvantages , Need to be supplemented by the interpreter , Two together , It is the best ( Yida makes money )
4. JIT compiler
4.1 . summary
- HotSpot VM It is one of the representative works of high performance virtual machine on the market . It uses an interpreter and just in time compiler architecture .
- 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 .
- In today's ,Java The performance of the program has been completely changed , Have reached the point where you can and C/C++ The program is higher and lower .
What then? We have more advanced JIT compiler , And keep the interpreter ?
- 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
- When the program starts , The interpreter can work immediately , Fast response , 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 .
- 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 .
- When an interpreter coexists with an instant compiler , stay 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 ”.
- 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 <.........
本文为[Irving the procedural ape]所创，转载请带上原文链接，感谢
- C++ 数字、string和char*的转换
- Won the CKA + CKS certificate with the highest gold content in kubernetes in 31 days!
- C + + number, string and char * conversion
- C + + Learning -- capacity() and resize() in C + +
- C + + Learning -- about code performance optimization
C + + programming experience (6): using C + + style type conversion
Latest party and government work report ppt - Park ppt
Online ID number extraction birthday tool
Field pointer? Dangling pointer? This article will help you understand!
GVRP of hcna Routing & Switching
- LeetCode 91. 解码方法
- Seq2seq implements chat robot
- [chat robot] principle of seq2seq model
- Leetcode 91. Decoding method
- HCNA Routing＆Switching之GVRP
- GVRP of hcna Routing & Switching
- HDU7016 Random Walk 2
- [Code+＃1]Yazid 的新生舞会
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- HDU7016 Random Walk 2
- [code + 1] Yazid's freshman ball
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- Qt Creator 自动补齐变慢的解决
- HALCON 20.11：如何处理标定助手品质问题
- HALCON 20.11：标定助手使用注意事项
- Solution of QT creator's automatic replenishment slowing down
- Halcon 20.11: how to deal with the quality problem of calibration assistant
- Halcon 20.11: precautions for use of calibration assistant
- "Top ten scientific and technological issues" announced| Young scientists 50 ² forum
- Reverse linked list
- JS data type
- Remember the bug encountered in reading and writing a file
- Singleton mode
- 在这个 N 多编程语言争霸的世界，C++ 究竟还有没有未来？
- In this world of N programming languages, is there a future for C + +?
- js Promise
- js 数组方法 回顾
- ES6 template characters
- js Promise
- JS array method review
- 【Golang】️走进 Go 语言️ 第一课 Hello World
- [golang] go into go language lesson 1 Hello World