- 杂志
 搜 索: 
 论坛 » ARM开发 » CPU学习 (Cache Coherence)
广告招商
版主: l0p0c , twentyone , sunshine0606
 发表新贴 
 发表回复 
 CPU学习 (Cache Coherence)  发表于 2008-07-17 16:59:34 [您是本帖的第200位阅读者]     [1楼] 
呢称:8wy103405
加我为好友
大多数现代CPU都One-die了L1和L2Cache。对于L1 Cache,大多是write though的;L2 Cache则是write BACK的,不会立即写回memory,这就会导致Cache和Memory的内容的不一致;另外,对于MP(MULTI Processors)的环境,由于Cache是CPU私有的,不同CPU的Cache的内容也存在不一致的问题,因此很多MP的的计算架构,不论是ccNUMA还是SMP都实现了Cache Coherence的机制,即不同CPU的Cache一致性机制。

  Cache Coherence的一种实现是通过Cache-snooping协议,每个CPU通过对Bus的Snoop实现对其它CPU读写Cache的监控:

  首先,Cache line是Cache和Memory之间数据传输的最小单元。

  1. 当CPU1要写Cache时,其它CPU就会检查自己Cache中对应的Cache line,如果是dirty的,就write BACK到Memory,并且会将CPU1的相关Cache line刷新;如果不是dirty的,就Invalidate该Cache line.

  2. 当CPU1要读Cache时,其它CPU就会将自己Cache中对应的Cache line中标记为dirty的部分write BACK到Memory,并且会将CPU1的相关Cache line刷新。

  所以,提高CPU的Cache hit RATE,减少Cache和Memory之间的数据传输,将会提高系统的性能。

  因此,在程序和二进制对象的内存分配中保持Cache line aligned就十分重要,如果不保证Cache line对齐,出现多个CPU中并行运行的进程或者线程同时读写同一个Cache line的情况的概率就会很大。这时CPU的Cache和Memory之间会反复出现Write BACK和Refresh情况,这种情形就叫做Cache thrashing。

  为了有效的避免Cache thrashing,通常有以下两种途径:

  1. 对于Heap的分配,很多系统在malloc调用中实现了强制的alignment.
2. 对于Stack的分配,很多编译器提供了Stack aligned的选项。

  当然,如果在编译器指定了Stack aligned,程序的尺寸将会变大,会占用更多的内存。因此,这中间的取舍需要仔细考虑,下面是我在google上搜索到的一段讨论:

One of our customers complained about the additional code geneRATEd to
maintain the stack aligned to 16-byte boundaries, and suggested us to
default to the minimum alignment when optimizing for code size. This
has the caveat that, when you LINK code optimized for size with code
optimized for speed, if a function optimized for size calls a
performance-critical function with the stack misaligned, the
performance-critical function may perform poorly.


 
共1条 1/1 1   
   快速回复主题
  用户名 : 匿名不能发帖!请先 [ 登陆 ]
  标 题:
  内 容 :
 
EEPW元器件搜索  强力提供
关于我们 | 广告服务 | 企业会员服务 | 新手上路 | 联系我们 | 友情链接
《电子产品世界》杂志社 版权所有 北京东晓国际技术信息咨询有限公司
Copyright ©2002 ELECTRONIC ENGINEERING & PRODUCT WORLD. All rights reserved.
京ICP备060382号