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/module.h>
14 #include <linux/cpu.h>
16 void __get_zone_counts(unsigned long *active, unsigned long *inactive,
17 unsigned long *free, struct pglist_data *pgdat)
19 struct zone *zones = pgdat->node_zones;
25 for (i = 0; i < MAX_NR_ZONES; i++) {
26 *active += zones[i].nr_active;
27 *inactive += zones[i].nr_inactive;
28 *free += zones[i].free_pages;
32 void get_zone_counts(unsigned long *active,
33 unsigned long *inactive, unsigned long *free)
35 struct pglist_data *pgdat;
40 for_each_online_pgdat(pgdat) {
41 unsigned long l, m, n;
42 __get_zone_counts(&l, &m, &n, pgdat);
49 #ifdef CONFIG_VM_EVENT_COUNTERS
50 DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
51 EXPORT_PER_CPU_SYMBOL(vm_event_states);
53 static void sum_vm_events(unsigned long *ret, cpumask_t *cpumask)
58 memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
60 cpu = first_cpu(*cpumask);
61 while (cpu < NR_CPUS) {
62 struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
64 cpu = next_cpu(cpu, *cpumask);
67 prefetch(&per_cpu(vm_event_states, cpu));
70 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
71 ret[i] += this->event[i];
76 * Accumulate the vm event counters across all CPUs.
77 * The result is unavoidably approximate - it can change
78 * during and after execution of this function.
80 void all_vm_events(unsigned long *ret)
82 sum_vm_events(ret, &cpu_online_map);
84 EXPORT_SYMBOL_GPL(all_vm_events);
88 * Fold the foreign cpu events into our own.
90 * This is adding to the events on one processor
91 * but keeps the global counts constant.
93 void vm_events_fold_cpu(int cpu)
95 struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu);
98 for (i = 0; i < NR_VM_EVENT_ITEMS; i++) {
99 count_vm_events(i, fold_state->event[i]);
100 fold_state->event[i] = 0;
103 #endif /* CONFIG_HOTPLUG */
105 #endif /* CONFIG_VM_EVENT_COUNTERS */
108 * Manage combined zone based / global counters
110 * vm_stat contains the global counters
112 atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
113 EXPORT_SYMBOL(vm_stat);
117 static int calculate_threshold(struct zone *zone)
120 int mem; /* memory in 128 MB units */
123 * The threshold scales with the number of processors and the amount
124 * of memory per zone. More memory means that we can defer updates for
125 * longer, more processors could lead to more contention.
126 * fls() is used to have a cheap way of logarithmic scaling.
128 * Some sample thresholds:
130 * Threshold Processors (fls) Zonesize fls(mem+1)
131 * ------------------------------------------------------------------
148 * 125 1024 10 8-16 GB 8
149 * 125 1024 10 16-32 GB 9
152 mem = zone->present_pages >> (27 - PAGE_SHIFT);
154 threshold = 2 * fls(num_online_cpus()) * (1 + fls(mem));
157 * Maximum threshold is 125
159 threshold = min(125, threshold);
165 * Refresh the thresholds for each zone.
167 static void refresh_zone_stat_thresholds(void)
173 for_each_zone(zone) {
175 if (!zone->present_pages)
178 threshold = calculate_threshold(zone);
180 for_each_online_cpu(cpu)
181 zone_pcp(zone, cpu)->stat_threshold = threshold;
186 * For use when we know that interrupts are disabled.
188 void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
191 struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
192 s8 *p = pcp->vm_stat_diff + item;
197 if (unlikely(x > pcp->stat_threshold || x < -pcp->stat_threshold)) {
198 zone_page_state_add(x, zone, item);
203 EXPORT_SYMBOL(__mod_zone_page_state);
206 * For an unknown interrupt state
208 void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
213 local_irq_save(flags);
214 __mod_zone_page_state(zone, item, delta);
215 local_irq_restore(flags);
217 EXPORT_SYMBOL(mod_zone_page_state);
220 * Optimized increment and decrement functions.
222 * These are only for a single page and therefore can take a struct page *
223 * argument instead of struct zone *. This allows the inclusion of the code
224 * generated for page_zone(page) into the optimized functions.
226 * No overflow check is necessary and therefore the differential can be
227 * incremented or decremented in place which may allow the compilers to
228 * generate better code.
229 * The increment or decrement is known and therefore one boundary check can
232 * NOTE: These functions are very performance sensitive. Change only
235 * Some processors have inc/dec instructions that are atomic vs an interrupt.
236 * However, the code must first determine the differential location in a zone
237 * based on the processor number and then inc/dec the counter. There is no
238 * guarantee without disabling preemption that the processor will not change
239 * in between and therefore the atomicity vs. interrupt cannot be exploited
240 * in a useful way here.
242 static void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
244 struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
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 __inc_zone_page_state(struct page *page, enum zone_stat_item item)
259 __inc_zone_state(page_zone(page), item);
261 EXPORT_SYMBOL(__inc_zone_page_state);
263 void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
265 struct zone *zone = page_zone(page);
266 struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
267 s8 *p = pcp->vm_stat_diff + item;
271 if (unlikely(*p < - pcp->stat_threshold)) {
272 int overstep = pcp->stat_threshold / 2;
274 zone_page_state_add(*p - overstep, zone, item);
278 EXPORT_SYMBOL(__dec_zone_page_state);
280 void inc_zone_state(struct zone *zone, enum zone_stat_item item)
284 local_irq_save(flags);
285 __inc_zone_state(zone, item);
286 local_irq_restore(flags);
289 void inc_zone_page_state(struct page *page, enum zone_stat_item item)
294 zone = page_zone(page);
295 local_irq_save(flags);
296 __inc_zone_state(zone, item);
297 local_irq_restore(flags);
299 EXPORT_SYMBOL(inc_zone_page_state);
301 void dec_zone_page_state(struct page *page, enum zone_stat_item item)
305 local_irq_save(flags);
306 __dec_zone_page_state(page, item);
307 local_irq_restore(flags);
309 EXPORT_SYMBOL(dec_zone_page_state);
312 * Update the zone counters for one cpu.
314 void refresh_cpu_vm_stats(int cpu)
320 for_each_zone(zone) {
321 struct per_cpu_pageset *pcp;
323 pcp = zone_pcp(zone, cpu);
325 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
326 if (pcp->vm_stat_diff[i]) {
327 local_irq_save(flags);
328 zone_page_state_add(pcp->vm_stat_diff[i],
330 pcp->vm_stat_diff[i] = 0;
331 local_irq_restore(flags);
336 static void __refresh_cpu_vm_stats(void *dummy)
338 refresh_cpu_vm_stats(smp_processor_id());
342 * Consolidate all counters.
344 * Note that the result is less inaccurate but still inaccurate
345 * if concurrent processes are allowed to run.
347 void refresh_vm_stats(void)
349 on_each_cpu(__refresh_cpu_vm_stats, NULL, 0, 1);
351 EXPORT_SYMBOL(refresh_vm_stats);
357 * zonelist = the list of zones passed to the allocator
358 * z = the zone from which the allocation occurred.
360 * Must be called with interrupts disabled.
362 void zone_statistics(struct zonelist *zonelist, struct zone *z)
364 if (z->zone_pgdat == zonelist->zones[0]->zone_pgdat) {
365 __inc_zone_state(z, NUMA_HIT);
367 __inc_zone_state(z, NUMA_MISS);
368 __inc_zone_state(zonelist->zones[0], NUMA_FOREIGN);
370 if (z->zone_pgdat == NODE_DATA(numa_node_id()))
371 __inc_zone_state(z, NUMA_LOCAL);
373 __inc_zone_state(z, NUMA_OTHER);
377 #ifdef CONFIG_PROC_FS
379 #include <linux/seq_file.h>
381 static void *frag_start(struct seq_file *m, loff_t *pos)
385 for (pgdat = first_online_pgdat();
387 pgdat = next_online_pgdat(pgdat))
393 static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
395 pg_data_t *pgdat = (pg_data_t *)arg;
398 return next_online_pgdat(pgdat);
401 static void frag_stop(struct seq_file *m, void *arg)
406 * This walks the free areas for each zone.
408 static int frag_show(struct seq_file *m, void *arg)
410 pg_data_t *pgdat = (pg_data_t *)arg;
412 struct zone *node_zones = pgdat->node_zones;
416 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
417 if (!populated_zone(zone))
420 spin_lock_irqsave(&zone->lock, flags);
421 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
422 for (order = 0; order < MAX_ORDER; ++order)
423 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
424 spin_unlock_irqrestore(&zone->lock, flags);
430 struct seq_operations fragmentation_op = {
437 static char *vmstat_text[] = {
438 /* Zoned VM counters */
443 "nr_page_table_pages",
458 #ifdef CONFIG_VM_EVENT_COUNTERS
487 "pgscan_kswapd_dma32",
488 "pgscan_kswapd_normal",
489 "pgscan_kswapd_high",
492 "pgscan_direct_dma32",
493 "pgscan_direct_normal",
494 "pgscan_direct_high",
508 * Output information about zones in @pgdat.
510 static int zoneinfo_show(struct seq_file *m, void *arg)
512 pg_data_t *pgdat = arg;
514 struct zone *node_zones = pgdat->node_zones;
517 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
520 if (!populated_zone(zone))
523 spin_lock_irqsave(&zone->lock, flags);
524 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
532 "\n scanned %lu (a: %lu i: %lu)"
542 zone->nr_scan_active, zone->nr_scan_inactive,
544 zone->present_pages);
546 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
547 seq_printf(m, "\n %-12s %lu", vmstat_text[i],
548 zone_page_state(zone, i));
551 "\n protection: (%lu",
552 zone->lowmem_reserve[0]);
553 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
554 seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
558 for_each_online_cpu(i) {
559 struct per_cpu_pageset *pageset;
562 pageset = zone_pcp(zone, i);
563 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
564 if (pageset->pcp[j].count)
567 if (j == ARRAY_SIZE(pageset->pcp))
569 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
576 pageset->pcp[j].count,
577 pageset->pcp[j].high,
578 pageset->pcp[j].batch);
581 seq_printf(m, "\n vm stats threshold: %d",
582 pageset->stat_threshold);
586 "\n all_unreclaimable: %u"
587 "\n prev_priority: %i"
589 zone->all_unreclaimable,
591 zone->zone_start_pfn);
592 spin_unlock_irqrestore(&zone->lock, flags);
598 struct seq_operations zoneinfo_op = {
599 .start = frag_start, /* iterate over all zones. The same as in
603 .show = zoneinfo_show,
606 static void *vmstat_start(struct seq_file *m, loff_t *pos)
609 #ifdef CONFIG_VM_EVENT_COUNTERS
614 if (*pos >= ARRAY_SIZE(vmstat_text))
617 #ifdef CONFIG_VM_EVENT_COUNTERS
618 v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long)
619 + sizeof(struct vm_event_state), GFP_KERNEL);
621 v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long),
626 return ERR_PTR(-ENOMEM);
627 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
628 v[i] = global_page_state(i);
629 #ifdef CONFIG_VM_EVENT_COUNTERS
630 e = v + NR_VM_ZONE_STAT_ITEMS;
632 e[PGPGIN] /= 2; /* sectors -> kbytes */
638 static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
641 if (*pos >= ARRAY_SIZE(vmstat_text))
643 return (unsigned long *)m->private + *pos;
646 static int vmstat_show(struct seq_file *m, void *arg)
648 unsigned long *l = arg;
649 unsigned long off = l - (unsigned long *)m->private;
651 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
655 static void vmstat_stop(struct seq_file *m, void *arg)
661 struct seq_operations vmstat_op = {
662 .start = vmstat_start,
668 #endif /* CONFIG_PROC_FS */
672 * Use the cpu notifier to insure that the thresholds are recalculated
675 static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
676 unsigned long action,
681 case CPU_UP_CANCELED:
683 refresh_zone_stat_thresholds();
691 static struct notifier_block __cpuinitdata vmstat_notifier =
692 { &vmstat_cpuup_callback, NULL, 0 };
694 int __init setup_vmstat(void)
696 refresh_zone_stat_thresholds();
697 register_cpu_notifier(&vmstat_notifier);
700 module_init(setup_vmstat)