A brief description of the CPU multi-core concurrent cache architecture

First, let’s talk about how the CPU works

Early CPUs were low-performance, single-core, and interacted directly with main memory, reading and writing data from it. As CPUs were upgraded, the match between main memory and CPUs became worse and worse, so caching was introduced.

The CPU reads the data and first checks the registers to see if there is any data it wants.

If the register is not available, it is looked up in the level 1 cache.

If the level 1 cache is not available, look it up in the level 2 cache.

There’s nothing at level two, and then you look up the data in main memory.

Note:One thing to note when you finally interact with main memory is that there are multiple CPUs that need to fetch data from main memory. CPU1 fetches data, and CPU2 fetches data, so which data should be stored? So the cache consistency protocol is introducedMESI. I won’t go into that here. In short, this protocol can ensure the consistency of data.

JVM memory model

Java automatically divides the data into these regions when it runs the program. Blue represents the shared data area of the thread, and green is the private area of each thread.

  • Methods area:

    Used to store compiled data. Include:Class information, constants, static variables, and run-time constant poolsStores compiled symbol references and compile-generated literal variables.
  • The JVM heap

    The largest chunk of JVM memory, mainly used forAllocates memory for an object instanceAnd he isGarbage collection managementThe main area.
  • Program counter

    Holds the next operation of the current threadBytecode line numberThe bytecode interpreter reads this line number as it works and decides to branch, loop, jump, exception handling, and so on.
  • The virtual stack appears with the creation of threads, where each method executes and records its arguments, Pointers, return values, and so on.
  • This part of the local method stack is mainly related to Native methods used by the virtual machine.

    An overview of the Java multithreaded memory model (important)

    The model is an abstraction, a specification. It specifies the specification for threads to access variables from memory.

    So first of all Java will give youEach thread allocates memoryEach thread that wants to access a variable in main memory (a shared variable) must drop this variableA copy ofTo your own working memory, make changes to it and put it back into main memory.

    See the figure for detailed access details

  • To ensure that changes to shared variables are consistent, each thread must Lock the variable after reading the data and then Unlock it after reading it.
  • Read

    Get the data in main memory, take itTo the memory where the thread is working.
  • Load

    The data to be fetched in memory while the thread is workingcopyA.
  • If the thread counter reads the bytecode operand associated with the changed data, it passes the resulting copy of the data to the execution engine.
  • Assign

    In the virtual opportunity to assign a value to the bytecode instruction, the execution engine will pass the valueVariable assigned to workspace memory.
  • Store

    Put variables in working memoryTo pass toThe main memory.
  • Write

    To get the working variablewriteInto main memory.

Picture source: Liu Naiyuan’s handout