Reputation: 18552
Suppose I have a 8 byte long pointer
to a memory location in a physical memory in my program. My program is small and can fit entirely in the cache. The program is simple. It simply maps (via mmap() call) an arbitrary memory location from physical memory in /dev/mem
, read and write back to that location via that 8 byte long pointer. However, that location is very far away from where my program resides in physical memory and for that reason, the L1/L2 cache can't cover that address.
According to the article Getting Physical With Memory (written by Gustavo Duarte, I can't link), a memory write only occurs when a cacheline that has that memory location is ready to be written to RAM:
Typically kernels treat all RAM memory as write-back, which yields the best performance. In write-back mode the unit of memory access is the cache line, 64 bytes in the Core 2. If a program reads a single byte in memory, the processor loads the whole cache line that contains that byte into the L2 and L1 caches. When a program writes to memory, the processor only modifies the line in the cache, but does not update main memory. Later, when it becomes necessary to post the modified line to the bus, the whole cache line is written at once
But what if targeted memory location is not in the cache (as I described above), will it be written immediately?
Upvotes: 2
Views: 721
Reputation: 1136
The reference you quote explains what will happen in this circumstance - if the memory address has not been accessed recently, and is not already loaded into a cache line, then your read to that location will result in the contents of that memory location - and likely surrounding locations - being read into a cache line. Once in the cache writes to the relevant memory address will take place to this cache line, until at some point main memory is updated with the modified cache line.
Upvotes: 1