標題: swapout protection token(swap token)防止頁面“抖動”處理 [打印本頁] 作者: hauto 時間: 2007-05-02 20:28 標題: swapout protection token(swap token)防止頁面“抖動”處理 版權聲明:可以任意轉載,轉載時請務必以超鏈接形式標明文章原始出處和作者信息及本聲明
hhj.cublog.cn
為了解決可能出現(xiàn)的頁面換入換出時的“抖動”,內核采用swap token技術。swap token通過
一個mm_struct指針swap_token_mm實現(xiàn)。swap_token_mm指向擁有swap token的進程的mm_struct
結構。擁有swap token的進程的內存active隊列中的頁面,不會轉移到inactive隊列中,從而避免
換入到磁盤上。函數(shù)grab_swap_token決定當前進程是否能獲得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)) {
//若當前進程已經(jīng)占有swap token,則記錄下最近換入標志,然后返回。
current->mm->recent_pagein = 1;
if (unlikely(!swap_token_default_timeout))
disable_swap_token();
return;
}
//執(zhí)行到這里,當前進程沒有占有swap token
if (time_after(jiffies, swap_token_check)) {
//每隔swap_token_check時間,檢測是否要將swap token 重新分配
if (!swap_token_default_timeout) {
swap_token_check = jiffies + SWAP_TOKEN_CHECK_INTERVAL;
return;
}
/* ... or if we recently held the token. */
//若當前進程最近占有過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時間之前,之前占有swap token的進程不會重新獲得swap token
swap_token_timeout = jiffies + swap_token_default_timeout;
swap_token_mm = current->mm;//將swap token賦給當前進程
}
spin_unlock(&swap_token_lock);
}
return;
}
這里用到了函數(shù)should_release_swap_token,判斷當前占有swap token的進程是否
應該釋放swap token.
static int should_release_swap_token(struct mm_struct *mm)
{
int ret = 0;
if (!mm->recent_pagein)//當前進程沒有過換入操作
ret = SWAP_TOKEN_ENOUGH_RSS;
else if (time_after(jiffies, swap_token_timeout))
//當前進程有換入操作,但占有swap token時間超時
ret = SWAP_TOKEN_TIMED_OUT;
mm->recent_pagein = 0;
return ret;
}
本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u1/36646/showart_290713.html作者: dj_ukyo 時間: 2015-04-09 08:29
今天也看到這個地方,按照2.6.24跟新
參考 mm_struct
……
/* Swap token stuff */
/*
* Last value of global fault stamp as seen by this process.
* In other words, this value gives an indication of how long
* it has been since this task got the token.
* Look at mm/thrash.c
*/
unsigned int faultstamp;
unsigned int token_priority;
unsigned int last_interval;
……
/* First come first served */
if (swap_token_mm == NULL) { // token空閑,一切很簡單
current->mm->token_priority = current->mm->token_priority + 2;
swap_token_mm = current->mm;
goto out;
}
if (current->mm != swap_token_mm) { // 我不持有token嗎?
if (current_interval < current->mm->last_interval) // 當前等待的記數(shù)<自己上一次的等待記數(shù)
current->mm->token_priority++;
else {
if (likely(current->mm->token_priority > 0))
current->mm->token_priority--;
}
/* Check if we deserve the token */
if (current->mm->token_priority >
swap_token_mm->token_priority) { // 我的優(yōu)先級>持有令牌進程的優(yōu)先級
current->mm->token_priority += 2;
swap_token_mm = current->mm; // 核心:交換
}
} else { // 繼續(xù)持有,而且提高優(yōu)先級獎勵
/* Token holder came in again! */
current->mm->token_priority += 2;
}