Linux 内存cache

Strong order -> noncached;memcpy 必须要 源地址 与 目的地址对齐。
使用pgprot_noncached() 映射的内存
-------------------------------分界线----------------------------------------------
非strong order -> cachabled. memcpy 允许 源地址 与 目的地址 非对齐。
使用pgprot_writecombine() 映射的内存

 


Cache类型:

IA中,Cache类型一共有6种,


 

l  Strong Uncacheable (UC)

这种cache类型的memory,任何读写操作都不经过cache。一般是memory-map的IO地址可以使用这种类型。一般的ram强烈推荐不使用这种cache,否则效率会非常低。

 

l  Uncacheable (UC-)

特性与UC(Strong uncacheable)相同,唯一不同的是,这种类型的memory,可以通过修改MTRR来把它改变成WC

 

l  Write Combining (WC)

这种类型的cache,特性与UC相似,不同的地方是它可以被speculative read(什么叫speculative read?)每次write都可能被delay,write的内容会buffer到一个叫“write combining buffer”的地方。可以通过 对MTRR编程来设置WC,也可以通过设置PAT来设置WC(pat是什么?)

 

l  Write – through (WT)

这个很好理解,每次write,都要write到memory,同时write到对应的cache(if write hits)。WT方式保证了cache与memory是一致的。

 

l  Write – back (WB)

这种类型的memory,read和write,都跟一般的cache一样。只是write的时候,当写到了cache中,不会立即write到memory里(这个就跟WT不一样了)。CPU会等到适当的时候再write到memory里—比如当cache满了。 这种类型是效率最高的类型,

 

l  Write-protected (WP)

Read跟wb一样,但每次write,都会引起cache invalidate

 

 

#define ioremap(addr, size)        __ioremap((addr), (size), pgprot_noncached(PAGE_KERNEL))
#define ioremap_wc(addr, size)        __ioremap((addr), (size), pgprot_writecombine(PAGE_KERNEL))
#define ioremap_nocache(addr, size)    ioremap((addr), (size))
#define ioremap_wt(addr, size)        ioremap((addr), (size))
#define ioremap_cache            ioremap_cache
 

void *memremap(resource_size_t offset, size_t size, unsigned long flags)
{
    int is_ram = region_intersects(offset, size,
                       IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
    void *addr = NULL;

    if (!flags)
        return NULL;

    if (is_ram == REGION_MIXED) {
        WARN_ONCE(1, "memremap attempted on mixed range %pa size: %#lx\n",
                &offset, (unsigned long) size);
        return NULL;
    }

    /* Try all mapping types requested until one returns non-NULL */
    if (flags & MEMREMAP_WB) {
        /*
         * MEMREMAP_WB is special in that it can be satisifed
         * from the direct map.  Some archs depend on the
         * capability of memremap() to autodetect cases where
         * the requested range is potentially in System RAM.
         */
        if (is_ram == REGION_INTERSECTS)
            addr = try_ram_remap(offset, size);
        if (!addr)
            addr = arch_memremap_wb(offset, size);
    }

    /*
     * If we don't have a mapping yet and other request flags are
     * present then we will be attempting to establish a new virtual
     * address mapping.  Enforce that this mapping is not aliasing
     * System RAM.
     */
    if (!addr && is_ram == REGION_INTERSECTS && flags != MEMREMAP_WB) {
        WARN_ONCE(1, "memremap attempted on ram %pa size: %#lx\n",
                &offset, (unsigned long) size);
        return NULL;
    }

    if (!addr && (flags & MEMREMAP_WT))
        addr = ioremap_wt(offset, size);

    if (!addr && (flags & MEMREMAP_WC))
        addr = ioremap_wc(offset, size);

    return addr;
}
 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux内存buffer/cache很大是因为Linux系统采用了一种称为页缓存(Page Cache)的机制来提高文件系统的性能。具体来说,当文件被读取到内存中时,Linux会将读取到的文件数据缓存到内存的页缓存中,以便下次访问相同文件时可以直接从页缓存中获取,而不需要再次从磁盘中读取。这种机制可以大大提升系统IO的性能。 Linux内存buffer/cache的大小受到系统资源管理的影响,它会充分利用可用的内存空间来提供更高效的文件访问。当系统运行时,如果有大量的可用内存,并且没有其他进程需要使用,Linux会将这些多余的内存用于buffer/cache。这样一来,当需要从磁盘读取文件时,大部分情况下可以直接从内存中获取,而不必再次访问磁盘,从而大大提高系统的响应速度。 值得注意的是,虽然buffer/cache占用了大量内存,但这些内存并不是完全被占用,而是可以根据需要被释放的。当其他程序需要更多的内存时,Linux会自动将buffer/cache中的数据释放出来,以满足其他程序的需求。这个过程被称为自动缓存(Automatic caching),可以确保系统内存始终处于最佳状态。 总而言之,Linux内存buffer/cache很大是Linux系统为了提高文件系统的性能而采取的一种机制。通过将文件数据缓存到内存中,可以减少对磁盘的读取操作,从而大大提升了系统的响应速度。同时,这些内存可以根据需要进行释放,确保系统内存始终处于最佳状态。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值