提交 7602bdf2 编写于 作者: A Ashwin Chaugule 提交者: Linus Torvalds

[PATCH] new scheme to preempt swap token

The new swap token patches replace the current token traversal algo.  The old
algo had a crude timeout parameter that was used to handover the token from
one task to another.  This algo, transfers the token to the tasks that are in
need of the token.  The urgency for the token is based on the number of times
a task is required to swap-in pages.  Accordingly, the priority of a task is
incremented if it has been badly affected due to swap-outs.  To ensure that
the token doesnt bounce around rapidly, the token holders are given a priority
boost.  The priority of tasks is also decremented, if their rate of swap-in's
keeps reducing.  This way, the condition to check whether to pre-empt the swap
token, is a matter of comparing two task's priority fields.

[akpm@osdl.org: cleanups]
Signed-off-by: NAshwin Chaugule <ashwin.chaugule@celunite.com>
Cc: Rik van Riel <riel@redhat.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Signed-off-by: NAndrew Morton <akpm@osdl.org>
Signed-off-by: NLinus Torvalds <torvalds@osdl.org>
上级 098fe651
...@@ -344,9 +344,16 @@ struct mm_struct { ...@@ -344,9 +344,16 @@ struct mm_struct {
/* Architecture-specific MM context */ /* Architecture-specific MM context */
mm_context_t context; mm_context_t context;
/* Token based thrashing protection. */ /* Swap token stuff */
unsigned long swap_token_time; /*
char recent_pagein; * 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;
/* coredumping support */ /* coredumping support */
int core_waiters; int core_waiters;
......
...@@ -259,7 +259,6 @@ extern spinlock_t swap_lock; ...@@ -259,7 +259,6 @@ extern spinlock_t swap_lock;
/* linux/mm/thrash.c */ /* linux/mm/thrash.c */
extern struct mm_struct * swap_token_mm; extern struct mm_struct * swap_token_mm;
extern unsigned long swap_token_default_timeout;
extern void grab_swap_token(void); extern void grab_swap_token(void);
extern void __put_swap_token(struct mm_struct *); extern void __put_swap_token(struct mm_struct *);
......
...@@ -479,6 +479,10 @@ static struct mm_struct *dup_mm(struct task_struct *tsk) ...@@ -479,6 +479,10 @@ static struct mm_struct *dup_mm(struct task_struct *tsk)
memcpy(mm, oldmm, sizeof(*mm)); memcpy(mm, oldmm, sizeof(*mm));
/* Initializing for Swap token stuff */
mm->token_priority = 0;
mm->last_interval = 0;
if (!mm_init(mm)) if (!mm_init(mm))
goto fail_nomem; goto fail_nomem;
...@@ -542,6 +546,10 @@ static int copy_mm(unsigned long clone_flags, struct task_struct * tsk) ...@@ -542,6 +546,10 @@ static int copy_mm(unsigned long clone_flags, struct task_struct * tsk)
goto fail_nomem; goto fail_nomem;
good_mm: good_mm:
/* Initializing for Swap token stuff */
mm->token_priority = 0;
mm->last_interval = 0;
tsk->mm = mm; tsk->mm = mm;
tsk->active_mm = mm; tsk->active_mm = mm;
return 0; return 0;
......
...@@ -977,17 +977,6 @@ static ctl_table vm_table[] = { ...@@ -977,17 +977,6 @@ static ctl_table vm_table[] = {
.extra1 = &zero, .extra1 = &zero,
}, },
#endif #endif
#ifdef CONFIG_SWAP
{
.ctl_name = VM_SWAP_TOKEN_TIMEOUT,
.procname = "swap_token_timeout",
.data = &swap_token_default_timeout,
.maxlen = sizeof(swap_token_default_timeout),
.mode = 0644,
.proc_handler = &proc_dointvec_jiffies,
.strategy = &sysctl_jiffies,
},
#endif
#ifdef CONFIG_NUMA #ifdef CONFIG_NUMA
{ {
.ctl_name = VM_ZONE_RECLAIM_MODE, .ctl_name = VM_ZONE_RECLAIM_MODE,
......
...@@ -7,100 +7,74 @@ ...@@ -7,100 +7,74 @@
* *
* Simple token based thrashing protection, using the algorithm * Simple token based thrashing protection, using the algorithm
* described in: http://www.cs.wm.edu/~sjiang/token.pdf * described in: http://www.cs.wm.edu/~sjiang/token.pdf
*
* Sep 2006, Ashwin Chaugule <ashwin.chaugule@celunite.com>
* Improved algorithm to pass token:
* Each task has a priority which is incremented if it contended
* for the token in an interval less than its previous attempt.
* If the token is acquired, that task's priority is boosted to prevent
* the token from bouncing around too often and to let the task make
* some progress in its execution.
*/ */
#include <linux/jiffies.h> #include <linux/jiffies.h>
#include <linux/mm.h> #include <linux/mm.h>
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/swap.h> #include <linux/swap.h>
static DEFINE_SPINLOCK(swap_token_lock); static DEFINE_SPINLOCK(swap_token_lock);
static unsigned long swap_token_timeout; struct mm_struct *swap_token_mm;
static unsigned long swap_token_check; unsigned int global_faults;
struct mm_struct * swap_token_mm = &init_mm;
#define SWAP_TOKEN_CHECK_INTERVAL (HZ * 2)
#define SWAP_TOKEN_TIMEOUT (300 * HZ)
/*
* Currently disabled; Needs further code to work at HZ * 300.
*/
unsigned long swap_token_default_timeout = SWAP_TOKEN_TIMEOUT;
/*
* Take the token away if the process had no page faults
* in the last interval, or if it has held the token for
* too long.
*/
#define SWAP_TOKEN_ENOUGH_RSS 1
#define SWAP_TOKEN_TIMED_OUT 2
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))
ret = SWAP_TOKEN_TIMED_OUT;
mm->recent_pagein = 0;
return ret;
}
/*
* 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) void grab_swap_token(void)
{ {
struct mm_struct *mm; int current_interval;
int reason;
/* We have the token. Let others know we still need it. */ global_faults++;
if (has_swap_token(current->mm)) {
current->mm->recent_pagein = 1;
if (unlikely(!swap_token_default_timeout))
disable_swap_token();
return;
}
if (time_after(jiffies, swap_token_check)) {
if (!swap_token_default_timeout) { current_interval = global_faults - current->mm->faultstamp;
swap_token_check = jiffies + SWAP_TOKEN_CHECK_INTERVAL;
return;
}
/* ... or if we recently held the token. */
if (time_before(jiffies, current->mm->swap_token_time))
return;
if (!spin_trylock(&swap_token_lock)) if (!spin_trylock(&swap_token_lock))
return; return;
swap_token_check = jiffies + SWAP_TOKEN_CHECK_INTERVAL; /* First come first served */
if (swap_token_mm == NULL) {
current->mm->token_priority = current->mm->token_priority + 2;
swap_token_mm = current->mm;
goto out;
}
mm = swap_token_mm; if (current->mm != swap_token_mm) {
if ((reason = should_release_swap_token(mm))) { if (current_interval < current->mm->last_interval)
unsigned long eligible = jiffies; current->mm->token_priority++;
if (reason == SWAP_TOKEN_TIMED_OUT) { else {
eligible += swap_token_default_timeout; current->mm->token_priority--;
} if (unlikely(current->mm->token_priority < 0))
mm->swap_token_time = eligible; current->mm->token_priority = 0;
swap_token_timeout = jiffies + swap_token_default_timeout; }
/* 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; swap_token_mm = current->mm;
} }
spin_unlock(&swap_token_lock); } else {
/* Token holder came in again! */
current->mm->token_priority += 2;
} }
return;
out:
current->mm->faultstamp = global_faults;
current->mm->last_interval = current_interval;
spin_unlock(&swap_token_lock);
return;
} }
/* Called on process exit. */ /* Called on process exit. */
void __put_swap_token(struct mm_struct *mm) void __put_swap_token(struct mm_struct *mm)
{ {
spin_lock(&swap_token_lock); spin_lock(&swap_token_lock);
if (likely(mm == swap_token_mm)) { if (likely(mm == swap_token_mm))
mm->swap_token_time = jiffies + SWAP_TOKEN_CHECK_INTERVAL; swap_token_mm = NULL;
swap_token_mm = &init_mm;
swap_token_check = jiffies;
}
spin_unlock(&swap_token_lock); spin_unlock(&swap_token_lock);
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册