LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH] Autoregulated VM swappiness
@ 2004-05-30 13:30 Con Kolivas
  2004-05-31 11:50 ` Aristeu Sergio Rozanski Filho
  0 siblings, 1 reply; 5+ messages in thread
From: Con Kolivas @ 2004-05-30 13:30 UTC (permalink / raw)
  To: Linux Kernel Mailinglist

[-- Attachment #1: Type: Text/Plain, Size: 1576 bytes --]

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

With all the recent attention paid to swap I thought I'd resync this patch 
with 2.6.7-rc2 in it's more recent incarnation.

What this patch does is this:
The swappiness is made proportional to the amount of ram consumed by 
application pages, and inversely proportional to the amount of the last 
(sizeof physical ram) of swap ram used up. This has the effect of hardly ever 
using swap if you have large amounts of cached data by say copying an iso 
image or manipulating video files. Conversely if you have lots of 
applications running at once it will allow the less used ones to swap out by 
increasing the swappiness, giving preference to the "foreground" one. 

Changes from the first version announced on lkml:
Amount of swap space consumed is also taken into account, and the size of it 
compared to the physical ram is taken into consideration when making it's 
effect on the value of swappiness. With this patch, this should make any 
machine that has swapspace as resistant to OOM as possible.
This version by default autoregulates the swappiness, but also allows you to 
choose a manual setting if you so desire by

echo 0 > /proc/sys/vm/autoswappiness

and then setting the swappiness the manual way as previously. This makes 
comparison with autoregulation easy.
A few bugfixes.

Con


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAueIEZUg7+tp6mRURAnx5AJ9bkBsu+nAxT3fXJe2qLQ3PSYVL7wCbBrrC
pZOZHLOBhVsVdrd1a1ozKS4=
=XeIZ
-----END PGP SIGNATURE-----

[-- Attachment #2: patch-2.6.7-rc2-am10 --]
[-- Type: text/x-diff, Size: 3258 bytes --]

diff -Naurp linux-2.6.7-rc2-base/include/linux/swap.h linux-2.6.7-rc2-am9/include/linux/swap.h
--- linux-2.6.7-rc2-base/include/linux/swap.h	2004-05-30 17:51:45.896165842 +1000
+++ linux-2.6.7-rc2-am9/include/linux/swap.h	2004-05-30 23:07:34.718254427 +1000
@@ -175,6 +175,7 @@ extern void swap_setup(void);
 extern int try_to_free_pages(struct zone **, unsigned int, unsigned int);
 extern int shrink_all_memory(int);
 extern int vm_swappiness;
+extern int auto_swappiness;
 
 #ifdef CONFIG_MMU
 /* linux/mm/shmem.c */
diff -Naurp linux-2.6.7-rc2-base/include/linux/sysctl.h linux-2.6.7-rc2-am9/include/linux/sysctl.h
--- linux-2.6.7-rc2-base/include/linux/sysctl.h	2004-05-30 17:51:45.897165687 +1000
+++ linux-2.6.7-rc2-am9/include/linux/sysctl.h	2004-05-30 23:08:29.022809409 +1000
@@ -164,6 +164,7 @@ enum
 	VM_LAPTOP_MODE=23,	/* vm laptop mode */
 	VM_BLOCK_DUMP=24,	/* block dump mode */
 	VM_HUGETLB_GROUP=25,	/* permitted hugetlb group */
+	VM_AUTO_SWAPPINESS=26,	/* Make vm_swappiness autoregulated */
 };
 
 
diff -Naurp linux-2.6.7-rc2-base/kernel/sysctl.c linux-2.6.7-rc2-am9/kernel/sysctl.c
--- linux-2.6.7-rc2-base/kernel/sysctl.c	2004-05-30 17:51:45.978153070 +1000
+++ linux-2.6.7-rc2-am9/kernel/sysctl.c	2004-05-30 23:07:14.588384816 +1000
@@ -727,6 +727,17 @@ static ctl_table vm_table[] = {
 		.extra1		= &zero,
 		.extra2		= &one_hundred,
 	},
+	{
+		.ctl_name	= VM_AUTO_SWAPPINESS,
+		.procname	= "autoswappiness",
+		.data		= &auto_swappiness,
+		.maxlen		= sizeof(auto_swappiness),
+		.mode		= 0644,
+		.proc_handler	= &proc_dointvec_minmax,
+		.strategy	= &sysctl_intvec,
+		.extra1		= &zero,
+		.extra2		= &one_hundred,
+	},
 #ifdef CONFIG_HUGETLB_PAGE
 	 {
 		.ctl_name	= VM_HUGETLB_PAGES,
diff -Naurp linux-2.6.7-rc2-base/mm/vmscan.c linux-2.6.7-rc2-am9/mm/vmscan.c
--- linux-2.6.7-rc2-base/mm/vmscan.c	2004-05-30 17:51:46.041143257 +1000
+++ linux-2.6.7-rc2-am9/mm/vmscan.c	2004-05-30 23:07:14.260435815 +1000
@@ -43,6 +43,7 @@
  * From 0 .. 100.  Higher means more swappy.
  */
 int vm_swappiness = 60;
+int auto_swappiness = 1;
 static long total_memory;
 
 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
@@ -634,6 +635,41 @@ refill_inactive_zone(struct zone *zone, 
 	 */
 	mapped_ratio = (ps->nr_mapped * 100) / total_memory;
 
+	if (auto_swappiness) {
+#ifdef CONFIG_SWAP
+		int app_percent;
+		struct sysinfo i;
+		
+		si_swapinfo(&i);
+			
+		if (likely(i.totalswap >= 100)) {
+			int swap_centile;
+	
+			/*
+			 * app_percent is the percentage of physical ram used
+			 * by application pages.
+			 */
+			si_meminfo(&i);
+			app_percent = 100 - ((i.freeram + get_page_cache_size() -
+				swapper_space.nrpages) / (i.totalram / 100));
+	
+			/*
+			 * swap_centile is the percentage of the last (sizeof physical
+			 * ram) of swap free.
+			 */
+			swap_centile = i.freeswap / 
+				(min(i.totalswap, i.totalram) / 100);
+	
+			/*
+			 * Autoregulate vm_swappiness to be equal to the lowest of
+			 * app_percent and swap_centile. -ck
+			 */
+			vm_swappiness = min(app_percent, swap_centile);
+		} else 
+			vm_swappiness = 0;
+#endif
+	}
+	
 	/*
 	 * Now decide how much we really want to unmap some pages.  The mapped
 	 * ratio is downgraded - just because there's a lot of mapped memory

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Autoregulated VM swappiness
  2004-05-30 13:30 [PATCH] Autoregulated VM swappiness Con Kolivas
@ 2004-05-31 11:50 ` Aristeu Sergio Rozanski Filho
  2004-05-31 11:54   ` Con Kolivas
  0 siblings, 1 reply; 5+ messages in thread
From: Aristeu Sergio Rozanski Filho @ 2004-05-31 11:50 UTC (permalink / raw)
  To: Con Kolivas; +Cc: Linux Kernel Mailinglist

Hi Con,

+               .ctl_name       = VM_AUTO_SWAPPINESS,
+               .procname       = "autoswappiness",
+               .data           = &auto_swappiness,
+               .maxlen         = sizeof(auto_swappiness),
+               .mode           = 0644,
+               .proc_handler   = &proc_dointvec_minmax,
+               .strategy       = &sysctl_intvec,
+               .extra1         = &zero,
+               .extra2         = &one_hundred,
+       },
shouldn't be proc_dointvec here? seems minmax isn't needed.

-- 
Aristeu


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Autoregulated VM swappiness
  2004-05-31 11:50 ` Aristeu Sergio Rozanski Filho
@ 2004-05-31 11:54   ` Con Kolivas
  2004-05-31 13:47     ` Con Kolivas
  0 siblings, 1 reply; 5+ messages in thread
From: Con Kolivas @ 2004-05-31 11:54 UTC (permalink / raw)
  To: Aristeu Sergio Rozanski Filho; +Cc: Linux Kernel Mailinglist

On Mon, 31 May 2004 21:50, Aristeu Sergio Rozanski Filho wrote:
> Hi Con,
>
> +               .ctl_name       = VM_AUTO_SWAPPINESS,
> +               .procname       = "autoswappiness",
> +               .data           = &auto_swappiness,
> +               .maxlen         = sizeof(auto_swappiness),
> +               .mode           = 0644,
> +               .proc_handler   = &proc_dointvec_minmax,
> +               .strategy       = &sysctl_intvec,
> +               .extra1         = &zero,
> +               .extra2         = &one_hundred,
> +       },
> shouldn't be proc_dointvec here? seems minmax isn't needed.

Err yeah sure, thanks.

Con

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Autoregulated VM swappiness
  2004-05-31 11:54   ` Con Kolivas
@ 2004-05-31 13:47     ` Con Kolivas
  2004-05-31 14:34       ` Aristeu Sergio Rozanski Filho
  0 siblings, 1 reply; 5+ messages in thread
From: Con Kolivas @ 2004-05-31 13:47 UTC (permalink / raw)
  To: Aristeu Sergio Rozanski Filho; +Cc: Linux Kernel Mailinglist

[-- Attachment #1: Type: text/plain, Size: 754 bytes --]

On Mon, 31 May 2004 21:54, Con Kolivas wrote:
> On Mon, 31 May 2004 21:50, Aristeu Sergio Rozanski Filho wrote:
> > Hi Con,
> >
> > +               .ctl_name       = VM_AUTO_SWAPPINESS,
> > +               .procname       = "autoswappiness",
> > +               .data           = &auto_swappiness,
> > +               .maxlen         = sizeof(auto_swappiness),
> > +               .mode           = 0644,
> > +               .proc_handler   = &proc_dointvec_minmax,
> > +               .strategy       = &sysctl_intvec,
> > +               .extra1         = &zero,
> > +               .extra2         = &one_hundred,
> > +       },
> > shouldn't be proc_dointvec here? seems minmax isn't needed.
>
> Err yeah sure, thanks.

How's this version look?

Con

[-- Attachment #2: patch-2.6.7-rc2-am11 --]
[-- Type: text/x-diff, Size: 3263 bytes --]

diff -Naurp --exclude-from=dontdiff linux-2.6.7-rc2-base/include/linux/swap.h linux-2.6.7-rc2-am11/include/linux/swap.h
--- linux-2.6.7-rc2-base/include/linux/swap.h	2004-05-31 21:29:21.000000000 +1000
+++ linux-2.6.7-rc2-am11/include/linux/swap.h	2004-05-31 23:39:26.020055153 +1000
@@ -175,6 +175,7 @@ extern void swap_setup(void);
 extern int try_to_free_pages(struct zone **, unsigned int, unsigned int);
 extern int shrink_all_memory(int);
 extern int vm_swappiness;
+extern int auto_swappiness;
 
 #ifdef CONFIG_MMU
 /* linux/mm/shmem.c */
diff -Naurp --exclude-from=dontdiff linux-2.6.7-rc2-base/include/linux/sysctl.h linux-2.6.7-rc2-am11/include/linux/sysctl.h
--- linux-2.6.7-rc2-base/include/linux/sysctl.h	2004-05-31 21:29:21.000000000 +1000
+++ linux-2.6.7-rc2-am11/include/linux/sysctl.h	2004-05-31 23:39:26.021054997 +1000
@@ -164,6 +164,7 @@ enum
 	VM_LAPTOP_MODE=23,	/* vm laptop mode */
 	VM_BLOCK_DUMP=24,	/* block dump mode */
 	VM_HUGETLB_GROUP=25,	/* permitted hugetlb group */
+	VM_AUTO_SWAPPINESS=26,	/* Make vm_swappiness autoregulated */
 };
 
 
diff -Naurp --exclude-from=dontdiff linux-2.6.7-rc2-base/kernel/sysctl.c linux-2.6.7-rc2-am11/kernel/sysctl.c
--- linux-2.6.7-rc2-base/kernel/sysctl.c	2004-05-31 21:29:24.000000000 +1000
+++ linux-2.6.7-rc2-am11/kernel/sysctl.c	2004-05-31 23:40:57.658756170 +1000
@@ -727,6 +727,14 @@ static ctl_table vm_table[] = {
 		.extra1		= &zero,
 		.extra2		= &one_hundred,
 	},
+	{
+		.ctl_name	= VM_AUTO_SWAPPINESS,
+		.procname	= "autoswappiness",
+		.data		= &auto_swappiness,
+		.maxlen		= sizeof(int),
+		.mode		= 0644,
+		.proc_handler	= &proc_dointvec,
+	},
 #ifdef CONFIG_HUGETLB_PAGE
 	 {
 		.ctl_name	= VM_HUGETLB_PAGES,
diff -Naurp --exclude-from=dontdiff linux-2.6.7-rc2-base/mm/vmscan.c linux-2.6.7-rc2-am11/mm/vmscan.c
--- linux-2.6.7-rc2-base/mm/vmscan.c	2004-05-31 21:29:24.000000000 +1000
+++ linux-2.6.7-rc2-am11/mm/vmscan.c	2004-05-31 23:39:26.051050316 +1000
@@ -43,6 +43,7 @@
  * From 0 .. 100.  Higher means more swappy.
  */
 int vm_swappiness = 60;
+int auto_swappiness = 1;
 static long total_memory;
 
 #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
@@ -634,6 +635,41 @@ refill_inactive_zone(struct zone *zone, 
 	 */
 	mapped_ratio = (ps->nr_mapped * 100) / total_memory;
 
+	if (auto_swappiness) {
+#ifdef CONFIG_SWAP
+		int app_percent;
+		struct sysinfo i;
+		
+		si_swapinfo(&i);
+			
+		if (likely(i.totalswap >= 100)) {
+			int swap_centile;
+	
+			/*
+			 * app_percent is the percentage of physical ram used
+			 * by application pages.
+			 */
+			si_meminfo(&i);
+			app_percent = 100 - ((i.freeram + get_page_cache_size() -
+				swapper_space.nrpages) / (i.totalram / 100));
+	
+			/*
+			 * swap_centile is the percentage of the last (sizeof physical
+			 * ram) of swap free.
+			 */
+			swap_centile = i.freeswap / 
+				(min(i.totalswap, i.totalram) / 100);
+	
+			/*
+			 * Autoregulate vm_swappiness to be equal to the lowest of
+			 * app_percent and swap_centile. -ck
+			 */
+			vm_swappiness = min(app_percent, swap_centile);
+		} else 
+			vm_swappiness = 0;
+#endif
+	}
+	
 	/*
 	 * Now decide how much we really want to unmap some pages.  The mapped
 	 * ratio is downgraded - just because there's a lot of mapped memory

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] Autoregulated VM swappiness
  2004-05-31 13:47     ` Con Kolivas
@ 2004-05-31 14:34       ` Aristeu Sergio Rozanski Filho
  0 siblings, 0 replies; 5+ messages in thread
From: Aristeu Sergio Rozanski Filho @ 2004-05-31 14:34 UTC (permalink / raw)
  To: Con Kolivas; +Cc: Linux Kernel Mailinglist

Hi Con,
> How's this version look?
seems fine to me, thanks

-- 
Aristeu


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-05-31 14:36 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-05-30 13:30 [PATCH] Autoregulated VM swappiness Con Kolivas
2004-05-31 11:50 ` Aristeu Sergio Rozanski Filho
2004-05-31 11:54   ` Con Kolivas
2004-05-31 13:47     ` Con Kolivas
2004-05-31 14:34       ` Aristeu Sergio Rozanski Filho

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).