diff -Naurp --exclude-from=/home/con/kernel/dontdiff linux-2.6.7-rc3-base/include/linux/swap.h linux-2.6.7-rc3-as2/include/linux/swap.h --- linux-2.6.7-rc3-base/include/linux/swap.h 2004-06-10 23:29:04.000000000 +1000 +++ linux-2.6.7-rc3-as2/include/linux/swap.h 2004-06-14 01:39:51.503649893 +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 vm_autoregulate; #ifdef CONFIG_MMU /* linux/mm/shmem.c */ diff -Naurp --exclude-from=/home/con/kernel/dontdiff linux-2.6.7-rc3-base/include/linux/sysctl.h linux-2.6.7-rc3-as2/include/linux/sysctl.h --- linux-2.6.7-rc3-base/include/linux/sysctl.h 2004-06-10 23:29:04.000000000 +1000 +++ linux-2.6.7-rc3-as2/include/linux/sysctl.h 2004-06-14 01:39:22.565122113 +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_AUTOREGULATE=26, /* swappiness and inactivation autoregulated */ }; diff -Naurp --exclude-from=/home/con/kernel/dontdiff linux-2.6.7-rc3-base/kernel/sysctl.c linux-2.6.7-rc3-as2/kernel/sysctl.c --- linux-2.6.7-rc3-base/kernel/sysctl.c 2004-06-10 23:29:04.000000000 +1000 +++ linux-2.6.7-rc3-as2/kernel/sysctl.c 2004-06-14 01:40:55.567749225 +1000 @@ -727,6 +727,14 @@ static ctl_table vm_table[] = { .extra1 = &zero, .extra2 = &one_hundred, }, + { + .ctl_name = VM_AUTOREGULATE, + .procname = "autoregulate", + .data = &vm_autoregulate, + .maxlen = sizeof(int), + .mode = 0644, + .proc_handler = &proc_dointvec, + }, #ifdef CONFIG_HUGETLB_PAGE { .ctl_name = VM_HUGETLB_PAGES, diff -Naurp --exclude-from=/home/con/kernel/dontdiff linux-2.6.7-rc3-base/mm/vmscan.c linux-2.6.7-rc3-as2/mm/vmscan.c --- linux-2.6.7-rc3-base/mm/vmscan.c 2004-06-10 23:29:04.000000000 +1000 +++ linux-2.6.7-rc3-as2/mm/vmscan.c 2004-06-14 01:40:28.433942581 +1000 @@ -42,7 +42,9 @@ /* * From 0 .. 100. Higher means more swappy. */ -int vm_swappiness = 60; +int vm_swappiness = 0; +int vm_autoregulate = 1; +static int app_percent = 1; static long total_memory; #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru)) @@ -588,11 +590,13 @@ refill_inactive_zone(struct zone *zone, LIST_HEAD(l_active); /* Pages to go onto the active_list */ struct page *page; struct pagevec pvec; - int reclaim_mapped = 0; + int reclaim_mapped = 0; long mapped_ratio; long distress; long swap_tendency; + struct sysinfo i; + si_meminfo(&i); lru_add_drain(); pgmoved = 0; spin_lock_irq(&zone->lru_lock); @@ -635,6 +639,37 @@ refill_inactive_zone(struct zone *zone, mapped_ratio = (ps->nr_mapped * 100) / total_memory; /* + * app_percent is the percentage of physical ram used + * by application pages. + */ + app_percent = 100 - ((i.freeram + get_page_cache_size() - + swapper_space.nrpages) / (i.totalram / 100)); + +#ifdef CONFIG_SWAP + if (vm_autoregulate) { + si_swapinfo(&i); + + if (likely(i.totalswap >= 100)) { + int swap_centile; + + /* + * 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. Bias it downwards -ck + */ + vm_swappiness = min(app_percent, swap_centile); + vm_swappiness = vm_swappiness * vm_swappiness / 100; + } 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 * doesn't necessarily mean that page reclaim isn't succeeding. @@ -743,9 +778,13 @@ static int shrink_zone(struct zone *zone, int max_scan, unsigned int gfp_mask, int *total_scanned, struct page_state *ps, int do_writepage) { - unsigned long scan_active; + unsigned long scan_active, biased_active; int count; + if (vm_autoregulate) + biased_active = zone->nr_active / (101 - app_percent) * 100; + else + biased_active = zone->nr_active; /* * Try to keep the active list 2/3 of the size of the cache. And * make sure that refill_inactive is given a decent number of pages. @@ -756,7 +795,7 @@ shrink_zone(struct zone *zone, int max_s * `scan_active' just to make sure that the kernel will slowly sift * through the active list. */ - if (zone->nr_active >= 4*(zone->nr_inactive*2 + 1)) { + if (biased_active >= 4*(zone->nr_inactive*2 + 1)) { /* Don't scan more than 4 times the inactive list scan size */ scan_active = 4*max_scan; } else { @@ -764,7 +803,7 @@ shrink_zone(struct zone *zone, int max_s /* Cast to long long so the multiply doesn't overflow */ - tmp = (unsigned long long)max_scan * zone->nr_active; + tmp = (unsigned long long)max_scan * biased_active; do_div(tmp, zone->nr_inactive*2 + 1); scan_active = (unsigned long)tmp; }