1f6ac2354SChristoph Lameter /* 2f6ac2354SChristoph Lameter * linux/mm/vmstat.c 3f6ac2354SChristoph Lameter * 4f6ac2354SChristoph Lameter * Manages VM statistics 5f6ac2354SChristoph Lameter * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 62244b95aSChristoph Lameter * 72244b95aSChristoph Lameter * zoned VM statistics 82244b95aSChristoph Lameter * Copyright (C) 2006 Silicon Graphics, Inc., 92244b95aSChristoph Lameter * Christoph Lameter <christoph@lameter.com> 10f6ac2354SChristoph Lameter */ 118f32f7e5SAlexey Dobriyan #include <linux/fs.h> 12f6ac2354SChristoph Lameter #include <linux/mm.h> 134e950f6fSAlexey Dobriyan #include <linux/err.h> 142244b95aSChristoph Lameter #include <linux/module.h> 155a0e3ad6STejun Heo #include <linux/slab.h> 16df9ecabaSChristoph Lameter #include <linux/cpu.h> 17c748e134SAdrian Bunk #include <linux/vmstat.h> 18e8edc6e0SAlexey Dobriyan #include <linux/sched.h> 19f1a5ab12SMel Gorman #include <linux/math64.h> 2079da826aSMichael Rubin #include <linux/writeback.h> 2136deb0beSNamhyung Kim #include <linux/compaction.h> 226e543d57SLisa Du #include <linux/mm_inline.h> 236e543d57SLisa Du 246e543d57SLisa Du #include "internal.h" 25f6ac2354SChristoph Lameter 26f8891e5eSChristoph Lameter #ifdef CONFIG_VM_EVENT_COUNTERS 27f8891e5eSChristoph Lameter DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}}; 28f8891e5eSChristoph Lameter EXPORT_PER_CPU_SYMBOL(vm_event_states); 29f8891e5eSChristoph Lameter 3031f961a8SMinchan Kim static void sum_vm_events(unsigned long *ret) 31f8891e5eSChristoph Lameter { 329eccf2a8SChristoph Lameter int cpu; 33f8891e5eSChristoph Lameter int i; 34f8891e5eSChristoph Lameter 35f8891e5eSChristoph Lameter memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long)); 36f8891e5eSChristoph Lameter 3731f961a8SMinchan Kim for_each_online_cpu(cpu) { 38f8891e5eSChristoph Lameter struct vm_event_state *this = &per_cpu(vm_event_states, cpu); 39f8891e5eSChristoph Lameter 40f8891e5eSChristoph Lameter for (i = 0; i < NR_VM_EVENT_ITEMS; i++) 41f8891e5eSChristoph Lameter ret[i] += this->event[i]; 42f8891e5eSChristoph Lameter } 43f8891e5eSChristoph Lameter } 44f8891e5eSChristoph Lameter 45f8891e5eSChristoph Lameter /* 46f8891e5eSChristoph Lameter * Accumulate the vm event counters across all CPUs. 47f8891e5eSChristoph Lameter * The result is unavoidably approximate - it can change 48f8891e5eSChristoph Lameter * during and after execution of this function. 49f8891e5eSChristoph Lameter */ 50f8891e5eSChristoph Lameter void all_vm_events(unsigned long *ret) 51f8891e5eSChristoph Lameter { 52b5be1132SKOSAKI Motohiro get_online_cpus(); 5331f961a8SMinchan Kim sum_vm_events(ret); 54b5be1132SKOSAKI Motohiro put_online_cpus(); 55f8891e5eSChristoph Lameter } 5632dd66fcSHeiko Carstens EXPORT_SYMBOL_GPL(all_vm_events); 57f8891e5eSChristoph Lameter 58f8891e5eSChristoph Lameter /* 59f8891e5eSChristoph Lameter * Fold the foreign cpu events into our own. 60f8891e5eSChristoph Lameter * 61f8891e5eSChristoph Lameter * This is adding to the events on one processor 62f8891e5eSChristoph Lameter * but keeps the global counts constant. 63f8891e5eSChristoph Lameter */ 64f8891e5eSChristoph Lameter void vm_events_fold_cpu(int cpu) 65f8891e5eSChristoph Lameter { 66f8891e5eSChristoph Lameter struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu); 67f8891e5eSChristoph Lameter int i; 68f8891e5eSChristoph Lameter 69f8891e5eSChristoph Lameter for (i = 0; i < NR_VM_EVENT_ITEMS; i++) { 70f8891e5eSChristoph Lameter count_vm_events(i, fold_state->event[i]); 71f8891e5eSChristoph Lameter fold_state->event[i] = 0; 72f8891e5eSChristoph Lameter } 73f8891e5eSChristoph Lameter } 74f8891e5eSChristoph Lameter 75f8891e5eSChristoph Lameter #endif /* CONFIG_VM_EVENT_COUNTERS */ 76f8891e5eSChristoph Lameter 772244b95aSChristoph Lameter /* 782244b95aSChristoph Lameter * Manage combined zone based / global counters 792244b95aSChristoph Lameter * 802244b95aSChristoph Lameter * vm_stat contains the global counters 812244b95aSChristoph Lameter */ 82a1cb2c60SDimitri Sivanich atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS] __cacheline_aligned_in_smp; 832244b95aSChristoph Lameter EXPORT_SYMBOL(vm_stat); 842244b95aSChristoph Lameter 852244b95aSChristoph Lameter #ifdef CONFIG_SMP 862244b95aSChristoph Lameter 87b44129b3SMel Gorman int calculate_pressure_threshold(struct zone *zone) 8888f5acf8SMel Gorman { 8988f5acf8SMel Gorman int threshold; 9088f5acf8SMel Gorman int watermark_distance; 9188f5acf8SMel Gorman 9288f5acf8SMel Gorman /* 9388f5acf8SMel Gorman * As vmstats are not up to date, there is drift between the estimated 9488f5acf8SMel Gorman * and real values. For high thresholds and a high number of CPUs, it 9588f5acf8SMel Gorman * is possible for the min watermark to be breached while the estimated 9688f5acf8SMel Gorman * value looks fine. The pressure threshold is a reduced value such 9788f5acf8SMel Gorman * that even the maximum amount of drift will not accidentally breach 9888f5acf8SMel Gorman * the min watermark 9988f5acf8SMel Gorman */ 10088f5acf8SMel Gorman watermark_distance = low_wmark_pages(zone) - min_wmark_pages(zone); 10188f5acf8SMel Gorman threshold = max(1, (int)(watermark_distance / num_online_cpus())); 10288f5acf8SMel Gorman 10388f5acf8SMel Gorman /* 10488f5acf8SMel Gorman * Maximum threshold is 125 10588f5acf8SMel Gorman */ 10688f5acf8SMel Gorman threshold = min(125, threshold); 10788f5acf8SMel Gorman 10888f5acf8SMel Gorman return threshold; 10988f5acf8SMel Gorman } 11088f5acf8SMel Gorman 111b44129b3SMel Gorman int calculate_normal_threshold(struct zone *zone) 112df9ecabaSChristoph Lameter { 113df9ecabaSChristoph Lameter int threshold; 114df9ecabaSChristoph Lameter int mem; /* memory in 128 MB units */ 1152244b95aSChristoph Lameter 1162244b95aSChristoph Lameter /* 117df9ecabaSChristoph Lameter * The threshold scales with the number of processors and the amount 118df9ecabaSChristoph Lameter * of memory per zone. More memory means that we can defer updates for 119df9ecabaSChristoph Lameter * longer, more processors could lead to more contention. 120df9ecabaSChristoph Lameter * fls() is used to have a cheap way of logarithmic scaling. 1212244b95aSChristoph Lameter * 122df9ecabaSChristoph Lameter * Some sample thresholds: 123df9ecabaSChristoph Lameter * 124df9ecabaSChristoph Lameter * Threshold Processors (fls) Zonesize fls(mem+1) 125df9ecabaSChristoph Lameter * ------------------------------------------------------------------ 126df9ecabaSChristoph Lameter * 8 1 1 0.9-1 GB 4 127df9ecabaSChristoph Lameter * 16 2 2 0.9-1 GB 4 128df9ecabaSChristoph Lameter * 20 2 2 1-2 GB 5 129df9ecabaSChristoph Lameter * 24 2 2 2-4 GB 6 130df9ecabaSChristoph Lameter * 28 2 2 4-8 GB 7 131df9ecabaSChristoph Lameter * 32 2 2 8-16 GB 8 132df9ecabaSChristoph Lameter * 4 2 2 <128M 1 133df9ecabaSChristoph Lameter * 30 4 3 2-4 GB 5 134df9ecabaSChristoph Lameter * 48 4 3 8-16 GB 8 135df9ecabaSChristoph Lameter * 32 8 4 1-2 GB 4 136df9ecabaSChristoph Lameter * 32 8 4 0.9-1GB 4 137df9ecabaSChristoph Lameter * 10 16 5 <128M 1 138df9ecabaSChristoph Lameter * 40 16 5 900M 4 139df9ecabaSChristoph Lameter * 70 64 7 2-4 GB 5 140df9ecabaSChristoph Lameter * 84 64 7 4-8 GB 6 141df9ecabaSChristoph Lameter * 108 512 9 4-8 GB 6 142df9ecabaSChristoph Lameter * 125 1024 10 8-16 GB 8 143df9ecabaSChristoph Lameter * 125 1024 10 16-32 GB 9 1442244b95aSChristoph Lameter */ 145df9ecabaSChristoph Lameter 146b40da049SJiang Liu mem = zone->managed_pages >> (27 - PAGE_SHIFT); 147df9ecabaSChristoph Lameter 148df9ecabaSChristoph Lameter threshold = 2 * fls(num_online_cpus()) * (1 + fls(mem)); 149df9ecabaSChristoph Lameter 150df9ecabaSChristoph Lameter /* 151df9ecabaSChristoph Lameter * Maximum threshold is 125 152df9ecabaSChristoph Lameter */ 153df9ecabaSChristoph Lameter threshold = min(125, threshold); 154df9ecabaSChristoph Lameter 155df9ecabaSChristoph Lameter return threshold; 156df9ecabaSChristoph Lameter } 157df9ecabaSChristoph Lameter 158df9ecabaSChristoph Lameter /* 159df9ecabaSChristoph Lameter * Refresh the thresholds for each zone. 160df9ecabaSChristoph Lameter */ 161a6cccdc3SKOSAKI Motohiro void refresh_zone_stat_thresholds(void) 1622244b95aSChristoph Lameter { 163df9ecabaSChristoph Lameter struct zone *zone; 164df9ecabaSChristoph Lameter int cpu; 165df9ecabaSChristoph Lameter int threshold; 166df9ecabaSChristoph Lameter 167ee99c71cSKOSAKI Motohiro for_each_populated_zone(zone) { 168aa454840SChristoph Lameter unsigned long max_drift, tolerate_drift; 169aa454840SChristoph Lameter 170b44129b3SMel Gorman threshold = calculate_normal_threshold(zone); 171df9ecabaSChristoph Lameter 172df9ecabaSChristoph Lameter for_each_online_cpu(cpu) 17399dcc3e5SChristoph Lameter per_cpu_ptr(zone->pageset, cpu)->stat_threshold 17499dcc3e5SChristoph Lameter = threshold; 175aa454840SChristoph Lameter 176aa454840SChristoph Lameter /* 177aa454840SChristoph Lameter * Only set percpu_drift_mark if there is a danger that 178aa454840SChristoph Lameter * NR_FREE_PAGES reports the low watermark is ok when in fact 179aa454840SChristoph Lameter * the min watermark could be breached by an allocation 180aa454840SChristoph Lameter */ 181aa454840SChristoph Lameter tolerate_drift = low_wmark_pages(zone) - min_wmark_pages(zone); 182aa454840SChristoph Lameter max_drift = num_online_cpus() * threshold; 183aa454840SChristoph Lameter if (max_drift > tolerate_drift) 184aa454840SChristoph Lameter zone->percpu_drift_mark = high_wmark_pages(zone) + 185aa454840SChristoph Lameter max_drift; 186df9ecabaSChristoph Lameter } 1872244b95aSChristoph Lameter } 1882244b95aSChristoph Lameter 189b44129b3SMel Gorman void set_pgdat_percpu_threshold(pg_data_t *pgdat, 190b44129b3SMel Gorman int (*calculate_pressure)(struct zone *)) 19188f5acf8SMel Gorman { 19288f5acf8SMel Gorman struct zone *zone; 19388f5acf8SMel Gorman int cpu; 19488f5acf8SMel Gorman int threshold; 19588f5acf8SMel Gorman int i; 19688f5acf8SMel Gorman 19788f5acf8SMel Gorman for (i = 0; i < pgdat->nr_zones; i++) { 19888f5acf8SMel Gorman zone = &pgdat->node_zones[i]; 19988f5acf8SMel Gorman if (!zone->percpu_drift_mark) 20088f5acf8SMel Gorman continue; 20188f5acf8SMel Gorman 202b44129b3SMel Gorman threshold = (*calculate_pressure)(zone); 203b44129b3SMel Gorman for_each_possible_cpu(cpu) 20488f5acf8SMel Gorman per_cpu_ptr(zone->pageset, cpu)->stat_threshold 20588f5acf8SMel Gorman = threshold; 20688f5acf8SMel Gorman } 20788f5acf8SMel Gorman } 20888f5acf8SMel Gorman 2092244b95aSChristoph Lameter /* 2102244b95aSChristoph Lameter * For use when we know that interrupts are disabled. 2112244b95aSChristoph Lameter */ 2122244b95aSChristoph Lameter void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item, 2132244b95aSChristoph Lameter int delta) 2142244b95aSChristoph Lameter { 21512938a92SChristoph Lameter struct per_cpu_pageset __percpu *pcp = zone->pageset; 21612938a92SChristoph Lameter s8 __percpu *p = pcp->vm_stat_diff + item; 2172244b95aSChristoph Lameter long x; 21812938a92SChristoph Lameter long t; 2192244b95aSChristoph Lameter 22012938a92SChristoph Lameter x = delta + __this_cpu_read(*p); 2212244b95aSChristoph Lameter 22212938a92SChristoph Lameter t = __this_cpu_read(pcp->stat_threshold); 22312938a92SChristoph Lameter 22412938a92SChristoph Lameter if (unlikely(x > t || x < -t)) { 2252244b95aSChristoph Lameter zone_page_state_add(x, zone, item); 2262244b95aSChristoph Lameter x = 0; 2272244b95aSChristoph Lameter } 22812938a92SChristoph Lameter __this_cpu_write(*p, x); 2292244b95aSChristoph Lameter } 2302244b95aSChristoph Lameter EXPORT_SYMBOL(__mod_zone_page_state); 2312244b95aSChristoph Lameter 2322244b95aSChristoph Lameter /* 2332244b95aSChristoph Lameter * Optimized increment and decrement functions. 2342244b95aSChristoph Lameter * 2352244b95aSChristoph Lameter * These are only for a single page and therefore can take a struct page * 2362244b95aSChristoph Lameter * argument instead of struct zone *. This allows the inclusion of the code 2372244b95aSChristoph Lameter * generated for page_zone(page) into the optimized functions. 2382244b95aSChristoph Lameter * 2392244b95aSChristoph Lameter * No overflow check is necessary and therefore the differential can be 2402244b95aSChristoph Lameter * incremented or decremented in place which may allow the compilers to 2412244b95aSChristoph Lameter * generate better code. 2422244b95aSChristoph Lameter * The increment or decrement is known and therefore one boundary check can 2432244b95aSChristoph Lameter * be omitted. 2442244b95aSChristoph Lameter * 245df9ecabaSChristoph Lameter * NOTE: These functions are very performance sensitive. Change only 246df9ecabaSChristoph Lameter * with care. 247df9ecabaSChristoph Lameter * 2482244b95aSChristoph Lameter * Some processors have inc/dec instructions that are atomic vs an interrupt. 2492244b95aSChristoph Lameter * However, the code must first determine the differential location in a zone 2502244b95aSChristoph Lameter * based on the processor number and then inc/dec the counter. There is no 2512244b95aSChristoph Lameter * guarantee without disabling preemption that the processor will not change 2522244b95aSChristoph Lameter * in between and therefore the atomicity vs. interrupt cannot be exploited 2532244b95aSChristoph Lameter * in a useful way here. 2542244b95aSChristoph Lameter */ 255c8785385SChristoph Lameter void __inc_zone_state(struct zone *zone, enum zone_stat_item item) 2562244b95aSChristoph Lameter { 25712938a92SChristoph Lameter struct per_cpu_pageset __percpu *pcp = zone->pageset; 25812938a92SChristoph Lameter s8 __percpu *p = pcp->vm_stat_diff + item; 25912938a92SChristoph Lameter s8 v, t; 2602244b95aSChristoph Lameter 261908ee0f1SChristoph Lameter v = __this_cpu_inc_return(*p); 26212938a92SChristoph Lameter t = __this_cpu_read(pcp->stat_threshold); 26312938a92SChristoph Lameter if (unlikely(v > t)) { 26412938a92SChristoph Lameter s8 overstep = t >> 1; 2652244b95aSChristoph Lameter 26612938a92SChristoph Lameter zone_page_state_add(v + overstep, zone, item); 26712938a92SChristoph Lameter __this_cpu_write(*p, -overstep); 2682244b95aSChristoph Lameter } 2692244b95aSChristoph Lameter } 270ca889e6cSChristoph Lameter 271ca889e6cSChristoph Lameter void __inc_zone_page_state(struct page *page, enum zone_stat_item item) 272ca889e6cSChristoph Lameter { 273ca889e6cSChristoph Lameter __inc_zone_state(page_zone(page), item); 274ca889e6cSChristoph Lameter } 2752244b95aSChristoph Lameter EXPORT_SYMBOL(__inc_zone_page_state); 2762244b95aSChristoph Lameter 277c8785385SChristoph Lameter void __dec_zone_state(struct zone *zone, enum zone_stat_item item) 2782244b95aSChristoph Lameter { 27912938a92SChristoph Lameter struct per_cpu_pageset __percpu *pcp = zone->pageset; 28012938a92SChristoph Lameter s8 __percpu *p = pcp->vm_stat_diff + item; 28112938a92SChristoph Lameter s8 v, t; 2822244b95aSChristoph Lameter 283908ee0f1SChristoph Lameter v = __this_cpu_dec_return(*p); 28412938a92SChristoph Lameter t = __this_cpu_read(pcp->stat_threshold); 28512938a92SChristoph Lameter if (unlikely(v < - t)) { 28612938a92SChristoph Lameter s8 overstep = t >> 1; 2872244b95aSChristoph Lameter 28812938a92SChristoph Lameter zone_page_state_add(v - overstep, zone, item); 28912938a92SChristoph Lameter __this_cpu_write(*p, overstep); 2902244b95aSChristoph Lameter } 2912244b95aSChristoph Lameter } 292c8785385SChristoph Lameter 293c8785385SChristoph Lameter void __dec_zone_page_state(struct page *page, enum zone_stat_item item) 294c8785385SChristoph Lameter { 295c8785385SChristoph Lameter __dec_zone_state(page_zone(page), item); 296c8785385SChristoph Lameter } 2972244b95aSChristoph Lameter EXPORT_SYMBOL(__dec_zone_page_state); 2982244b95aSChristoph Lameter 2994156153cSHeiko Carstens #ifdef CONFIG_HAVE_CMPXCHG_LOCAL 3007c839120SChristoph Lameter /* 3017c839120SChristoph Lameter * If we have cmpxchg_local support then we do not need to incur the overhead 3027c839120SChristoph Lameter * that comes with local_irq_save/restore if we use this_cpu_cmpxchg. 3037c839120SChristoph Lameter * 3047c839120SChristoph Lameter * mod_state() modifies the zone counter state through atomic per cpu 3057c839120SChristoph Lameter * operations. 3067c839120SChristoph Lameter * 3077c839120SChristoph Lameter * Overstep mode specifies how overstep should handled: 3087c839120SChristoph Lameter * 0 No overstepping 3097c839120SChristoph Lameter * 1 Overstepping half of threshold 3107c839120SChristoph Lameter * -1 Overstepping minus half of threshold 3117c839120SChristoph Lameter */ 3127c839120SChristoph Lameter static inline void mod_state(struct zone *zone, 3137c839120SChristoph Lameter enum zone_stat_item item, int delta, int overstep_mode) 3147c839120SChristoph Lameter { 3157c839120SChristoph Lameter struct per_cpu_pageset __percpu *pcp = zone->pageset; 3167c839120SChristoph Lameter s8 __percpu *p = pcp->vm_stat_diff + item; 3177c839120SChristoph Lameter long o, n, t, z; 3187c839120SChristoph Lameter 3197c839120SChristoph Lameter do { 3207c839120SChristoph Lameter z = 0; /* overflow to zone counters */ 3217c839120SChristoph Lameter 3227c839120SChristoph Lameter /* 3237c839120SChristoph Lameter * The fetching of the stat_threshold is racy. We may apply 3247c839120SChristoph Lameter * a counter threshold to the wrong the cpu if we get 325d3bc2367SChristoph Lameter * rescheduled while executing here. However, the next 326d3bc2367SChristoph Lameter * counter update will apply the threshold again and 327d3bc2367SChristoph Lameter * therefore bring the counter under the threshold again. 328d3bc2367SChristoph Lameter * 329d3bc2367SChristoph Lameter * Most of the time the thresholds are the same anyways 330d3bc2367SChristoph Lameter * for all cpus in a zone. 3317c839120SChristoph Lameter */ 3327c839120SChristoph Lameter t = this_cpu_read(pcp->stat_threshold); 3337c839120SChristoph Lameter 3347c839120SChristoph Lameter o = this_cpu_read(*p); 3357c839120SChristoph Lameter n = delta + o; 3367c839120SChristoph Lameter 3377c839120SChristoph Lameter if (n > t || n < -t) { 3387c839120SChristoph Lameter int os = overstep_mode * (t >> 1) ; 3397c839120SChristoph Lameter 3407c839120SChristoph Lameter /* Overflow must be added to zone counters */ 3417c839120SChristoph Lameter z = n + os; 3427c839120SChristoph Lameter n = -os; 3437c839120SChristoph Lameter } 3447c839120SChristoph Lameter } while (this_cpu_cmpxchg(*p, o, n) != o); 3457c839120SChristoph Lameter 3467c839120SChristoph Lameter if (z) 3477c839120SChristoph Lameter zone_page_state_add(z, zone, item); 3487c839120SChristoph Lameter } 3497c839120SChristoph Lameter 3507c839120SChristoph Lameter void mod_zone_page_state(struct zone *zone, enum zone_stat_item item, 3517c839120SChristoph Lameter int delta) 3527c839120SChristoph Lameter { 3537c839120SChristoph Lameter mod_state(zone, item, delta, 0); 3547c839120SChristoph Lameter } 3557c839120SChristoph Lameter EXPORT_SYMBOL(mod_zone_page_state); 3567c839120SChristoph Lameter 3577c839120SChristoph Lameter void inc_zone_state(struct zone *zone, enum zone_stat_item item) 3587c839120SChristoph Lameter { 3597c839120SChristoph Lameter mod_state(zone, item, 1, 1); 3607c839120SChristoph Lameter } 3617c839120SChristoph Lameter 3627c839120SChristoph Lameter void inc_zone_page_state(struct page *page, enum zone_stat_item item) 3637c839120SChristoph Lameter { 3647c839120SChristoph Lameter mod_state(page_zone(page), item, 1, 1); 3657c839120SChristoph Lameter } 3667c839120SChristoph Lameter EXPORT_SYMBOL(inc_zone_page_state); 3677c839120SChristoph Lameter 3687c839120SChristoph Lameter void dec_zone_page_state(struct page *page, enum zone_stat_item item) 3697c839120SChristoph Lameter { 3707c839120SChristoph Lameter mod_state(page_zone(page), item, -1, -1); 3717c839120SChristoph Lameter } 3727c839120SChristoph Lameter EXPORT_SYMBOL(dec_zone_page_state); 3737c839120SChristoph Lameter #else 3747c839120SChristoph Lameter /* 3757c839120SChristoph Lameter * Use interrupt disable to serialize counter updates 3767c839120SChristoph Lameter */ 3777c839120SChristoph Lameter void mod_zone_page_state(struct zone *zone, enum zone_stat_item item, 3787c839120SChristoph Lameter int delta) 3797c839120SChristoph Lameter { 3807c839120SChristoph Lameter unsigned long flags; 3817c839120SChristoph Lameter 3827c839120SChristoph Lameter local_irq_save(flags); 3837c839120SChristoph Lameter __mod_zone_page_state(zone, item, delta); 3847c839120SChristoph Lameter local_irq_restore(flags); 3857c839120SChristoph Lameter } 3867c839120SChristoph Lameter EXPORT_SYMBOL(mod_zone_page_state); 3877c839120SChristoph Lameter 388ca889e6cSChristoph Lameter void inc_zone_state(struct zone *zone, enum zone_stat_item item) 389ca889e6cSChristoph Lameter { 390ca889e6cSChristoph Lameter unsigned long flags; 391ca889e6cSChristoph Lameter 392ca889e6cSChristoph Lameter local_irq_save(flags); 393ca889e6cSChristoph Lameter __inc_zone_state(zone, item); 394ca889e6cSChristoph Lameter local_irq_restore(flags); 395ca889e6cSChristoph Lameter } 396ca889e6cSChristoph Lameter 3972244b95aSChristoph Lameter void inc_zone_page_state(struct page *page, enum zone_stat_item item) 3982244b95aSChristoph Lameter { 3992244b95aSChristoph Lameter unsigned long flags; 4002244b95aSChristoph Lameter struct zone *zone; 4012244b95aSChristoph Lameter 4022244b95aSChristoph Lameter zone = page_zone(page); 4032244b95aSChristoph Lameter local_irq_save(flags); 404ca889e6cSChristoph Lameter __inc_zone_state(zone, item); 4052244b95aSChristoph Lameter local_irq_restore(flags); 4062244b95aSChristoph Lameter } 4072244b95aSChristoph Lameter EXPORT_SYMBOL(inc_zone_page_state); 4082244b95aSChristoph Lameter 4092244b95aSChristoph Lameter void dec_zone_page_state(struct page *page, enum zone_stat_item item) 4102244b95aSChristoph Lameter { 4112244b95aSChristoph Lameter unsigned long flags; 4122244b95aSChristoph Lameter 4132244b95aSChristoph Lameter local_irq_save(flags); 414a302eb4eSChristoph Lameter __dec_zone_page_state(page, item); 4152244b95aSChristoph Lameter local_irq_restore(flags); 4162244b95aSChristoph Lameter } 4172244b95aSChristoph Lameter EXPORT_SYMBOL(dec_zone_page_state); 4187c839120SChristoph Lameter #endif 4192244b95aSChristoph Lameter 4204edb0748SChristoph Lameter static inline void fold_diff(int *diff) 4214edb0748SChristoph Lameter { 4224edb0748SChristoph Lameter int i; 4234edb0748SChristoph Lameter 4244edb0748SChristoph Lameter for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) 4254edb0748SChristoph Lameter if (diff[i]) 4264edb0748SChristoph Lameter atomic_long_add(diff[i], &vm_stat[i]); 4274edb0748SChristoph Lameter } 4284edb0748SChristoph Lameter 4292244b95aSChristoph Lameter /* 4302bb921e5SChristoph Lameter * Update the zone counters for the current cpu. 431a7f75e25SChristoph Lameter * 4324037d452SChristoph Lameter * Note that refresh_cpu_vm_stats strives to only access 4334037d452SChristoph Lameter * node local memory. The per cpu pagesets on remote zones are placed 4344037d452SChristoph Lameter * in the memory local to the processor using that pageset. So the 4354037d452SChristoph Lameter * loop over all zones will access a series of cachelines local to 4364037d452SChristoph Lameter * the processor. 4374037d452SChristoph Lameter * 4384037d452SChristoph Lameter * The call to zone_page_state_add updates the cachelines with the 4394037d452SChristoph Lameter * statistics in the remote zone struct as well as the global cachelines 4404037d452SChristoph Lameter * with the global counters. These could cause remote node cache line 4414037d452SChristoph Lameter * bouncing and will have to be only done when necessary. 4422244b95aSChristoph Lameter */ 443fbc2edb0SChristoph Lameter static void refresh_cpu_vm_stats(void) 4442244b95aSChristoph Lameter { 4452244b95aSChristoph Lameter struct zone *zone; 4462244b95aSChristoph Lameter int i; 447a7f75e25SChristoph Lameter int global_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, }; 4482244b95aSChristoph Lameter 449ee99c71cSKOSAKI Motohiro for_each_populated_zone(zone) { 450fbc2edb0SChristoph Lameter struct per_cpu_pageset __percpu *p = zone->pageset; 4512244b95aSChristoph Lameter 452fbc2edb0SChristoph Lameter for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) { 453a7f75e25SChristoph Lameter int v; 454a7f75e25SChristoph Lameter 455fbc2edb0SChristoph Lameter v = this_cpu_xchg(p->vm_stat_diff[i], 0); 456fbc2edb0SChristoph Lameter if (v) { 457fbc2edb0SChristoph Lameter 458a7f75e25SChristoph Lameter atomic_long_add(v, &zone->vm_stat[i]); 459a7f75e25SChristoph Lameter global_diff[i] += v; 4604037d452SChristoph Lameter #ifdef CONFIG_NUMA 4614037d452SChristoph Lameter /* 3 seconds idle till flush */ 462fbc2edb0SChristoph Lameter __this_cpu_write(p->expire, 3); 4634037d452SChristoph Lameter #endif 4642244b95aSChristoph Lameter } 465fbc2edb0SChristoph Lameter } 466468fd62eSDimitri Sivanich cond_resched(); 4674037d452SChristoph Lameter #ifdef CONFIG_NUMA 4684037d452SChristoph Lameter /* 4694037d452SChristoph Lameter * Deal with draining the remote pageset of this 4704037d452SChristoph Lameter * processor 4714037d452SChristoph Lameter * 4724037d452SChristoph Lameter * Check if there are pages remaining in this pageset 4734037d452SChristoph Lameter * if not then there is nothing to expire. 4744037d452SChristoph Lameter */ 475fbc2edb0SChristoph Lameter if (!__this_cpu_read(p->expire) || 476fbc2edb0SChristoph Lameter !__this_cpu_read(p->pcp.count)) 4774037d452SChristoph Lameter continue; 4784037d452SChristoph Lameter 4794037d452SChristoph Lameter /* 4804037d452SChristoph Lameter * We never drain zones local to this processor. 4814037d452SChristoph Lameter */ 4824037d452SChristoph Lameter if (zone_to_nid(zone) == numa_node_id()) { 483fbc2edb0SChristoph Lameter __this_cpu_write(p->expire, 0); 4844037d452SChristoph Lameter continue; 4854037d452SChristoph Lameter } 4864037d452SChristoph Lameter 487fbc2edb0SChristoph Lameter 488fbc2edb0SChristoph Lameter if (__this_cpu_dec_return(p->expire)) 4894037d452SChristoph Lameter continue; 4904037d452SChristoph Lameter 491fbc2edb0SChristoph Lameter if (__this_cpu_read(p->pcp.count)) 492fbc2edb0SChristoph Lameter drain_zone_pages(zone, __this_cpu_ptr(&p->pcp)); 4934037d452SChristoph Lameter #endif 4942244b95aSChristoph Lameter } 4954edb0748SChristoph Lameter fold_diff(global_diff); 4962244b95aSChristoph Lameter } 4972244b95aSChristoph Lameter 49840f4b1eaSCody P Schafer /* 4992bb921e5SChristoph Lameter * Fold the data for an offline cpu into the global array. 5002bb921e5SChristoph Lameter * There cannot be any access by the offline cpu and therefore 5012bb921e5SChristoph Lameter * synchronization is simplified. 5022bb921e5SChristoph Lameter */ 5032bb921e5SChristoph Lameter void cpu_vm_stats_fold(int cpu) 5042bb921e5SChristoph Lameter { 5052bb921e5SChristoph Lameter struct zone *zone; 5062bb921e5SChristoph Lameter int i; 5072bb921e5SChristoph Lameter int global_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, }; 5082bb921e5SChristoph Lameter 5092bb921e5SChristoph Lameter for_each_populated_zone(zone) { 5102bb921e5SChristoph Lameter struct per_cpu_pageset *p; 5112bb921e5SChristoph Lameter 5122bb921e5SChristoph Lameter p = per_cpu_ptr(zone->pageset, cpu); 5132bb921e5SChristoph Lameter 5142bb921e5SChristoph Lameter for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) 5152bb921e5SChristoph Lameter if (p->vm_stat_diff[i]) { 5162bb921e5SChristoph Lameter int v; 5172bb921e5SChristoph Lameter 5182bb921e5SChristoph Lameter v = p->vm_stat_diff[i]; 5192bb921e5SChristoph Lameter p->vm_stat_diff[i] = 0; 5202bb921e5SChristoph Lameter atomic_long_add(v, &zone->vm_stat[i]); 5212bb921e5SChristoph Lameter global_diff[i] += v; 5222bb921e5SChristoph Lameter } 5232bb921e5SChristoph Lameter } 5242bb921e5SChristoph Lameter 5254edb0748SChristoph Lameter fold_diff(global_diff); 5262bb921e5SChristoph Lameter } 5272bb921e5SChristoph Lameter 5282bb921e5SChristoph Lameter /* 52940f4b1eaSCody P Schafer * this is only called if !populated_zone(zone), which implies no other users of 53040f4b1eaSCody P Schafer * pset->vm_stat_diff[] exsist. 53140f4b1eaSCody P Schafer */ 5325a883813SMinchan Kim void drain_zonestat(struct zone *zone, struct per_cpu_pageset *pset) 5335a883813SMinchan Kim { 5345a883813SMinchan Kim int i; 5355a883813SMinchan Kim 5365a883813SMinchan Kim for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) 5375a883813SMinchan Kim if (pset->vm_stat_diff[i]) { 5385a883813SMinchan Kim int v = pset->vm_stat_diff[i]; 5395a883813SMinchan Kim pset->vm_stat_diff[i] = 0; 5405a883813SMinchan Kim atomic_long_add(v, &zone->vm_stat[i]); 5415a883813SMinchan Kim atomic_long_add(v, &vm_stat[i]); 5425a883813SMinchan Kim } 5435a883813SMinchan Kim } 5442244b95aSChristoph Lameter #endif 5452244b95aSChristoph Lameter 546ca889e6cSChristoph Lameter #ifdef CONFIG_NUMA 547ca889e6cSChristoph Lameter /* 548ca889e6cSChristoph Lameter * zonelist = the list of zones passed to the allocator 549ca889e6cSChristoph Lameter * z = the zone from which the allocation occurred. 550ca889e6cSChristoph Lameter * 551ca889e6cSChristoph Lameter * Must be called with interrupts disabled. 55278afd561SAndi Kleen * 55378afd561SAndi Kleen * When __GFP_OTHER_NODE is set assume the node of the preferred 55478afd561SAndi Kleen * zone is the local node. This is useful for daemons who allocate 55578afd561SAndi Kleen * memory on behalf of other processes. 556ca889e6cSChristoph Lameter */ 55778afd561SAndi Kleen void zone_statistics(struct zone *preferred_zone, struct zone *z, gfp_t flags) 558ca889e6cSChristoph Lameter { 55918ea7e71SMel Gorman if (z->zone_pgdat == preferred_zone->zone_pgdat) { 560ca889e6cSChristoph Lameter __inc_zone_state(z, NUMA_HIT); 561ca889e6cSChristoph Lameter } else { 562ca889e6cSChristoph Lameter __inc_zone_state(z, NUMA_MISS); 56318ea7e71SMel Gorman __inc_zone_state(preferred_zone, NUMA_FOREIGN); 564ca889e6cSChristoph Lameter } 56578afd561SAndi Kleen if (z->node == ((flags & __GFP_OTHER_NODE) ? 56678afd561SAndi Kleen preferred_zone->node : numa_node_id())) 567ca889e6cSChristoph Lameter __inc_zone_state(z, NUMA_LOCAL); 568ca889e6cSChristoph Lameter else 569ca889e6cSChristoph Lameter __inc_zone_state(z, NUMA_OTHER); 570ca889e6cSChristoph Lameter } 571ca889e6cSChristoph Lameter #endif 572ca889e6cSChristoph Lameter 573d7a5752cSMel Gorman #ifdef CONFIG_COMPACTION 57436deb0beSNamhyung Kim 575d7a5752cSMel Gorman struct contig_page_info { 576d7a5752cSMel Gorman unsigned long free_pages; 577d7a5752cSMel Gorman unsigned long free_blocks_total; 578d7a5752cSMel Gorman unsigned long free_blocks_suitable; 579d7a5752cSMel Gorman }; 580d7a5752cSMel Gorman 581d7a5752cSMel Gorman /* 582d7a5752cSMel Gorman * Calculate the number of free pages in a zone, how many contiguous 583d7a5752cSMel Gorman * pages are free and how many are large enough to satisfy an allocation of 584d7a5752cSMel Gorman * the target size. Note that this function makes no attempt to estimate 585d7a5752cSMel Gorman * how many suitable free blocks there *might* be if MOVABLE pages were 586d7a5752cSMel Gorman * migrated. Calculating that is possible, but expensive and can be 587d7a5752cSMel Gorman * figured out from userspace 588d7a5752cSMel Gorman */ 589d7a5752cSMel Gorman static void fill_contig_page_info(struct zone *zone, 590d7a5752cSMel Gorman unsigned int suitable_order, 591d7a5752cSMel Gorman struct contig_page_info *info) 592d7a5752cSMel Gorman { 593d7a5752cSMel Gorman unsigned int order; 594d7a5752cSMel Gorman 595d7a5752cSMel Gorman info->free_pages = 0; 596d7a5752cSMel Gorman info->free_blocks_total = 0; 597d7a5752cSMel Gorman info->free_blocks_suitable = 0; 598d7a5752cSMel Gorman 599d7a5752cSMel Gorman for (order = 0; order < MAX_ORDER; order++) { 600d7a5752cSMel Gorman unsigned long blocks; 601d7a5752cSMel Gorman 602d7a5752cSMel Gorman /* Count number of free blocks */ 603d7a5752cSMel Gorman blocks = zone->free_area[order].nr_free; 604d7a5752cSMel Gorman info->free_blocks_total += blocks; 605d7a5752cSMel Gorman 606d7a5752cSMel Gorman /* Count free base pages */ 607d7a5752cSMel Gorman info->free_pages += blocks << order; 608d7a5752cSMel Gorman 609d7a5752cSMel Gorman /* Count the suitable free blocks */ 610d7a5752cSMel Gorman if (order >= suitable_order) 611d7a5752cSMel Gorman info->free_blocks_suitable += blocks << 612d7a5752cSMel Gorman (order - suitable_order); 613d7a5752cSMel Gorman } 614d7a5752cSMel Gorman } 615f1a5ab12SMel Gorman 616f1a5ab12SMel Gorman /* 617f1a5ab12SMel Gorman * A fragmentation index only makes sense if an allocation of a requested 618f1a5ab12SMel Gorman * size would fail. If that is true, the fragmentation index indicates 619f1a5ab12SMel Gorman * whether external fragmentation or a lack of memory was the problem. 620f1a5ab12SMel Gorman * The value can be used to determine if page reclaim or compaction 621f1a5ab12SMel Gorman * should be used 622f1a5ab12SMel Gorman */ 62356de7263SMel Gorman static int __fragmentation_index(unsigned int order, struct contig_page_info *info) 624f1a5ab12SMel Gorman { 625f1a5ab12SMel Gorman unsigned long requested = 1UL << order; 626f1a5ab12SMel Gorman 627f1a5ab12SMel Gorman if (!info->free_blocks_total) 628f1a5ab12SMel Gorman return 0; 629f1a5ab12SMel Gorman 630f1a5ab12SMel Gorman /* Fragmentation index only makes sense when a request would fail */ 631f1a5ab12SMel Gorman if (info->free_blocks_suitable) 632f1a5ab12SMel Gorman return -1000; 633f1a5ab12SMel Gorman 634f1a5ab12SMel Gorman /* 635f1a5ab12SMel Gorman * Index is between 0 and 1 so return within 3 decimal places 636f1a5ab12SMel Gorman * 637f1a5ab12SMel Gorman * 0 => allocation would fail due to lack of memory 638f1a5ab12SMel Gorman * 1 => allocation would fail due to fragmentation 639f1a5ab12SMel Gorman */ 640f1a5ab12SMel Gorman return 1000 - div_u64( (1000+(div_u64(info->free_pages * 1000ULL, requested))), info->free_blocks_total); 641f1a5ab12SMel Gorman } 64256de7263SMel Gorman 64356de7263SMel Gorman /* Same as __fragmentation index but allocs contig_page_info on stack */ 64456de7263SMel Gorman int fragmentation_index(struct zone *zone, unsigned int order) 64556de7263SMel Gorman { 64656de7263SMel Gorman struct contig_page_info info; 64756de7263SMel Gorman 64856de7263SMel Gorman fill_contig_page_info(zone, order, &info); 64956de7263SMel Gorman return __fragmentation_index(order, &info); 65056de7263SMel Gorman } 651d7a5752cSMel Gorman #endif 652d7a5752cSMel Gorman 653d7a5752cSMel Gorman #if defined(CONFIG_PROC_FS) || defined(CONFIG_COMPACTION) 6548f32f7e5SAlexey Dobriyan #include <linux/proc_fs.h> 655f6ac2354SChristoph Lameter #include <linux/seq_file.h> 656f6ac2354SChristoph Lameter 657467c996cSMel Gorman static char * const migratetype_names[MIGRATE_TYPES] = { 658467c996cSMel Gorman "Unmovable", 659467c996cSMel Gorman "Reclaimable", 660467c996cSMel Gorman "Movable", 661467c996cSMel Gorman "Reserve", 66247118af0SMichal Nazarewicz #ifdef CONFIG_CMA 66347118af0SMichal Nazarewicz "CMA", 66447118af0SMichal Nazarewicz #endif 665194159fbSMinchan Kim #ifdef CONFIG_MEMORY_ISOLATION 66691446b06SKOSAKI Motohiro "Isolate", 667194159fbSMinchan Kim #endif 668467c996cSMel Gorman }; 669467c996cSMel Gorman 670f6ac2354SChristoph Lameter static void *frag_start(struct seq_file *m, loff_t *pos) 671f6ac2354SChristoph Lameter { 672f6ac2354SChristoph Lameter pg_data_t *pgdat; 673f6ac2354SChristoph Lameter loff_t node = *pos; 674f6ac2354SChristoph Lameter for (pgdat = first_online_pgdat(); 675f6ac2354SChristoph Lameter pgdat && node; 676f6ac2354SChristoph Lameter pgdat = next_online_pgdat(pgdat)) 677f6ac2354SChristoph Lameter --node; 678f6ac2354SChristoph Lameter 679f6ac2354SChristoph Lameter return pgdat; 680f6ac2354SChristoph Lameter } 681f6ac2354SChristoph Lameter 682f6ac2354SChristoph Lameter static void *frag_next(struct seq_file *m, void *arg, loff_t *pos) 683f6ac2354SChristoph Lameter { 684f6ac2354SChristoph Lameter pg_data_t *pgdat = (pg_data_t *)arg; 685f6ac2354SChristoph Lameter 686f6ac2354SChristoph Lameter (*pos)++; 687f6ac2354SChristoph Lameter return next_online_pgdat(pgdat); 688f6ac2354SChristoph Lameter } 689f6ac2354SChristoph Lameter 690f6ac2354SChristoph Lameter static void frag_stop(struct seq_file *m, void *arg) 691f6ac2354SChristoph Lameter { 692f6ac2354SChristoph Lameter } 693f6ac2354SChristoph Lameter 694467c996cSMel Gorman /* Walk all the zones in a node and print using a callback */ 695467c996cSMel Gorman static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat, 696467c996cSMel Gorman void (*print)(struct seq_file *m, pg_data_t *, struct zone *)) 697f6ac2354SChristoph Lameter { 698f6ac2354SChristoph Lameter struct zone *zone; 699f6ac2354SChristoph Lameter struct zone *node_zones = pgdat->node_zones; 700f6ac2354SChristoph Lameter unsigned long flags; 701f6ac2354SChristoph Lameter 702f6ac2354SChristoph Lameter for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) { 703f6ac2354SChristoph Lameter if (!populated_zone(zone)) 704f6ac2354SChristoph Lameter continue; 705f6ac2354SChristoph Lameter 706f6ac2354SChristoph Lameter spin_lock_irqsave(&zone->lock, flags); 707467c996cSMel Gorman print(m, pgdat, zone); 708467c996cSMel Gorman spin_unlock_irqrestore(&zone->lock, flags); 709467c996cSMel Gorman } 710467c996cSMel Gorman } 711d7a5752cSMel Gorman #endif 712467c996cSMel Gorman 7130d6617c7SDavid Rientjes #if defined(CONFIG_PROC_FS) || defined(CONFIG_SYSFS) || defined(CONFIG_NUMA) 714fa25c503SKOSAKI Motohiro #ifdef CONFIG_ZONE_DMA 715fa25c503SKOSAKI Motohiro #define TEXT_FOR_DMA(xx) xx "_dma", 716fa25c503SKOSAKI Motohiro #else 717fa25c503SKOSAKI Motohiro #define TEXT_FOR_DMA(xx) 718fa25c503SKOSAKI Motohiro #endif 719fa25c503SKOSAKI Motohiro 720fa25c503SKOSAKI Motohiro #ifdef CONFIG_ZONE_DMA32 721fa25c503SKOSAKI Motohiro #define TEXT_FOR_DMA32(xx) xx "_dma32", 722fa25c503SKOSAKI Motohiro #else 723fa25c503SKOSAKI Motohiro #define TEXT_FOR_DMA32(xx) 724fa25c503SKOSAKI Motohiro #endif 725fa25c503SKOSAKI Motohiro 726fa25c503SKOSAKI Motohiro #ifdef CONFIG_HIGHMEM 727fa25c503SKOSAKI Motohiro #define TEXT_FOR_HIGHMEM(xx) xx "_high", 728fa25c503SKOSAKI Motohiro #else 729fa25c503SKOSAKI Motohiro #define TEXT_FOR_HIGHMEM(xx) 730fa25c503SKOSAKI Motohiro #endif 731fa25c503SKOSAKI Motohiro 732fa25c503SKOSAKI Motohiro #define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \ 733fa25c503SKOSAKI Motohiro TEXT_FOR_HIGHMEM(xx) xx "_movable", 734fa25c503SKOSAKI Motohiro 735fa25c503SKOSAKI Motohiro const char * const vmstat_text[] = { 736fa25c503SKOSAKI Motohiro /* Zoned VM counters */ 737fa25c503SKOSAKI Motohiro "nr_free_pages", 73881c0a2bbSJohannes Weiner "nr_alloc_batch", 739fa25c503SKOSAKI Motohiro "nr_inactive_anon", 740fa25c503SKOSAKI Motohiro "nr_active_anon", 741fa25c503SKOSAKI Motohiro "nr_inactive_file", 742fa25c503SKOSAKI Motohiro "nr_active_file", 743fa25c503SKOSAKI Motohiro "nr_unevictable", 744fa25c503SKOSAKI Motohiro "nr_mlock", 745fa25c503SKOSAKI Motohiro "nr_anon_pages", 746fa25c503SKOSAKI Motohiro "nr_mapped", 747fa25c503SKOSAKI Motohiro "nr_file_pages", 748fa25c503SKOSAKI Motohiro "nr_dirty", 749fa25c503SKOSAKI Motohiro "nr_writeback", 750fa25c503SKOSAKI Motohiro "nr_slab_reclaimable", 751fa25c503SKOSAKI Motohiro "nr_slab_unreclaimable", 752fa25c503SKOSAKI Motohiro "nr_page_table_pages", 753fa25c503SKOSAKI Motohiro "nr_kernel_stack", 754fa25c503SKOSAKI Motohiro "nr_unstable", 755fa25c503SKOSAKI Motohiro "nr_bounce", 756fa25c503SKOSAKI Motohiro "nr_vmscan_write", 75749ea7eb6SMel Gorman "nr_vmscan_immediate_reclaim", 758fa25c503SKOSAKI Motohiro "nr_writeback_temp", 759fa25c503SKOSAKI Motohiro "nr_isolated_anon", 760fa25c503SKOSAKI Motohiro "nr_isolated_file", 761fa25c503SKOSAKI Motohiro "nr_shmem", 762fa25c503SKOSAKI Motohiro "nr_dirtied", 763fa25c503SKOSAKI Motohiro "nr_written", 764fa25c503SKOSAKI Motohiro 765fa25c503SKOSAKI Motohiro #ifdef CONFIG_NUMA 766fa25c503SKOSAKI Motohiro "numa_hit", 767fa25c503SKOSAKI Motohiro "numa_miss", 768fa25c503SKOSAKI Motohiro "numa_foreign", 769fa25c503SKOSAKI Motohiro "numa_interleave", 770fa25c503SKOSAKI Motohiro "numa_local", 771fa25c503SKOSAKI Motohiro "numa_other", 772fa25c503SKOSAKI Motohiro #endif 773fa25c503SKOSAKI Motohiro "nr_anon_transparent_hugepages", 774d1ce749aSBartlomiej Zolnierkiewicz "nr_free_cma", 775fa25c503SKOSAKI Motohiro "nr_dirty_threshold", 776fa25c503SKOSAKI Motohiro "nr_dirty_background_threshold", 777fa25c503SKOSAKI Motohiro 778fa25c503SKOSAKI Motohiro #ifdef CONFIG_VM_EVENT_COUNTERS 779fa25c503SKOSAKI Motohiro "pgpgin", 780fa25c503SKOSAKI Motohiro "pgpgout", 781fa25c503SKOSAKI Motohiro "pswpin", 782fa25c503SKOSAKI Motohiro "pswpout", 783fa25c503SKOSAKI Motohiro 784fa25c503SKOSAKI Motohiro TEXTS_FOR_ZONES("pgalloc") 785fa25c503SKOSAKI Motohiro 786fa25c503SKOSAKI Motohiro "pgfree", 787fa25c503SKOSAKI Motohiro "pgactivate", 788fa25c503SKOSAKI Motohiro "pgdeactivate", 789fa25c503SKOSAKI Motohiro 790fa25c503SKOSAKI Motohiro "pgfault", 791fa25c503SKOSAKI Motohiro "pgmajfault", 792fa25c503SKOSAKI Motohiro 793fa25c503SKOSAKI Motohiro TEXTS_FOR_ZONES("pgrefill") 794904249aaSYing Han TEXTS_FOR_ZONES("pgsteal_kswapd") 795904249aaSYing Han TEXTS_FOR_ZONES("pgsteal_direct") 796fa25c503SKOSAKI Motohiro TEXTS_FOR_ZONES("pgscan_kswapd") 797fa25c503SKOSAKI Motohiro TEXTS_FOR_ZONES("pgscan_direct") 79868243e76SMel Gorman "pgscan_direct_throttle", 799fa25c503SKOSAKI Motohiro 800fa25c503SKOSAKI Motohiro #ifdef CONFIG_NUMA 801fa25c503SKOSAKI Motohiro "zone_reclaim_failed", 802fa25c503SKOSAKI Motohiro #endif 803fa25c503SKOSAKI Motohiro "pginodesteal", 804fa25c503SKOSAKI Motohiro "slabs_scanned", 805fa25c503SKOSAKI Motohiro "kswapd_inodesteal", 806fa25c503SKOSAKI Motohiro "kswapd_low_wmark_hit_quickly", 807fa25c503SKOSAKI Motohiro "kswapd_high_wmark_hit_quickly", 808fa25c503SKOSAKI Motohiro "pageoutrun", 809fa25c503SKOSAKI Motohiro "allocstall", 810fa25c503SKOSAKI Motohiro 811fa25c503SKOSAKI Motohiro "pgrotated", 812fa25c503SKOSAKI Motohiro 81303c5a6e1SMel Gorman #ifdef CONFIG_NUMA_BALANCING 81403c5a6e1SMel Gorman "numa_pte_updates", 81572403b4aSMel Gorman "numa_huge_pte_updates", 81603c5a6e1SMel Gorman "numa_hint_faults", 81703c5a6e1SMel Gorman "numa_hint_faults_local", 81803c5a6e1SMel Gorman "numa_pages_migrated", 81903c5a6e1SMel Gorman #endif 8205647bc29SMel Gorman #ifdef CONFIG_MIGRATION 8215647bc29SMel Gorman "pgmigrate_success", 8225647bc29SMel Gorman "pgmigrate_fail", 8235647bc29SMel Gorman #endif 824fa25c503SKOSAKI Motohiro #ifdef CONFIG_COMPACTION 825397487dbSMel Gorman "compact_migrate_scanned", 826397487dbSMel Gorman "compact_free_scanned", 827397487dbSMel Gorman "compact_isolated", 828fa25c503SKOSAKI Motohiro "compact_stall", 829fa25c503SKOSAKI Motohiro "compact_fail", 830fa25c503SKOSAKI Motohiro "compact_success", 831fa25c503SKOSAKI Motohiro #endif 832fa25c503SKOSAKI Motohiro 833fa25c503SKOSAKI Motohiro #ifdef CONFIG_HUGETLB_PAGE 834fa25c503SKOSAKI Motohiro "htlb_buddy_alloc_success", 835fa25c503SKOSAKI Motohiro "htlb_buddy_alloc_fail", 836fa25c503SKOSAKI Motohiro #endif 837fa25c503SKOSAKI Motohiro "unevictable_pgs_culled", 838fa25c503SKOSAKI Motohiro "unevictable_pgs_scanned", 839fa25c503SKOSAKI Motohiro "unevictable_pgs_rescued", 840fa25c503SKOSAKI Motohiro "unevictable_pgs_mlocked", 841fa25c503SKOSAKI Motohiro "unevictable_pgs_munlocked", 842fa25c503SKOSAKI Motohiro "unevictable_pgs_cleared", 843fa25c503SKOSAKI Motohiro "unevictable_pgs_stranded", 844fa25c503SKOSAKI Motohiro 845fa25c503SKOSAKI Motohiro #ifdef CONFIG_TRANSPARENT_HUGEPAGE 846fa25c503SKOSAKI Motohiro "thp_fault_alloc", 847fa25c503SKOSAKI Motohiro "thp_fault_fallback", 848fa25c503SKOSAKI Motohiro "thp_collapse_alloc", 849fa25c503SKOSAKI Motohiro "thp_collapse_alloc_failed", 850fa25c503SKOSAKI Motohiro "thp_split", 851d8a8e1f0SKirill A. Shutemov "thp_zero_page_alloc", 852d8a8e1f0SKirill A. Shutemov "thp_zero_page_alloc_failed", 853fa25c503SKOSAKI Motohiro #endif 854*ec659934SMel Gorman #ifdef CONFIG_DEBUG_TLBFLUSH 8556df46865SDave Hansen #ifdef CONFIG_SMP 8569824cf97SDave Hansen "nr_tlb_remote_flush", 8579824cf97SDave Hansen "nr_tlb_remote_flush_received", 858*ec659934SMel Gorman #endif /* CONFIG_SMP */ 8599824cf97SDave Hansen "nr_tlb_local_flush_all", 8609824cf97SDave Hansen "nr_tlb_local_flush_one", 861*ec659934SMel Gorman #endif /* CONFIG_DEBUG_TLBFLUSH */ 862fa25c503SKOSAKI Motohiro 863fa25c503SKOSAKI Motohiro #endif /* CONFIG_VM_EVENTS_COUNTERS */ 864fa25c503SKOSAKI Motohiro }; 8650d6617c7SDavid Rientjes #endif /* CONFIG_PROC_FS || CONFIG_SYSFS || CONFIG_NUMA */ 866fa25c503SKOSAKI Motohiro 867fa25c503SKOSAKI Motohiro 868d7a5752cSMel Gorman #ifdef CONFIG_PROC_FS 869467c996cSMel Gorman static void frag_show_print(struct seq_file *m, pg_data_t *pgdat, 870467c996cSMel Gorman struct zone *zone) 871467c996cSMel Gorman { 872467c996cSMel Gorman int order; 873467c996cSMel Gorman 874f6ac2354SChristoph Lameter seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name); 875f6ac2354SChristoph Lameter for (order = 0; order < MAX_ORDER; ++order) 876f6ac2354SChristoph Lameter seq_printf(m, "%6lu ", zone->free_area[order].nr_free); 877f6ac2354SChristoph Lameter seq_putc(m, '\n'); 878f6ac2354SChristoph Lameter } 879467c996cSMel Gorman 880467c996cSMel Gorman /* 881467c996cSMel Gorman * This walks the free areas for each zone. 882467c996cSMel Gorman */ 883467c996cSMel Gorman static int frag_show(struct seq_file *m, void *arg) 884467c996cSMel Gorman { 885467c996cSMel Gorman pg_data_t *pgdat = (pg_data_t *)arg; 886467c996cSMel Gorman walk_zones_in_node(m, pgdat, frag_show_print); 887467c996cSMel Gorman return 0; 888467c996cSMel Gorman } 889467c996cSMel Gorman 890467c996cSMel Gorman static void pagetypeinfo_showfree_print(struct seq_file *m, 891467c996cSMel Gorman pg_data_t *pgdat, struct zone *zone) 892467c996cSMel Gorman { 893467c996cSMel Gorman int order, mtype; 894467c996cSMel Gorman 895467c996cSMel Gorman for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) { 896467c996cSMel Gorman seq_printf(m, "Node %4d, zone %8s, type %12s ", 897467c996cSMel Gorman pgdat->node_id, 898467c996cSMel Gorman zone->name, 899467c996cSMel Gorman migratetype_names[mtype]); 900467c996cSMel Gorman for (order = 0; order < MAX_ORDER; ++order) { 901467c996cSMel Gorman unsigned long freecount = 0; 902467c996cSMel Gorman struct free_area *area; 903467c996cSMel Gorman struct list_head *curr; 904467c996cSMel Gorman 905467c996cSMel Gorman area = &(zone->free_area[order]); 906467c996cSMel Gorman 907467c996cSMel Gorman list_for_each(curr, &area->free_list[mtype]) 908467c996cSMel Gorman freecount++; 909467c996cSMel Gorman seq_printf(m, "%6lu ", freecount); 910467c996cSMel Gorman } 911467c996cSMel Gorman seq_putc(m, '\n'); 912467c996cSMel Gorman } 913467c996cSMel Gorman } 914467c996cSMel Gorman 915467c996cSMel Gorman /* Print out the free pages at each order for each migatetype */ 916467c996cSMel Gorman static int pagetypeinfo_showfree(struct seq_file *m, void *arg) 917467c996cSMel Gorman { 918467c996cSMel Gorman int order; 919467c996cSMel Gorman pg_data_t *pgdat = (pg_data_t *)arg; 920467c996cSMel Gorman 921467c996cSMel Gorman /* Print header */ 922467c996cSMel Gorman seq_printf(m, "%-43s ", "Free pages count per migrate type at order"); 923467c996cSMel Gorman for (order = 0; order < MAX_ORDER; ++order) 924467c996cSMel Gorman seq_printf(m, "%6d ", order); 925467c996cSMel Gorman seq_putc(m, '\n'); 926467c996cSMel Gorman 927467c996cSMel Gorman walk_zones_in_node(m, pgdat, pagetypeinfo_showfree_print); 928467c996cSMel Gorman 929467c996cSMel Gorman return 0; 930467c996cSMel Gorman } 931467c996cSMel Gorman 932467c996cSMel Gorman static void pagetypeinfo_showblockcount_print(struct seq_file *m, 933467c996cSMel Gorman pg_data_t *pgdat, struct zone *zone) 934467c996cSMel Gorman { 935467c996cSMel Gorman int mtype; 936467c996cSMel Gorman unsigned long pfn; 937467c996cSMel Gorman unsigned long start_pfn = zone->zone_start_pfn; 938108bcc96SCody P Schafer unsigned long end_pfn = zone_end_pfn(zone); 939467c996cSMel Gorman unsigned long count[MIGRATE_TYPES] = { 0, }; 940467c996cSMel Gorman 941467c996cSMel Gorman for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { 942467c996cSMel Gorman struct page *page; 943467c996cSMel Gorman 944467c996cSMel Gorman if (!pfn_valid(pfn)) 945467c996cSMel Gorman continue; 946467c996cSMel Gorman 947467c996cSMel Gorman page = pfn_to_page(pfn); 948eb33575cSMel Gorman 949eb33575cSMel Gorman /* Watch for unexpected holes punched in the memmap */ 950eb33575cSMel Gorman if (!memmap_valid_within(pfn, page, zone)) 951e80d6a24SMel Gorman continue; 952eb33575cSMel Gorman 953467c996cSMel Gorman mtype = get_pageblock_migratetype(page); 954467c996cSMel Gorman 955e80d6a24SMel Gorman if (mtype < MIGRATE_TYPES) 956467c996cSMel Gorman count[mtype]++; 957467c996cSMel Gorman } 958467c996cSMel Gorman 959467c996cSMel Gorman /* Print counts */ 960467c996cSMel Gorman seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name); 961467c996cSMel Gorman for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) 962467c996cSMel Gorman seq_printf(m, "%12lu ", count[mtype]); 963467c996cSMel Gorman seq_putc(m, '\n'); 964467c996cSMel Gorman } 965467c996cSMel Gorman 966467c996cSMel Gorman /* Print out the free pages at each order for each migratetype */ 967467c996cSMel Gorman static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg) 968467c996cSMel Gorman { 969467c996cSMel Gorman int mtype; 970467c996cSMel Gorman pg_data_t *pgdat = (pg_data_t *)arg; 971467c996cSMel Gorman 972467c996cSMel Gorman seq_printf(m, "\n%-23s", "Number of blocks type "); 973467c996cSMel Gorman for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) 974467c996cSMel Gorman seq_printf(m, "%12s ", migratetype_names[mtype]); 975467c996cSMel Gorman seq_putc(m, '\n'); 976467c996cSMel Gorman walk_zones_in_node(m, pgdat, pagetypeinfo_showblockcount_print); 977467c996cSMel Gorman 978467c996cSMel Gorman return 0; 979467c996cSMel Gorman } 980467c996cSMel Gorman 981467c996cSMel Gorman /* 982467c996cSMel Gorman * This prints out statistics in relation to grouping pages by mobility. 983467c996cSMel Gorman * It is expensive to collect so do not constantly read the file. 984467c996cSMel Gorman */ 985467c996cSMel Gorman static int pagetypeinfo_show(struct seq_file *m, void *arg) 986467c996cSMel Gorman { 987467c996cSMel Gorman pg_data_t *pgdat = (pg_data_t *)arg; 988467c996cSMel Gorman 98941b25a37SKOSAKI Motohiro /* check memoryless node */ 990a47b53c5SLai Jiangshan if (!node_state(pgdat->node_id, N_MEMORY)) 99141b25a37SKOSAKI Motohiro return 0; 99241b25a37SKOSAKI Motohiro 993467c996cSMel Gorman seq_printf(m, "Page block order: %d\n", pageblock_order); 994467c996cSMel Gorman seq_printf(m, "Pages per block: %lu\n", pageblock_nr_pages); 995467c996cSMel Gorman seq_putc(m, '\n'); 996467c996cSMel Gorman pagetypeinfo_showfree(m, pgdat); 997467c996cSMel Gorman pagetypeinfo_showblockcount(m, pgdat); 998467c996cSMel Gorman 999f6ac2354SChristoph Lameter return 0; 1000f6ac2354SChristoph Lameter } 1001f6ac2354SChristoph Lameter 10028f32f7e5SAlexey Dobriyan static const struct seq_operations fragmentation_op = { 1003f6ac2354SChristoph Lameter .start = frag_start, 1004f6ac2354SChristoph Lameter .next = frag_next, 1005f6ac2354SChristoph Lameter .stop = frag_stop, 1006f6ac2354SChristoph Lameter .show = frag_show, 1007f6ac2354SChristoph Lameter }; 1008f6ac2354SChristoph Lameter 10098f32f7e5SAlexey Dobriyan static int fragmentation_open(struct inode *inode, struct file *file) 10108f32f7e5SAlexey Dobriyan { 10118f32f7e5SAlexey Dobriyan return seq_open(file, &fragmentation_op); 10128f32f7e5SAlexey Dobriyan } 10138f32f7e5SAlexey Dobriyan 10148f32f7e5SAlexey Dobriyan static const struct file_operations fragmentation_file_operations = { 10158f32f7e5SAlexey Dobriyan .open = fragmentation_open, 10168f32f7e5SAlexey Dobriyan .read = seq_read, 10178f32f7e5SAlexey Dobriyan .llseek = seq_lseek, 10188f32f7e5SAlexey Dobriyan .release = seq_release, 10198f32f7e5SAlexey Dobriyan }; 10208f32f7e5SAlexey Dobriyan 102174e2e8e8SAlexey Dobriyan static const struct seq_operations pagetypeinfo_op = { 1022467c996cSMel Gorman .start = frag_start, 1023467c996cSMel Gorman .next = frag_next, 1024467c996cSMel Gorman .stop = frag_stop, 1025467c996cSMel Gorman .show = pagetypeinfo_show, 1026467c996cSMel Gorman }; 1027467c996cSMel Gorman 102874e2e8e8SAlexey Dobriyan static int pagetypeinfo_open(struct inode *inode, struct file *file) 102974e2e8e8SAlexey Dobriyan { 103074e2e8e8SAlexey Dobriyan return seq_open(file, &pagetypeinfo_op); 103174e2e8e8SAlexey Dobriyan } 103274e2e8e8SAlexey Dobriyan 103374e2e8e8SAlexey Dobriyan static const struct file_operations pagetypeinfo_file_ops = { 103474e2e8e8SAlexey Dobriyan .open = pagetypeinfo_open, 103574e2e8e8SAlexey Dobriyan .read = seq_read, 103674e2e8e8SAlexey Dobriyan .llseek = seq_lseek, 103774e2e8e8SAlexey Dobriyan .release = seq_release, 103874e2e8e8SAlexey Dobriyan }; 103974e2e8e8SAlexey Dobriyan 1040467c996cSMel Gorman static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat, 1041467c996cSMel Gorman struct zone *zone) 1042f6ac2354SChristoph Lameter { 1043f6ac2354SChristoph Lameter int i; 1044f6ac2354SChristoph Lameter seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name); 1045f6ac2354SChristoph Lameter seq_printf(m, 1046f6ac2354SChristoph Lameter "\n pages free %lu" 1047f6ac2354SChristoph Lameter "\n min %lu" 1048f6ac2354SChristoph Lameter "\n low %lu" 1049f6ac2354SChristoph Lameter "\n high %lu" 105008d9ae7cSWu Fengguang "\n scanned %lu" 1051f6ac2354SChristoph Lameter "\n spanned %lu" 10529feedc9dSJiang Liu "\n present %lu" 10539feedc9dSJiang Liu "\n managed %lu", 105488f5acf8SMel Gorman zone_page_state(zone, NR_FREE_PAGES), 105541858966SMel Gorman min_wmark_pages(zone), 105641858966SMel Gorman low_wmark_pages(zone), 105741858966SMel Gorman high_wmark_pages(zone), 1058f6ac2354SChristoph Lameter zone->pages_scanned, 1059f6ac2354SChristoph Lameter zone->spanned_pages, 10609feedc9dSJiang Liu zone->present_pages, 10619feedc9dSJiang Liu zone->managed_pages); 10622244b95aSChristoph Lameter 10632244b95aSChristoph Lameter for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) 10642244b95aSChristoph Lameter seq_printf(m, "\n %-12s %lu", vmstat_text[i], 10652244b95aSChristoph Lameter zone_page_state(zone, i)); 10662244b95aSChristoph Lameter 1067f6ac2354SChristoph Lameter seq_printf(m, 1068f6ac2354SChristoph Lameter "\n protection: (%lu", 1069f6ac2354SChristoph Lameter zone->lowmem_reserve[0]); 1070f6ac2354SChristoph Lameter for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++) 1071f6ac2354SChristoph Lameter seq_printf(m, ", %lu", zone->lowmem_reserve[i]); 1072f6ac2354SChristoph Lameter seq_printf(m, 1073f6ac2354SChristoph Lameter ")" 1074f6ac2354SChristoph Lameter "\n pagesets"); 1075f6ac2354SChristoph Lameter for_each_online_cpu(i) { 1076f6ac2354SChristoph Lameter struct per_cpu_pageset *pageset; 1077f6ac2354SChristoph Lameter 107899dcc3e5SChristoph Lameter pageset = per_cpu_ptr(zone->pageset, i); 1079f6ac2354SChristoph Lameter seq_printf(m, 10803dfa5721SChristoph Lameter "\n cpu: %i" 1081f6ac2354SChristoph Lameter "\n count: %i" 1082f6ac2354SChristoph Lameter "\n high: %i" 1083f6ac2354SChristoph Lameter "\n batch: %i", 10843dfa5721SChristoph Lameter i, 10853dfa5721SChristoph Lameter pageset->pcp.count, 10863dfa5721SChristoph Lameter pageset->pcp.high, 10873dfa5721SChristoph Lameter pageset->pcp.batch); 1088df9ecabaSChristoph Lameter #ifdef CONFIG_SMP 1089df9ecabaSChristoph Lameter seq_printf(m, "\n vm stats threshold: %d", 1090df9ecabaSChristoph Lameter pageset->stat_threshold); 1091df9ecabaSChristoph Lameter #endif 1092f6ac2354SChristoph Lameter } 1093f6ac2354SChristoph Lameter seq_printf(m, 1094f6ac2354SChristoph Lameter "\n all_unreclaimable: %u" 1095556adecbSRik van Riel "\n start_pfn: %lu" 1096556adecbSRik van Riel "\n inactive_ratio: %u", 10976e543d57SLisa Du !zone_reclaimable(zone), 1098556adecbSRik van Riel zone->zone_start_pfn, 1099556adecbSRik van Riel zone->inactive_ratio); 1100f6ac2354SChristoph Lameter seq_putc(m, '\n'); 1101f6ac2354SChristoph Lameter } 1102467c996cSMel Gorman 1103467c996cSMel Gorman /* 1104467c996cSMel Gorman * Output information about zones in @pgdat. 1105467c996cSMel Gorman */ 1106467c996cSMel Gorman static int zoneinfo_show(struct seq_file *m, void *arg) 1107467c996cSMel Gorman { 1108467c996cSMel Gorman pg_data_t *pgdat = (pg_data_t *)arg; 1109467c996cSMel Gorman walk_zones_in_node(m, pgdat, zoneinfo_show_print); 1110f6ac2354SChristoph Lameter return 0; 1111f6ac2354SChristoph Lameter } 1112f6ac2354SChristoph Lameter 11135c9fe628SAlexey Dobriyan static const struct seq_operations zoneinfo_op = { 1114f6ac2354SChristoph Lameter .start = frag_start, /* iterate over all zones. The same as in 1115f6ac2354SChristoph Lameter * fragmentation. */ 1116f6ac2354SChristoph Lameter .next = frag_next, 1117f6ac2354SChristoph Lameter .stop = frag_stop, 1118f6ac2354SChristoph Lameter .show = zoneinfo_show, 1119f6ac2354SChristoph Lameter }; 1120f6ac2354SChristoph Lameter 11215c9fe628SAlexey Dobriyan static int zoneinfo_open(struct inode *inode, struct file *file) 11225c9fe628SAlexey Dobriyan { 11235c9fe628SAlexey Dobriyan return seq_open(file, &zoneinfo_op); 11245c9fe628SAlexey Dobriyan } 11255c9fe628SAlexey Dobriyan 11265c9fe628SAlexey Dobriyan static const struct file_operations proc_zoneinfo_file_operations = { 11275c9fe628SAlexey Dobriyan .open = zoneinfo_open, 11285c9fe628SAlexey Dobriyan .read = seq_read, 11295c9fe628SAlexey Dobriyan .llseek = seq_lseek, 11305c9fe628SAlexey Dobriyan .release = seq_release, 11315c9fe628SAlexey Dobriyan }; 11325c9fe628SAlexey Dobriyan 113379da826aSMichael Rubin enum writeback_stat_item { 113479da826aSMichael Rubin NR_DIRTY_THRESHOLD, 113579da826aSMichael Rubin NR_DIRTY_BG_THRESHOLD, 113679da826aSMichael Rubin NR_VM_WRITEBACK_STAT_ITEMS, 113779da826aSMichael Rubin }; 113879da826aSMichael Rubin 1139f6ac2354SChristoph Lameter static void *vmstat_start(struct seq_file *m, loff_t *pos) 1140f6ac2354SChristoph Lameter { 11412244b95aSChristoph Lameter unsigned long *v; 114279da826aSMichael Rubin int i, stat_items_size; 1143f6ac2354SChristoph Lameter 1144f6ac2354SChristoph Lameter if (*pos >= ARRAY_SIZE(vmstat_text)) 1145f6ac2354SChristoph Lameter return NULL; 114679da826aSMichael Rubin stat_items_size = NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long) + 114779da826aSMichael Rubin NR_VM_WRITEBACK_STAT_ITEMS * sizeof(unsigned long); 1148f6ac2354SChristoph Lameter 1149f8891e5eSChristoph Lameter #ifdef CONFIG_VM_EVENT_COUNTERS 115079da826aSMichael Rubin stat_items_size += sizeof(struct vm_event_state); 1151f8891e5eSChristoph Lameter #endif 115279da826aSMichael Rubin 115379da826aSMichael Rubin v = kmalloc(stat_items_size, GFP_KERNEL); 11542244b95aSChristoph Lameter m->private = v; 11552244b95aSChristoph Lameter if (!v) 1156f6ac2354SChristoph Lameter return ERR_PTR(-ENOMEM); 11572244b95aSChristoph Lameter for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++) 11582244b95aSChristoph Lameter v[i] = global_page_state(i); 115979da826aSMichael Rubin v += NR_VM_ZONE_STAT_ITEMS; 116079da826aSMichael Rubin 116179da826aSMichael Rubin global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD, 116279da826aSMichael Rubin v + NR_DIRTY_THRESHOLD); 116379da826aSMichael Rubin v += NR_VM_WRITEBACK_STAT_ITEMS; 116479da826aSMichael Rubin 1165f8891e5eSChristoph Lameter #ifdef CONFIG_VM_EVENT_COUNTERS 116679da826aSMichael Rubin all_vm_events(v); 116779da826aSMichael Rubin v[PGPGIN] /= 2; /* sectors -> kbytes */ 116879da826aSMichael Rubin v[PGPGOUT] /= 2; 1169f8891e5eSChristoph Lameter #endif 1170ff8b16d7SWu Fengguang return (unsigned long *)m->private + *pos; 1171f6ac2354SChristoph Lameter } 1172f6ac2354SChristoph Lameter 1173f6ac2354SChristoph Lameter static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos) 1174f6ac2354SChristoph Lameter { 1175f6ac2354SChristoph Lameter (*pos)++; 1176f6ac2354SChristoph Lameter if (*pos >= ARRAY_SIZE(vmstat_text)) 1177f6ac2354SChristoph Lameter return NULL; 1178f6ac2354SChristoph Lameter return (unsigned long *)m->private + *pos; 1179f6ac2354SChristoph Lameter } 1180f6ac2354SChristoph Lameter 1181f6ac2354SChristoph Lameter static int vmstat_show(struct seq_file *m, void *arg) 1182f6ac2354SChristoph Lameter { 1183f6ac2354SChristoph Lameter unsigned long *l = arg; 1184f6ac2354SChristoph Lameter unsigned long off = l - (unsigned long *)m->private; 1185f6ac2354SChristoph Lameter 1186f6ac2354SChristoph Lameter seq_printf(m, "%s %lu\n", vmstat_text[off], *l); 1187f6ac2354SChristoph Lameter return 0; 1188f6ac2354SChristoph Lameter } 1189f6ac2354SChristoph Lameter 1190f6ac2354SChristoph Lameter static void vmstat_stop(struct seq_file *m, void *arg) 1191f6ac2354SChristoph Lameter { 1192f6ac2354SChristoph Lameter kfree(m->private); 1193f6ac2354SChristoph Lameter m->private = NULL; 1194f6ac2354SChristoph Lameter } 1195f6ac2354SChristoph Lameter 1196b6aa44abSAlexey Dobriyan static const struct seq_operations vmstat_op = { 1197f6ac2354SChristoph Lameter .start = vmstat_start, 1198f6ac2354SChristoph Lameter .next = vmstat_next, 1199f6ac2354SChristoph Lameter .stop = vmstat_stop, 1200f6ac2354SChristoph Lameter .show = vmstat_show, 1201f6ac2354SChristoph Lameter }; 1202f6ac2354SChristoph Lameter 1203b6aa44abSAlexey Dobriyan static int vmstat_open(struct inode *inode, struct file *file) 1204b6aa44abSAlexey Dobriyan { 1205b6aa44abSAlexey Dobriyan return seq_open(file, &vmstat_op); 1206b6aa44abSAlexey Dobriyan } 1207b6aa44abSAlexey Dobriyan 1208b6aa44abSAlexey Dobriyan static const struct file_operations proc_vmstat_file_operations = { 1209b6aa44abSAlexey Dobriyan .open = vmstat_open, 1210b6aa44abSAlexey Dobriyan .read = seq_read, 1211b6aa44abSAlexey Dobriyan .llseek = seq_lseek, 1212b6aa44abSAlexey Dobriyan .release = seq_release, 1213b6aa44abSAlexey Dobriyan }; 1214f6ac2354SChristoph Lameter #endif /* CONFIG_PROC_FS */ 1215f6ac2354SChristoph Lameter 1216df9ecabaSChristoph Lameter #ifdef CONFIG_SMP 1217d1187ed2SChristoph Lameter static DEFINE_PER_CPU(struct delayed_work, vmstat_work); 121877461ab3SChristoph Lameter int sysctl_stat_interval __read_mostly = HZ; 1219d1187ed2SChristoph Lameter 1220d1187ed2SChristoph Lameter static void vmstat_update(struct work_struct *w) 1221d1187ed2SChristoph Lameter { 1222fbc2edb0SChristoph Lameter refresh_cpu_vm_stats(); 122377461ab3SChristoph Lameter schedule_delayed_work(&__get_cpu_var(vmstat_work), 122498f4ebb2SAnton Blanchard round_jiffies_relative(sysctl_stat_interval)); 1225d1187ed2SChristoph Lameter } 1226d1187ed2SChristoph Lameter 12270db0628dSPaul Gortmaker static void start_cpu_timer(int cpu) 1228d1187ed2SChristoph Lameter { 12291871e52cSTejun Heo struct delayed_work *work = &per_cpu(vmstat_work, cpu); 1230d1187ed2SChristoph Lameter 1231203b42f7STejun Heo INIT_DEFERRABLE_WORK(work, vmstat_update); 12321871e52cSTejun Heo schedule_delayed_work_on(cpu, work, __round_jiffies_relative(HZ, cpu)); 1233d1187ed2SChristoph Lameter } 1234d1187ed2SChristoph Lameter 1235807a1bd2SToshi Kani static void vmstat_cpu_dead(int node) 1236807a1bd2SToshi Kani { 1237807a1bd2SToshi Kani int cpu; 1238807a1bd2SToshi Kani 1239807a1bd2SToshi Kani get_online_cpus(); 1240807a1bd2SToshi Kani for_each_online_cpu(cpu) 1241807a1bd2SToshi Kani if (cpu_to_node(cpu) == node) 1242807a1bd2SToshi Kani goto end; 1243807a1bd2SToshi Kani 1244807a1bd2SToshi Kani node_clear_state(node, N_CPU); 1245807a1bd2SToshi Kani end: 1246807a1bd2SToshi Kani put_online_cpus(); 1247807a1bd2SToshi Kani } 1248807a1bd2SToshi Kani 1249df9ecabaSChristoph Lameter /* 1250df9ecabaSChristoph Lameter * Use the cpu notifier to insure that the thresholds are recalculated 1251df9ecabaSChristoph Lameter * when necessary. 1252df9ecabaSChristoph Lameter */ 12530db0628dSPaul Gortmaker static int vmstat_cpuup_callback(struct notifier_block *nfb, 1254df9ecabaSChristoph Lameter unsigned long action, 1255df9ecabaSChristoph Lameter void *hcpu) 1256df9ecabaSChristoph Lameter { 1257d1187ed2SChristoph Lameter long cpu = (long)hcpu; 1258d1187ed2SChristoph Lameter 1259df9ecabaSChristoph Lameter switch (action) { 1260d1187ed2SChristoph Lameter case CPU_ONLINE: 1261d1187ed2SChristoph Lameter case CPU_ONLINE_FROZEN: 12625ee28a44SKAMEZAWA Hiroyuki refresh_zone_stat_thresholds(); 1263d1187ed2SChristoph Lameter start_cpu_timer(cpu); 1264ad596925SChristoph Lameter node_set_state(cpu_to_node(cpu), N_CPU); 1265d1187ed2SChristoph Lameter break; 1266d1187ed2SChristoph Lameter case CPU_DOWN_PREPARE: 1267d1187ed2SChristoph Lameter case CPU_DOWN_PREPARE_FROZEN: 1268afe2c511STejun Heo cancel_delayed_work_sync(&per_cpu(vmstat_work, cpu)); 1269d1187ed2SChristoph Lameter per_cpu(vmstat_work, cpu).work.func = NULL; 1270d1187ed2SChristoph Lameter break; 1271d1187ed2SChristoph Lameter case CPU_DOWN_FAILED: 1272d1187ed2SChristoph Lameter case CPU_DOWN_FAILED_FROZEN: 1273d1187ed2SChristoph Lameter start_cpu_timer(cpu); 1274d1187ed2SChristoph Lameter break; 1275df9ecabaSChristoph Lameter case CPU_DEAD: 12768bb78442SRafael J. Wysocki case CPU_DEAD_FROZEN: 1277df9ecabaSChristoph Lameter refresh_zone_stat_thresholds(); 1278807a1bd2SToshi Kani vmstat_cpu_dead(cpu_to_node(cpu)); 1279df9ecabaSChristoph Lameter break; 1280df9ecabaSChristoph Lameter default: 1281df9ecabaSChristoph Lameter break; 1282df9ecabaSChristoph Lameter } 1283df9ecabaSChristoph Lameter return NOTIFY_OK; 1284df9ecabaSChristoph Lameter } 1285df9ecabaSChristoph Lameter 12860db0628dSPaul Gortmaker static struct notifier_block vmstat_notifier = 1287df9ecabaSChristoph Lameter { &vmstat_cpuup_callback, NULL, 0 }; 12888f32f7e5SAlexey Dobriyan #endif 1289df9ecabaSChristoph Lameter 1290e2fc88d0SAdrian Bunk static int __init setup_vmstat(void) 1291df9ecabaSChristoph Lameter { 12928f32f7e5SAlexey Dobriyan #ifdef CONFIG_SMP 1293d1187ed2SChristoph Lameter int cpu; 1294d1187ed2SChristoph Lameter 1295df9ecabaSChristoph Lameter register_cpu_notifier(&vmstat_notifier); 1296d1187ed2SChristoph Lameter 1297d7e0b37aSToshi Kani get_online_cpus(); 1298d7e0b37aSToshi Kani for_each_online_cpu(cpu) { 1299d1187ed2SChristoph Lameter start_cpu_timer(cpu); 1300d7e0b37aSToshi Kani node_set_state(cpu_to_node(cpu), N_CPU); 1301d7e0b37aSToshi Kani } 1302d7e0b37aSToshi Kani put_online_cpus(); 13038f32f7e5SAlexey Dobriyan #endif 13048f32f7e5SAlexey Dobriyan #ifdef CONFIG_PROC_FS 13058f32f7e5SAlexey Dobriyan proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations); 130674e2e8e8SAlexey Dobriyan proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops); 1307b6aa44abSAlexey Dobriyan proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations); 13085c9fe628SAlexey Dobriyan proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations); 13098f32f7e5SAlexey Dobriyan #endif 1310df9ecabaSChristoph Lameter return 0; 1311df9ecabaSChristoph Lameter } 1312df9ecabaSChristoph Lameter module_init(setup_vmstat) 1313d7a5752cSMel Gorman 1314d7a5752cSMel Gorman #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION) 1315d7a5752cSMel Gorman #include <linux/debugfs.h> 1316d7a5752cSMel Gorman 1317d7a5752cSMel Gorman 1318d7a5752cSMel Gorman /* 1319d7a5752cSMel Gorman * Return an index indicating how much of the available free memory is 1320d7a5752cSMel Gorman * unusable for an allocation of the requested size. 1321d7a5752cSMel Gorman */ 1322d7a5752cSMel Gorman static int unusable_free_index(unsigned int order, 1323d7a5752cSMel Gorman struct contig_page_info *info) 1324d7a5752cSMel Gorman { 1325d7a5752cSMel Gorman /* No free memory is interpreted as all free memory is unusable */ 1326d7a5752cSMel Gorman if (info->free_pages == 0) 1327d7a5752cSMel Gorman return 1000; 1328d7a5752cSMel Gorman 1329d7a5752cSMel Gorman /* 1330d7a5752cSMel Gorman * Index should be a value between 0 and 1. Return a value to 3 1331d7a5752cSMel Gorman * decimal places. 1332d7a5752cSMel Gorman * 1333d7a5752cSMel Gorman * 0 => no fragmentation 1334d7a5752cSMel Gorman * 1 => high fragmentation 1335d7a5752cSMel Gorman */ 1336d7a5752cSMel Gorman return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages); 1337d7a5752cSMel Gorman 1338d7a5752cSMel Gorman } 1339d7a5752cSMel Gorman 1340d7a5752cSMel Gorman static void unusable_show_print(struct seq_file *m, 1341d7a5752cSMel Gorman pg_data_t *pgdat, struct zone *zone) 1342d7a5752cSMel Gorman { 1343d7a5752cSMel Gorman unsigned int order; 1344d7a5752cSMel Gorman int index; 1345d7a5752cSMel Gorman struct contig_page_info info; 1346d7a5752cSMel Gorman 1347d7a5752cSMel Gorman seq_printf(m, "Node %d, zone %8s ", 1348d7a5752cSMel Gorman pgdat->node_id, 1349d7a5752cSMel Gorman zone->name); 1350d7a5752cSMel Gorman for (order = 0; order < MAX_ORDER; ++order) { 1351d7a5752cSMel Gorman fill_contig_page_info(zone, order, &info); 1352d7a5752cSMel Gorman index = unusable_free_index(order, &info); 1353d7a5752cSMel Gorman seq_printf(m, "%d.%03d ", index / 1000, index % 1000); 1354d7a5752cSMel Gorman } 1355d7a5752cSMel Gorman 1356d7a5752cSMel Gorman seq_putc(m, '\n'); 1357d7a5752cSMel Gorman } 1358d7a5752cSMel Gorman 1359d7a5752cSMel Gorman /* 1360d7a5752cSMel Gorman * Display unusable free space index 1361d7a5752cSMel Gorman * 1362d7a5752cSMel Gorman * The unusable free space index measures how much of the available free 1363d7a5752cSMel Gorman * memory cannot be used to satisfy an allocation of a given size and is a 1364d7a5752cSMel Gorman * value between 0 and 1. The higher the value, the more of free memory is 1365d7a5752cSMel Gorman * unusable and by implication, the worse the external fragmentation is. This 1366d7a5752cSMel Gorman * can be expressed as a percentage by multiplying by 100. 1367d7a5752cSMel Gorman */ 1368d7a5752cSMel Gorman static int unusable_show(struct seq_file *m, void *arg) 1369d7a5752cSMel Gorman { 1370d7a5752cSMel Gorman pg_data_t *pgdat = (pg_data_t *)arg; 1371d7a5752cSMel Gorman 1372d7a5752cSMel Gorman /* check memoryless node */ 1373a47b53c5SLai Jiangshan if (!node_state(pgdat->node_id, N_MEMORY)) 1374d7a5752cSMel Gorman return 0; 1375d7a5752cSMel Gorman 1376d7a5752cSMel Gorman walk_zones_in_node(m, pgdat, unusable_show_print); 1377d7a5752cSMel Gorman 1378d7a5752cSMel Gorman return 0; 1379d7a5752cSMel Gorman } 1380d7a5752cSMel Gorman 1381d7a5752cSMel Gorman static const struct seq_operations unusable_op = { 1382d7a5752cSMel Gorman .start = frag_start, 1383d7a5752cSMel Gorman .next = frag_next, 1384d7a5752cSMel Gorman .stop = frag_stop, 1385d7a5752cSMel Gorman .show = unusable_show, 1386d7a5752cSMel Gorman }; 1387d7a5752cSMel Gorman 1388d7a5752cSMel Gorman static int unusable_open(struct inode *inode, struct file *file) 1389d7a5752cSMel Gorman { 1390d7a5752cSMel Gorman return seq_open(file, &unusable_op); 1391d7a5752cSMel Gorman } 1392d7a5752cSMel Gorman 1393d7a5752cSMel Gorman static const struct file_operations unusable_file_ops = { 1394d7a5752cSMel Gorman .open = unusable_open, 1395d7a5752cSMel Gorman .read = seq_read, 1396d7a5752cSMel Gorman .llseek = seq_lseek, 1397d7a5752cSMel Gorman .release = seq_release, 1398d7a5752cSMel Gorman }; 1399d7a5752cSMel Gorman 1400f1a5ab12SMel Gorman static void extfrag_show_print(struct seq_file *m, 1401f1a5ab12SMel Gorman pg_data_t *pgdat, struct zone *zone) 1402f1a5ab12SMel Gorman { 1403f1a5ab12SMel Gorman unsigned int order; 1404f1a5ab12SMel Gorman int index; 1405f1a5ab12SMel Gorman 1406f1a5ab12SMel Gorman /* Alloc on stack as interrupts are disabled for zone walk */ 1407f1a5ab12SMel Gorman struct contig_page_info info; 1408f1a5ab12SMel Gorman 1409f1a5ab12SMel Gorman seq_printf(m, "Node %d, zone %8s ", 1410f1a5ab12SMel Gorman pgdat->node_id, 1411f1a5ab12SMel Gorman zone->name); 1412f1a5ab12SMel Gorman for (order = 0; order < MAX_ORDER; ++order) { 1413f1a5ab12SMel Gorman fill_contig_page_info(zone, order, &info); 141456de7263SMel Gorman index = __fragmentation_index(order, &info); 1415f1a5ab12SMel Gorman seq_printf(m, "%d.%03d ", index / 1000, index % 1000); 1416f1a5ab12SMel Gorman } 1417f1a5ab12SMel Gorman 1418f1a5ab12SMel Gorman seq_putc(m, '\n'); 1419f1a5ab12SMel Gorman } 1420f1a5ab12SMel Gorman 1421f1a5ab12SMel Gorman /* 1422f1a5ab12SMel Gorman * Display fragmentation index for orders that allocations would fail for 1423f1a5ab12SMel Gorman */ 1424f1a5ab12SMel Gorman static int extfrag_show(struct seq_file *m, void *arg) 1425f1a5ab12SMel Gorman { 1426f1a5ab12SMel Gorman pg_data_t *pgdat = (pg_data_t *)arg; 1427f1a5ab12SMel Gorman 1428f1a5ab12SMel Gorman walk_zones_in_node(m, pgdat, extfrag_show_print); 1429f1a5ab12SMel Gorman 1430f1a5ab12SMel Gorman return 0; 1431f1a5ab12SMel Gorman } 1432f1a5ab12SMel Gorman 1433f1a5ab12SMel Gorman static const struct seq_operations extfrag_op = { 1434f1a5ab12SMel Gorman .start = frag_start, 1435f1a5ab12SMel Gorman .next = frag_next, 1436f1a5ab12SMel Gorman .stop = frag_stop, 1437f1a5ab12SMel Gorman .show = extfrag_show, 1438f1a5ab12SMel Gorman }; 1439f1a5ab12SMel Gorman 1440f1a5ab12SMel Gorman static int extfrag_open(struct inode *inode, struct file *file) 1441f1a5ab12SMel Gorman { 1442f1a5ab12SMel Gorman return seq_open(file, &extfrag_op); 1443f1a5ab12SMel Gorman } 1444f1a5ab12SMel Gorman 1445f1a5ab12SMel Gorman static const struct file_operations extfrag_file_ops = { 1446f1a5ab12SMel Gorman .open = extfrag_open, 1447f1a5ab12SMel Gorman .read = seq_read, 1448f1a5ab12SMel Gorman .llseek = seq_lseek, 1449f1a5ab12SMel Gorman .release = seq_release, 1450f1a5ab12SMel Gorman }; 1451f1a5ab12SMel Gorman 1452d7a5752cSMel Gorman static int __init extfrag_debug_init(void) 1453d7a5752cSMel Gorman { 1454bde8bd8aSSasikantha babu struct dentry *extfrag_debug_root; 1455bde8bd8aSSasikantha babu 1456d7a5752cSMel Gorman extfrag_debug_root = debugfs_create_dir("extfrag", NULL); 1457d7a5752cSMel Gorman if (!extfrag_debug_root) 1458d7a5752cSMel Gorman return -ENOMEM; 1459d7a5752cSMel Gorman 1460d7a5752cSMel Gorman if (!debugfs_create_file("unusable_index", 0444, 1461d7a5752cSMel Gorman extfrag_debug_root, NULL, &unusable_file_ops)) 1462bde8bd8aSSasikantha babu goto fail; 1463d7a5752cSMel Gorman 1464f1a5ab12SMel Gorman if (!debugfs_create_file("extfrag_index", 0444, 1465f1a5ab12SMel Gorman extfrag_debug_root, NULL, &extfrag_file_ops)) 1466bde8bd8aSSasikantha babu goto fail; 1467f1a5ab12SMel Gorman 1468d7a5752cSMel Gorman return 0; 1469bde8bd8aSSasikantha babu fail: 1470bde8bd8aSSasikantha babu debugfs_remove_recursive(extfrag_debug_root); 1471bde8bd8aSSasikantha babu return -ENOMEM; 1472d7a5752cSMel Gorman } 1473d7a5752cSMel Gorman 1474d7a5752cSMel Gorman module_init(extfrag_debug_init); 1475d7a5752cSMel Gorman #endif 1476