[BATTERY] Universal power supply class (was: battery class)
[linux-2.6/openmoko-kernel/knife-kernel.git] / mm / vmstat.c
blobeceaf496210fcf241d55482e9dd317c2a9d5dc56
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>
15 #include <linux/sched.h>
17 #ifdef CONFIG_VM_EVENT_COUNTERS
18 DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
19 EXPORT_PER_CPU_SYMBOL(vm_event_states);
21 static void sum_vm_events(unsigned long *ret, cpumask_t *cpumask)
23 int cpu = 0;
24 int i;
26 memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
28 cpu = first_cpu(*cpumask);
29 while (cpu < NR_CPUS) {
30 struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
32 cpu = next_cpu(cpu, *cpumask);
34 if (cpu < NR_CPUS)
35 prefetch(&per_cpu(vm_event_states, cpu));
38 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
39 ret[i] += this->event[i];
44 * Accumulate the vm event counters across all CPUs.
45 * The result is unavoidably approximate - it can change
46 * during and after execution of this function.
48 void all_vm_events(unsigned long *ret)
50 sum_vm_events(ret, &cpu_online_map);
52 EXPORT_SYMBOL_GPL(all_vm_events);
54 #ifdef CONFIG_HOTPLUG
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);
64 int i;
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);
83 #ifdef CONFIG_SMP
85 static int calculate_threshold(struct zone *zone)
87 int threshold;
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 * ------------------------------------------------------------------
100 * 8 1 1 0.9-1 GB 4
101 * 16 2 2 0.9-1 GB 4
102 * 20 2 2 1-2 GB 5
103 * 24 2 2 2-4 GB 6
104 * 28 2 2 4-8 GB 7
105 * 32 2 2 8-16 GB 8
106 * 4 2 2 <128M 1
107 * 30 4 3 2-4 GB 5
108 * 48 4 3 8-16 GB 8
109 * 32 8 4 1-2 GB 4
110 * 32 8 4 0.9-1GB 4
111 * 10 16 5 <128M 1
112 * 40 16 5 900M 4
113 * 70 64 7 2-4 GB 5
114 * 84 64 7 4-8 GB 6
115 * 108 512 9 4-8 GB 6
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);
129 return threshold;
133 * Refresh the thresholds for each zone.
135 static void refresh_zone_stat_thresholds(void)
137 struct zone *zone;
138 int cpu;
139 int threshold;
141 for_each_zone(zone) {
143 if (!zone->present_pages)
144 continue;
146 threshold = calculate_threshold(zone);
148 for_each_online_cpu(cpu)
149 zone_pcp(zone, cpu)->stat_threshold = threshold;
154 * For use when we know that interrupts are disabled.
156 void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
157 int delta)
159 struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
160 s8 *p = pcp->vm_stat_diff + item;
161 long x;
163 x = delta + *p;
165 if (unlikely(x > pcp->stat_threshold || x < -pcp->stat_threshold)) {
166 zone_page_state_add(x, zone, item);
167 x = 0;
169 *p = x;
171 EXPORT_SYMBOL(__mod_zone_page_state);
174 * For an unknown interrupt state
176 void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
177 int delta)
179 unsigned long flags;
181 local_irq_save(flags);
182 __mod_zone_page_state(zone, item, delta);
183 local_irq_restore(flags);
185 EXPORT_SYMBOL(mod_zone_page_state);
188 * Optimized increment and decrement functions.
190 * These are only for a single page and therefore can take a struct page *
191 * argument instead of struct zone *. This allows the inclusion of the code
192 * generated for page_zone(page) into the optimized functions.
194 * No overflow check is necessary and therefore the differential can be
195 * incremented or decremented in place which may allow the compilers to
196 * generate better code.
197 * The increment or decrement is known and therefore one boundary check can
198 * be omitted.
200 * NOTE: These functions are very performance sensitive. Change only
201 * with care.
203 * Some processors have inc/dec instructions that are atomic vs an interrupt.
204 * However, the code must first determine the differential location in a zone
205 * based on the processor number and then inc/dec the counter. There is no
206 * guarantee without disabling preemption that the processor will not change
207 * in between and therefore the atomicity vs. interrupt cannot be exploited
208 * in a useful way here.
210 void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
212 struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
213 s8 *p = pcp->vm_stat_diff + item;
215 (*p)++;
217 if (unlikely(*p > pcp->stat_threshold)) {
218 int overstep = pcp->stat_threshold / 2;
220 zone_page_state_add(*p + overstep, zone, item);
221 *p = -overstep;
225 void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
227 __inc_zone_state(page_zone(page), item);
229 EXPORT_SYMBOL(__inc_zone_page_state);
231 void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
233 struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
234 s8 *p = pcp->vm_stat_diff + item;
236 (*p)--;
238 if (unlikely(*p < - pcp->stat_threshold)) {
239 int overstep = pcp->stat_threshold / 2;
241 zone_page_state_add(*p - overstep, zone, item);
242 *p = overstep;
246 void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
248 __dec_zone_state(page_zone(page), item);
250 EXPORT_SYMBOL(__dec_zone_page_state);
252 void inc_zone_state(struct zone *zone, enum zone_stat_item item)
254 unsigned long flags;
256 local_irq_save(flags);
257 __inc_zone_state(zone, item);
258 local_irq_restore(flags);
261 void inc_zone_page_state(struct page *page, enum zone_stat_item item)
263 unsigned long flags;
264 struct zone *zone;
266 zone = page_zone(page);
267 local_irq_save(flags);
268 __inc_zone_state(zone, item);
269 local_irq_restore(flags);
271 EXPORT_SYMBOL(inc_zone_page_state);
273 void dec_zone_page_state(struct page *page, enum zone_stat_item item)
275 unsigned long flags;
277 local_irq_save(flags);
278 __dec_zone_page_state(page, item);
279 local_irq_restore(flags);
281 EXPORT_SYMBOL(dec_zone_page_state);
284 * Update the zone counters for one cpu.
286 * Note that refresh_cpu_vm_stats strives to only access
287 * node local memory. The per cpu pagesets on remote zones are placed
288 * in the memory local to the processor using that pageset. So the
289 * loop over all zones will access a series of cachelines local to
290 * the processor.
292 * The call to zone_page_state_add updates the cachelines with the
293 * statistics in the remote zone struct as well as the global cachelines
294 * with the global counters. These could cause remote node cache line
295 * bouncing and will have to be only done when necessary.
297 void refresh_cpu_vm_stats(int cpu)
299 struct zone *zone;
300 int i;
301 unsigned long flags;
303 for_each_zone(zone) {
304 struct per_cpu_pageset *p;
306 if (!populated_zone(zone))
307 continue;
309 p = zone_pcp(zone, cpu);
311 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
312 if (p->vm_stat_diff[i]) {
313 local_irq_save(flags);
314 zone_page_state_add(p->vm_stat_diff[i],
315 zone, i);
316 p->vm_stat_diff[i] = 0;
317 #ifdef CONFIG_NUMA
318 /* 3 seconds idle till flush */
319 p->expire = 3;
320 #endif
321 local_irq_restore(flags);
323 #ifdef CONFIG_NUMA
325 * Deal with draining the remote pageset of this
326 * processor
328 * Check if there are pages remaining in this pageset
329 * if not then there is nothing to expire.
331 if (!p->expire || (!p->pcp[0].count && !p->pcp[1].count))
332 continue;
335 * We never drain zones local to this processor.
337 if (zone_to_nid(zone) == numa_node_id()) {
338 p->expire = 0;
339 continue;
342 p->expire--;
343 if (p->expire)
344 continue;
346 if (p->pcp[0].count)
347 drain_zone_pages(zone, p->pcp + 0);
349 if (p->pcp[1].count)
350 drain_zone_pages(zone, p->pcp + 1);
351 #endif
355 static void __refresh_cpu_vm_stats(void *dummy)
357 refresh_cpu_vm_stats(smp_processor_id());
361 * Consolidate all counters.
363 * Note that the result is less inaccurate but still inaccurate
364 * if concurrent processes are allowed to run.
366 void refresh_vm_stats(void)
368 on_each_cpu(__refresh_cpu_vm_stats, NULL, 0, 1);
370 EXPORT_SYMBOL(refresh_vm_stats);
372 #endif
374 #ifdef CONFIG_NUMA
376 * zonelist = the list of zones passed to the allocator
377 * z = the zone from which the allocation occurred.
379 * Must be called with interrupts disabled.
381 void zone_statistics(struct zonelist *zonelist, struct zone *z)
383 if (z->zone_pgdat == zonelist->zones[0]->zone_pgdat) {
384 __inc_zone_state(z, NUMA_HIT);
385 } else {
386 __inc_zone_state(z, NUMA_MISS);
387 __inc_zone_state(zonelist->zones[0], NUMA_FOREIGN);
389 if (z->node == numa_node_id())
390 __inc_zone_state(z, NUMA_LOCAL);
391 else
392 __inc_zone_state(z, NUMA_OTHER);
394 #endif
396 #ifdef CONFIG_PROC_FS
398 #include <linux/seq_file.h>
400 static void *frag_start(struct seq_file *m, loff_t *pos)
402 pg_data_t *pgdat;
403 loff_t node = *pos;
404 for (pgdat = first_online_pgdat();
405 pgdat && node;
406 pgdat = next_online_pgdat(pgdat))
407 --node;
409 return pgdat;
412 static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
414 pg_data_t *pgdat = (pg_data_t *)arg;
416 (*pos)++;
417 return next_online_pgdat(pgdat);
420 static void frag_stop(struct seq_file *m, void *arg)
425 * This walks the free areas for each zone.
427 static int frag_show(struct seq_file *m, void *arg)
429 pg_data_t *pgdat = (pg_data_t *)arg;
430 struct zone *zone;
431 struct zone *node_zones = pgdat->node_zones;
432 unsigned long flags;
433 int order;
435 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
436 if (!populated_zone(zone))
437 continue;
439 spin_lock_irqsave(&zone->lock, flags);
440 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
441 for (order = 0; order < MAX_ORDER; ++order)
442 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
443 spin_unlock_irqrestore(&zone->lock, flags);
444 seq_putc(m, '\n');
446 return 0;
449 const struct seq_operations fragmentation_op = {
450 .start = frag_start,
451 .next = frag_next,
452 .stop = frag_stop,
453 .show = frag_show,
456 #ifdef CONFIG_ZONE_DMA
457 #define TEXT_FOR_DMA(xx) xx "_dma",
458 #else
459 #define TEXT_FOR_DMA(xx)
460 #endif
462 #ifdef CONFIG_ZONE_DMA32
463 #define TEXT_FOR_DMA32(xx) xx "_dma32",
464 #else
465 #define TEXT_FOR_DMA32(xx)
466 #endif
468 #ifdef CONFIG_HIGHMEM
469 #define TEXT_FOR_HIGHMEM(xx) xx "_high",
470 #else
471 #define TEXT_FOR_HIGHMEM(xx)
472 #endif
474 #define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
475 TEXT_FOR_HIGHMEM(xx)
477 static const char * const vmstat_text[] = {
478 /* Zoned VM counters */
479 "nr_free_pages",
480 "nr_inactive",
481 "nr_active",
482 "nr_anon_pages",
483 "nr_mapped",
484 "nr_file_pages",
485 "nr_dirty",
486 "nr_writeback",
487 "nr_slab_reclaimable",
488 "nr_slab_unreclaimable",
489 "nr_page_table_pages",
490 "nr_unstable",
491 "nr_bounce",
492 "nr_vmscan_write",
494 #ifdef CONFIG_NUMA
495 "numa_hit",
496 "numa_miss",
497 "numa_foreign",
498 "numa_interleave",
499 "numa_local",
500 "numa_other",
501 #endif
503 #ifdef CONFIG_VM_EVENT_COUNTERS
504 "pgpgin",
505 "pgpgout",
506 "pswpin",
507 "pswpout",
509 TEXTS_FOR_ZONES("pgalloc")
511 "pgfree",
512 "pgactivate",
513 "pgdeactivate",
515 "pgfault",
516 "pgmajfault",
518 TEXTS_FOR_ZONES("pgrefill")
519 TEXTS_FOR_ZONES("pgsteal")
520 TEXTS_FOR_ZONES("pgscan_kswapd")
521 TEXTS_FOR_ZONES("pgscan_direct")
523 "pginodesteal",
524 "slabs_scanned",
525 "kswapd_steal",
526 "kswapd_inodesteal",
527 "pageoutrun",
528 "allocstall",
530 "pgrotated",
531 #endif
535 * Output information about zones in @pgdat.
537 static int zoneinfo_show(struct seq_file *m, void *arg)
539 pg_data_t *pgdat = arg;
540 struct zone *zone;
541 struct zone *node_zones = pgdat->node_zones;
542 unsigned long flags;
544 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
545 int i;
547 if (!populated_zone(zone))
548 continue;
550 spin_lock_irqsave(&zone->lock, flags);
551 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
552 seq_printf(m,
553 "\n pages free %lu"
554 "\n min %lu"
555 "\n low %lu"
556 "\n high %lu"
557 "\n scanned %lu (a: %lu i: %lu)"
558 "\n spanned %lu"
559 "\n present %lu",
560 zone_page_state(zone, NR_FREE_PAGES),
561 zone->pages_min,
562 zone->pages_low,
563 zone->pages_high,
564 zone->pages_scanned,
565 zone->nr_scan_active, zone->nr_scan_inactive,
566 zone->spanned_pages,
567 zone->present_pages);
569 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
570 seq_printf(m, "\n %-12s %lu", vmstat_text[i],
571 zone_page_state(zone, i));
573 seq_printf(m,
574 "\n protection: (%lu",
575 zone->lowmem_reserve[0]);
576 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
577 seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
578 seq_printf(m,
580 "\n pagesets");
581 for_each_online_cpu(i) {
582 struct per_cpu_pageset *pageset;
583 int j;
585 pageset = zone_pcp(zone, i);
586 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
587 seq_printf(m,
588 "\n cpu: %i pcp: %i"
589 "\n count: %i"
590 "\n high: %i"
591 "\n batch: %i",
592 i, j,
593 pageset->pcp[j].count,
594 pageset->pcp[j].high,
595 pageset->pcp[j].batch);
597 #ifdef CONFIG_SMP
598 seq_printf(m, "\n vm stats threshold: %d",
599 pageset->stat_threshold);
600 #endif
602 seq_printf(m,
603 "\n all_unreclaimable: %u"
604 "\n prev_priority: %i"
605 "\n start_pfn: %lu",
606 zone->all_unreclaimable,
607 zone->prev_priority,
608 zone->zone_start_pfn);
609 spin_unlock_irqrestore(&zone->lock, flags);
610 seq_putc(m, '\n');
612 return 0;
615 const struct seq_operations zoneinfo_op = {
616 .start = frag_start, /* iterate over all zones. The same as in
617 * fragmentation. */
618 .next = frag_next,
619 .stop = frag_stop,
620 .show = zoneinfo_show,
623 static void *vmstat_start(struct seq_file *m, loff_t *pos)
625 unsigned long *v;
626 #ifdef CONFIG_VM_EVENT_COUNTERS
627 unsigned long *e;
628 #endif
629 int i;
631 if (*pos >= ARRAY_SIZE(vmstat_text))
632 return NULL;
634 #ifdef CONFIG_VM_EVENT_COUNTERS
635 v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long)
636 + sizeof(struct vm_event_state), GFP_KERNEL);
637 #else
638 v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long),
639 GFP_KERNEL);
640 #endif
641 m->private = v;
642 if (!v)
643 return ERR_PTR(-ENOMEM);
644 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
645 v[i] = global_page_state(i);
646 #ifdef CONFIG_VM_EVENT_COUNTERS
647 e = v + NR_VM_ZONE_STAT_ITEMS;
648 all_vm_events(e);
649 e[PGPGIN] /= 2; /* sectors -> kbytes */
650 e[PGPGOUT] /= 2;
651 #endif
652 return v + *pos;
655 static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
657 (*pos)++;
658 if (*pos >= ARRAY_SIZE(vmstat_text))
659 return NULL;
660 return (unsigned long *)m->private + *pos;
663 static int vmstat_show(struct seq_file *m, void *arg)
665 unsigned long *l = arg;
666 unsigned long off = l - (unsigned long *)m->private;
668 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
669 return 0;
672 static void vmstat_stop(struct seq_file *m, void *arg)
674 kfree(m->private);
675 m->private = NULL;
678 const struct seq_operations vmstat_op = {
679 .start = vmstat_start,
680 .next = vmstat_next,
681 .stop = vmstat_stop,
682 .show = vmstat_show,
685 #endif /* CONFIG_PROC_FS */
687 #ifdef CONFIG_SMP
688 static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
689 int sysctl_stat_interval __read_mostly = HZ;
691 static void vmstat_update(struct work_struct *w)
693 refresh_cpu_vm_stats(smp_processor_id());
694 schedule_delayed_work(&__get_cpu_var(vmstat_work),
695 sysctl_stat_interval);
698 static void __devinit start_cpu_timer(int cpu)
700 struct delayed_work *vmstat_work = &per_cpu(vmstat_work, cpu);
702 INIT_DELAYED_WORK_DEFERRABLE(vmstat_work, vmstat_update);
703 schedule_delayed_work_on(cpu, vmstat_work, HZ + cpu);
707 * Use the cpu notifier to insure that the thresholds are recalculated
708 * when necessary.
710 static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
711 unsigned long action,
712 void *hcpu)
714 long cpu = (long)hcpu;
716 switch (action) {
717 case CPU_ONLINE:
718 case CPU_ONLINE_FROZEN:
719 start_cpu_timer(cpu);
720 break;
721 case CPU_DOWN_PREPARE:
722 case CPU_DOWN_PREPARE_FROZEN:
723 cancel_rearming_delayed_work(&per_cpu(vmstat_work, cpu));
724 per_cpu(vmstat_work, cpu).work.func = NULL;
725 break;
726 case CPU_DOWN_FAILED:
727 case CPU_DOWN_FAILED_FROZEN:
728 start_cpu_timer(cpu);
729 break;
730 case CPU_DEAD:
731 case CPU_DEAD_FROZEN:
732 refresh_zone_stat_thresholds();
733 break;
734 default:
735 break;
737 return NOTIFY_OK;
740 static struct notifier_block __cpuinitdata vmstat_notifier =
741 { &vmstat_cpuup_callback, NULL, 0 };
743 int __init setup_vmstat(void)
745 int cpu;
747 refresh_zone_stat_thresholds();
748 register_cpu_notifier(&vmstat_notifier);
750 for_each_online_cpu(cpu)
751 start_cpu_timer(cpu);
752 return 0;
754 module_init(setup_vmstat)
755 #endif