- 论坛徽章:
- 0
|
版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明
hhj.cublog.cn
为了解决可能出现的页面换入换出时的“抖动”,内核采用swap token技术。swap token通过
一个mm_struct指针swap_token_mm实现。swap_token_mm指向拥有swap token的进程的mm_struct
结构。拥有swap token的进程的内存active队列中的页面,不会转移到inactive队列中,从而避免
换入到磁盘上。函数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)) {
//若当前进程已经占有swap token,则记录下最近换入标志,然后返回。
current->mm->recent_pagein = 1;
if (unlikely(!swap_token_default_timeout))
disable_swap_token();
return;
}
//执行到这里,当前进程没有占有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;
}
这里用到了函数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 |
|