4 * Manages VM statistics
5 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
8 * Copyright (C) 2006 Silicon Graphics, Inc.,
9 * Christoph Lameter <christoph@lameter.com>
13 #include <linux/err.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/cpu.h>
17 #include <linux/vmstat.h>
18 #include <linux/sched.h>
19 #include <linux/math64.h>
20 #include <linux/writeback.h>
22 #ifdef CONFIG_VM_EVENT_COUNTERS
23 DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
24 EXPORT_PER_CPU_SYMBOL(vm_event_states);
26 static void sum_vm_events(unsigned long *ret)
31 memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
33 for_each_online_cpu(cpu) {
34 struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
36 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
37 ret[i] += this->event[i];
42 * Accumulate the vm event counters across all CPUs.
43 * The result is unavoidably approximate - it can change
44 * during and after execution of this function.
46 void all_vm_events(unsigned long *ret)
52 EXPORT_SYMBOL_GPL(all_vm_events);
56 * Fold the foreign cpu events into our own.
58 * This is adding to the events on one processor
59 * but keeps the global counts constant.
61 void vm_events_fold_cpu(int cpu)
63 struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu);
66 for (i = 0; i < NR_VM_EVENT_ITEMS; i++) {
67 count_vm_events(i, fold_state->event[i]);
68 fold_state->event[i] = 0;
71 #endif /* CONFIG_HOTPLUG */
73 #endif /* CONFIG_VM_EVENT_COUNTERS */
76 * Manage combined zone based / global counters
78 * vm_stat contains the global counters
80 atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
81 EXPORT_SYMBOL(vm_stat);
85 static int calculate_threshold(struct zone *zone)
88 int mem; /* memory in 128 MB units */
91 * The threshold scales with the number of processors and the amount
92 * of memory per zone. More memory means that we can defer updates for
93 * longer, more processors could lead to more contention.
94 * fls() is used to have a cheap way of logarithmic scaling.
96 * Some sample thresholds:
98 * Threshold Processors (fls) Zonesize fls(mem+1)
99 * ------------------------------------------------------------------
116 * 125 1024 10 8-16 GB 8
117 * 125 1024 10 16-32 GB 9
120 mem = zone->present_pages >> (27 - PAGE_SHIFT);
122 threshold = 2 * fls(num_online_cpus()) * (1 + fls(mem));
125 * Maximum threshold is 125
127 threshold = min(125, threshold);
133 * Refresh the thresholds for each zone.
135 static void refresh_zone_stat_thresholds(void)
141 for_each_populated_zone(zone) {
142 unsigned long max_drift, tolerate_drift;
144 threshold = calculate_threshold(zone);
146 for_each_online_cpu(cpu)
147 per_cpu_ptr(zone->pageset, cpu)->stat_threshold
151 * Only set percpu_drift_mark if there is a danger that
152 * NR_FREE_PAGES reports the low watermark is ok when in fact
153 * the min watermark could be breached by an allocation
155 tolerate_drift = low_wmark_pages(zone) - min_wmark_pages(zone);
156 max_drift = num_online_cpus() * threshold;
157 if (max_drift > tolerate_drift)
158 zone->percpu_drift_mark = high_wmark_pages(zone) +
164 * For use when we know that interrupts are disabled.
166 void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
169 struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
171 s8 *p = pcp->vm_stat_diff + item;
176 if (unlikely(x > pcp->stat_threshold || x < -pcp->stat_threshold)) {
177 zone_page_state_add(x, zone, item);
182 EXPORT_SYMBOL(__mod_zone_page_state);
185 * For an unknown interrupt state
187 void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
192 local_irq_save(flags);
193 __mod_zone_page_state(zone, item, delta);
194 local_irq_restore(flags);
196 EXPORT_SYMBOL(mod_zone_page_state);
199 * Optimized increment and decrement functions.
201 * These are only for a single page and therefore can take a struct page *
202 * argument instead of struct zone *. This allows the inclusion of the code
203 * generated for page_zone(page) into the optimized functions.
205 * No overflow check is necessary and therefore the differential can be
206 * incremented or decremented in place which may allow the compilers to
207 * generate better code.
208 * The increment or decrement is known and therefore one boundary check can
211 * NOTE: These functions are very performance sensitive. Change only
214 * Some processors have inc/dec instructions that are atomic vs an interrupt.
215 * However, the code must first determine the differential location in a zone
216 * based on the processor number and then inc/dec the counter. There is no
217 * guarantee without disabling preemption that the processor will not change
218 * in between and therefore the atomicity vs. interrupt cannot be exploited
219 * in a useful way here.
221 void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
223 struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
224 s8 *p = pcp->vm_stat_diff + item;
228 if (unlikely(*p > pcp->stat_threshold)) {
229 int overstep = pcp->stat_threshold / 2;
231 zone_page_state_add(*p + overstep, zone, item);
236 void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
238 __inc_zone_state(page_zone(page), item);
240 EXPORT_SYMBOL(__inc_zone_page_state);
242 void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
244 struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
245 s8 *p = pcp->vm_stat_diff + item;
249 if (unlikely(*p < - pcp->stat_threshold)) {
250 int overstep = pcp->stat_threshold / 2;
252 zone_page_state_add(*p - overstep, zone, item);
257 void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
259 __dec_zone_state(page_zone(page), item);
261 EXPORT_SYMBOL(__dec_zone_page_state);
263 void inc_zone_state(struct zone *zone, enum zone_stat_item item)
267 local_irq_save(flags);
268 __inc_zone_state(zone, item);
269 local_irq_restore(flags);
272 void inc_zone_page_state(struct page *page, enum zone_stat_item item)
277 zone = page_zone(page);
278 local_irq_save(flags);
279 __inc_zone_state(zone, item);
280 local_irq_restore(flags);
282 EXPORT_SYMBOL(inc_zone_page_state);
284 void dec_zone_page_state(struct page *page, enum zone_stat_item item)
288 local_irq_save(flags);
289 __dec_zone_page_state(page, item);
290 local_irq_restore(flags);
292 EXPORT_SYMBOL(dec_zone_page_state);
295 * Update the zone counters for one cpu.
297 * The cpu specified must be either the current cpu or a processor that
298 * is not online. If it is the current cpu then the execution thread must
299 * be pinned to the current cpu.
301 * Note that refresh_cpu_vm_stats strives to only access
302 * node local memory. The per cpu pagesets on remote zones are placed
303 * in the memory local to the processor using that pageset. So the
304 * loop over all zones will access a series of cachelines local to
307 * The call to zone_page_state_add updates the cachelines with the
308 * statistics in the remote zone struct as well as the global cachelines
309 * with the global counters. These could cause remote node cache line
310 * bouncing and will have to be only done when necessary.
312 void refresh_cpu_vm_stats(int cpu)
316 int global_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
318 for_each_populated_zone(zone) {
319 struct per_cpu_pageset *p;
321 p = per_cpu_ptr(zone->pageset, cpu);
323 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
324 if (p->vm_stat_diff[i]) {
328 local_irq_save(flags);
329 v = p->vm_stat_diff[i];
330 p->vm_stat_diff[i] = 0;
331 local_irq_restore(flags);
332 atomic_long_add(v, &zone->vm_stat[i]);
335 /* 3 seconds idle till flush */
342 * Deal with draining the remote pageset of this
345 * Check if there are pages remaining in this pageset
346 * if not then there is nothing to expire.
348 if (!p->expire || !p->pcp.count)
352 * We never drain zones local to this processor.
354 if (zone_to_nid(zone) == numa_node_id()) {
364 drain_zone_pages(zone, &p->pcp);
368 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
370 atomic_long_add(global_diff[i], &vm_stat[i]);
377 * zonelist = the list of zones passed to the allocator
378 * z = the zone from which the allocation occurred.
380 * Must be called with interrupts disabled.
382 void zone_statistics(struct zone *preferred_zone, struct zone *z)
384 if (z->zone_pgdat == preferred_zone->zone_pgdat) {
385 __inc_zone_state(z, NUMA_HIT);
387 __inc_zone_state(z, NUMA_MISS);
388 __inc_zone_state(preferred_zone, NUMA_FOREIGN);
390 if (z->node == numa_node_id())
391 __inc_zone_state(z, NUMA_LOCAL);
393 __inc_zone_state(z, NUMA_OTHER);
397 #ifdef CONFIG_COMPACTION
398 struct contig_page_info {
399 unsigned long free_pages;
400 unsigned long free_blocks_total;
401 unsigned long free_blocks_suitable;
405 * Calculate the number of free pages in a zone, how many contiguous
406 * pages are free and how many are large enough to satisfy an allocation of
407 * the target size. Note that this function makes no attempt to estimate
408 * how many suitable free blocks there *might* be if MOVABLE pages were
409 * migrated. Calculating that is possible, but expensive and can be
410 * figured out from userspace
412 static void fill_contig_page_info(struct zone *zone,
413 unsigned int suitable_order,
414 struct contig_page_info *info)
418 info->free_pages = 0;
419 info->free_blocks_total = 0;
420 info->free_blocks_suitable = 0;
422 for (order = 0; order < MAX_ORDER; order++) {
423 unsigned long blocks;
425 /* Count number of free blocks */
426 blocks = zone->free_area[order].nr_free;
427 info->free_blocks_total += blocks;
429 /* Count free base pages */
430 info->free_pages += blocks << order;
432 /* Count the suitable free blocks */
433 if (order >= suitable_order)
434 info->free_blocks_suitable += blocks <<
435 (order - suitable_order);
440 * A fragmentation index only makes sense if an allocation of a requested
441 * size would fail. If that is true, the fragmentation index indicates
442 * whether external fragmentation or a lack of memory was the problem.
443 * The value can be used to determine if page reclaim or compaction
446 static int __fragmentation_index(unsigned int order, struct contig_page_info *info)
448 unsigned long requested = 1UL << order;
450 if (!info->free_blocks_total)
453 /* Fragmentation index only makes sense when a request would fail */
454 if (info->free_blocks_suitable)
458 * Index is between 0 and 1 so return within 3 decimal places
460 * 0 => allocation would fail due to lack of memory
461 * 1 => allocation would fail due to fragmentation
463 return 1000 - div_u64( (1000+(div_u64(info->free_pages * 1000ULL, requested))), info->free_blocks_total);
466 /* Same as __fragmentation index but allocs contig_page_info on stack */
467 int fragmentation_index(struct zone *zone, unsigned int order)
469 struct contig_page_info info;
471 fill_contig_page_info(zone, order, &info);
472 return __fragmentation_index(order, &info);
476 #if defined(CONFIG_PROC_FS) || defined(CONFIG_COMPACTION)
477 #include <linux/proc_fs.h>
478 #include <linux/seq_file.h>
480 static char * const migratetype_names[MIGRATE_TYPES] = {
488 static void *frag_start(struct seq_file *m, loff_t *pos)
492 for (pgdat = first_online_pgdat();
494 pgdat = next_online_pgdat(pgdat))
500 static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
502 pg_data_t *pgdat = (pg_data_t *)arg;
505 return next_online_pgdat(pgdat);
508 static void frag_stop(struct seq_file *m, void *arg)
512 /* Walk all the zones in a node and print using a callback */
513 static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat,
514 void (*print)(struct seq_file *m, pg_data_t *, struct zone *))
517 struct zone *node_zones = pgdat->node_zones;
520 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
521 if (!populated_zone(zone))
524 spin_lock_irqsave(&zone->lock, flags);
525 print(m, pgdat, zone);
526 spin_unlock_irqrestore(&zone->lock, flags);
531 #ifdef CONFIG_PROC_FS
532 static void frag_show_print(struct seq_file *m, pg_data_t *pgdat,
537 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
538 for (order = 0; order < MAX_ORDER; ++order)
539 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
544 * This walks the free areas for each zone.
546 static int frag_show(struct seq_file *m, void *arg)
548 pg_data_t *pgdat = (pg_data_t *)arg;
549 walk_zones_in_node(m, pgdat, frag_show_print);
553 static void pagetypeinfo_showfree_print(struct seq_file *m,
554 pg_data_t *pgdat, struct zone *zone)
558 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
559 seq_printf(m, "Node %4d, zone %8s, type %12s ",
562 migratetype_names[mtype]);
563 for (order = 0; order < MAX_ORDER; ++order) {
564 unsigned long freecount = 0;
565 struct free_area *area;
566 struct list_head *curr;
568 area = &(zone->free_area[order]);
570 list_for_each(curr, &area->free_list[mtype])
572 seq_printf(m, "%6lu ", freecount);
578 /* Print out the free pages at each order for each migatetype */
579 static int pagetypeinfo_showfree(struct seq_file *m, void *arg)
582 pg_data_t *pgdat = (pg_data_t *)arg;
585 seq_printf(m, "%-43s ", "Free pages count per migrate type at order");
586 for (order = 0; order < MAX_ORDER; ++order)
587 seq_printf(m, "%6d ", order);
590 walk_zones_in_node(m, pgdat, pagetypeinfo_showfree_print);
595 static void pagetypeinfo_showblockcount_print(struct seq_file *m,
596 pg_data_t *pgdat, struct zone *zone)
600 unsigned long start_pfn = zone->zone_start_pfn;
601 unsigned long end_pfn = start_pfn + zone->spanned_pages;
602 unsigned long count[MIGRATE_TYPES] = { 0, };
604 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
610 page = pfn_to_page(pfn);
612 /* Watch for unexpected holes punched in the memmap */
613 if (!memmap_valid_within(pfn, page, zone))
616 mtype = get_pageblock_migratetype(page);
618 if (mtype < MIGRATE_TYPES)
623 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
624 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
625 seq_printf(m, "%12lu ", count[mtype]);
629 /* Print out the free pages at each order for each migratetype */
630 static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg)
633 pg_data_t *pgdat = (pg_data_t *)arg;
635 seq_printf(m, "\n%-23s", "Number of blocks type ");
636 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
637 seq_printf(m, "%12s ", migratetype_names[mtype]);
639 walk_zones_in_node(m, pgdat, pagetypeinfo_showblockcount_print);
645 * This prints out statistics in relation to grouping pages by mobility.
646 * It is expensive to collect so do not constantly read the file.
648 static int pagetypeinfo_show(struct seq_file *m, void *arg)
650 pg_data_t *pgdat = (pg_data_t *)arg;
652 /* check memoryless node */
653 if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
656 seq_printf(m, "Page block order: %d\n", pageblock_order);
657 seq_printf(m, "Pages per block: %lu\n", pageblock_nr_pages);
659 pagetypeinfo_showfree(m, pgdat);
660 pagetypeinfo_showblockcount(m, pgdat);
665 static const struct seq_operations fragmentation_op = {
672 static int fragmentation_open(struct inode *inode, struct file *file)
674 return seq_open(file, &fragmentation_op);
677 static const struct file_operations fragmentation_file_operations = {
678 .open = fragmentation_open,
681 .release = seq_release,
684 static const struct seq_operations pagetypeinfo_op = {
688 .show = pagetypeinfo_show,
691 static int pagetypeinfo_open(struct inode *inode, struct file *file)
693 return seq_open(file, &pagetypeinfo_op);
696 static const struct file_operations pagetypeinfo_file_ops = {
697 .open = pagetypeinfo_open,
700 .release = seq_release,
703 #ifdef CONFIG_ZONE_DMA
704 #define TEXT_FOR_DMA(xx) xx "_dma",
706 #define TEXT_FOR_DMA(xx)
709 #ifdef CONFIG_ZONE_DMA32
710 #define TEXT_FOR_DMA32(xx) xx "_dma32",
712 #define TEXT_FOR_DMA32(xx)
715 #ifdef CONFIG_HIGHMEM
716 #define TEXT_FOR_HIGHMEM(xx) xx "_high",
718 #define TEXT_FOR_HIGHMEM(xx)
721 #define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
722 TEXT_FOR_HIGHMEM(xx) xx "_movable",
724 static const char * const vmstat_text[] = {
725 /* Zoned VM counters */
738 "nr_slab_reclaimable",
739 "nr_slab_unreclaimable",
740 "nr_page_table_pages",
751 "nr_dirty_threshold",
752 "nr_dirty_background_threshold",
763 #ifdef CONFIG_VM_EVENT_COUNTERS
769 TEXTS_FOR_ZONES("pgalloc")
778 TEXTS_FOR_ZONES("pgrefill")
779 TEXTS_FOR_ZONES("pgsteal")
780 TEXTS_FOR_ZONES("pgscan_kswapd")
781 TEXTS_FOR_ZONES("pgscan_direct")
784 "zone_reclaim_failed",
790 "kswapd_low_wmark_hit_quickly",
791 "kswapd_high_wmark_hit_quickly",
792 "kswapd_skip_congestion_wait",
798 #ifdef CONFIG_COMPACTION
799 "compact_blocks_moved",
800 "compact_pages_moved",
801 "compact_pagemigrate_failed",
807 #ifdef CONFIG_HUGETLB_PAGE
808 "htlb_buddy_alloc_success",
809 "htlb_buddy_alloc_fail",
811 "unevictable_pgs_culled",
812 "unevictable_pgs_scanned",
813 "unevictable_pgs_rescued",
814 "unevictable_pgs_mlocked",
815 "unevictable_pgs_munlocked",
816 "unevictable_pgs_cleared",
817 "unevictable_pgs_stranded",
818 "unevictable_pgs_mlockfreed",
822 static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
826 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
835 zone_nr_free_pages(zone),
836 min_wmark_pages(zone),
837 low_wmark_pages(zone),
838 high_wmark_pages(zone),
841 zone->present_pages);
843 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
844 seq_printf(m, "\n %-12s %lu", vmstat_text[i],
845 zone_page_state(zone, i));
848 "\n protection: (%lu",
849 zone->lowmem_reserve[0]);
850 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
851 seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
855 for_each_online_cpu(i) {
856 struct per_cpu_pageset *pageset;
858 pageset = per_cpu_ptr(zone->pageset, i);
869 seq_printf(m, "\n vm stats threshold: %d",
870 pageset->stat_threshold);
874 "\n all_unreclaimable: %u"
876 "\n inactive_ratio: %u",
877 zone->all_unreclaimable,
878 zone->zone_start_pfn,
879 zone->inactive_ratio);
884 * Output information about zones in @pgdat.
886 static int zoneinfo_show(struct seq_file *m, void *arg)
888 pg_data_t *pgdat = (pg_data_t *)arg;
889 walk_zones_in_node(m, pgdat, zoneinfo_show_print);
893 static const struct seq_operations zoneinfo_op = {
894 .start = frag_start, /* iterate over all zones. The same as in
898 .show = zoneinfo_show,
901 static int zoneinfo_open(struct inode *inode, struct file *file)
903 return seq_open(file, &zoneinfo_op);
906 static const struct file_operations proc_zoneinfo_file_operations = {
907 .open = zoneinfo_open,
910 .release = seq_release,
913 enum writeback_stat_item {
915 NR_DIRTY_BG_THRESHOLD,
916 NR_VM_WRITEBACK_STAT_ITEMS,
919 static void *vmstat_start(struct seq_file *m, loff_t *pos)
922 int i, stat_items_size;
924 if (*pos >= ARRAY_SIZE(vmstat_text))
926 stat_items_size = NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long) +
927 NR_VM_WRITEBACK_STAT_ITEMS * sizeof(unsigned long);
929 #ifdef CONFIG_VM_EVENT_COUNTERS
930 stat_items_size += sizeof(struct vm_event_state);
933 v = kmalloc(stat_items_size, GFP_KERNEL);
936 return ERR_PTR(-ENOMEM);
937 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
938 v[i] = global_page_state(i);
939 v += NR_VM_ZONE_STAT_ITEMS;
941 global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD,
942 v + NR_DIRTY_THRESHOLD);
943 v += NR_VM_WRITEBACK_STAT_ITEMS;
945 #ifdef CONFIG_VM_EVENT_COUNTERS
947 v[PGPGIN] /= 2; /* sectors -> kbytes */
950 return m->private + *pos;
953 static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
956 if (*pos >= ARRAY_SIZE(vmstat_text))
958 return (unsigned long *)m->private + *pos;
961 static int vmstat_show(struct seq_file *m, void *arg)
963 unsigned long *l = arg;
964 unsigned long off = l - (unsigned long *)m->private;
966 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
970 static void vmstat_stop(struct seq_file *m, void *arg)
976 static const struct seq_operations vmstat_op = {
977 .start = vmstat_start,
983 static int vmstat_open(struct inode *inode, struct file *file)
985 return seq_open(file, &vmstat_op);
988 static const struct file_operations proc_vmstat_file_operations = {
992 .release = seq_release,
994 #endif /* CONFIG_PROC_FS */
997 static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
998 int sysctl_stat_interval __read_mostly = HZ;
1000 static void vmstat_update(struct work_struct *w)
1002 refresh_cpu_vm_stats(smp_processor_id());
1003 schedule_delayed_work(&__get_cpu_var(vmstat_work),
1004 round_jiffies_relative(sysctl_stat_interval));
1007 static void __cpuinit start_cpu_timer(int cpu)
1009 struct delayed_work *work = &per_cpu(vmstat_work, cpu);
1011 INIT_DELAYED_WORK_DEFERRABLE(work, vmstat_update);
1012 schedule_delayed_work_on(cpu, work, __round_jiffies_relative(HZ, cpu));
1016 * Use the cpu notifier to insure that the thresholds are recalculated
1019 static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
1020 unsigned long action,
1023 long cpu = (long)hcpu;
1027 case CPU_ONLINE_FROZEN:
1028 refresh_zone_stat_thresholds();
1029 start_cpu_timer(cpu);
1030 node_set_state(cpu_to_node(cpu), N_CPU);
1032 case CPU_DOWN_PREPARE:
1033 case CPU_DOWN_PREPARE_FROZEN:
1034 cancel_rearming_delayed_work(&per_cpu(vmstat_work, cpu));
1035 per_cpu(vmstat_work, cpu).work.func = NULL;
1037 case CPU_DOWN_FAILED:
1038 case CPU_DOWN_FAILED_FROZEN:
1039 start_cpu_timer(cpu);
1042 case CPU_DEAD_FROZEN:
1043 refresh_zone_stat_thresholds();
1051 static struct notifier_block __cpuinitdata vmstat_notifier =
1052 { &vmstat_cpuup_callback, NULL, 0 };
1055 static int __init setup_vmstat(void)
1060 refresh_zone_stat_thresholds();
1061 register_cpu_notifier(&vmstat_notifier);
1063 for_each_online_cpu(cpu)
1064 start_cpu_timer(cpu);
1066 #ifdef CONFIG_PROC_FS
1067 proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
1068 proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
1069 proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
1070 proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
1074 module_init(setup_vmstat)
1076 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
1077 #include <linux/debugfs.h>
1079 static struct dentry *extfrag_debug_root;
1082 * Return an index indicating how much of the available free memory is
1083 * unusable for an allocation of the requested size.
1085 static int unusable_free_index(unsigned int order,
1086 struct contig_page_info *info)
1088 /* No free memory is interpreted as all free memory is unusable */
1089 if (info->free_pages == 0)
1093 * Index should be a value between 0 and 1. Return a value to 3
1096 * 0 => no fragmentation
1097 * 1 => high fragmentation
1099 return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages);
1103 static void unusable_show_print(struct seq_file *m,
1104 pg_data_t *pgdat, struct zone *zone)
1108 struct contig_page_info info;
1110 seq_printf(m, "Node %d, zone %8s ",
1113 for (order = 0; order < MAX_ORDER; ++order) {
1114 fill_contig_page_info(zone, order, &info);
1115 index = unusable_free_index(order, &info);
1116 seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
1123 * Display unusable free space index
1125 * The unusable free space index measures how much of the available free
1126 * memory cannot be used to satisfy an allocation of a given size and is a
1127 * value between 0 and 1. The higher the value, the more of free memory is
1128 * unusable and by implication, the worse the external fragmentation is. This
1129 * can be expressed as a percentage by multiplying by 100.
1131 static int unusable_show(struct seq_file *m, void *arg)
1133 pg_data_t *pgdat = (pg_data_t *)arg;
1135 /* check memoryless node */
1136 if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
1139 walk_zones_in_node(m, pgdat, unusable_show_print);
1144 static const struct seq_operations unusable_op = {
1145 .start = frag_start,
1148 .show = unusable_show,
1151 static int unusable_open(struct inode *inode, struct file *file)
1153 return seq_open(file, &unusable_op);
1156 static const struct file_operations unusable_file_ops = {
1157 .open = unusable_open,
1159 .llseek = seq_lseek,
1160 .release = seq_release,
1163 static void extfrag_show_print(struct seq_file *m,
1164 pg_data_t *pgdat, struct zone *zone)
1169 /* Alloc on stack as interrupts are disabled for zone walk */
1170 struct contig_page_info info;
1172 seq_printf(m, "Node %d, zone %8s ",
1175 for (order = 0; order < MAX_ORDER; ++order) {
1176 fill_contig_page_info(zone, order, &info);
1177 index = __fragmentation_index(order, &info);
1178 seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
1185 * Display fragmentation index for orders that allocations would fail for
1187 static int extfrag_show(struct seq_file *m, void *arg)
1189 pg_data_t *pgdat = (pg_data_t *)arg;
1191 walk_zones_in_node(m, pgdat, extfrag_show_print);
1196 static const struct seq_operations extfrag_op = {
1197 .start = frag_start,
1200 .show = extfrag_show,
1203 static int extfrag_open(struct inode *inode, struct file *file)
1205 return seq_open(file, &extfrag_op);
1208 static const struct file_operations extfrag_file_ops = {
1209 .open = extfrag_open,
1211 .llseek = seq_lseek,
1212 .release = seq_release,
1215 static int __init extfrag_debug_init(void)
1217 extfrag_debug_root = debugfs_create_dir("extfrag", NULL);
1218 if (!extfrag_debug_root)
1221 if (!debugfs_create_file("unusable_index", 0444,
1222 extfrag_debug_root, NULL, &unusable_file_ops))
1225 if (!debugfs_create_file("extfrag_index", 0444,
1226 extfrag_debug_root, NULL, &extfrag_file_ops))
1232 module_init(extfrag_debug_init);