android之ION内存管理器(2)-- cache

by JHJ (jianghuijunj211@gmail.com

ION如何实现buffer共享的思路倒是很清晰的,但是深入代码研究,发现ION是依赖于DMA Mapping的,而DMA mapping模块对我而言还是挺复杂的,看这个模块遇到的第一个问题就是cache。既然是DMA mapping,直接将申请的buffer设置为non-cacheable不就行了?这样就可以保证CPU通过DMA读写缓冲区的一致性了。为什么还有Consistent DMA mappings 和 Streaming DMA mappings一说呢?下面先介绍一下Cache的概念。

Cache

Cache写机制:Write-through与Write-back[5]

Cache写机制分为write through和write back两种。

  • Write-through- Write is done synchronously both to the cache and to the backing store.
  • Write-back (or Write-behind) – Writing is done only to the cache. A modified cache block is written back to the store, just before it is replaced.

Write-through(直写模式)在数据更新时,同时写入缓存Cache和后端存储。此模式的优点是操作简单;缺点是因为数据修改需要同时写入存储,数据写入速度较慢。

Write-back(回写模式)在数据更新时只写入缓存Cache。只在数据被替换出缓存时,被修改的缓存数据才会被写到后端存储。此模式的优点是数据写入速度快,因为不需要写存储;缺点是一旦更新后的数据未被写入存储时出现系统掉电的情况,数据将无法找回。

Write-misses写缺失的处理方式

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

  • Write allocate (aka Fetch on write) – Datum at the missed-write location is loaded to cache, followed by a write-hit operation. In this approach, write misses are similar to read-misses.
  • No-write allocate (aka Write-no-allocateWrite around) – Datum at the missed-write location is not loaded to cache, and is written directly to the backing store. In this approach, actually only system reads are being cached.

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则没有帮助。

Cache的两个函数

    --Flush
    把Cache内容写回Memory,当Cache为Write through,不需要Flush
    --Invalidate
    把Cache内容直接丢掉不要。

Cache的使用场合

    当有DMA在使用memory的时候,一般要用到cache的处理。因为DMA在访问memory时是不经过cache的。比较典型的比如在Ethernet,wireless,USB等driver里,DMA会操作descriptors和packet buffers,Driver要做这些处理
    --如果driver使用descripter和packet buffer的地址都是cache的地址,那么
    a).Driver在读descripter里一些状态,有没有收到包时,要对descripter当前结构里的内容做cache invalidate,收到packet后,也要对packet buffer做cache invalidate
    b).Driver在写descripter里一些状态,要发送包时,要对descripter当前结构里的内容做cache flush,发送packet时,也要对packet buffer做cache flush

    --有些driver会对descripter使用uncache 地址,那么上面两种情况里invalidate/flush就不用做了。一般很少会对packet buffer也用uncache地址的,因为对packet内容的处理将会很频繁,使用uncache会很慢。而descripter一般由于结构比较小,如果也使用cache地址的话,做invalidate/flush的时间消耗可能会比uncache的还要多。

下面文字采自于[6]




C B 位的具体含义
0 0 无cache,无写缓冲(strongly-ordered);任何对memory的读写都反映到总线上。对 memory 的操作过程中CPU需要等待。
0 1 无cache,有写缓冲(shareable device);读操作直接反映到总线上;写操作,CPU将数据写入到写缓冲后继续运行,由写缓冲进行写回操作。
1 0 有cache,写通模式(write-through);读操作首先考虑cache hit;写操作时直接将数据写入写缓冲,如果同时出现cache hit,那么也更新cache。
1 1 有cache,写回模式(write-back);读操作首先考虑cache hit;写操作也首先考虑cache hit。

处理流程图

Write-through模式处理流程:


A Write-Through cache with No-Write Allocation

Write-back模式处理流程:


参考文献

1. https://wiki.linaro.org/BenjaminGaignard/ion

2. http://lwn.net/Articles/480055/

3. http://lwn.net/Articles/472984/

4. 《linux设备驱动程序》第三版 JONATHAN CORBET

5. http://witmax.cn/cache-writing-policies.html

6. ARM Architecture Reference Manual, ARMv7-A and ARMv7-R edition  DDI0406C

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值