X-Git-Url: http://ftp.safe.ca/?a=blobdiff_plain;f=mm%2Fthrash.c;h=2372d4ed5dd8ff763d3aaed5cb546c8033eca1ff;hb=51c24aaacaea90c8e87f1dec75a2ac7622b593f8;hp=f4c560b4a2b79862340fe7457af001f8e6141715;hpb=f7b7fd8f3ebbb2810d6893295aa984acd0fd30db;p=safe%2Fjmp%2Flinux-2.6 diff --git a/mm/thrash.c b/mm/thrash.c index f4c560b..2372d4e 100644 --- a/mm/thrash.c +++ b/mm/thrash.c @@ -7,100 +7,71 @@ * * Simple token based thrashing protection, using the algorithm * described in: http://www.cs.wm.edu/~sjiang/token.pdf + * + * Sep 2006, Ashwin Chaugule + * 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 #include #include #include static DEFINE_SPINLOCK(swap_token_lock); -static unsigned long swap_token_timeout; -static unsigned long swap_token_check; -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; +struct mm_struct *swap_token_mm; +static unsigned int global_faults; -/* - * 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) +void grab_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; -} + int current_interval; -/* - * 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; + global_faults++; - /* We have the token. Let others know we still need it. */ - if (has_swap_token(current->mm)) { - current->mm->recent_pagein = 1; - if (unlikely(!swap_token_default_timeout)) - disable_swap_token(); + current_interval = global_faults - mm->faultstamp; + + if (!spin_trylock(&swap_token_lock)) return; - } - if (time_after(jiffies, swap_token_check)) { + /* First come first served */ + if (swap_token_mm == NULL) { + mm->token_priority = mm->token_priority + 2; + swap_token_mm = mm; + goto out; + } - if (!swap_token_default_timeout) { - swap_token_check = jiffies + SWAP_TOKEN_CHECK_INTERVAL; - return; + if (mm != swap_token_mm) { + if (current_interval < mm->last_interval) + mm->token_priority++; + else { + if (likely(mm->token_priority > 0)) + mm->token_priority--; } - - /* ... or if we recently held the 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_timeout = jiffies + swap_token_default_timeout; - swap_token_mm = current->mm; + /* Check if we deserve the token */ + if (mm->token_priority > swap_token_mm->token_priority) { + mm->token_priority += 2; + swap_token_mm = mm; } - spin_unlock(&swap_token_lock); + } else { + /* Token holder came in again! */ + mm->token_priority += 2; } - return; + +out: + mm->faultstamp = global_faults; + mm->last_interval = current_interval; + spin_unlock(&swap_token_lock); } /* Called on process exit. */ void __put_swap_token(struct mm_struct *mm) { spin_lock(&swap_token_lock); - if (likely(mm == swap_token_mm)) { - mm->swap_token_time = jiffies + SWAP_TOKEN_CHECK_INTERVAL; - swap_token_mm = &init_mm; - swap_token_check = jiffies; - } + if (likely(mm == swap_token_mm)) + swap_token_mm = NULL; spin_unlock(&swap_token_lock); }