aboutsummaryrefslogtreecommitdiffstats
path: root/mm/thrash.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /mm/thrash.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'mm/thrash.c')
-rw-r--r--mm/thrash.c102
1 files changed, 102 insertions, 0 deletions
diff --git a/mm/thrash.c b/mm/thrash.c
new file mode 100644
index 000000000000..11461f7ad830
--- /dev/null
+++ b/mm/thrash.c
@@ -0,0 +1,102 @@
1/*
2 * mm/thrash.c
3 *
4 * Copyright (C) 2004, Red Hat, Inc.
5 * Copyright (C) 2004, Rik van Riel <riel@redhat.com>
6 * Released under the GPL, see the file COPYING for details.
7 *
8 * Simple token based thrashing protection, using the algorithm
9 * described in: http://www.cs.wm.edu/~sjiang/token.pdf
10 */
11#include <linux/jiffies.h>
12#include <linux/mm.h>
13#include <linux/sched.h>
14#include <linux/swap.h>
15
16static DEFINE_SPINLOCK(swap_token_lock);
17static unsigned long swap_token_timeout;
18static unsigned long swap_token_check;
19struct mm_struct * swap_token_mm = &init_mm;
20
21#define SWAP_TOKEN_CHECK_INTERVAL (HZ * 2)
22#define SWAP_TOKEN_TIMEOUT 0
23/*
24 * Currently disabled; Needs further code to work at HZ * 300.
25 */
26unsigned long swap_token_default_timeout = SWAP_TOKEN_TIMEOUT;
27
28/*
29 * Take the token away if the process had no page faults
30 * in the last interval, or if it has held the token for
31 * too long.
32 */
33#define SWAP_TOKEN_ENOUGH_RSS 1
34#define SWAP_TOKEN_TIMED_OUT 2
35static int should_release_swap_token(struct mm_struct *mm)
36{
37 int ret = 0;
38 if (!mm->recent_pagein)
39 ret = SWAP_TOKEN_ENOUGH_RSS;
40 else if (time_after(jiffies, swap_token_timeout))
41 ret = SWAP_TOKEN_TIMED_OUT;
42 mm->recent_pagein = 0;
43 return ret;
44}
45
46/*
47 * Try to grab the swapout protection token. We only try to
48 * grab it once every TOKEN_CHECK_INTERVAL, both to prevent
49 * SMP lock contention and to check that the process that held
50 * the token before is no longer thrashing.
51 */
52void grab_swap_token(void)
53{
54 struct mm_struct *mm;
55 int reason;
56
57 /* We have the token. Let others know we still need it. */
58 if (has_swap_token(current->mm)) {
59 current->mm->recent_pagein = 1;
60 return;
61 }
62
63 if (time_after(jiffies, swap_token_check)) {
64
65 /* Can't get swapout protection if we exceed our RSS limit. */
66 // if (current->mm->rss > current->mm->rlimit_rss)
67 // return;
68
69 /* ... or if we recently held the token. */
70 if (time_before(jiffies, current->mm->swap_token_time))
71 return;
72
73 if (!spin_trylock(&swap_token_lock))
74 return;
75
76 swap_token_check = jiffies + SWAP_TOKEN_CHECK_INTERVAL;
77
78 mm = swap_token_mm;
79 if ((reason = should_release_swap_token(mm))) {
80 unsigned long eligible = jiffies;
81 if (reason == SWAP_TOKEN_TIMED_OUT) {
82 eligible += swap_token_default_timeout;
83 }
84 mm->swap_token_time = eligible;
85 swap_token_timeout = jiffies + swap_token_default_timeout;
86 swap_token_mm = current->mm;
87 }
88 spin_unlock(&swap_token_lock);
89 }
90 return;
91}
92
93/* Called on process exit. */
94void __put_swap_token(struct mm_struct *mm)
95{
96 spin_lock(&swap_token_lock);
97 if (likely(mm == swap_token_mm)) {
98 swap_token_mm = &init_mm;
99 swap_token_check = jiffies;
100 }
101 spin_unlock(&swap_token_lock);
102}