译:内存屏障:软件hacker的硬件视角

    410
    最后修改于

    Memory Barriers: a Hardware View for Software Hackers

    (PDF) Memory Barriers: a Hardware View for Software Hackers (researchgate.net)
    hwViewForSwHackers.pdf (puppetmastertrading.com)

    So what possessed CPU designers to cause them to inflict memory barriers on poor unsuspecting SMP software designers?

    In short, because reordering memory references allows much better performance, and so memory barriers are needed to force ordering in things like synchronization primitives whose correct operation depends on ordered memory references.

    Getting a more detailed answer to this question requires a good understanding of how CPU caches work, and especially what is required to make caches really work well. The following sections:

    1. present the structure of a cache,
    2. describe how cache-coherency protocols ensure that CPUs agree on the value of each location in memory, and, finally,
    3. outline how store buffers and invalidate queues help caches and cache-coherency protocols achieve high performance.

    We will see that memory barriers are a necessary evil that is required to enable good performance and scalability, an evil that stems from the fact that CPUs are orders of magnitude faster than are both the interconnects between them and the memory they are attempting to access.

    缓存结构 Cache Structure#

    Modern CPUs are much faster than are modern memory systems. A 2006 CPU might be capable of executing ten instructions per nanosecond, but will require many tens of nanoseconds to fetch a data item from main memory. This disparity in speed — more than two orders of magnitude — has resulted in the multimegabyte caches found on modern CPUs. These caches are associated with the CPUs as shown in Figure 1, and can typically be accessed in a few cycles(注 1)

    注 1:It is standard practice to use multiple levels of cache, with a small level-one cache close to the CPU with single-cycle access time, and a larger level-two cache with a longer access time, perhaps roughly ten clock cycles. Higher-performance CPUs often have three or even four levels of cache.

    Data flows among the CPUs’ caches and memory in fixed-length blocks called “cache lines”, which are normally a power of two in size, ranging from 16 to 256 bytes. When a given data item is first accessed by

    • 🥳0
    • 👍0
    • 💩0
    • 🤩0