(轉)Linux核心中的Cache段 —— __read_mostly__Linux

來源:互聯網
上載者:User

Linux核心中的Cache段 —— NeilWong (neilwong@hotmail.com)

原文地址:http://blogold.chinaunix.net/u2/85263/showart_1743693.html

    最近移植LEON3的核心時,瞭解了一些簡單知識,特提出來供大家分享.

    我們知道Linux 核心包含很多Section段,例如主要的.text段,.data段等等.但另外還有一種段,其可以在核心載入時自動存放到相應平台的Cache中,以方便被快速讀取,該Section的名稱為以下兩種:

   .data.cacheline_aligned
   .data.read_mostly

   其中.data.read_mostly是在比較新的核心中才出現的,

   例如在Linux-2.6.11/fs/bio.c中的bvec_array是這樣定義的:

#define BV(x) { .nr_vecs = x, .name = "biovec-"__stringify(x) }
static struct biovec_pool bvec_array[BIOVEC_NR_POOLS] = {
        BV(1), BV(4), BV(16), BV(64), BV(128), BV(BIO_MAX_PAGES),
};
#undef BV

    而在Linux-2.6.21或更高版本中,該定義已經增加了 __read_mostly短語  

#define BV(x) { .nr_vecs = x, .name = "biovec-"__stringify(x) }
static struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS] __read_mostly = {
BV(1), BV(4), BV(16), BV(64), BV(128), BV(BIO_MAX_PAGES),
};
#undef BV

   __read_mostly原語將定義為存放在.data.read_mostly段中

  include/asm/cache.h

#define __read_mostly __attribute__((__section__(".data.read_mostly")))

    由此可見,我們可以將經常需要被讀取的資料定義為 __read_mostly類型, 這樣Linux核心被載入時,該資料將自動被存放到Cache中,以提高整個系統的執行效率.

   另一方面,如果所在的平台沒有Cache,或者雖然有Cache,但並不提供存放資料的介面,(也就是並不允許人工放置資料在Cache中), 這樣定義為 __read_mostly類型的資料將不能存放在Linux核心中,甚至也不能夠被載入到系統記憶體去執行,將造成Linux 核心啟動失敗.

   解決的方法有兩種:

   修改include/asm/cache.h中的__ready_mostly定義為:

   #define __read_mostly

   或者修改arch/xxx/kernel/vmlinux.S

   修改.data.read_mostly段的位置到實際記憶體空間中去,例如放置在 .data段之後等等

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.