当前位置:网站首页>04 program counter (PC register)

04 program counter (PC register)

2022-06-25 18:22:00 Name is too tangled

1. PC Register Introduce

 Insert picture description here
https://docs.oracle.com/javase/specs/jvms/se8/html/index.html

1.1 PC Register Introduce

 Insert picture description here

  • JVM Program count register in (Program Counter
    Register),Register The name of comes from CPU The register of , Registers store field information about instructions .CPU Only loading data into registers can run .
  • here , It's not a physical register in a broad sense , Maybe translate it into PC Counter ( Or instruction counter ) It will be more appropriate ( Also known as program hook ), And it's not easy to cause some unnecessary misunderstandings .JVM Medium PC Registers are for physics PC An abstract simulation of registers .
  • It's a small piece of memory , I can almost ignore it . It's also the fastest running storage area .
  • stay JVM Specification , Each thread has its own program counter , Threads are private , The life cycle is consistent with the life cycle of the thread .
  • There is only one method in a thread at any time , It's called The current method . The program counter stores what the current thread is executing Java Methodical JVM Instruction address ; perhaps , If it's execution native Method , The value is not specified (undefined).
  • It is an indicator of the flow of program control , Branch 、 loop 、 Jump 、 exception handling 、 Basic functions such as thread recovery rely on this counter
  • The bytecode interpreter works by changing the value of this counter to select the next bytecode instruction to execute
  • It is the only one in Java Nothing is specified in the virtual machine specification OutofMemoryError Area of situation
  • Virtual machine stack 、 Local method area 、PC Register not GC; But the virtual machine stack and local method area may appear OOM
  • The heap and method areas already exist GC There will be OOM

1.2 effect

PC Registers are used to store the address to the next instruction , That is, the instruction code to be executed . The execution engine reads the next instruction .

 Insert picture description here

2. Illustrate with examples

public class PCRegisterTest {
    
    public static void main(String[] args) {
    
        int i = 10;
        int j = 20;
        int k = i+j;

        String s = "abc";
        System.out.println(i);
        System.out.println(k);
    }
}

Decompile instructions :javap -v PCRegisterTest.class

PC Registers store instruction addresses , The execution engine is based on PC The address in the register finds the operation instruction , And execute the instruction

 Insert picture description here  Insert picture description here

3. Two common questions

Use PC register What's the use of storing bytecode instruction addresses ?
or
Why use PC register To record the execution address of the current thread ?

  • because CPU You need to switch threads all the time , Now, after switching back , You need to know where to start after switching back
  • JVM The bytecode interpreter needs to be changed PC Register to determine what kind of bytecode instruction should be executed next
     Insert picture description here

PC Why is the register set to x Thread private ?

  • We all know that the so-called multithreading will only execute one of the methods in a specific period of time ,CPU I'll keep switching tasks , This inevitably leads to frequent interruptions or recoveries , How to make sure there is no difference ?
  • In order to accurately record the current bytecode instruction address that each thread is executing , The best way, of course, is to assign one to each thread PC register , thus , Independent calculations can be performed between threads , So that there will be no mutual interference
  • because CPU Time slice wheel limit , Many threads are executing concurrently , Any definite moment , A core in a processor or multi-core processor , Only one instruction in a thread will be executed
  • This inevitably leads to frequent interruptions or recoveries , How to make sure there is no difference ? After each thread is created , Will produce their own program counter and stack frame , Program counters do not affect each other among threads

4. Time slice

  • CPU Time slice is CPU Time allocated to each program , Each thread is assigned a time period , It's called a time slice
  • On the macro : We can open multiple applications at once , Each program runs parallel , Running at the same time
  • But at the micro level : Since there is only one CPU, Only part of the program's requirements can be processed at a time , How to deal with fairness , One way is to introduce time slices , Each program is executed in turn
     Insert picture description here
原网站

版权声明
本文为[Name is too tangled]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202190531522385.html