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>
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
)
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
)
53 EXPORT_SYMBOL_GPL(all_vm_events
);
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
);
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
);
86 int calculate_pressure_threshold(struct zone
*zone
)
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
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
);
110 int calculate_normal_threshold(struct zone
*zone
)
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 * ------------------------------------------------------------------
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
);
158 * Refresh the thresholds for each zone.
160 static void refresh_zone_stat_thresholds(void)
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
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
) +
188 void set_pgdat_percpu_threshold(pg_data_t
*pgdat
,
189 int (*calculate_pressure
)(struct zone
*))
196 for (i
= 0; i
< pgdat
->nr_zones
; i
++) {
197 zone
= &pgdat
->node_zones
[i
];
198 if (!zone
->percpu_drift_mark
)
201 threshold
= (*calculate_pressure
)(zone
);
202 for_each_possible_cpu(cpu
)
203 per_cpu_ptr(zone
->pageset
, cpu
)->stat_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
,
214 struct per_cpu_pageset
*pcp
= this_cpu_ptr(zone
->pageset
);
216 s8
*p
= pcp
->vm_stat_diff
+ item
;
221 if (unlikely(x
> pcp
->stat_threshold
|| x
< -pcp
->stat_threshold
)) {
222 zone_page_state_add(x
, zone
, item
);
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
,
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
256 * NOTE: These functions are very performance sensitive. Change only
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
;
273 if (unlikely(*p
> pcp
->stat_threshold
)) {
274 int overstep
= pcp
->stat_threshold
/ 2;
276 zone_page_state_add(*p
+ overstep
, zone
, item
);
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
;
294 if (unlikely(*p
< - pcp
->stat_threshold
)) {
295 int overstep
= pcp
->stat_threshold
/ 2;
297 zone_page_state_add(*p
- overstep
, zone
, item
);
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
)
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
)
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
)
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
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
)
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
]) {
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
]);
380 /* 3 seconds idle till flush */
387 * Deal with draining the remote pageset of this
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
)
397 * We never drain zones local to this processor.
399 if (zone_to_nid(zone
) == numa_node_id()) {
409 drain_zone_pages(zone
, &p
->pcp
);
413 for (i
= 0; i
< NR_VM_ZONE_STAT_ITEMS
; i
++)
415 atomic_long_add(global_diff
[i
], &vm_stat
[i
]);
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
);
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
);
438 __inc_zone_state(z
, NUMA_OTHER
);
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
)
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
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
)
499 /* Fragmentation index only makes sense when a request would fail */
500 if (info
->free_blocks_suitable
)
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
);
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
] = {
534 static void *frag_start(struct seq_file
*m
, loff_t
*pos
)
538 for (pgdat
= first_online_pgdat();
540 pgdat
= next_online_pgdat(pgdat
))
546 static void *frag_next(struct seq_file
*m
, void *arg
, loff_t
*pos
)
548 pg_data_t
*pgdat
= (pg_data_t
*)arg
;
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
*))
563 struct zone
*node_zones
= pgdat
->node_zones
;
566 for (zone
= node_zones
; zone
- node_zones
< MAX_NR_ZONES
; ++zone
) {
567 if (!populated_zone(zone
))
570 spin_lock_irqsave(&zone
->lock
, flags
);
571 print(m
, pgdat
, zone
);
572 spin_unlock_irqrestore(&zone
->lock
, flags
);
577 #ifdef CONFIG_PROC_FS
578 static void frag_show_print(struct seq_file
*m
, pg_data_t
*pgdat
,
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
);
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
);
599 static void pagetypeinfo_showfree_print(struct seq_file
*m
,
600 pg_data_t
*pgdat
, struct zone
*zone
)
604 for (mtype
= 0; mtype
< MIGRATE_TYPES
; mtype
++) {
605 seq_printf(m
, "Node %4d, zone %8s, type %12s ",
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
])
618 seq_printf(m
, "%6lu ", freecount
);
624 /* Print out the free pages at each order for each migatetype */
625 static int pagetypeinfo_showfree(struct seq_file
*m
, void *arg
)
628 pg_data_t
*pgdat
= (pg_data_t
*)arg
;
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
);
636 walk_zones_in_node(m
, pgdat
, pagetypeinfo_showfree_print
);
641 static void pagetypeinfo_showblockcount_print(struct seq_file
*m
,
642 pg_data_t
*pgdat
, struct zone
*zone
)
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
) {
656 page
= pfn_to_page(pfn
);
658 /* Watch for unexpected holes punched in the memmap */
659 if (!memmap_valid_within(pfn
, page
, zone
))
662 mtype
= get_pageblock_migratetype(page
);
664 if (mtype
< MIGRATE_TYPES
)
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
]);
675 /* Print out the free pages at each order for each migratetype */
676 static int pagetypeinfo_showblockcount(struct seq_file
*m
, void *arg
)
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
]);
685 walk_zones_in_node(m
, pgdat
, pagetypeinfo_showblockcount_print
);
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
))
702 seq_printf(m
, "Page block order: %d\n", pageblock_order
);
703 seq_printf(m
, "Pages per block: %lu\n", pageblock_nr_pages
);
705 pagetypeinfo_showfree(m
, pgdat
);
706 pagetypeinfo_showblockcount(m
, pgdat
);
711 static const struct seq_operations fragmentation_op
= {
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
,
727 .release
= seq_release
,
730 static const struct seq_operations pagetypeinfo_op
= {
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
,
746 .release
= seq_release
,
749 #ifdef CONFIG_ZONE_DMA
750 #define TEXT_FOR_DMA(xx) xx "_dma",
752 #define TEXT_FOR_DMA(xx)
755 #ifdef CONFIG_ZONE_DMA32
756 #define TEXT_FOR_DMA32(xx) xx "_dma32",
758 #define TEXT_FOR_DMA32(xx)
761 #ifdef CONFIG_HIGHMEM
762 #define TEXT_FOR_HIGHMEM(xx) xx "_high",
764 #define TEXT_FOR_HIGHMEM(xx)
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 */
784 "nr_slab_reclaimable",
785 "nr_slab_unreclaimable",
786 "nr_page_table_pages",
806 "nr_dirty_threshold",
807 "nr_dirty_background_threshold",
809 #ifdef CONFIG_VM_EVENT_COUNTERS
815 TEXTS_FOR_ZONES("pgalloc")
824 TEXTS_FOR_ZONES("pgrefill")
825 TEXTS_FOR_ZONES("pgsteal")
826 TEXTS_FOR_ZONES("pgscan_kswapd")
827 TEXTS_FOR_ZONES("pgscan_direct")
830 "zone_reclaim_failed",
836 "kswapd_low_wmark_hit_quickly",
837 "kswapd_high_wmark_hit_quickly",
838 "kswapd_skip_congestion_wait",
844 #ifdef CONFIG_COMPACTION
845 "compact_blocks_moved",
846 "compact_pages_moved",
847 "compact_pagemigrate_failed",
853 #ifdef CONFIG_HUGETLB_PAGE
854 "htlb_buddy_alloc_success",
855 "htlb_buddy_alloc_fail",
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",
868 static void zoneinfo_show_print(struct seq_file
*m
, pg_data_t
*pgdat
,
872 seq_printf(m
, "Node %d, zone %8s", pgdat
->node_id
, zone
->name
);
881 zone_page_state(zone
, NR_FREE_PAGES
),
882 min_wmark_pages(zone
),
883 low_wmark_pages(zone
),
884 high_wmark_pages(zone
),
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
));
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
]);
901 for_each_online_cpu(i
) {
902 struct per_cpu_pageset
*pageset
;
904 pageset
= per_cpu_ptr(zone
->pageset
, i
);
915 seq_printf(m
, "\n vm stats threshold: %d",
916 pageset
->stat_threshold
);
920 "\n all_unreclaimable: %u"
922 "\n inactive_ratio: %u",
923 zone
->all_unreclaimable
,
924 zone
->zone_start_pfn
,
925 zone
->inactive_ratio
);
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
);
939 static const struct seq_operations zoneinfo_op
= {
940 .start
= frag_start
, /* iterate over all zones. The same as in
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
,
956 .release
= seq_release
,
959 enum writeback_stat_item
{
961 NR_DIRTY_BG_THRESHOLD
,
962 NR_VM_WRITEBACK_STAT_ITEMS
,
965 static void *vmstat_start(struct seq_file
*m
, loff_t
*pos
)
968 int i
, stat_items_size
;
970 if (*pos
>= ARRAY_SIZE(vmstat_text
))
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
);
979 v
= kmalloc(stat_items_size
, GFP_KERNEL
);
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
993 v
[PGPGIN
] /= 2; /* sectors -> kbytes */
996 return (unsigned long *)m
->private + *pos
;
999 static void *vmstat_next(struct seq_file
*m
, void *arg
, loff_t
*pos
)
1002 if (*pos
>= ARRAY_SIZE(vmstat_text
))
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
);
1016 static void vmstat_stop(struct seq_file
*m
, void *arg
)
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
,
1037 .llseek
= seq_lseek
,
1038 .release
= seq_release
,
1040 #endif /* CONFIG_PROC_FS */
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
1065 static int __cpuinit
vmstat_cpuup_callback(struct notifier_block
*nfb
,
1066 unsigned long action
,
1069 long cpu
= (long)hcpu
;
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
);
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
;
1083 case CPU_DOWN_FAILED
:
1084 case CPU_DOWN_FAILED_FROZEN
:
1085 start_cpu_timer(cpu
);
1088 case CPU_DEAD_FROZEN
:
1089 refresh_zone_stat_thresholds();
1097 static struct notifier_block __cpuinitdata vmstat_notifier
=
1098 { &vmstat_cpuup_callback
, NULL
, 0 };
1101 static int __init
setup_vmstat(void)
1106 refresh_zone_stat_thresholds();
1107 register_cpu_notifier(&vmstat_notifier
);
1109 for_each_online_cpu(cpu
)
1110 start_cpu_timer(cpu
);
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
);
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)
1139 * Index should be a value between 0 and 1. Return a value to 3
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
)
1154 struct contig_page_info info
;
1156 seq_printf(m
, "Node %d, zone %8s ",
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);
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
))
1185 walk_zones_in_node(m
, pgdat
, unusable_show_print
);
1190 static const struct seq_operations unusable_op
= {
1191 .start
= frag_start
,
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
,
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
)
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 ",
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);
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
);
1242 static const struct seq_operations extfrag_op
= {
1243 .start
= frag_start
,
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
,
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
)
1267 if (!debugfs_create_file("unusable_index", 0444,
1268 extfrag_debug_root
, NULL
, &unusable_file_ops
))
1271 if (!debugfs_create_file("extfrag_index", 0444,
1272 extfrag_debug_root
, NULL
, &extfrag_file_ops
))
1278 module_init(extfrag_debug_init
);