cache write-allocate policy

本文介绍了三种主要的缓存写策略:Write-through、Write-around 和 Write-back,详细探讨了各自的优缺点及其适用场景。Write-through 确保数据更新的安全性,Write-around 减少了缓存的负载,而 Write-back 则提供了最佳的性能。

Write-through, write-around and write-back cache

There are three main caching techniques that can be deployed, each with their own pros and cons.

  • Write-through cache directs write I/O ontocache and through to underlying permanent storage before confirming I/O completion to the host. This ensures data updates are safely stored on, for example, a shared storage array, but has the disadvantage that I/O still experiences latency based on writing to that storage. Write-through cache is good for applications that write and then re-read data frequently as data is stored in cache and results in low read latency. 同时写cache和主存。
  • Write-around cache is a similar technique to write-through cache, but write I/O is written directly to permanent storage, by passing the cache. This can reduce the cache being flooded with write I/O that will not subsequently be re-read, but has the disadvantage is that a read request for recently written data will create a “cache miss” and have to be read from slower bulk storage and experience higher latency. 只写内存,绕过cache。
  • Write-back cache is where write I/O is directed to cache and completion is immediately confirmed to the host. This results in low latency and high throughput for write-intensive 集中写 applications, but there is data availability exposure risk because the only copy of the written data is in cache. As we will discuss later, suppliers have added resiliency with products that duplicate writes. Users need to consider whether write-back cache solutions offer enough protection as data is exposed until it is staged to external storage. Write-back cache is the best performing solution for mixed workloads as both read and write I/O have similar response time levels.

----- 在有cache的单机系统中,通常有两种写策略:write through 和 write back

两种写策略都是针对写命中(write hit)情况而言的:

--------- write through是既写cache也写main memory

--------- write back是只写cache,并使用dirty标志位记录cache的修改,直到被修改的cache 块被替换时,才把修改的内容写回main memory

那么在写失效(write miss)时,即所要写的地址不在cache中,该怎么办呢?

一种办法就是把要写的内容直接写回main memory,这种办法叫做no write allocate policy

另一种办法就是把要写的地址所在的块先从main memory调入cache中,然后写cache,这种办法叫做write allocate policy

在有cache的多处理器系统中仍然会有写失效(write miss)的情况,而且no write allocate policy和write allocate policy也仍然都可以使用。需要讨论的只是在出现write miss这种情况后,snooping cache该如何处理的问题。假设执行写操作的是P1,监听的是P2的cache,那么无论P1执行写操作时是write hit还是write miss,P2的cache(即snooping cache)都会检查P1所写的地址是否在P2的cache中,假如执行的是write invalid策略,那么snooping cache或者把相应的块置为invalid,或者什么都不做(因为它没有相应的块),肯定不会把其它处理器所要写的块调入自己的cache中(因为没有任何意义)。




### I-Cache RAM 的作用 I-Cache(指令缓存)是CPU中专门用于存储从主内存中读取的指令副本的高速缓存。它的主要作用是减少处理器访问主内存的延迟,从而提高程序执行的速度。由于现代处理器执行指令的速度远高于主内存的访问速度,因此通过使用I-Cache可以显著提升性能[^1]。 在计算机体系结构中,I-Cache通常与D-Cache(数据缓存)分离,这种设计允许同时获取指令和数据,进一步增强了处理效率。I-Cache的存在使得CPU能够快速地重复利用最近已经执行过的指令或者相邻位置的指令,这在循环或分支预测场景下特别有效[^1]。 ### I-Cache RAM 的工作原理 I-Cache的工作基于局部性原理,特别是时间局部性和空间局部性。时间局部性指的是如果一个信息项正在被访问,那么它很可能在不久的将来再次被访问;空间局部性则指如果一个存储位置被访问了,那么其附近的存储位置也很可能很快就会被访问到。I-Cache利用这些特性来决定哪些指令应该保留在高速缓存中以供快速访问。 当CPU需要执行一条新的指令时,它首先检查I-Cache是否包含该指令。如果有,则直接从I-Cache取出指令进行解码和执行,这个过程称为命中(hit)。如果没有找到所需的指令,即发生未命中(miss),那么就需要向更低层级的缓存或者主内存请求这条指令,并且通常会将这条指令连同其周围的一块区域一起加载进I-Cache中,以便后续可能的连续指令也能受益于高速缓存的效果。 对于不同的架构,如ARM体系,I-Cache的设计还需要考虑与其他组件之间的缓存一致性问题。例如,在多核系统中,每个核心都拥有自己的I-Cache,这就需要特定机制来保证各个核心间共享的数据保持一致[^2]。 ### 计算机架构中的 I-Cache 管理策略 在管理I-Cache方面,存在多种策略,其中包括分配策略(allocate policy)和替换策略(replacement policy)。其中,read allocate policy仅在读取操作导致缓存未命中时才为数据分配缓存行;而write allocate policy则是在写入操作导致缓存未命中时也分配缓存行。这两种策略影响着I-Cache如何响应不同类型的内存访问请求[^3]。 至于替换策略,它是用来确定当I-Cache满载且需要腾出空间给新到达的指令时,哪一部分现有的内容应当被移除。常见的替换算法包括最近最少使用(LRU)、随机选择等。这些策略对I-Cache的整体效能有着重要的影响[^3]。 ```python # 示例:模拟简单的I-Cache行为 class SimpleICache: def __init__(self, size): self.cache = {} self.size = size # 缓存大小 def fetch_instruction(self, address): if address in self.cache: print(f"Instruction at {address} found in I-Cache.") return self.cache[address] else: print(f"Instruction at {address} not in I-Cache. Fetching from main memory...") # 模拟从主内存获取指令的过程 instruction = f"Instruction_{address}" # 加载到I-Cache self._load_to_cache(address, instruction) return instruction def _load_to_cache(self, address, data): # 如果缓存已满,应用替换策略 if len(self.cache) >= self.size: # 这里简化处理,实际应采用更复杂的替换逻辑 oldest_key = next(iter(self.cache)) del self.cache[oldest_key] self.cache[address] = data # 创建一个容量为5的简单I-Cache实例 simple_icache = SimpleICache(5) # 测试获取指令 for addr in range(10): simple_icache.fetch_instruction(addr) ``` 上述代码提供了一个非常基础的I-Cache模拟实现,展示了当CPU尝试获取指令时,I-Cache是如何工作的。请注意,真实的I-Cache实现要复杂得多,涉及更多细节和技术考量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值