我的环境是x86、Linux和内核空间。
我想知道是否有任何方法可以刷新内存区域的缓存而不使缓存失效?
我已经查看了 clflush_cache_range(),但该方法使用了 clflushopt 指令,我相信这会使缓存失效。
最佳答案
确实在某些情况下,您希望确保数据写回内存,以防核心崩溃(可靠性),或者如果您正在处理事务并且拥有某种版本化内存。在这些情况下,您希望通过将数据同步到内存来确保数据的一致性和持久性,但您不希望遭受刷新整个缓存甚至只是您正在处理的行的惩罚,因为您需要继续使用它。
在 x86 中,对于这种情况,您应该检查 CLWB(缓存行 WB)和 PCOMMIT。两者都是最近发布的,因此大多数现有产品可能尚不支持它们(我已经看到了一些可能对 Skylake 的引用,但没有正式的内容)。这是一个很好的总结,包括一些性能分析 - http://danluu.com/clwb-pcommit/
中国劳工及福利局:
CLWB acts like CLFLUSH, in that it forces the data to get written out to memory. However, it doesn’t force the cache to throw away the data, which makes future reads and writes a lot faster. Also, CLFLUSH is only ordered with respect to MFENCE, but CLWB is also ordered with respect to SFENCE.
和 PCOMMIT:
PCOMMIT is applied to entire memory ranges and ensures that everything in the memory range is committed to persistent storage.
这两个不同之处在于它们所使用的内存和存储类型,以及其他一些微妙之处,例如它们相对于围栏或其他存储的排序方式。完整的描述在手册中。
关于linux - 刷新CPU缓存而不使缓存失效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32361509/