Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the cpu decide which data it puts in what memory (ram, cache, registers)?

When the cpu is executing a program, does it move all data through the memory pipeline? Then any piece of data would be moved from ram->cache->registers so all data that's executed goes in the cpu registers at some point. Or does it somehow select the code it puts in those faster memory types, or can you as a programmer select specific code you want to keep in, for example, the cache for optimization?

like image 394
jtht Avatar asked Jan 08 '15 12:01

jtht


People also ask

How does cache memory work in a CPU?

Rather than access instructions and data from RAM one at a time, whole blocks of instructions and data that are in use by the CPU are copied into the cache memory, along with the associated memory addresses. If the CPU needs to access a memory address it first checks the cache memory to see if there is a match.

How does the CPU affect the memory of a computer?

The CPU is once again responsible for that. It sends a command to the drive to send the data that it needs over to the RAM. While the data is being transferred to the RAM, the CPU does other tasks. This is known under the name of “ direct memory access ” or DMA for short. All modern computers use this feature.

Why does the CPU control unit check the cache?

The CPU control unit automatically checks cache for instructions before requesting data from RAM. This saves fetching the instructions and data repeatedly from RAM – a relatively slow process which might otherwise keep the CPU waiting. Transfers to and from cache take less time than transfers to and from RAM.

How does random access memory (RAM) work?

Random-access memory, or commonly known as RAM, does what its name suggests. It temporarily stores the data that the CPU needs to access. The Central processing unit, or CPU, is the brain of your computer. So, how do CPU and RAM work together? The RAM stores the data that your CPU retrieves and processes it.


1 Answers

The answer to this question is an entire course in itself! A very brief summary of what (usually) happens is that:

  1. You, the programmer, specify what goes in RAM. Well, the compiler does it on your behalf, but you're in control of this by how you declare your variables.
  2. Whenever your code accesses a variable the CPU's MMU will check if the value is in the cache and if it is not, then it will fetch the 'line' that contains the variable from RAM into the cache. Some CPU instruction sets may allow you to prevent it from doing so (causing a stall) for specific low-frequecy operations, but it requires very low-level code to do so. When you update a value, the MMU will perform a 'cache flush' operation, committing the cached memory to RAM. Again, you can affect how and when this happens by low-level code. It will also depend on the MMU configuration such as whether the cache is write-through, etc.
  3. If you are going to do any kind of operation on the value that will require it being used by an ALU (arithmetic Logic Unit) or similar, then it will be loaded into an appropriate register from the cache. Which register will depend on the instruction the compiler generated.

Some CPUs support Dynamic Memory Access (DMA), which provides a shortcut for operations that do not really require the CPU to be involved. These include memory-to-memory copies and the transfer of data between memory and memory-mapped peripheral control blocks (such as UARTs and other I/O blocks). These will cause data to be moved, read or written in RAM without actually affecting the CPU core at all.

At a higher level, some operating systems that support multiple processes will save the RAM allocated to the current process to the hard disk when the process is swapped out, and load it back in again from the disk when the process runs again. (This is why you may find 'Page Files' on your C: drive and the options to limit their size.) This allows all of the running processes to utilise most of the available RAM, even though they can't actually share it all simultaneously. Paging is yet another subject worthy of a course on its own. (Thanks to Leeor for mentioning this.)

like image 53
3 revs Avatar answered Oct 01 '22 00:10

3 revs