Btrfs: deal with short returns from copy_from_user
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / mm / vmstat.c
blob35556366aa0fe4cf069e04ae1a09f2a211ff3760
1 /*
2 * linux/mm/vmstat.c
4 * Manages VM statistics
5 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
7 * zoned VM statistics
8 * Copyright (C) 2006 Silicon Graphics, Inc.,
9 * Christoph Lameter <christoph@lameter.com>
11 #include <linux/fs.h>
12 #include <linux/mm.h>
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>
21 #include <linux/compaction.h>
23 #ifdef CONFIG_VM_EVENT_COUNTERS
24 DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
25 EXPORT_PER_CPU_SYMBOL(vm_event_states);
27 static void sum_vm_events(unsigned long *ret)
29 int cpu;
30 int i;
32 memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
34 for_each_online_cpu(cpu) {
35 struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
37 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
38 ret[i] += this->event[i];
43 * Accumulate the vm event counters across all CPUs.
44 * The result is unavoidably approximate - it can change
45 * during and after execution of this function.
47 void all_vm_events(unsigned long *ret)
49 get_online_cpus();
50 sum_vm_events(ret);
51 put_online_cpus();
53 EXPORT_SYMBOL_GPL(all_vm_events);
55 #ifdef CONFIG_HOTPLUG
57 * Fold the foreign cpu events into our own.
59 * This is adding to the events on one processor
60 * but keeps the global counts constant.
62 void vm_events_fold_cpu(int cpu)
64 struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu);
65 int i;
67 for (i = 0; i < NR_VM_EVENT_ITEMS; i++) {
68 count_vm_events(i, fold_state->event[i]);
69 fold_state->event[i] = 0;
72 #endif /* CONFIG_HOTPLUG */
74 #endif /* CONFIG_VM_EVENT_COUNTERS */
77 * Manage combined zone based / global counters
79 * vm_stat contains the global counters
81 atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
82 EXPORT_SYMBOL(vm_stat);
84 #ifdef CONFIG_SMP
86 int calculate_pressure_threshold(struct zone *zone)
88 int threshold;
89 int watermark_distance;
92 * As vmstats are not up to date, there is drift between the estimated
93 * and real values. For high thresholds and a high number of CPUs, it
94 * is possible for the min watermark to be breached while the estimated
95 * value looks fine. The pressure threshold is a reduced value such
96 * that even the maximum amount of drift will not accidentally breach
97 * the min watermark
99 watermark_distance = low_wmark_pages(zone) - min_wmark_pages(zone);
100 threshold = max(1, (int)(watermark_distance / num_online_cpus()));
103 * Maximum threshold is 125
105 threshold = min(125, threshold);
107 return threshold;
110 int calculate_normal_threshold(struct zone *zone)
112 int threshold;
113 int mem; /* memory in 128 MB units */
116 * The threshold scales with the number of processors and the amount
117 * of memory per zone. More memory means that we can defer updates for
118 * longer, more processors could lead to more contention.
119 * fls() is used to have a cheap way of logarithmic scaling.
121 * Some sample thresholds:
123 * Threshold Processors (fls) Zonesize fls(mem+1)
124 * ------------------------------------------------------------------
125 * 8 1 1 0.9-1 GB 4
126 * 16 2 2 0.9-1 GB 4
127 * 20 2 2 1-2 GB 5
128 * 24 2 2 2-4 GB 6
129 * 28 2 2 4-8 GB 7
130 * 32 2 2 8-16 GB 8
131 * 4 2 2 <128M 1
132 * 30 4 3 2-4 GB 5
133 * 48 4 3 8-16 GB 8
134 * 32 8 4 1-2 GB 4
135 * 32 8 4 0.9-1GB 4
136 * 10 16 5 <128M 1
137 * 40 16 5 900M 4
138 * 70 64 7 2-4 GB 5
139 * 84 64 7 4-8 GB 6
140 * 108 512 9 4-8 GB 6
141 * 125 1024 10 8-16 GB 8
142 * 125 1024 10 16-32 GB 9
145 mem = zone->present_pages >> (27 - PAGE_SHIFT);
147 threshold = 2 * fls(num_online_cpus()) * (1 + fls(mem));
150 * Maximum threshold is 125
152 threshold = min(125, threshold);
154 return threshold;
158 * Refresh the thresholds for each zone.
160 static void refresh_zone_stat_thresholds(void)
162 struct zone *zone;
163 int cpu;
164 int threshold;
166 for_each_populated_zone(zone) {
167 unsigned long max_drift, tolerate_drift;
169 threshold = calculate_normal_threshold(zone);
171 for_each_online_cpu(cpu)
172 per_cpu_ptr(zone->pageset, cpu)->stat_threshold
173 = threshold;
176 * Only set percpu_drift_mark if there is a danger that
177 * NR_FREE_PAGES reports the low watermark is ok when in fact
178 * the min watermark could be breached by an allocation
180 tolerate_drift = low_wmark_pages(zone) - min_wmark_pages(zone);
181 max_drift = num_online_cpus() * threshold;
182 if (max_drift > tolerate_drift)
183 zone->percpu_drift_mark = high_wmark_pages(zone) +
184 max_drift;
188 void set_pgdat_percpu_threshold(pg_data_t *pgdat,
189 int (*calculate_pressure)(struct zone *))
191 struct zone *zone;
192 int cpu;
193 int threshold;
194 int i;
196 for (i = 0; i < pgdat->nr_zones; i++) {
197 zone = &pgdat->node_zones[i];
198 if (!zone->percpu_drift_mark)
199 continue;
201 threshold = (*calculate_pressure)(zone);
202 for_each_possible_cpu(cpu)
203 per_cpu_ptr(zone->pageset, cpu)->stat_threshold
204 = threshold;
209 * For use when we know that interrupts are disabled.
211 void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
212 int delta)
214 struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
216 s8 *p = pcp->vm_stat_diff + item;
217 long x;
219 x = delta + *p;
221 if (unlikely(x > pcp->stat_threshold || x < -pcp->stat_threshold)) {
222 zone_page_state_add(x, zone, item);
223 x = 0;
225 *p = x;
227 EXPORT_SYMBOL(__mod_zone_page_state);
230 * For an unknown interrupt state
232 void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
233 int delta)
235 unsigned long flags;
237 local_irq_save(flags);
238 __mod_zone_page_state(zone, item, delta);
239 local_irq_restore(flags);
241 EXPORT_SYMBOL(mod_zone_page_state);
244 * Optimized increment and decrement functions.
246 * These are only for a single page and therefore can take a struct page *
247 * argument instead of struct zone *. This allows the inclusion of the code
248 * generated for page_zone(page) into the optimized functions.
250 * No overflow check is necessary and therefore the differential can be
251 * incremented or decremented in place which may allow the compilers to
252 * generate better code.
253 * The increment or decrement is known and therefore one boundary check can
254 * be omitted.
256 * NOTE: These functions are very performance sensitive. Change only
257 * with care.
259 * Some processors have inc/dec instructions that are atomic vs an interrupt.
260 * However, the code must first determine the differential location in a zone
261 * based on the processor number and then inc/dec the counter. There is no
262 * guarantee without disabling preemption that the processor will not change
263 * in between and therefore the atomicity vs. interrupt cannot be exploited
264 * in a useful way here.
266 void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
268 struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
269 s8 *p = pcp->vm_stat_diff + item;
271 (*p)++;
273 if (unlikely(*p > pcp->stat_threshold)) {
274 int overstep = pcp->stat_threshold / 2;
276 zone_page_state_add(*p + overstep, zone, item);
277 *p = -overstep;
281 void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
283 __inc_zone_state(page_zone(page), item);
285 EXPORT_SYMBOL(__inc_zone_page_state);
287 void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
289 struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
290 s8 *p = pcp->vm_stat_diff + item;
292 (*p)--;
294 if (unlikely(*p < - pcp->stat_threshold)) {
295 int overstep = pcp->stat_threshold / 2;
297 zone_page_state_add(*p - overstep, zone, item);
298 *p = overstep;
302 void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
304 __dec_zone_state(page_zone(page), item);
306 EXPORT_SYMBOL(__dec_zone_page_state);
308 void inc_zone_state(struct zone *zone, enum zone_stat_item item)
310 unsigned long flags;
312 local_irq_save(flags);
313 __inc_zone_state(zone, item);
314 local_irq_restore(flags);
317 void inc_zone_page_state(struct page *page, enum zone_stat_item item)
319 unsigned long flags;
320 struct zone *zone;
322 zone = page_zone(page);
323 local_irq_save(flags);
324 __inc_zone_state(zone, item);
325 local_irq_restore(flags);
327 EXPORT_SYMBOL(inc_zone_page_state);
329 void dec_zone_page_state(struct page *page, enum zone_stat_item item)
331 unsigned long flags;
333 local_irq_save(flags);
334 __dec_zone_page_state(page, item);
335 local_irq_restore(flags);
337 EXPORT_SYMBOL(dec_zone_page_state);
340 * Update the zone counters for one cpu.
342 * The cpu specified must be either the current cpu or a processor that
343 * is not online. If it is the current cpu then the execution thread must
344 * be pinned to the current cpu.
346 * Note that refresh_cpu_vm_stats strives to only access
347 * node local memory. The per cpu pagesets on remote zones are placed
348 * in the memory local to the processor using that pageset. So the
349 * loop over all zones will access a series of cachelines local to
350 * the processor.
352 * The call to zone_page_state_add updates the cachelines with the
353 * statistics in the remote zone struct as well as the global cachelines
354 * with the global counters. These could cause remote node cache line
355 * bouncing and will have to be only done when necessary.
357 void refresh_cpu_vm_stats(int cpu)
359 struct zone *zone;
360 int i;
361 int global_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
363 for_each_populated_zone(zone) {
364 struct per_cpu_pageset *p;
366 p = per_cpu_ptr(zone->pageset, cpu);
368 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
369 if (p->vm_stat_diff[i]) {
370 unsigned long flags;
371 int v;
373 local_irq_save(flags);
374 v = p->vm_stat_diff[i];
375 p->vm_stat_diff[i] = 0;
376 local_irq_restore(flags);
377 atomic_long_add(v, &zone->vm_stat[i]);
378 global_diff[i] += v;
379 #ifdef CONFIG_NUMA
380 /* 3 seconds idle till flush */
381 p->expire = 3;
382 #endif
384 cond_resched();
385 #ifdef CONFIG_NUMA
387 * Deal with draining the remote pageset of this
388 * processor
390 * Check if there are pages remaining in this pageset
391 * if not then there is nothing to expire.
393 if (!p->expire || !p->pcp.count)
394 continue;
397 * We never drain zones local to this processor.
399 if (zone_to_nid(zone) == numa_node_id()) {
400 p->expire = 0;
401 continue;
404 p->expire--;
405 if (p->expire)
406 continue;
408 if (p->pcp.count)
409 drain_zone_pages(zone, &p->pcp);
410 #endif
413 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
414 if (global_diff[i])
415 atomic_long_add(global_diff[i], &vm_stat[i]);
418 #endif
420 #ifdef CONFIG_NUMA
422 * zonelist = the list of zones passed to the allocator
423 * z = the zone from which the allocation occurred.
425 * Must be called with interrupts disabled.
427 void zone_statistics(struct zone *preferred_zone, struct zone *z)
429 if (z->zone_pgdat == preferred_zone->zone_pgdat) {
430 __inc_zone_state(z, NUMA_HIT);
431 } else {
432 __inc_zone_state(z, NUMA_MISS);
433 __inc_zone_state(preferred_zone, NUMA_FOREIGN);
435 if (z->node == numa_node_id())
436 __inc_zone_state(z, NUMA_LOCAL);
437 else
438 __inc_zone_state(z, NUMA_OTHER);
440 #endif
442 #ifdef CONFIG_COMPACTION
444 struct contig_page_info {
445 unsigned long free_pages;
446 unsigned long free_blocks_total;
447 unsigned long free_blocks_suitable;
451 * Calculate the number of free pages in a zone, how many contiguous
452 * pages are free and how many are large enough to satisfy an allocation of
453 * the target size. Note that this function makes no attempt to estimate
454 * how many suitable free blocks there *might* be if MOVABLE pages were
455 * migrated. Calculating that is possible, but expensive and can be
456 * figured out from userspace
458 static void fill_contig_page_info(struct zone *zone,
459 unsigned int suitable_order,
460 struct contig_page_info *info)
462 unsigned int order;
464 info->free_pages = 0;
465 info->free_blocks_total = 0;
466 info->free_blocks_suitable = 0;
468 for (order = 0; order < MAX_ORDER; order++) {
469 unsigned long blocks;
471 /* Count number of free blocks */
472 blocks = zone->free_area[order].nr_free;
473 info->free_blocks_total += blocks;
475 /* Count free base pages */
476 info->free_pages += blocks << order;
478 /* Count the suitable free blocks */
479 if (order >= suitable_order)
480 info->free_blocks_suitable += blocks <<
481 (order - suitable_order);
486 * A fragmentation index only makes sense if an allocation of a requested
487 * size would fail. If that is true, the fragmentation index indicates
488 * whether external fragmentation or a lack of memory was the problem.
489 * The value can be used to determine if page reclaim or compaction
490 * should be used
492 static int __fragmentation_index(unsigned int order, struct contig_page_info *info)
494 unsigned long requested = 1UL << order;
496 if (!info->free_blocks_total)
497 return 0;
499 /* Fragmentation index only makes sense when a request would fail */
500 if (info->free_blocks_suitable)
501 return -1000;
504 * Index is between 0 and 1 so return within 3 decimal places
506 * 0 => allocation would fail due to lack of memory
507 * 1 => allocation would fail due to fragmentation
509 return 1000 - div_u64( (1000+(div_u64(info->free_pages * 1000ULL, requested))), info->free_blocks_total);
512 /* Same as __fragmentation index but allocs contig_page_info on stack */
513 int fragmentation_index(struct zone *zone, unsigned int order)
515 struct contig_page_info info;
517 fill_contig_page_info(zone, order, &info);
518 return __fragmentation_index(order, &info);
520 #endif
522 #if defined(CONFIG_PROC_FS) || defined(CONFIG_COMPACTION)
523 #include <linux/proc_fs.h>
524 #include <linux/seq_file.h>
526 static char * const migratetype_names[MIGRATE_TYPES] = {
527 "Unmovable",
528 "Reclaimable",
529 "Movable",
530 "Reserve",
531 "Isolate",
534 static void *frag_start(struct seq_file *m, loff_t *pos)
536 pg_data_t *pgdat;
537 loff_t node = *pos;
538 for (pgdat = first_online_pgdat();
539 pgdat && node;
540 pgdat = next_online_pgdat(pgdat))
541 --node;
543 return pgdat;
546 static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
548 pg_data_t *pgdat = (pg_data_t *)arg;
550 (*pos)++;
551 return next_online_pgdat(pgdat);
554 static void frag_stop(struct seq_file *m, void *arg)
558 /* Walk all the zones in a node and print using a callback */
559 static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat,
560 void (*print)(struct seq_file *m, pg_data_t *, struct zone *))
562 struct zone *zone;
563 struct zone *node_zones = pgdat->node_zones;
564 unsigned long flags;
566 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
567 if (!populated_zone(zone))
568 continue;
570 spin_lock_irqsave(&zone->lock, flags);
571 print(m, pgdat, zone);
572 spin_unlock_irqrestore(&zone->lock, flags);
575 #endif
577 #ifdef CONFIG_PROC_FS
578 static void frag_show_print(struct seq_file *m, pg_data_t *pgdat,
579 struct zone *zone)
581 int order;
583 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
584 for (order = 0; order < MAX_ORDER; ++order)
585 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
586 seq_putc(m, '\n');
590 * This walks the free areas for each zone.
592 static int frag_show(struct seq_file *m, void *arg)
594 pg_data_t *pgdat = (pg_data_t *)arg;
595 walk_zones_in_node(m, pgdat, frag_show_print);
596 return 0;
599 static void pagetypeinfo_showfree_print(struct seq_file *m,
600 pg_data_t *pgdat, struct zone *zone)
602 int order, mtype;
604 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
605 seq_printf(m, "Node %4d, zone %8s, type %12s ",
606 pgdat->node_id,
607 zone->name,
608 migratetype_names[mtype]);
609 for (order = 0; order < MAX_ORDER; ++order) {
610 unsigned long freecount = 0;
611 struct free_area *area;
612 struct list_head *curr;
614 area = &(zone->free_area[order]);
616 list_for_each(curr, &area->free_list[mtype])
617 freecount++;
618 seq_printf(m, "%6lu ", freecount);
620 seq_putc(m, '\n');
624 /* Print out the free pages at each order for each migatetype */
625 static int pagetypeinfo_showfree(struct seq_file *m, void *arg)
627 int order;
628 pg_data_t *pgdat = (pg_data_t *)arg;
630 /* Print header */
631 seq_printf(m, "%-43s ", "Free pages count per migrate type at order");
632 for (order = 0; order < MAX_ORDER; ++order)
633 seq_printf(m, "%6d ", order);
634 seq_putc(m, '\n');
636 walk_zones_in_node(m, pgdat, pagetypeinfo_showfree_print);
638 return 0;
641 static void pagetypeinfo_showblockcount_print(struct seq_file *m,
642 pg_data_t *pgdat, struct zone *zone)
644 int mtype;
645 unsigned long pfn;
646 unsigned long start_pfn = zone->zone_start_pfn;
647 unsigned long end_pfn = start_pfn + zone->spanned_pages;
648 unsigned long count[MIGRATE_TYPES] = { 0, };
650 for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
651 struct page *page;
653 if (!pfn_valid(pfn))
654 continue;
656 page = pfn_to_page(pfn);
658 /* Watch for unexpected holes punched in the memmap */
659 if (!memmap_valid_within(pfn, page, zone))
660 continue;
662 mtype = get_pageblock_migratetype(page);
664 if (mtype < MIGRATE_TYPES)
665 count[mtype]++;
668 /* Print counts */
669 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
670 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
671 seq_printf(m, "%12lu ", count[mtype]);
672 seq_putc(m, '\n');
675 /* Print out the free pages at each order for each migratetype */
676 static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg)
678 int mtype;
679 pg_data_t *pgdat = (pg_data_t *)arg;
681 seq_printf(m, "\n%-23s", "Number of blocks type ");
682 for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
683 seq_printf(m, "%12s ", migratetype_names[mtype]);
684 seq_putc(m, '\n');
685 walk_zones_in_node(m, pgdat, pagetypeinfo_showblockcount_print);
687 return 0;
691 * This prints out statistics in relation to grouping pages by mobility.
692 * It is expensive to collect so do not constantly read the file.
694 static int pagetypeinfo_show(struct seq_file *m, void *arg)
696 pg_data_t *pgdat = (pg_data_t *)arg;
698 /* check memoryless node */
699 if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
700 return 0;
702 seq_printf(m, "Page block order: %d\n", pageblock_order);
703 seq_printf(m, "Pages per block: %lu\n", pageblock_nr_pages);
704 seq_putc(m, '\n');
705 pagetypeinfo_showfree(m, pgdat);
706 pagetypeinfo_showblockcount(m, pgdat);
708 return 0;
711 static const struct seq_operations fragmentation_op = {
712 .start = frag_start,
713 .next = frag_next,
714 .stop = frag_stop,
715 .show = frag_show,
718 static int fragmentation_open(struct inode *inode, struct file *file)
720 return seq_open(file, &fragmentation_op);
723 static const struct file_operations fragmentation_file_operations = {
724 .open = fragmentation_open,
725 .read = seq_read,
726 .llseek = seq_lseek,
727 .release = seq_release,
730 static const struct seq_operations pagetypeinfo_op = {
731 .start = frag_start,
732 .next = frag_next,
733 .stop = frag_stop,
734 .show = pagetypeinfo_show,
737 static int pagetypeinfo_open(struct inode *inode, struct file *file)
739 return seq_open(file, &pagetypeinfo_op);
742 static const struct file_operations pagetypeinfo_file_ops = {
743 .open = pagetypeinfo_open,
744 .read = seq_read,
745 .llseek = seq_lseek,
746 .release = seq_release,
749 #ifdef CONFIG_ZONE_DMA
750 #define TEXT_FOR_DMA(xx) xx "_dma",
751 #else
752 #define TEXT_FOR_DMA(xx)
753 #endif
755 #ifdef CONFIG_ZONE_DMA32
756 #define TEXT_FOR_DMA32(xx) xx "_dma32",
757 #else
758 #define TEXT_FOR_DMA32(xx)
759 #endif
761 #ifdef CONFIG_HIGHMEM
762 #define TEXT_FOR_HIGHMEM(xx) xx "_high",
763 #else
764 #define TEXT_FOR_HIGHMEM(xx)
765 #endif
767 #define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
768 TEXT_FOR_HIGHMEM(xx) xx "_movable",
770 static const char * const vmstat_text[] = {
771 /* Zoned VM counters */
772 "nr_free_pages",
773 "nr_inactive_anon",
774 "nr_active_anon",
775 "nr_inactive_file",
776 "nr_active_file",
777 "nr_unevictable",
778 "nr_mlock",
779 "nr_anon_pages",
780 "nr_mapped",
781 "nr_file_pages",
782 "nr_dirty",
783 "nr_writeback",
784 "nr_slab_reclaimable",
785 "nr_slab_unreclaimable",
786 "nr_page_table_pages",
787 "nr_kernel_stack",
788 "nr_unstable",
789 "nr_bounce",
790 "nr_vmscan_write",
791 "nr_writeback_temp",
792 "nr_isolated_anon",
793 "nr_isolated_file",
794 "nr_shmem",
795 "nr_dirtied",
796 "nr_written",
798 #ifdef CONFIG_NUMA
799 "numa_hit",
800 "numa_miss",
801 "numa_foreign",
802 "numa_interleave",
803 "numa_local",
804 "numa_other",
805 #endif
806 "nr_dirty_threshold",
807 "nr_dirty_background_threshold",
809 #ifdef CONFIG_VM_EVENT_COUNTERS
810 "pgpgin",
811 "pgpgout",
812 "pswpin",
813 "pswpout",
815 TEXTS_FOR_ZONES("pgalloc")
817 "pgfree",
818 "pgactivate",
819 "pgdeactivate",
821 "pgfault",
822 "pgmajfault",
824 TEXTS_FOR_ZONES("pgrefill")
825 TEXTS_FOR_ZONES("pgsteal")
826 TEXTS_FOR_ZONES("pgscan_kswapd")
827 TEXTS_FOR_ZONES("pgscan_direct")
829 #ifdef CONFIG_NUMA
830 "zone_reclaim_failed",
831 #endif
832 "pginodesteal",
833 "slabs_scanned",
834 "kswapd_steal",
835 "kswapd_inodesteal",
836 "kswapd_low_wmark_hit_quickly",
837 "kswapd_high_wmark_hit_quickly",
838 "kswapd_skip_congestion_wait",
839 "pageoutrun",
840 "allocstall",
842 "pgrotated",
844 #ifdef CONFIG_COMPACTION
845 "compact_blocks_moved",
846 "compact_pages_moved",
847 "compact_pagemigrate_failed",
848 "compact_stall",
849 "compact_fail",
850 "compact_success",
851 #endif
853 #ifdef CONFIG_HUGETLB_PAGE
854 "htlb_buddy_alloc_success",
855 "htlb_buddy_alloc_fail",
856 #endif
857 "unevictable_pgs_culled",
858 "unevictable_pgs_scanned",
859 "unevictable_pgs_rescued",
860 "unevictable_pgs_mlocked",
861 "unevictable_pgs_munlocked",
862 "unevictable_pgs_cleared",
863 "unevictable_pgs_stranded",
864 "unevictable_pgs_mlockfreed",
865 #endif
868 static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
869 struct zone *zone)
871 int i;
872 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
873 seq_printf(m,
874 "\n pages free %lu"
875 "\n min %lu"
876 "\n low %lu"
877 "\n high %lu"
878 "\n scanned %lu"
879 "\n spanned %lu"
880 "\n present %lu",
881 zone_page_state(zone, NR_FREE_PAGES),
882 min_wmark_pages(zone),
883 low_wmark_pages(zone),
884 high_wmark_pages(zone),
885 zone->pages_scanned,
886 zone->spanned_pages,
887 zone->present_pages);
889 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
890 seq_printf(m, "\n %-12s %lu", vmstat_text[i],
891 zone_page_state(zone, i));
893 seq_printf(m,
894 "\n protection: (%lu",
895 zone->lowmem_reserve[0]);
896 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
897 seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
898 seq_printf(m,
900 "\n pagesets");
901 for_each_online_cpu(i) {
902 struct per_cpu_pageset *pageset;
904 pageset = per_cpu_ptr(zone->pageset, i);
905 seq_printf(m,
906 "\n cpu: %i"
907 "\n count: %i"
908 "\n high: %i"
909 "\n batch: %i",
911 pageset->pcp.count,
912 pageset->pcp.high,
913 pageset->pcp.batch);
914 #ifdef CONFIG_SMP
915 seq_printf(m, "\n vm stats threshold: %d",
916 pageset->stat_threshold);
917 #endif
919 seq_printf(m,
920 "\n all_unreclaimable: %u"
921 "\n start_pfn: %lu"
922 "\n inactive_ratio: %u",
923 zone->all_unreclaimable,
924 zone->zone_start_pfn,
925 zone->inactive_ratio);
926 seq_putc(m, '\n');
930 * Output information about zones in @pgdat.
932 static int zoneinfo_show(struct seq_file *m, void *arg)
934 pg_data_t *pgdat = (pg_data_t *)arg;
935 walk_zones_in_node(m, pgdat, zoneinfo_show_print);
936 return 0;
939 static const struct seq_operations zoneinfo_op = {
940 .start = frag_start, /* iterate over all zones. The same as in
941 * fragmentation. */
942 .next = frag_next,
943 .stop = frag_stop,
944 .show = zoneinfo_show,
947 static int zoneinfo_open(struct inode *inode, struct file *file)
949 return seq_open(file, &zoneinfo_op);
952 static const struct file_operations proc_zoneinfo_file_operations = {
953 .open = zoneinfo_open,
954 .read = seq_read,
955 .llseek = seq_lseek,
956 .release = seq_release,
959 enum writeback_stat_item {
960 NR_DIRTY_THRESHOLD,
961 NR_DIRTY_BG_THRESHOLD,
962 NR_VM_WRITEBACK_STAT_ITEMS,
965 static void *vmstat_start(struct seq_file *m, loff_t *pos)
967 unsigned long *v;
968 int i, stat_items_size;
970 if (*pos >= ARRAY_SIZE(vmstat_text))
971 return NULL;
972 stat_items_size = NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long) +
973 NR_VM_WRITEBACK_STAT_ITEMS * sizeof(unsigned long);
975 #ifdef CONFIG_VM_EVENT_COUNTERS
976 stat_items_size += sizeof(struct vm_event_state);
977 #endif
979 v = kmalloc(stat_items_size, GFP_KERNEL);
980 m->private = v;
981 if (!v)
982 return ERR_PTR(-ENOMEM);
983 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
984 v[i] = global_page_state(i);
985 v += NR_VM_ZONE_STAT_ITEMS;
987 global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD,
988 v + NR_DIRTY_THRESHOLD);
989 v += NR_VM_WRITEBACK_STAT_ITEMS;
991 #ifdef CONFIG_VM_EVENT_COUNTERS
992 all_vm_events(v);
993 v[PGPGIN] /= 2; /* sectors -> kbytes */
994 v[PGPGOUT] /= 2;
995 #endif
996 return (unsigned long *)m->private + *pos;
999 static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
1001 (*pos)++;
1002 if (*pos >= ARRAY_SIZE(vmstat_text))
1003 return NULL;
1004 return (unsigned long *)m->private + *pos;
1007 static int vmstat_show(struct seq_file *m, void *arg)
1009 unsigned long *l = arg;
1010 unsigned long off = l - (unsigned long *)m->private;
1012 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
1013 return 0;
1016 static void vmstat_stop(struct seq_file *m, void *arg)
1018 kfree(m->private);
1019 m->private = NULL;
1022 static const struct seq_operations vmstat_op = {
1023 .start = vmstat_start,
1024 .next = vmstat_next,
1025 .stop = vmstat_stop,
1026 .show = vmstat_show,
1029 static int vmstat_open(struct inode *inode, struct file *file)
1031 return seq_open(file, &vmstat_op);
1034 static const struct file_operations proc_vmstat_file_operations = {
1035 .open = vmstat_open,
1036 .read = seq_read,
1037 .llseek = seq_lseek,
1038 .release = seq_release,
1040 #endif /* CONFIG_PROC_FS */
1042 #ifdef CONFIG_SMP
1043 static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
1044 int sysctl_stat_interval __read_mostly = HZ;
1046 static void vmstat_update(struct work_struct *w)
1048 refresh_cpu_vm_stats(smp_processor_id());
1049 schedule_delayed_work(&__get_cpu_var(vmstat_work),
1050 round_jiffies_relative(sysctl_stat_interval));
1053 static void __cpuinit start_cpu_timer(int cpu)
1055 struct delayed_work *work = &per_cpu(vmstat_work, cpu);
1057 INIT_DELAYED_WORK_DEFERRABLE(work, vmstat_update);
1058 schedule_delayed_work_on(cpu, work, __round_jiffies_relative(HZ, cpu));
1062 * Use the cpu notifier to insure that the thresholds are recalculated
1063 * when necessary.
1065 static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
1066 unsigned long action,
1067 void *hcpu)
1069 long cpu = (long)hcpu;
1071 switch (action) {
1072 case CPU_ONLINE:
1073 case CPU_ONLINE_FROZEN:
1074 refresh_zone_stat_thresholds();
1075 start_cpu_timer(cpu);
1076 node_set_state(cpu_to_node(cpu), N_CPU);
1077 break;
1078 case CPU_DOWN_PREPARE:
1079 case CPU_DOWN_PREPARE_FROZEN:
1080 cancel_rearming_delayed_work(&per_cpu(vmstat_work, cpu));
1081 per_cpu(vmstat_work, cpu).work.func = NULL;
1082 break;
1083 case CPU_DOWN_FAILED:
1084 case CPU_DOWN_FAILED_FROZEN:
1085 start_cpu_timer(cpu);
1086 break;
1087 case CPU_DEAD:
1088 case CPU_DEAD_FROZEN:
1089 refresh_zone_stat_thresholds();
1090 break;
1091 default:
1092 break;
1094 return NOTIFY_OK;
1097 static struct notifier_block __cpuinitdata vmstat_notifier =
1098 { &vmstat_cpuup_callback, NULL, 0 };
1099 #endif
1101 static int __init setup_vmstat(void)
1103 #ifdef CONFIG_SMP
1104 int cpu;
1106 refresh_zone_stat_thresholds();
1107 register_cpu_notifier(&vmstat_notifier);
1109 for_each_online_cpu(cpu)
1110 start_cpu_timer(cpu);
1111 #endif
1112 #ifdef CONFIG_PROC_FS
1113 proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
1114 proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
1115 proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
1116 proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
1117 #endif
1118 return 0;
1120 module_init(setup_vmstat)
1122 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
1123 #include <linux/debugfs.h>
1125 static struct dentry *extfrag_debug_root;
1128 * Return an index indicating how much of the available free memory is
1129 * unusable for an allocation of the requested size.
1131 static int unusable_free_index(unsigned int order,
1132 struct contig_page_info *info)
1134 /* No free memory is interpreted as all free memory is unusable */
1135 if (info->free_pages == 0)
1136 return 1000;
1139 * Index should be a value between 0 and 1. Return a value to 3
1140 * decimal places.
1142 * 0 => no fragmentation
1143 * 1 => high fragmentation
1145 return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages);
1149 static void unusable_show_print(struct seq_file *m,
1150 pg_data_t *pgdat, struct zone *zone)
1152 unsigned int order;
1153 int index;
1154 struct contig_page_info info;
1156 seq_printf(m, "Node %d, zone %8s ",
1157 pgdat->node_id,
1158 zone->name);
1159 for (order = 0; order < MAX_ORDER; ++order) {
1160 fill_contig_page_info(zone, order, &info);
1161 index = unusable_free_index(order, &info);
1162 seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
1165 seq_putc(m, '\n');
1169 * Display unusable free space index
1171 * The unusable free space index measures how much of the available free
1172 * memory cannot be used to satisfy an allocation of a given size and is a
1173 * value between 0 and 1. The higher the value, the more of free memory is
1174 * unusable and by implication, the worse the external fragmentation is. This
1175 * can be expressed as a percentage by multiplying by 100.
1177 static int unusable_show(struct seq_file *m, void *arg)
1179 pg_data_t *pgdat = (pg_data_t *)arg;
1181 /* check memoryless node */
1182 if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
1183 return 0;
1185 walk_zones_in_node(m, pgdat, unusable_show_print);
1187 return 0;
1190 static const struct seq_operations unusable_op = {
1191 .start = frag_start,
1192 .next = frag_next,
1193 .stop = frag_stop,
1194 .show = unusable_show,
1197 static int unusable_open(struct inode *inode, struct file *file)
1199 return seq_open(file, &unusable_op);
1202 static const struct file_operations unusable_file_ops = {
1203 .open = unusable_open,
1204 .read = seq_read,
1205 .llseek = seq_lseek,
1206 .release = seq_release,
1209 static void extfrag_show_print(struct seq_file *m,
1210 pg_data_t *pgdat, struct zone *zone)
1212 unsigned int order;
1213 int index;
1215 /* Alloc on stack as interrupts are disabled for zone walk */
1216 struct contig_page_info info;
1218 seq_printf(m, "Node %d, zone %8s ",
1219 pgdat->node_id,
1220 zone->name);
1221 for (order = 0; order < MAX_ORDER; ++order) {
1222 fill_contig_page_info(zone, order, &info);
1223 index = __fragmentation_index(order, &info);
1224 seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
1227 seq_putc(m, '\n');
1231 * Display fragmentation index for orders that allocations would fail for
1233 static int extfrag_show(struct seq_file *m, void *arg)
1235 pg_data_t *pgdat = (pg_data_t *)arg;
1237 walk_zones_in_node(m, pgdat, extfrag_show_print);
1239 return 0;
1242 static const struct seq_operations extfrag_op = {
1243 .start = frag_start,
1244 .next = frag_next,
1245 .stop = frag_stop,
1246 .show = extfrag_show,
1249 static int extfrag_open(struct inode *inode, struct file *file)
1251 return seq_open(file, &extfrag_op);
1254 static const struct file_operations extfrag_file_ops = {
1255 .open = extfrag_open,
1256 .read = seq_read,
1257 .llseek = seq_lseek,
1258 .release = seq_release,
1261 static int __init extfrag_debug_init(void)
1263 extfrag_debug_root = debugfs_create_dir("extfrag", NULL);
1264 if (!extfrag_debug_root)
1265 return -ENOMEM;
1267 if (!debugfs_create_file("unusable_index", 0444,
1268 extfrag_debug_root, NULL, &unusable_file_ops))
1269 return -ENOMEM;
1271 if (!debugfs_create_file("extfrag_index", 0444,
1272 extfrag_debug_root, NULL, &extfrag_file_ops))
1273 return -ENOMEM;
1275 return 0;
1278 module_init(extfrag_debug_init);
1279 #endif