This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / mm / vmstat.c
1 /*
2  *  linux/mm/vmstat.c
3  *
4  *  Manages VM statistics
5  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
6  *
7  *  zoned VM statistics
8  *  Copyright (C) 2006 Silicon Graphics, Inc.,
9  *              Christoph Lameter <christoph@lameter.com>
10  */
11
12 #include <linux/mm.h>
13 #include <linux/module.h>
14 #include <linux/cpu.h>
15
16 void __get_zone_counts(unsigned long *active, unsigned long *inactive,
17                         unsigned long *free, struct pglist_data *pgdat)
18 {
19         struct zone *zones = pgdat->node_zones;
20         int i;
21
22         *active = 0;
23         *inactive = 0;
24         *free = 0;
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;
29         }
30 }
31
32 void get_zone_counts(unsigned long *active,
33                 unsigned long *inactive, unsigned long *free)
34 {
35         struct pglist_data *pgdat;
36
37         *active = 0;
38         *inactive = 0;
39         *free = 0;
40         for_each_online_pgdat(pgdat) {
41                 unsigned long l, m, n;
42                 __get_zone_counts(&l, &m, &n, pgdat);
43                 *active += l;
44                 *inactive += m;
45                 *free += n;
46         }
47 }
48
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);
52
53 static void sum_vm_events(unsigned long *ret, cpumask_t *cpumask)
54 {
55         int cpu = 0;
56         int i;
57
58         memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
59
60         cpu = first_cpu(*cpumask);
61         while (cpu < NR_CPUS) {
62                 struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
63
64                 cpu = next_cpu(cpu, *cpumask);
65
66                 if (cpu < NR_CPUS)
67                         prefetch(&per_cpu(vm_event_states, cpu));
68
69
70                 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
71                         ret[i] += this->event[i];
72         }
73 }
74
75 /*
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.
79 */
80 void all_vm_events(unsigned long *ret)
81 {
82         sum_vm_events(ret, &cpu_online_map);
83 }
84 EXPORT_SYMBOL_GPL(all_vm_events);
85
86 #ifdef CONFIG_HOTPLUG
87 /*
88  * Fold the foreign cpu events into our own.
89  *
90  * This is adding to the events on one processor
91  * but keeps the global counts constant.
92  */
93 void vm_events_fold_cpu(int cpu)
94 {
95         struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu);
96         int i;
97
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;
101         }
102 }
103 #endif /* CONFIG_HOTPLUG */
104
105 #endif /* CONFIG_VM_EVENT_COUNTERS */
106
107 /*
108  * Manage combined zone based / global counters
109  *
110  * vm_stat contains the global counters
111  */
112 atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
113 EXPORT_SYMBOL(vm_stat);
114
115 #ifdef CONFIG_SMP
116
117 static int calculate_threshold(struct zone *zone)
118 {
119         int threshold;
120         int mem;        /* memory in 128 MB units */
121
122         /*
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.
127          *
128          * Some sample thresholds:
129          *
130          * Threshold    Processors      (fls)   Zonesize        fls(mem+1)
131          * ------------------------------------------------------------------
132          * 8            1               1       0.9-1 GB        4
133          * 16           2               2       0.9-1 GB        4
134          * 20           2               2       1-2 GB          5
135          * 24           2               2       2-4 GB          6
136          * 28           2               2       4-8 GB          7
137          * 32           2               2       8-16 GB         8
138          * 4            2               2       <128M           1
139          * 30           4               3       2-4 GB          5
140          * 48           4               3       8-16 GB         8
141          * 32           8               4       1-2 GB          4
142          * 32           8               4       0.9-1GB         4
143          * 10           16              5       <128M           1
144          * 40           16              5       900M            4
145          * 70           64              7       2-4 GB          5
146          * 84           64              7       4-8 GB          6
147          * 108          512             9       4-8 GB          6
148          * 125          1024            10      8-16 GB         8
149          * 125          1024            10      16-32 GB        9
150          */
151
152         mem = zone->present_pages >> (27 - PAGE_SHIFT);
153
154         threshold = 2 * fls(num_online_cpus()) * (1 + fls(mem));
155
156         /*
157          * Maximum threshold is 125
158          */
159         threshold = min(125, threshold);
160
161         return threshold;
162 }
163
164 /*
165  * Refresh the thresholds for each zone.
166  */
167 static void refresh_zone_stat_thresholds(void)
168 {
169         struct zone *zone;
170         int cpu;
171         int threshold;
172
173         for_each_zone(zone) {
174
175                 if (!zone->present_pages)
176                         continue;
177
178                 threshold = calculate_threshold(zone);
179
180                 for_each_online_cpu(cpu)
181                         zone_pcp(zone, cpu)->stat_threshold = threshold;
182         }
183 }
184
185 /*
186  * For use when we know that interrupts are disabled.
187  */
188 void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
189                                 int delta)
190 {
191         struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
192         s8 *p = pcp->vm_stat_diff + item;
193         long x;
194
195         x = delta + *p;
196
197         if (unlikely(x > pcp->stat_threshold || x < -pcp->stat_threshold)) {
198                 zone_page_state_add(x, zone, item);
199                 x = 0;
200         }
201         *p = x;
202 }
203 EXPORT_SYMBOL(__mod_zone_page_state);
204
205 /*
206  * For an unknown interrupt state
207  */
208 void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
209                                         int delta)
210 {
211         unsigned long flags;
212
213         local_irq_save(flags);
214         __mod_zone_page_state(zone, item, delta);
215         local_irq_restore(flags);
216 }
217 EXPORT_SYMBOL(mod_zone_page_state);
218
219 /*
220  * Optimized increment and decrement functions.
221  *
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.
225  *
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
230  * be omitted.
231  *
232  * NOTE: These functions are very performance sensitive. Change only
233  * with care.
234  *
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.
241  */
242 static void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
243 {
244         struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
245         s8 *p = pcp->vm_stat_diff + item;
246
247         (*p)++;
248
249         if (unlikely(*p > pcp->stat_threshold)) {
250                 int overstep = pcp->stat_threshold / 2;
251
252                 zone_page_state_add(*p + overstep, zone, item);
253                 *p = -overstep;
254         }
255 }
256
257 void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
258 {
259         __inc_zone_state(page_zone(page), item);
260 }
261 EXPORT_SYMBOL(__inc_zone_page_state);
262
263 void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
264 {
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;
268
269         (*p)--;
270
271         if (unlikely(*p < - pcp->stat_threshold)) {
272                 int overstep = pcp->stat_threshold / 2;
273
274                 zone_page_state_add(*p - overstep, zone, item);
275                 *p = overstep;
276         }
277 }
278 EXPORT_SYMBOL(__dec_zone_page_state);
279
280 void inc_zone_state(struct zone *zone, enum zone_stat_item item)
281 {
282         unsigned long flags;
283
284         local_irq_save(flags);
285         __inc_zone_state(zone, item);
286         local_irq_restore(flags);
287 }
288
289 void inc_zone_page_state(struct page *page, enum zone_stat_item item)
290 {
291         unsigned long flags;
292         struct zone *zone;
293
294         zone = page_zone(page);
295         local_irq_save(flags);
296         __inc_zone_state(zone, item);
297         local_irq_restore(flags);
298 }
299 EXPORT_SYMBOL(inc_zone_page_state);
300
301 void dec_zone_page_state(struct page *page, enum zone_stat_item item)
302 {
303         unsigned long flags;
304
305         local_irq_save(flags);
306         __dec_zone_page_state(page, item);
307         local_irq_restore(flags);
308 }
309 EXPORT_SYMBOL(dec_zone_page_state);
310
311 /*
312  * Update the zone counters for one cpu.
313  */
314 void refresh_cpu_vm_stats(int cpu)
315 {
316         struct zone *zone;
317         int i;
318         unsigned long flags;
319
320         for_each_zone(zone) {
321                 struct per_cpu_pageset *pcp;
322
323                 pcp = zone_pcp(zone, cpu);
324
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],
329                                         zone, i);
330                                 pcp->vm_stat_diff[i] = 0;
331                                 local_irq_restore(flags);
332                         }
333         }
334 }
335
336 static void __refresh_cpu_vm_stats(void *dummy)
337 {
338         refresh_cpu_vm_stats(smp_processor_id());
339 }
340
341 /*
342  * Consolidate all counters.
343  *
344  * Note that the result is less inaccurate but still inaccurate
345  * if concurrent processes are allowed to run.
346  */
347 void refresh_vm_stats(void)
348 {
349         on_each_cpu(__refresh_cpu_vm_stats, NULL, 0, 1);
350 }
351 EXPORT_SYMBOL(refresh_vm_stats);
352
353 #endif
354
355 #ifdef CONFIG_NUMA
356 /*
357  * zonelist = the list of zones passed to the allocator
358  * z        = the zone from which the allocation occurred.
359  *
360  * Must be called with interrupts disabled.
361  */
362 void zone_statistics(struct zonelist *zonelist, struct zone *z)
363 {
364         if (z->zone_pgdat == zonelist->zones[0]->zone_pgdat) {
365                 __inc_zone_state(z, NUMA_HIT);
366         } else {
367                 __inc_zone_state(z, NUMA_MISS);
368                 __inc_zone_state(zonelist->zones[0], NUMA_FOREIGN);
369         }
370         if (z->zone_pgdat == NODE_DATA(numa_node_id()))
371                 __inc_zone_state(z, NUMA_LOCAL);
372         else
373                 __inc_zone_state(z, NUMA_OTHER);
374 }
375 #endif
376
377 #ifdef CONFIG_PROC_FS
378
379 #include <linux/seq_file.h>
380
381 static void *frag_start(struct seq_file *m, loff_t *pos)
382 {
383         pg_data_t *pgdat;
384         loff_t node = *pos;
385         for (pgdat = first_online_pgdat();
386              pgdat && node;
387              pgdat = next_online_pgdat(pgdat))
388                 --node;
389
390         return pgdat;
391 }
392
393 static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
394 {
395         pg_data_t *pgdat = (pg_data_t *)arg;
396
397         (*pos)++;
398         return next_online_pgdat(pgdat);
399 }
400
401 static void frag_stop(struct seq_file *m, void *arg)
402 {
403 }
404
405 /*
406  * This walks the free areas for each zone.
407  */
408 static int frag_show(struct seq_file *m, void *arg)
409 {
410         pg_data_t *pgdat = (pg_data_t *)arg;
411         struct zone *zone;
412         struct zone *node_zones = pgdat->node_zones;
413         unsigned long flags;
414         int order;
415
416         for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
417                 if (!populated_zone(zone))
418                         continue;
419
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);
425                 seq_putc(m, '\n');
426         }
427         return 0;
428 }
429
430 struct seq_operations fragmentation_op = {
431         .start  = frag_start,
432         .next   = frag_next,
433         .stop   = frag_stop,
434         .show   = frag_show,
435 };
436
437 static char *vmstat_text[] = {
438         /* Zoned VM counters */
439         "nr_anon_pages",
440         "nr_mapped",
441         "nr_file_pages",
442         "nr_slab",
443         "nr_page_table_pages",
444         "nr_dirty",
445         "nr_writeback",
446         "nr_unstable",
447         "nr_bounce",
448
449 #ifdef CONFIG_NUMA
450         "numa_hit",
451         "numa_miss",
452         "numa_foreign",
453         "numa_interleave",
454         "numa_local",
455         "numa_other",
456 #endif
457
458 #ifdef CONFIG_VM_EVENT_COUNTERS
459         "pgpgin",
460         "pgpgout",
461         "pswpin",
462         "pswpout",
463
464         "pgalloc_dma",
465         "pgalloc_dma32",
466         "pgalloc_normal",
467         "pgalloc_high",
468
469         "pgfree",
470         "pgactivate",
471         "pgdeactivate",
472
473         "pgfault",
474         "pgmajfault",
475
476         "pgrefill_dma",
477         "pgrefill_dma32",
478         "pgrefill_normal",
479         "pgrefill_high",
480
481         "pgsteal_dma",
482         "pgsteal_dma32",
483         "pgsteal_normal",
484         "pgsteal_high",
485
486         "pgscan_kswapd_dma",
487         "pgscan_kswapd_dma32",
488         "pgscan_kswapd_normal",
489         "pgscan_kswapd_high",
490
491         "pgscan_direct_dma",
492         "pgscan_direct_dma32",
493         "pgscan_direct_normal",
494         "pgscan_direct_high",
495
496         "pginodesteal",
497         "slabs_scanned",
498         "kswapd_steal",
499         "kswapd_inodesteal",
500         "pageoutrun",
501         "allocstall",
502
503         "pgrotated",
504 #endif
505 };
506
507 /*
508  * Output information about zones in @pgdat.
509  */
510 static int zoneinfo_show(struct seq_file *m, void *arg)
511 {
512         pg_data_t *pgdat = arg;
513         struct zone *zone;
514         struct zone *node_zones = pgdat->node_zones;
515         unsigned long flags;
516
517         for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
518                 int i;
519
520                 if (!populated_zone(zone))
521                         continue;
522
523                 spin_lock_irqsave(&zone->lock, flags);
524                 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
525                 seq_printf(m,
526                            "\n  pages free     %lu"
527                            "\n        min      %lu"
528                            "\n        low      %lu"
529                            "\n        high     %lu"
530                            "\n        active   %lu"
531                            "\n        inactive %lu"
532                            "\n        scanned  %lu (a: %lu i: %lu)"
533                            "\n        spanned  %lu"
534                            "\n        present  %lu",
535                            zone->free_pages,
536                            zone->pages_min,
537                            zone->pages_low,
538                            zone->pages_high,
539                            zone->nr_active,
540                            zone->nr_inactive,
541                            zone->pages_scanned,
542                            zone->nr_scan_active, zone->nr_scan_inactive,
543                            zone->spanned_pages,
544                            zone->present_pages);
545
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));
549
550                 seq_printf(m,
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]);
555                 seq_printf(m,
556                            ")"
557                            "\n  pagesets");
558                 for_each_online_cpu(i) {
559                         struct per_cpu_pageset *pageset;
560                         int j;
561
562                         pageset = zone_pcp(zone, i);
563                         for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
564                                 if (pageset->pcp[j].count)
565                                         break;
566                         }
567                         if (j == ARRAY_SIZE(pageset->pcp))
568                                 continue;
569                         for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
570                                 seq_printf(m,
571                                            "\n    cpu: %i pcp: %i"
572                                            "\n              count: %i"
573                                            "\n              high:  %i"
574                                            "\n              batch: %i",
575                                            i, j,
576                                            pageset->pcp[j].count,
577                                            pageset->pcp[j].high,
578                                            pageset->pcp[j].batch);
579                         }
580 #ifdef CONFIG_SMP
581                         seq_printf(m, "\n  vm stats threshold: %d",
582                                         pageset->stat_threshold);
583 #endif
584                 }
585                 seq_printf(m,
586                            "\n  all_unreclaimable: %u"
587                            "\n  prev_priority:     %i"
588                            "\n  start_pfn:         %lu",
589                            zone->all_unreclaimable,
590                            zone->prev_priority,
591                            zone->zone_start_pfn);
592                 spin_unlock_irqrestore(&zone->lock, flags);
593                 seq_putc(m, '\n');
594         }
595         return 0;
596 }
597
598 struct seq_operations zoneinfo_op = {
599         .start  = frag_start, /* iterate over all zones. The same as in
600                                * fragmentation. */
601         .next   = frag_next,
602         .stop   = frag_stop,
603         .show   = zoneinfo_show,
604 };
605
606 static void *vmstat_start(struct seq_file *m, loff_t *pos)
607 {
608         unsigned long *v;
609 #ifdef CONFIG_VM_EVENT_COUNTERS
610         unsigned long *e;
611 #endif
612         int i;
613
614         if (*pos >= ARRAY_SIZE(vmstat_text))
615                 return NULL;
616
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);
620 #else
621         v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long),
622                         GFP_KERNEL);
623 #endif
624         m->private = v;
625         if (!v)
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;
631         all_vm_events(e);
632         e[PGPGIN] /= 2;         /* sectors -> kbytes */
633         e[PGPGOUT] /= 2;
634 #endif
635         return v + *pos;
636 }
637
638 static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
639 {
640         (*pos)++;
641         if (*pos >= ARRAY_SIZE(vmstat_text))
642                 return NULL;
643         return (unsigned long *)m->private + *pos;
644 }
645
646 static int vmstat_show(struct seq_file *m, void *arg)
647 {
648         unsigned long *l = arg;
649         unsigned long off = l - (unsigned long *)m->private;
650
651         seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
652         return 0;
653 }
654
655 static void vmstat_stop(struct seq_file *m, void *arg)
656 {
657         kfree(m->private);
658         m->private = NULL;
659 }
660
661 struct seq_operations vmstat_op = {
662         .start  = vmstat_start,
663         .next   = vmstat_next,
664         .stop   = vmstat_stop,
665         .show   = vmstat_show,
666 };
667
668 #endif /* CONFIG_PROC_FS */
669
670 #ifdef CONFIG_SMP
671 /*
672  * Use the cpu notifier to insure that the thresholds are recalculated
673  * when necessary.
674  */
675 static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
676                 unsigned long action,
677                 void *hcpu)
678 {
679         switch (action) {
680                 case CPU_UP_PREPARE:
681                 case CPU_UP_CANCELED:
682                 case CPU_DEAD:
683                         refresh_zone_stat_thresholds();
684                         break;
685                 default:
686                         break;
687         }
688         return NOTIFY_OK;
689 }
690
691 static struct notifier_block __cpuinitdata vmstat_notifier =
692         { &vmstat_cpuup_callback, NULL, 0 };
693
694 int __init setup_vmstat(void)
695 {
696         refresh_zone_stat_thresholds();
697         register_cpu_notifier(&vmstat_notifier);
698         return 0;
699 }
700 module_init(setup_vmstat)
701 #endif