From: Wu Fengguang Collect info about the global available memory and its consumption speed. The data are used by the stateful method to estimate the thrashing threshold. They are the decisive factor of the correctness/accuracy of the resulting read-ahead size. - On NUMA systems, the accountings are done on a per-node basis. It works for the two common real-world schemes: - the reader process allocates caches in a node affined manner; - the reader process allocates caches _balancely_ from a set of nodes. - On non-NUMA systems, the readahead_aging is mainly increased on first access of the read-ahead pages, in order to make it go up constantly and smoothly. It helps improve the accuracy on small/fast read-aheads, with the cost of a little more overhead. Signed-off-by: Wu Fengguang Signed-off-by: Andrew Morton --- include/linux/mm.h | 9 +++++++++ include/linux/mmzone.h | 6 ++++++ mm/Kconfig | 4 ++++ mm/page_alloc.c | 16 ++++++++++++++++ mm/readahead.c | 34 ++++++++++++++++++++++++++++++++++ mm/swap.c | 2 ++ mm/vmscan.c | 4 ++++ 7 files changed, 75 insertions(+) diff -puN include/linux/mm.h~readahead-state-based-method-aging-accounting include/linux/mm.h --- a/include/linux/mm.h~readahead-state-based-method-aging-accounting +++ a/include/linux/mm.h @@ -1055,6 +1055,15 @@ static inline int prefer_adaptive_readah return readahead_ratio >= 10; } +DECLARE_PER_CPU(unsigned long, readahead_aging); +static inline void inc_readahead_aging(void) +{ +#ifdef CONFIG_READAHEAD_SMOOTH_AGING + if (prefer_adaptive_readahead()) + per_cpu(readahead_aging, raw_smp_processor_id())++; +#endif +} + /* Do stack extension */ extern int expand_stack(struct vm_area_struct *vma, unsigned long address); #ifdef CONFIG_IA64 diff -puN include/linux/mmzone.h~readahead-state-based-method-aging-accounting include/linux/mmzone.h --- a/include/linux/mmzone.h~readahead-state-based-method-aging-accounting +++ a/include/linux/mmzone.h @@ -197,6 +197,11 @@ struct zone { unsigned long pages_scanned; /* since last reclaim */ int all_unreclaimable; /* All pages pinned */ + /* The accumulated number of activities that may cause page aging, + * that is, make some pages closer to the tail of inactive_list. + */ + unsigned long aging_total; + /* A count of how many reclaimers are scanning this zone */ atomic_t reclaim_in_progress; @@ -358,6 +363,7 @@ void __get_zone_counts(unsigned long *ac unsigned long *free, struct pglist_data *pgdat); void get_zone_counts(unsigned long *active, unsigned long *inactive, unsigned long *free); +unsigned long nr_free_inactive_pages_node(int nid); void build_all_zonelists(void); void wakeup_kswapd(struct zone *zone, int order); int zone_watermark_ok(struct zone *z, int order, unsigned long mark, diff -puN mm/Kconfig~readahead-state-based-method-aging-accounting mm/Kconfig --- a/mm/Kconfig~readahead-state-based-method-aging-accounting +++ a/mm/Kconfig @@ -220,3 +220,7 @@ config DEBUG_READAHEAD Say N for production servers. +config READAHEAD_SMOOTH_AGING + def_bool n if NUMA + default y if !NUMA + depends on ADAPTIVE_READAHEAD diff -puN mm/page_alloc.c~readahead-state-based-method-aging-accounting mm/page_alloc.c --- a/mm/page_alloc.c~readahead-state-based-method-aging-accounting +++ a/mm/page_alloc.c @@ -1221,6 +1221,22 @@ static void show_node(struct zone *zone) #define show_node(zone) do { } while (0) #endif +/* + * The node's effective length of inactive_list(s). + */ +unsigned long nr_free_inactive_pages_node(int nid) +{ + unsigned int i; + unsigned long sum = 0; + struct zone *zones = NODE_DATA(nid)->node_zones; + + for (i = 0; i < MAX_NR_ZONES; i++) + sum += zones[i].nr_inactive + + zones[i].free_pages - zones[i].pages_low; + + return sum; +} + void si_meminfo(struct sysinfo *val) { val->totalram = totalram_pages; diff -puN mm/readahead.c~readahead-state-based-method-aging-accounting mm/readahead.c --- a/mm/readahead.c~readahead-state-based-method-aging-accounting +++ a/mm/readahead.c @@ -18,6 +18,8 @@ #include #include +#include + /* * Convienent macros for min/max read-ahead pages. * Note that MAX_RA_PAGES is rounded down, while MIN_RA_PAGES is rounded up. @@ -46,6 +48,14 @@ EXPORT_SYMBOL_GPL(readahead_ratio); int readahead_hit_rate = 1; /* + * Measures the aging process of inactive_list. + * Mainly increased on fresh page references to make it smooth. + */ +#ifdef CONFIG_READAHEAD_SMOOTH_AGING +DEFINE_PER_CPU(unsigned long, readahead_aging); +#endif + +/* * Detailed classification of read-ahead behaviors. */ #define RA_CLASS_SHIFT 4 @@ -805,6 +815,30 @@ out: } /* + * The node's accumulated aging activities. + */ +static unsigned long node_readahead_aging(void) +{ + unsigned long sum = 0; + +#ifdef CONFIG_READAHEAD_SMOOTH_AGING + unsigned long cpu; + cpumask_t mask = node_to_cpumask(numa_node_id()); + + for_each_cpu_mask(cpu, mask) + sum += per_cpu(readahead_aging, cpu); +#else + unsigned int i; + struct zone *zones = NODE_DATA(numa_node_id())->node_zones; + + for (i = 0; i < MAX_NR_ZONES; i++) + sum += zones[i].aging_total; +#endif + + return sum; +} + +/* * ra_min is mainly determined by the size of cache memory. Reasonable? * * Table of concrete numbers for 4KB page size: diff -puN mm/swap.c~readahead-state-based-method-aging-accounting mm/swap.c --- a/mm/swap.c~readahead-state-based-method-aging-accounting +++ a/mm/swap.c @@ -146,6 +146,8 @@ void fastcall mark_page_accessed(struct ClearPageReferenced(page); } else if (!PageReferenced(page)) { SetPageReferenced(page); + if (PageLRU(page)) + inc_readahead_aging(); } } diff -puN mm/vmscan.c~readahead-state-based-method-aging-accounting mm/vmscan.c --- a/mm/vmscan.c~readahead-state-based-method-aging-accounting +++ a/mm/vmscan.c @@ -458,6 +458,9 @@ static unsigned long shrink_page_list(st if (PageWriteback(page)) goto keep_locked; + if (!PageReferenced(page)) + inc_readahead_aging(); + referenced = page_referenced(page, 1); /* In active use or really unfreeable? Activate it. */ if (referenced && page_mapping_inuse(page)) @@ -656,6 +659,7 @@ static unsigned long shrink_inactive_lis &page_list, &nr_scan); zone->nr_inactive -= nr_taken; zone->pages_scanned += nr_scan; + zone->aging_total += nr_scan; spin_unlock_irq(&zone->lru_lock); nr_scanned += nr_scan; _