- 論壇徽章:
- 0
|
版權(quán)聲明:可以任意轉(zhuǎn)載,轉(zhuǎn)載時(shí)請(qǐng)務(wù)必以超鏈接形式標(biāo)明文章原始出處和作者信息及本聲明
hhj.cublog.cn
為了解決可能出現(xiàn)的頁(yè)面換入換出時(shí)的“抖動(dòng)”,內(nèi)核采用swap token技術(shù)。swap token通過(guò)
一個(gè)mm_struct指針swap_token_mm實(shí)現(xiàn)。swap_token_mm指向擁有swap token的進(jìn)程的mm_struct
結(jié)構(gòu)。擁有swap token的進(jìn)程的內(nèi)存active隊(duì)列中的頁(yè)面,不會(huì)轉(zhuǎn)移到inactive隊(duì)列中,從而避免
換入到磁盤(pán)上。函數(shù)grab_swap_token決定當(dāng)前進(jìn)程是否能獲得swap token。
/*
* Try to grab the swapout protection token. We only try to
* grab it once every TOKEN_CHECK_INTERVAL, both to prevent
* SMP lock contention and to check that the process that held
* the token before is no longer thrashing.
*/
void grab_swap_token(void)
{
struct mm_struct *mm;
int reason;
/* We have the token. Let others know we still need it. */
if (has_swap_token(current->mm)) {
//若當(dāng)前進(jìn)程已經(jīng)占有swap token,則記錄下最近換入標(biāo)志,然后返回。
current->mm->recent_pagein = 1;
if (unlikely(!swap_token_default_timeout))
disable_swap_token();
return;
}
//執(zhí)行到這里,當(dāng)前進(jìn)程沒(méi)有占有swap token
if (time_after(jiffies, swap_token_check)) {
//每隔swap_token_check時(shí)間,檢測(cè)是否要將swap token 重新分配
if (!swap_token_default_timeout) {
swap_token_check = jiffies + SWAP_TOKEN_CHECK_INTERVAL;
return;
}
/* ... or if we recently held the token. */
//若當(dāng)前進(jìn)程最近占有過(guò)swap token,則返回
if (time_before(jiffies, current->mm->swap_token_time))
return;
if (!spin_trylock(&swap_token_lock))
return;
swap_token_check = jiffies + SWAP_TOKEN_CHECK_INTERVAL;
mm = swap_token_mm;
if ((reason = should_release_swap_token(mm))) {
unsigned long eligible = jiffies;
if (reason == SWAP_TOKEN_TIMED_OUT) {
eligible += swap_token_default_timeout;
}
mm->swap_token_time = eligible;
//在swap_token_time時(shí)間之前,之前占有swap token的進(jìn)程不會(huì)重新獲得swap token
swap_token_timeout = jiffies + swap_token_default_timeout;
swap_token_mm = current->mm;//將swap token賦給當(dāng)前進(jìn)程
}
spin_unlock(&swap_token_lock);
}
return;
}
這里用到了函數(shù)should_release_swap_token,判斷當(dāng)前占有swap token的進(jìn)程是否
應(yīng)該釋放swap token.
static int should_release_swap_token(struct mm_struct *mm)
{
int ret = 0;
if (!mm->recent_pagein)//當(dāng)前進(jìn)程沒(méi)有過(guò)換入操作
ret = SWAP_TOKEN_ENOUGH_RSS;
else if (time_after(jiffies, swap_token_timeout))
//當(dāng)前進(jìn)程有換入操作,但占有swap token時(shí)間超時(shí)
ret = SWAP_TOKEN_TIMED_OUT;
mm->recent_pagein = 0;
return ret;
}
本文來(lái)自ChinaUnix博客,如果查看原文請(qǐng)點(diǎn):http://blog.chinaunix.net/u1/36646/showart_290713.html |
|