设为首页 收藏本站
查看: 943|回复: 0

[经验分享] CPU cache

[复制链接]

尚未签到

发表于 2017-6-21 20:00:55 | 显示全部楼层 |阅读模式
  cache是一种小而快的缓冲器,用在CPU和main memory之间进行数据读写。
  在processor访问主memory时,首先检查cache中是不是有一份copy,如果cache hit,则直接访问cache。
  现在的cache多有很多的level,L1目前多是split的,分为data和instruction,L2和L3多是cores之间share的。
  instruction cache:加速instruction fetch,data cache:加速data fetch and store。
  还有另外的一种cache,TLB(Translation lookaside buffer),MMU(memory management unit)中的部件,
  主要加速virtual---physical的address translation,包含data和instruction。
  cache的大小多是4,8,16KB这样倍数增加。
  Cache entries
  cache和memory之间的数据交换是以固定大小来交换的,叫做cache line,
  当一个cache line从memory copy到cache时,一个cache entry会被创建,cache entry除了包含数据还有一部分的memory location信息。
  processor读写main memory时,首先根据地址检查相应的cache line,找到,成为cache hit。
  没有找到称为cache miss,当cache miss时,首先新建一个cache entry,并从main memory中copy data。
  Cache performance
  cache miss对于read操作影响最大,CPU需要等待,
  对于write操作,数据从main memory copy到cache的操作可以background执行,影响不是很大。
  Replacement policies
  当需要新添加一个cache entry时,必须有一个cache entry要被撤销掉(evict),基本原则是选择未来最不可能被用到的cache entry 替换。
  最常用的算法时,least_recently used(LRU)
  为了更有效的利用cache资源,在系统初始化时,一些memory address被设置为non-cacheable,
  Write policies
  如果数据会被写入cache,那之后的某个时间一定会被写入main memory,写入的时间叫做write policy。
  write-through:每次的cache write都会被写入main memory。
  write-back:cache write后不会立即写入main memory,只是标记为dirty,
  当1)read cache miss,首先cache entry evict,然后read new data to cache;
  2)write cache miss,该cache evict,然后新建cache entry,读data。
  main memory中的数据,可能被DMA或者multi-core processor改变,导致cache coherence问题。
  CPU stall
  由于cache miss而带来的CPU的等待,称为cpu stall,消耗掉几百条指令的执行时间。
  目前的一些措施:1)out-of-order execution,应用在intel的core中。
  2)simultaneous multithreading(SMT),或者hyper-threading(HT)
  Cache entry structure
  tag------data block------flag bits
  data block,中保存cache line的数据,cache的大小等于每个data block的大小乘上block的个数,不包括tag/flag/error correction code等空间。
  tag,中保存data的部分address in main memory。
  Flag bits,对于instruction cache,一个cache entry,只有一个bit,valid,表示当前数据是不是valid
  对于data cache,一个cache entry,有两个bit,valid bit和dirty bit。
  Associativity
  如果main memory中的数据,可以选择cache中的任意entry进行copy,replacement,这样的方式叫做:fully associative
  如果main memory中的每个entry,只能选择cache中的一个entry,这样的方式叫做:direct mapped
  如果main memory中的每个entry,可以去cache中的N和place,这样的方式叫做:N-way set associative
  associativity也是一种trade-off,越大的N-way set associativity,cache miss会越少,但是遍历时间会更长,增加很多逻辑。
DSC0000.png

  Cache miss
  cache miss可以分为三大类:
  1)instruction read miss,影响最大的miss,导致thread的执行to wait/stall
  2)data read miss,影响相对较小的miss,一些not dependent on cache read的instruction可以先执行。
  3)data write miss, 由于write可以进入queue,并且对后续的instruction影响较小。
  影响cache miss的因素:size,associativity,block size,
  根据Mark Hill的研究,可以将cache miss的原因分为三部分:
  1)Compulsory miss,第一次访问memory中的某个地址,与cache size和associativity无关。可以通过prefetching和更大的cache block来保证。
  2)Capacity miss,与block size和associtivity无关,只是因为cache大小总是有限的。
  3)Conflict miss,1)mapping miss,由于某个特定的associativity,而导致的,无法避免。
  2)replacement miss,由于replacement policy导致的。
  Address translation
  目前的很多程序,或者跑在自己的一段virtual address上保存自己的code和data,或者所有的程序跑在一个整体的virtual address上。
  拥有virtual memory的系统,要求必须有一个translate的机构,将virtual address变为physical address。
  目前多在MMU中实现,包含TLB。
  在address translation过程中,最重要的三个方面:
  1)Latency:在virtual address产生的几个cycle之后,physical address才能在MMU中产生。
  2)Aliasing:多个virtual address可能对应一个physical address,所以processor必须保证physical address上的更新以program的order来进行,
  3)Granularity:virtual address多分为pages,每一个page可以单独map。
  Cache根据tag/index与physical/virtual address的关系,可以分为四类:
  1)PIPT(Physical indexed, physical tagged):use physical address for both index and tag。最简单却也是最慢的方式,因为MMU必须先将
  virtual address变为physical address。(不会有aliasing问题),优点:low latency
  2)VIVT(virtual indexed, virtual tagged):use virtual address for both index and tag。最快的方式,但是存在aliasing问题,
  多个virtual address对应同一个physical address,存在coherency问题。
  同一个virtual address对应多个physical address,存在homonyms问题。
  没有办法区分是否对应同一个physical address
  3)VIPT(virtual indexed, physical tag):virtual address for the index,physical address in the tag,可以避免homonyms,保持部分VIVT的优点。
  4)PIVT(physical indexed, virtual tag):用的较少。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-386499-1-1.html 上篇帖子: 《从入门到精通云服务器》-6 下篇帖子: JavaScript单词汇总
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表