Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is a CPU cache line flushed to memory after a write?

I'm working in C# and want to avoid unsafe code if possible. If I have an object or array that is sized to fill a cache line and I want to write to every field of the object or index of the array, will the CPU wait for all the writes to occur before flushing the written-to line or will it flush early when only one or a few of the writes have occurred?

If I want to have the flush happen only once all writes to the line have occurred, should I do all the writes at the end of a routine in quick succession? I know that CPUs and cache coherence protocols may differ on this, I'm looking for a generally true rule of thumb answer.

like image 588
hatch22 Avatar asked Feb 22 '13 16:02

hatch22


People also ask

When should you flush cache?

When the amount of data contained in caches exceeds certain levels, people are suggested to flush the cache. Cache flushing will clear that information in order to help smoothen and improve computer speed.

What is flushing a cache line?

Purpose. Writes a line of modified data from the data cache to main memory, or invalidates cached instructions or unmodified data.

Why do you flush cache?

There's a lot of reasons to regularly flush your DNS cache. It can help prevent phishing schemes or other attacks on your computer, direct you to the most up-to-date versions of your most frequently visited sites, restore your internet connection, and keep your data private.

What is CPU flush?

A pipeline flush, is also known as a pipeline break or a pipeline stall. It's a procedure enacted by a CPU when it cannot ensure that it will correctly process its instruction pipeline in the next clock cycle.


2 Answers

will the CPU wait for all the writes to occur before flushing the written-to line or will it flush early when only one or a few of the writes have occurred?

The CPU might flush the line early, but only if that set is under high pressure from other access that belong in the cache. That's unlikely. Caches are structured to help avoid prematurely flushing recently accessed data.

should I do all the writes at the end of a routine in quick succession?

In general yes. Temporal locality is important, meaning caches perform best when accesses are grouped closely in time. Other tricks may apply as well. For example, you can try to 'warm' the cache line by doing dummy write to your structure in advance of the required writes. This allows some memory level parallelism in which the core loads the cache line while intervening code executes. By the time you perform the real writes, chances are better that the cache line will be ready in the L1.

In general, be very cautious about unnatural acts in your code to improve cache performance. Caches do a pretty good job just left to themselves. You should always measure performance before and after any change. What you think may be an improvement may actually hurt. If your program is multi-threaded, another big can of worms comes into play with cache contention between cores.

like image 93
srking Avatar answered Nov 09 '22 22:11

srking


Naturally the CPU will try to make as few memory accesses as possible, but that doesn't neccessarily mean that "your" block of memory will be kept in the cache as long as you want it to.

Normally the memory block would be read once and written once, but there is no guarantee for that. Something may happen that interrupts your code, and the system may decide to flush that cache line to make room for something else. The entire memory area may even be removed from memory entirely and flushed to disk, so that when your code contines it will cause a page fault that will load the memory in again.

Having your writes closer in time will of course make it more likely that the memory will be kept in the cache for the duration of that operation.

like image 20
Guffa Avatar answered Nov 10 '22 00:11

Guffa