Linux Cache Write-Back and Write-Through

Cache写机制:Write-through与Write-back

通常有三种方法:

1.write through:CPU向cache写入数据时,同时向memory(后端存储)也写一份,使cache

和memory的数据保持一致。优点是简单,缺点是每次都要访问memory,

速度比较慢。

2. post write:CPU更新cache数据时,把更新的数据写入到一个更新缓冲器,在合适的

时候才对memory(后端存储)进行更新。这样可以提高cache访问速度,

但是,在数据连续被更新两次以上的时候,缓冲区将不够使用,被迫同

时更新memory(后端存储)。

3. write back:cpu更新cache时,只是把更新的cache区标记一下,并不同步更新memory

(后端存储)。只是在cache区要被新进入的数据取代时,才更新

memory(后端存储)。这样做的原因是考虑到很多时候cache存入的是中间结

果,没有必要同步更新memory(后端存储)。优点是CPU执行的效率提高,

缺点是实现起来技术比较复杂。

Write-through与Write-back和买卖东西相似,Write-Through就相当于你亲自去买东西,

你买到什么就可以亲手拿到;而Write-Back就和中介差不多,你给了中介钱,然后它告

诉你说你的东西买到了,然后就相信拿到这个东西了,但是要是出现特殊情况中介跑了,

你再去检查,东西原来没有真正到手。


对于写操作,存在写入缓存缺失数据的情况,这时有两种处理方式:

Write allocate:方式将写入位置读入缓存,然后采用write-hit

(缓存命中写入)操作。写缺失操作与读缺失操

作类似。

No-write allocate:方式并不将写入位置读入缓存,而是直接将

数据写入存储。这种方式下,只有读操作会

被缓存。

无论是Write-through还是Write-back都可以使用写缺失的两种方式之一。

只是通常Write-back采用Write allocate方式,而Write-through采用No-write allocate方式;因为多次写入同一缓存时,Write allocate配合Write-back

可以提升性能;而对于Write-through则没有帮助。

write-through

Write through is a storage method in which data is written into the cache and the corresponding main memory location at the same time. The cached data allows for fast retrieval on demand, while the same data in main memory ensures that nothing will get lost if a crash, power failure, or other system disruption occurs.


分享到:


相關文章: