Magic in swap code. :-)

Chirayu Patel (chirayu@wipro.tcpn.com)
Sun, 29 Mar 1998 14:56:03 +0530 (GMT+0530)


Hi ,

Was the topic attractive? ;) My machine is _really_ flying with this
patch. :-)

I am putting a small patch for improvement of the kswapd behaviour. The
patch is against 2.1.90 code. (2.1.91 used to hang :-( )

The jist of the patch is introduction of swap policies.

1. When we are freeing the pages in try_to_swap_out we just free all pages
with age 0 (basically we do not make a distinction between dirty and
non-dirty pages). What I propose is - when we want a free small number of
pages ( < freepages.low) we free only the non-dirty pages with age 0. The
flaw in my current code is that even when I am not freeing a dirty page, I
am aging it (This shouldnt be done). This policy is termed as
SWAP_READONLY.

If we do not get sufficient number of pages when we try this policy
(SWAP_READONLY) I change the policy to SWAP_NORMAL (the current policy).

Please ignore the comments in my code and directly see the code ;) The
policy SWAP_WRITEONLY is not used anywhere. Also i have to move the
#defines to a header file (I know :-)).

2. Another change (which is not present in this patch) - I am working on
is on a swap policy called SWAP_UNDIRTY.

This policy tries to write dirty pages (a few at a time) to the swap if
the system is free. This will decrease th load when we actually want to
free the pages. I am currently taking into account
numfreepages/numphyspage, total swap available in the system, how much
time should kswapd be idle before we schedule it with this policy and
the amount of hard-disk activity.

Please let me have your comments on it. I am working on a patch for
2.1.90, which is almost complete.

Anyway coming back to my current patch. Please try it out and send
appreciation/comments/flames to chirayu@wipro.tcpn.com

-- Chirayu

--- vmscan.c Fri Mar 27 19:08:11 1998
+++ vmscan.c.new Fri Mar 27 19:18:05 1998
@@ -7,6 +7,7 @@
* kswapd added: 7.1.96 sct
* Removed kswapd_ctl limits, and swap out as many pages as needed
* to bring the system back to freepages.high: 2.4.97, Rik van Riel.
+ * Introduction of swap policies: 3.27.98, Chirayu Patel
* Version: $Id: vmscan.c,v 1.5 1998/02/23 22:14:28 sct Exp $
*/

@@ -49,6 +50,22 @@
*/
static int kswapd_awake = 0;

+/*
+ * A new idea to increase speed up of the swap process.
+ * There are three swap policys with the kernel.
+ * SWAP_READONLY - Swap out pages which are not dirty.
+ * To be used if nr_free_pages <= (free_pages_min + free_pages_low)/2
+ * SWAP_WRITEONLY - Swap out write only pages.
+ * We will use this if nr_free_pages >= (free_pages_high + free_pages_low)/2
+ * SWAP_NORMAL - Do a normal swap. otherwise.
+ * -- Chirayu Patel 26MAR98
+ */
+
+#define SWAP_READONLY 1
+#define SWAP_WRITEONLY 2
+#define SWAP_NORMAL 3
+int swap_policy = SWAP_NORMAL;
+
static void init_swap_timer(void);

/*
@@ -135,8 +152,9 @@
age_page(page_map);
if (page_map->age)
return 0;
-
if (pte_dirty(pte)) {
+ if (swap_policy == SWAP_READONLY)
+ return 0;
if (vma->vm_ops && vma->vm_ops->swapout) {
pid_t pid = tsk->pid;
vma->vm_mm->rss--;
@@ -197,7 +215,8 @@
free_page_and_swap_cache(page);
return 1; /* we slept: the process may not exist any more */
}
-
+ if (swap_policy == SWAP_WRITEONLY)
+ return 0;
/* The page was _not_ dirty, but still has a zero age. It must
* already be uptodate on disk. If it is in the swap cache,
* then we can just unlink the page now. Remove the swap cache
@@ -273,7 +292,7 @@
pte++;
} while (address < end);
return 0;
-}
+}

static inline int swap_out_pgd(struct task_struct * tsk, struct vm_area_struct * vma,
pgd_t *dir, unsigned long address, unsigned long end, int gfp_mask)
@@ -477,6 +496,12 @@
i--;
} while ((i - stop) >= 0);
}
+ /*
+ * We came out of the loop without freeing any page.
+ * Chk if the swap_policy is too aggressive and make it normal if yes.
+ */
+ if ((swap_policy == SWAP_READONLY) || (swap_policy == SWAP_WRITEONLY))
+ swap_policy = SWAP_NORMAL;
return 0;
}

@@ -559,12 +584,19 @@
* free memory. As memory gets tighter, kswapd
* gets more and more agressive. -- Rik.
*/
+ if (nr_free_pages > freepages.high)
+ continue;
+ /* Decide on the swap_policy*/
tries = freepages.high - nr_free_pages;
+ swap_policy = SWAP_NORMAL;
+ if (tries >= freepages.low)
+ swap_policy = SWAP_READONLY;
if (tries < freepages.min) {
tries = freepages.min;
}
- if (nr_free_pages < freepages.high + freepages.low)
+ if (nr_free_pages < freepages.low)
tries <<= 1;
+
while (tries--) {
int gfp_mask;

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu