参见
按照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;
……
static DEFINE_SPINLOCK(swap_token_lock);
struct mm_struct *swap_token_mm; // 指向持有token的进程的mm_struct
static unsigned int global_faults; // 这个文件里的全局变量,用于表示时间的记数,但又不是时间
void grab_swap_token(void) // <-- do_swap_page <-- handle_pte_fault <-- handle_mm_fault
{
int current_interval;
global_faults++; // 唯一的赋值操作,每次尝试获得token都会对该计数器简单递增
current_interval = global_faults - current->mm->faultstamp; // 当前记数-我上一次得到token的记数=我有多久没占有token的记数
if (!spin_trylock(&swap_token_lock))
return;
/* 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) // 当前等待的记数<自己上一次的等待记数
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) { // 我的优先级>持有令牌进程的优先级
current->mm->token_priority += 2;
swap_token_mm = current->mm; // 核心:交换
}
} else { // 继续持有,而且提高优先级奖励
/* Token holder came in again! */
current->mm->token_priority += 2;
}
out: // 获得token后的操作
current->mm->faultstamp = global_faults; // 得到token时刻计数器的值,用于标记持有token记数长度
current->mm->last_interval = current_interval;
spin_unlock(&swap_token_lock);
return;
}
/* Called on process exit. */
void __put_swap_token(struct mm_struct *mm) // <-- put_swap_token <-- mmput
{
spin_lock(&swap_token_lock);
if (likely(mm == swap_token_mm))
swap_token_mm = NULL; // 释放token
spin_unlock(&swap_token_lock);
}
阅读(3075) | 评论(0) | 转发(0) |