[PATCH] Drop get_zone_counts()
[linux-2.6.git] / mm / vmstat.c
blob5ffa8c27ead81ea6837675c5b0d70e9405dbc329
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>
12 #include <linux/mm.h>
13 #include <linux/module.h>
14 #include <linux/cpu.h>
16 #ifdef CONFIG_VM_EVENT_COUNTERS
17 DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
18 EXPORT_PER_CPU_SYMBOL(vm_event_states);
20 static void sum_vm_events(unsigned long *ret, cpumask_t *cpumask)
22 int cpu = 0;
23 int i;
25 memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
27 cpu = first_cpu(*cpumask);
28 while (cpu < NR_CPUS) {
29 struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
31 cpu = next_cpu(cpu, *cpumask);
33 if (cpu < NR_CPUS)
34 prefetch(&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 sum_vm_events(ret, &cpu_online_map);
51 EXPORT_SYMBOL_GPL(all_vm_events);
53 #ifdef CONFIG_HOTPLUG
55 * Fold the foreign cpu events into our own.
57 * This is adding to the events on one processor
58 * but keeps the global counts constant.
60 void vm_events_fold_cpu(int cpu)
62 struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu);
63 int i;
65 for (i = 0; i < NR_VM_EVENT_ITEMS; i++) {
66 count_vm_events(i, fold_state->event[i]);
67 fold_state->event[i] = 0;
70 #endif /* CONFIG_HOTPLUG */
72 #endif /* CONFIG_VM_EVENT_COUNTERS */
75 * Manage combined zone based / global counters
77 * vm_stat contains the global counters
79 atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
80 EXPORT_SYMBOL(vm_stat);
82 #ifdef CONFIG_SMP
84 static int calculate_threshold(struct zone *zone)
86 int threshold;
87 int mem; /* memory in 128 MB units */
90 * The threshold scales with the number of processors and the amount
91 * of memory per zone. More memory means that we can defer updates for
92 * longer, more processors could lead to more contention.
93 * fls() is used to have a cheap way of logarithmic scaling.
95 * Some sample thresholds:
97 * Threshold Processors (fls) Zonesize fls(mem+1)
98 * ------------------------------------------------------------------
99 * 8 1 1 0.9-1 GB 4
100 * 16 2 2 0.9-1 GB 4
101 * 20 2 2 1-2 GB 5
102 * 24 2 2 2-4 GB 6
103 * 28 2 2 4-8 GB 7
104 * 32 2 2 8-16 GB 8
105 * 4 2 2 <128M 1
106 * 30 4 3 2-4 GB 5
107 * 48 4 3 8-16 GB 8
108 * 32 8 4 1-2 GB 4
109 * 32 8 4 0.9-1GB 4
110 * 10 16 5 <128M 1
111 * 40 16 5 900M 4
112 * 70 64 7 2-4 GB 5
113 * 84 64 7 4-8 GB 6
114 * 108 512 9 4-8 GB 6
115 * 125 1024 10 8-16 GB 8
116 * 125 1024 10 16-32 GB 9
119 mem = zone->present_pages >> (27 - PAGE_SHIFT);
121 threshold = 2 * fls(num_online_cpus()) * (1 + fls(mem));
124 * Maximum threshold is 125
126 threshold = min(125, threshold);
128 return threshold;
132 * Refresh the thresholds for each zone.
134 static void refresh_zone_stat_thresholds(void)
136 struct zone *zone;
137 int cpu;
138 int threshold;
140 for_each_zone(zone) {
142 if (!zone->present_pages)
143 continue;
145 threshold = calculate_threshold(zone);
147 for_each_online_cpu(cpu)
148 zone_pcp(zone, cpu)->stat_threshold = threshold;
153 * For use when we know that interrupts are disabled.
155 void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
156 int delta)
158 struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
159 s8 *p = pcp->vm_stat_diff + item;
160 long x;
162 x = delta + *p;
164 if (unlikely(x > pcp->stat_threshold || x < -pcp->stat_threshold)) {
165 zone_page_state_add(x, zone, item);
166 x = 0;
168 *p = x;
170 EXPORT_SYMBOL(__mod_zone_page_state);
173 * For an unknown interrupt state
175 void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
176 int delta)
178 unsigned long flags;
180 local_irq_save(flags);
181 __mod_zone_page_state(zone, item, delta);
182 local_irq_restore(flags);
184 EXPORT_SYMBOL(mod_zone_page_state);
187 * Optimized increment and decrement functions.
189 * These are only for a single page and therefore can take a struct page *
190 * argument instead of struct zone *. This allows the inclusion of the code
191 * generated for page_zone(page) into the optimized functions.
193 * No overflow check is necessary and therefore the differential can be
194 * incremented or decremented in place which may allow the compilers to
195 * generate better code.
196 * The increment or decrement is known and therefore one boundary check can
197 * be omitted.
199 * NOTE: These functions are very performance sensitive. Change only
200 * with care.
202 * Some processors have inc/dec instructions that are atomic vs an interrupt.
203 * However, the code must first determine the differential location in a zone
204 * based on the processor number and then inc/dec the counter. There is no
205 * guarantee without disabling preemption that the processor will not change
206 * in between and therefore the atomicity vs. interrupt cannot be exploited
207 * in a useful way here.
209 void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
211 struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
212 s8 *p = pcp->vm_stat_diff + item;
214 (*p)++;
216 if (unlikely(*p > pcp->stat_threshold)) {
217 int overstep = pcp->stat_threshold / 2;
219 zone_page_state_add(*p + overstep, zone, item);
220 *p = -overstep;
224 void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
226 __inc_zone_state(page_zone(page), item);
228 EXPORT_SYMBOL(__inc_zone_page_state);
230 void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
232 struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
233 s8 *p = pcp->vm_stat_diff + item;
235 (*p)--;
237 if (unlikely(*p < - pcp->stat_threshold)) {
238 int overstep = pcp->stat_threshold / 2;
240 zone_page_state_add(*p - overstep, zone, item);
241 *p = overstep;
245 void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
247 __dec_zone_state(page_zone(page), item);
249 EXPORT_SYMBOL(__dec_zone_page_state);
251 void inc_zone_state(struct zone *zone, enum zone_stat_item item)
253 unsigned long flags;
255 local_irq_save(flags);
256 __inc_zone_state(zone, item);
257 local_irq_restore(flags);
260 void inc_zone_page_state(struct page *page, enum zone_stat_item item)
262 unsigned long flags;
263 struct zone *zone;
265 zone = page_zone(page);
266 local_irq_save(flags);
267 __inc_zone_state(zone, item);
268 local_irq_restore(flags);
270 EXPORT_SYMBOL(inc_zone_page_state);
272 void dec_zone_page_state(struct page *page, enum zone_stat_item item)
274 unsigned long flags;
276 local_irq_save(flags);
277 __dec_zone_page_state(page, item);
278 local_irq_restore(flags);
280 EXPORT_SYMBOL(dec_zone_page_state);
283 * Update the zone counters for one cpu.
285 void refresh_cpu_vm_stats(int cpu)
287 struct zone *zone;
288 int i;
289 unsigned long flags;
291 for_each_zone(zone) {
292 struct per_cpu_pageset *pcp;
294 if (!populated_zone(zone))
295 continue;
297 pcp = zone_pcp(zone, cpu);
299 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
300 if (pcp->vm_stat_diff[i]) {
301 local_irq_save(flags);
302 zone_page_state_add(pcp->vm_stat_diff[i],
303 zone, i);
304 pcp->vm_stat_diff[i] = 0;
305 local_irq_restore(flags);
310 static void __refresh_cpu_vm_stats(void *dummy)
312 refresh_cpu_vm_stats(smp_processor_id());
316 * Consolidate all counters.
318 * Note that the result is less inaccurate but still inaccurate
319 * if concurrent processes are allowed to run.
321 void refresh_vm_stats(void)
323 on_each_cpu(__refresh_cpu_vm_stats, NULL, 0, 1);
325 EXPORT_SYMBOL(refresh_vm_stats);
327 #endif
329 #ifdef CONFIG_NUMA
331 * zonelist = the list of zones passed to the allocator
332 * z = the zone from which the allocation occurred.
334 * Must be called with interrupts disabled.
336 void zone_statistics(struct zonelist *zonelist, struct zone *z)
338 if (z->zone_pgdat == zonelist->zones[0]->zone_pgdat) {
339 __inc_zone_state(z, NUMA_HIT);
340 } else {
341 __inc_zone_state(z, NUMA_MISS);
342 __inc_zone_state(zonelist->zones[0], NUMA_FOREIGN);
344 if (z->node == numa_node_id())
345 __inc_zone_state(z, NUMA_LOCAL);
346 else
347 __inc_zone_state(z, NUMA_OTHER);
349 #endif
351 #ifdef CONFIG_PROC_FS
353 #include <linux/seq_file.h>
355 static void *frag_start(struct seq_file *m, loff_t *pos)
357 pg_data_t *pgdat;
358 loff_t node = *pos;
359 for (pgdat = first_online_pgdat();
360 pgdat && node;
361 pgdat = next_online_pgdat(pgdat))
362 --node;
364 return pgdat;
367 static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
369 pg_data_t *pgdat = (pg_data_t *)arg;
371 (*pos)++;
372 return next_online_pgdat(pgdat);
375 static void frag_stop(struct seq_file *m, void *arg)
380 * This walks the free areas for each zone.
382 static int frag_show(struct seq_file *m, void *arg)
384 pg_data_t *pgdat = (pg_data_t *)arg;
385 struct zone *zone;
386 struct zone *node_zones = pgdat->node_zones;
387 unsigned long flags;
388 int order;
390 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
391 if (!populated_zone(zone))
392 continue;
394 spin_lock_irqsave(&zone->lock, flags);
395 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
396 for (order = 0; order < MAX_ORDER; ++order)
397 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
398 spin_unlock_irqrestore(&zone->lock, flags);
399 seq_putc(m, '\n');
401 return 0;
404 const struct seq_operations fragmentation_op = {
405 .start = frag_start,
406 .next = frag_next,
407 .stop = frag_stop,
408 .show = frag_show,
411 #ifdef CONFIG_ZONE_DMA32
412 #define TEXT_FOR_DMA32(xx) xx "_dma32",
413 #else
414 #define TEXT_FOR_DMA32(xx)
415 #endif
417 #ifdef CONFIG_HIGHMEM
418 #define TEXT_FOR_HIGHMEM(xx) xx "_high",
419 #else
420 #define TEXT_FOR_HIGHMEM(xx)
421 #endif
423 #define TEXTS_FOR_ZONES(xx) xx "_dma", TEXT_FOR_DMA32(xx) xx "_normal", \
424 TEXT_FOR_HIGHMEM(xx)
426 static const char * const vmstat_text[] = {
427 /* Zoned VM counters */
428 "nr_free_pages",
429 "nr_active",
430 "nr_inactive",
431 "nr_anon_pages",
432 "nr_mapped",
433 "nr_file_pages",
434 "nr_dirty",
435 "nr_writeback",
436 "nr_slab_reclaimable",
437 "nr_slab_unreclaimable",
438 "nr_page_table_pages",
439 "nr_unstable",
440 "nr_bounce",
441 "nr_vmscan_write",
443 #ifdef CONFIG_NUMA
444 "numa_hit",
445 "numa_miss",
446 "numa_foreign",
447 "numa_interleave",
448 "numa_local",
449 "numa_other",
450 #endif
452 #ifdef CONFIG_VM_EVENT_COUNTERS
453 "pgpgin",
454 "pgpgout",
455 "pswpin",
456 "pswpout",
458 TEXTS_FOR_ZONES("pgalloc")
460 "pgfree",
461 "pgactivate",
462 "pgdeactivate",
464 "pgfault",
465 "pgmajfault",
467 TEXTS_FOR_ZONES("pgrefill")
468 TEXTS_FOR_ZONES("pgsteal")
469 TEXTS_FOR_ZONES("pgscan_kswapd")
470 TEXTS_FOR_ZONES("pgscan_direct")
472 "pginodesteal",
473 "slabs_scanned",
474 "kswapd_steal",
475 "kswapd_inodesteal",
476 "pageoutrun",
477 "allocstall",
479 "pgrotated",
480 #endif
484 * Output information about zones in @pgdat.
486 static int zoneinfo_show(struct seq_file *m, void *arg)
488 pg_data_t *pgdat = arg;
489 struct zone *zone;
490 struct zone *node_zones = pgdat->node_zones;
491 unsigned long flags;
493 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
494 int i;
496 if (!populated_zone(zone))
497 continue;
499 spin_lock_irqsave(&zone->lock, flags);
500 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
501 seq_printf(m,
502 "\n pages free %lu"
503 "\n min %lu"
504 "\n low %lu"
505 "\n high %lu"
506 "\n scanned %lu (a: %lu i: %lu)"
507 "\n spanned %lu"
508 "\n present %lu",
509 zone_page_state(zone, NR_FREE_PAGES),
510 zone->pages_min,
511 zone->pages_low,
512 zone->pages_high,
513 zone->pages_scanned,
514 zone->nr_scan_active, zone->nr_scan_inactive,
515 zone->spanned_pages,
516 zone->present_pages);
518 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
519 seq_printf(m, "\n %-12s %lu", vmstat_text[i],
520 zone_page_state(zone, i));
522 seq_printf(m,
523 "\n protection: (%lu",
524 zone->lowmem_reserve[0]);
525 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
526 seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
527 seq_printf(m,
529 "\n pagesets");
530 for_each_online_cpu(i) {
531 struct per_cpu_pageset *pageset;
532 int j;
534 pageset = zone_pcp(zone, i);
535 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
536 seq_printf(m,
537 "\n cpu: %i pcp: %i"
538 "\n count: %i"
539 "\n high: %i"
540 "\n batch: %i",
541 i, j,
542 pageset->pcp[j].count,
543 pageset->pcp[j].high,
544 pageset->pcp[j].batch);
546 #ifdef CONFIG_SMP
547 seq_printf(m, "\n vm stats threshold: %d",
548 pageset->stat_threshold);
549 #endif
551 seq_printf(m,
552 "\n all_unreclaimable: %u"
553 "\n prev_priority: %i"
554 "\n start_pfn: %lu",
555 zone->all_unreclaimable,
556 zone->prev_priority,
557 zone->zone_start_pfn);
558 spin_unlock_irqrestore(&zone->lock, flags);
559 seq_putc(m, '\n');
561 return 0;
564 const struct seq_operations zoneinfo_op = {
565 .start = frag_start, /* iterate over all zones. The same as in
566 * fragmentation. */
567 .next = frag_next,
568 .stop = frag_stop,
569 .show = zoneinfo_show,
572 static void *vmstat_start(struct seq_file *m, loff_t *pos)
574 unsigned long *v;
575 #ifdef CONFIG_VM_EVENT_COUNTERS
576 unsigned long *e;
577 #endif
578 int i;
580 if (*pos >= ARRAY_SIZE(vmstat_text))
581 return NULL;
583 #ifdef CONFIG_VM_EVENT_COUNTERS
584 v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long)
585 + sizeof(struct vm_event_state), GFP_KERNEL);
586 #else
587 v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long),
588 GFP_KERNEL);
589 #endif
590 m->private = v;
591 if (!v)
592 return ERR_PTR(-ENOMEM);
593 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
594 v[i] = global_page_state(i);
595 #ifdef CONFIG_VM_EVENT_COUNTERS
596 e = v + NR_VM_ZONE_STAT_ITEMS;
597 all_vm_events(e);
598 e[PGPGIN] /= 2; /* sectors -> kbytes */
599 e[PGPGOUT] /= 2;
600 #endif
601 return v + *pos;
604 static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
606 (*pos)++;
607 if (*pos >= ARRAY_SIZE(vmstat_text))
608 return NULL;
609 return (unsigned long *)m->private + *pos;
612 static int vmstat_show(struct seq_file *m, void *arg)
614 unsigned long *l = arg;
615 unsigned long off = l - (unsigned long *)m->private;
617 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
618 return 0;
621 static void vmstat_stop(struct seq_file *m, void *arg)
623 kfree(m->private);
624 m->private = NULL;
627 const struct seq_operations vmstat_op = {
628 .start = vmstat_start,
629 .next = vmstat_next,
630 .stop = vmstat_stop,
631 .show = vmstat_show,
634 #endif /* CONFIG_PROC_FS */
636 #ifdef CONFIG_SMP
638 * Use the cpu notifier to insure that the thresholds are recalculated
639 * when necessary.
641 static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
642 unsigned long action,
643 void *hcpu)
645 switch (action) {
646 case CPU_UP_PREPARE:
647 case CPU_UP_CANCELED:
648 case CPU_DEAD:
649 refresh_zone_stat_thresholds();
650 break;
651 default:
652 break;
654 return NOTIFY_OK;
657 static struct notifier_block __cpuinitdata vmstat_notifier =
658 { &vmstat_cpuup_callback, NULL, 0 };
660 int __init setup_vmstat(void)
662 refresh_zone_stat_thresholds();
663 register_cpu_notifier(&vmstat_notifier);
664 return 0;
666 module_init(setup_vmstat)
667 #endif