在笔者的nvdimm运行vdbench和自带的测试程序发现:不同的cache模式对系统性能的影响巨大,下面的数据生动地说明了这一点:


write-through模式:

Writes took 47.227898 Megabytes per second

Reads took 1873.360718 Megabytes per second


write-combing模式:

Writes took 1747.500977 Megabytes per second

Reads took 96.834496 Megabytes per second


write-back模式:

Writes took 1581.937622 Megabytes per second

Reads took 1854.365479 Megabytes per second


由上可见不同的cache模式对系统性能影响非常巨大,那么实际应用中就该选用性能最好的cache模式么?答案是否定的。

对应数据完整性要求高的写应用,比如掉电不丢数据的场合:必须用uncache模式,不管是最强的un-cache模式,还是write-combing/write-through,都要保证文件系统写返回前数据一定落到物理内存;


在对数据一致性要求高的情况些,如果对读性能的要求高于对写性能的要求,可使用write-through模式,但要注意这时候可以理解写 uncache, 只有读cache

   •    Write-through (WT) — Writes and reads to and from system memory are cached. Reads come from cache lines on cache hits; read misses cause cache fills. Speculative reads are allowed. All writes are written to a cache line (when possible) and through to system memory. 


对写性能要求高于读性能的应用,可使用write-combing模式,但需要注意数据先写到write-coming buffer,因此还需要sfence/lock/interrupt/uncache等指令把write-coming buffer的数据刷回去。这种模式可以认为写有合并,读之时猜测。所以这里写比读的性能好不少:

 

  Write Combining (WC) — System memory locations are not cached (as with uncacheable memory) and coherency is not enforced by the processor’s bus coherency protocol. Speculative reads are allowed. Writes may be delayed and combined in the write combining buffer (WC buffer) to reduce memory accesses. If the WC buffer is partially filled, the writes may be delayed until the next occurrence of a serializing event; such as, an SFENCE or MFENCE instruction, CPUID execution, a read or write to uncached memory, an interrupt occurrence, or a LOCK instruction execution.


对数据一致性没有太高的要求,write-back模式是最好的选择。