]> bbs.cooldavid.org Git - net-next-2.6.git/blob - mm/vmstat.c
baa4ab387db773c03200ec67ef94ab82b472de5a
[net-next-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 #include <linux/fs.h>
12 #include <linux/mm.h>
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
22 #ifdef CONFIG_VM_EVENT_COUNTERS
23 DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
24 EXPORT_PER_CPU_SYMBOL(vm_event_states);
25
26 static void sum_vm_events(unsigned long *ret)
27 {
28         int cpu;
29         int i;
30
31         memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
32
33         for_each_online_cpu(cpu) {
34                 struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
35
36                 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
37                         ret[i] += this->event[i];
38         }
39 }
40
41 /*
42  * Accumulate the vm event counters across all CPUs.
43  * The result is unavoidably approximate - it can change
44  * during and after execution of this function.
45 */
46 void all_vm_events(unsigned long *ret)
47 {
48         get_online_cpus();
49         sum_vm_events(ret);
50         put_online_cpus();
51 }
52 EXPORT_SYMBOL_GPL(all_vm_events);
53
54 #ifdef CONFIG_HOTPLUG
55 /*
56  * Fold the foreign cpu events into our own.
57  *
58  * This is adding to the events on one processor
59  * but keeps the global counts constant.
60  */
61 void vm_events_fold_cpu(int cpu)
62 {
63         struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu);
64         int i;
65
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;
69         }
70 }
71 #endif /* CONFIG_HOTPLUG */
72
73 #endif /* CONFIG_VM_EVENT_COUNTERS */
74
75 /*
76  * Manage combined zone based / global counters
77  *
78  * vm_stat contains the global counters
79  */
80 atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
81 EXPORT_SYMBOL(vm_stat);
82
83 #ifdef CONFIG_SMP
84
85 static int calculate_threshold(struct zone *zone)
86 {
87         int threshold;
88         int mem;        /* memory in 128 MB units */
89
90         /*
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.
95          *
96          * Some sample thresholds:
97          *
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
118          */
119
120         mem = zone->present_pages >> (27 - PAGE_SHIFT);
121
122         threshold = 2 * fls(num_online_cpus()) * (1 + fls(mem));
123
124         /*
125          * Maximum threshold is 125
126          */
127         threshold = min(125, threshold);
128
129         return threshold;
130 }
131
132 /*
133  * Refresh the thresholds for each zone.
134  */
135 static void refresh_zone_stat_thresholds(void)
136 {
137         struct zone *zone;
138         int cpu;
139         int threshold;
140
141         for_each_populated_zone(zone) {
142                 unsigned long max_drift, tolerate_drift;
143
144                 threshold = calculate_threshold(zone);
145
146                 for_each_online_cpu(cpu)
147                         per_cpu_ptr(zone->pageset, cpu)->stat_threshold
148                                                         = threshold;
149
150                 /*
151                  * Only set percpu_drift_mark if there is a danger that
152                  * NR_FREE_PAGES reports the low watermark is ok when in fact
153                  * the min watermark could be breached by an allocation
154                  */
155                 tolerate_drift = low_wmark_pages(zone) - min_wmark_pages(zone);
156                 max_drift = num_online_cpus() * threshold;
157                 if (max_drift > tolerate_drift)
158                         zone->percpu_drift_mark = high_wmark_pages(zone) +
159                                         max_drift;
160         }
161 }
162
163 /*
164  * For use when we know that interrupts are disabled.
165  */
166 void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
167                                 int delta)
168 {
169         struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
170
171         s8 *p = pcp->vm_stat_diff + item;
172         long x;
173
174         x = delta + *p;
175
176         if (unlikely(x > pcp->stat_threshold || x < -pcp->stat_threshold)) {
177                 zone_page_state_add(x, zone, item);
178                 x = 0;
179         }
180         *p = x;
181 }
182 EXPORT_SYMBOL(__mod_zone_page_state);
183
184 /*
185  * For an unknown interrupt state
186  */
187 void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
188                                         int delta)
189 {
190         unsigned long flags;
191
192         local_irq_save(flags);
193         __mod_zone_page_state(zone, item, delta);
194         local_irq_restore(flags);
195 }
196 EXPORT_SYMBOL(mod_zone_page_state);
197
198 /*
199  * Optimized increment and decrement functions.
200  *
201  * These are only for a single page and therefore can take a struct page *
202  * argument instead of struct zone *. This allows the inclusion of the code
203  * generated for page_zone(page) into the optimized functions.
204  *
205  * No overflow check is necessary and therefore the differential can be
206  * incremented or decremented in place which may allow the compilers to
207  * generate better code.
208  * The increment or decrement is known and therefore one boundary check can
209  * be omitted.
210  *
211  * NOTE: These functions are very performance sensitive. Change only
212  * with care.
213  *
214  * Some processors have inc/dec instructions that are atomic vs an interrupt.
215  * However, the code must first determine the differential location in a zone
216  * based on the processor number and then inc/dec the counter. There is no
217  * guarantee without disabling preemption that the processor will not change
218  * in between and therefore the atomicity vs. interrupt cannot be exploited
219  * in a useful way here.
220  */
221 void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
222 {
223         struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
224         s8 *p = pcp->vm_stat_diff + item;
225
226         (*p)++;
227
228         if (unlikely(*p > pcp->stat_threshold)) {
229                 int overstep = pcp->stat_threshold / 2;
230
231                 zone_page_state_add(*p + overstep, zone, item);
232                 *p = -overstep;
233         }
234 }
235
236 void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
237 {
238         __inc_zone_state(page_zone(page), item);
239 }
240 EXPORT_SYMBOL(__inc_zone_page_state);
241
242 void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
243 {
244         struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
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 __dec_zone_page_state(struct page *page, enum zone_stat_item item)
258 {
259         __dec_zone_state(page_zone(page), item);
260 }
261 EXPORT_SYMBOL(__dec_zone_page_state);
262
263 void inc_zone_state(struct zone *zone, enum zone_stat_item item)
264 {
265         unsigned long flags;
266
267         local_irq_save(flags);
268         __inc_zone_state(zone, item);
269         local_irq_restore(flags);
270 }
271
272 void inc_zone_page_state(struct page *page, enum zone_stat_item item)
273 {
274         unsigned long flags;
275         struct zone *zone;
276
277         zone = page_zone(page);
278         local_irq_save(flags);
279         __inc_zone_state(zone, item);
280         local_irq_restore(flags);
281 }
282 EXPORT_SYMBOL(inc_zone_page_state);
283
284 void dec_zone_page_state(struct page *page, enum zone_stat_item item)
285 {
286         unsigned long flags;
287
288         local_irq_save(flags);
289         __dec_zone_page_state(page, item);
290         local_irq_restore(flags);
291 }
292 EXPORT_SYMBOL(dec_zone_page_state);
293
294 /*
295  * Update the zone counters for one cpu.
296  *
297  * The cpu specified must be either the current cpu or a processor that
298  * is not online. If it is the current cpu then the execution thread must
299  * be pinned to the current cpu.
300  *
301  * Note that refresh_cpu_vm_stats strives to only access
302  * node local memory. The per cpu pagesets on remote zones are placed
303  * in the memory local to the processor using that pageset. So the
304  * loop over all zones will access a series of cachelines local to
305  * the processor.
306  *
307  * The call to zone_page_state_add updates the cachelines with the
308  * statistics in the remote zone struct as well as the global cachelines
309  * with the global counters. These could cause remote node cache line
310  * bouncing and will have to be only done when necessary.
311  */
312 void refresh_cpu_vm_stats(int cpu)
313 {
314         struct zone *zone;
315         int i;
316         int global_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
317
318         for_each_populated_zone(zone) {
319                 struct per_cpu_pageset *p;
320
321                 p = per_cpu_ptr(zone->pageset, cpu);
322
323                 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
324                         if (p->vm_stat_diff[i]) {
325                                 unsigned long flags;
326                                 int v;
327
328                                 local_irq_save(flags);
329                                 v = p->vm_stat_diff[i];
330                                 p->vm_stat_diff[i] = 0;
331                                 local_irq_restore(flags);
332                                 atomic_long_add(v, &zone->vm_stat[i]);
333                                 global_diff[i] += v;
334 #ifdef CONFIG_NUMA
335                                 /* 3 seconds idle till flush */
336                                 p->expire = 3;
337 #endif
338                         }
339                 cond_resched();
340 #ifdef CONFIG_NUMA
341                 /*
342                  * Deal with draining the remote pageset of this
343                  * processor
344                  *
345                  * Check if there are pages remaining in this pageset
346                  * if not then there is nothing to expire.
347                  */
348                 if (!p->expire || !p->pcp.count)
349                         continue;
350
351                 /*
352                  * We never drain zones local to this processor.
353                  */
354                 if (zone_to_nid(zone) == numa_node_id()) {
355                         p->expire = 0;
356                         continue;
357                 }
358
359                 p->expire--;
360                 if (p->expire)
361                         continue;
362
363                 if (p->pcp.count)
364                         drain_zone_pages(zone, &p->pcp);
365 #endif
366         }
367
368         for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
369                 if (global_diff[i])
370                         atomic_long_add(global_diff[i], &vm_stat[i]);
371 }
372
373 #endif
374
375 #ifdef CONFIG_NUMA
376 /*
377  * zonelist = the list of zones passed to the allocator
378  * z        = the zone from which the allocation occurred.
379  *
380  * Must be called with interrupts disabled.
381  */
382 void zone_statistics(struct zone *preferred_zone, struct zone *z)
383 {
384         if (z->zone_pgdat == preferred_zone->zone_pgdat) {
385                 __inc_zone_state(z, NUMA_HIT);
386         } else {
387                 __inc_zone_state(z, NUMA_MISS);
388                 __inc_zone_state(preferred_zone, NUMA_FOREIGN);
389         }
390         if (z->node == numa_node_id())
391                 __inc_zone_state(z, NUMA_LOCAL);
392         else
393                 __inc_zone_state(z, NUMA_OTHER);
394 }
395 #endif
396
397 #ifdef CONFIG_COMPACTION
398 struct contig_page_info {
399         unsigned long free_pages;
400         unsigned long free_blocks_total;
401         unsigned long free_blocks_suitable;
402 };
403
404 /*
405  * Calculate the number of free pages in a zone, how many contiguous
406  * pages are free and how many are large enough to satisfy an allocation of
407  * the target size. Note that this function makes no attempt to estimate
408  * how many suitable free blocks there *might* be if MOVABLE pages were
409  * migrated. Calculating that is possible, but expensive and can be
410  * figured out from userspace
411  */
412 static void fill_contig_page_info(struct zone *zone,
413                                 unsigned int suitable_order,
414                                 struct contig_page_info *info)
415 {
416         unsigned int order;
417
418         info->free_pages = 0;
419         info->free_blocks_total = 0;
420         info->free_blocks_suitable = 0;
421
422         for (order = 0; order < MAX_ORDER; order++) {
423                 unsigned long blocks;
424
425                 /* Count number of free blocks */
426                 blocks = zone->free_area[order].nr_free;
427                 info->free_blocks_total += blocks;
428
429                 /* Count free base pages */
430                 info->free_pages += blocks << order;
431
432                 /* Count the suitable free blocks */
433                 if (order >= suitable_order)
434                         info->free_blocks_suitable += blocks <<
435                                                 (order - suitable_order);
436         }
437 }
438
439 /*
440  * A fragmentation index only makes sense if an allocation of a requested
441  * size would fail. If that is true, the fragmentation index indicates
442  * whether external fragmentation or a lack of memory was the problem.
443  * The value can be used to determine if page reclaim or compaction
444  * should be used
445  */
446 static int __fragmentation_index(unsigned int order, struct contig_page_info *info)
447 {
448         unsigned long requested = 1UL << order;
449
450         if (!info->free_blocks_total)
451                 return 0;
452
453         /* Fragmentation index only makes sense when a request would fail */
454         if (info->free_blocks_suitable)
455                 return -1000;
456
457         /*
458          * Index is between 0 and 1 so return within 3 decimal places
459          *
460          * 0 => allocation would fail due to lack of memory
461          * 1 => allocation would fail due to fragmentation
462          */
463         return 1000 - div_u64( (1000+(div_u64(info->free_pages * 1000ULL, requested))), info->free_blocks_total);
464 }
465
466 /* Same as __fragmentation index but allocs contig_page_info on stack */
467 int fragmentation_index(struct zone *zone, unsigned int order)
468 {
469         struct contig_page_info info;
470
471         fill_contig_page_info(zone, order, &info);
472         return __fragmentation_index(order, &info);
473 }
474 #endif
475
476 #if defined(CONFIG_PROC_FS) || defined(CONFIG_COMPACTION)
477 #include <linux/proc_fs.h>
478 #include <linux/seq_file.h>
479
480 static char * const migratetype_names[MIGRATE_TYPES] = {
481         "Unmovable",
482         "Reclaimable",
483         "Movable",
484         "Reserve",
485         "Isolate",
486 };
487
488 static void *frag_start(struct seq_file *m, loff_t *pos)
489 {
490         pg_data_t *pgdat;
491         loff_t node = *pos;
492         for (pgdat = first_online_pgdat();
493              pgdat && node;
494              pgdat = next_online_pgdat(pgdat))
495                 --node;
496
497         return pgdat;
498 }
499
500 static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
501 {
502         pg_data_t *pgdat = (pg_data_t *)arg;
503
504         (*pos)++;
505         return next_online_pgdat(pgdat);
506 }
507
508 static void frag_stop(struct seq_file *m, void *arg)
509 {
510 }
511
512 /* Walk all the zones in a node and print using a callback */
513 static void walk_zones_in_node(struct seq_file *m, pg_data_t *pgdat,
514                 void (*print)(struct seq_file *m, pg_data_t *, struct zone *))
515 {
516         struct zone *zone;
517         struct zone *node_zones = pgdat->node_zones;
518         unsigned long flags;
519
520         for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
521                 if (!populated_zone(zone))
522                         continue;
523
524                 spin_lock_irqsave(&zone->lock, flags);
525                 print(m, pgdat, zone);
526                 spin_unlock_irqrestore(&zone->lock, flags);
527         }
528 }
529 #endif
530
531 #ifdef CONFIG_PROC_FS
532 static void frag_show_print(struct seq_file *m, pg_data_t *pgdat,
533                                                 struct zone *zone)
534 {
535         int order;
536
537         seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
538         for (order = 0; order < MAX_ORDER; ++order)
539                 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
540         seq_putc(m, '\n');
541 }
542
543 /*
544  * This walks the free areas for each zone.
545  */
546 static int frag_show(struct seq_file *m, void *arg)
547 {
548         pg_data_t *pgdat = (pg_data_t *)arg;
549         walk_zones_in_node(m, pgdat, frag_show_print);
550         return 0;
551 }
552
553 static void pagetypeinfo_showfree_print(struct seq_file *m,
554                                         pg_data_t *pgdat, struct zone *zone)
555 {
556         int order, mtype;
557
558         for (mtype = 0; mtype < MIGRATE_TYPES; mtype++) {
559                 seq_printf(m, "Node %4d, zone %8s, type %12s ",
560                                         pgdat->node_id,
561                                         zone->name,
562                                         migratetype_names[mtype]);
563                 for (order = 0; order < MAX_ORDER; ++order) {
564                         unsigned long freecount = 0;
565                         struct free_area *area;
566                         struct list_head *curr;
567
568                         area = &(zone->free_area[order]);
569
570                         list_for_each(curr, &area->free_list[mtype])
571                                 freecount++;
572                         seq_printf(m, "%6lu ", freecount);
573                 }
574                 seq_putc(m, '\n');
575         }
576 }
577
578 /* Print out the free pages at each order for each migatetype */
579 static int pagetypeinfo_showfree(struct seq_file *m, void *arg)
580 {
581         int order;
582         pg_data_t *pgdat = (pg_data_t *)arg;
583
584         /* Print header */
585         seq_printf(m, "%-43s ", "Free pages count per migrate type at order");
586         for (order = 0; order < MAX_ORDER; ++order)
587                 seq_printf(m, "%6d ", order);
588         seq_putc(m, '\n');
589
590         walk_zones_in_node(m, pgdat, pagetypeinfo_showfree_print);
591
592         return 0;
593 }
594
595 static void pagetypeinfo_showblockcount_print(struct seq_file *m,
596                                         pg_data_t *pgdat, struct zone *zone)
597 {
598         int mtype;
599         unsigned long pfn;
600         unsigned long start_pfn = zone->zone_start_pfn;
601         unsigned long end_pfn = start_pfn + zone->spanned_pages;
602         unsigned long count[MIGRATE_TYPES] = { 0, };
603
604         for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
605                 struct page *page;
606
607                 if (!pfn_valid(pfn))
608                         continue;
609
610                 page = pfn_to_page(pfn);
611
612                 /* Watch for unexpected holes punched in the memmap */
613                 if (!memmap_valid_within(pfn, page, zone))
614                         continue;
615
616                 mtype = get_pageblock_migratetype(page);
617
618                 if (mtype < MIGRATE_TYPES)
619                         count[mtype]++;
620         }
621
622         /* Print counts */
623         seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
624         for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
625                 seq_printf(m, "%12lu ", count[mtype]);
626         seq_putc(m, '\n');
627 }
628
629 /* Print out the free pages at each order for each migratetype */
630 static int pagetypeinfo_showblockcount(struct seq_file *m, void *arg)
631 {
632         int mtype;
633         pg_data_t *pgdat = (pg_data_t *)arg;
634
635         seq_printf(m, "\n%-23s", "Number of blocks type ");
636         for (mtype = 0; mtype < MIGRATE_TYPES; mtype++)
637                 seq_printf(m, "%12s ", migratetype_names[mtype]);
638         seq_putc(m, '\n');
639         walk_zones_in_node(m, pgdat, pagetypeinfo_showblockcount_print);
640
641         return 0;
642 }
643
644 /*
645  * This prints out statistics in relation to grouping pages by mobility.
646  * It is expensive to collect so do not constantly read the file.
647  */
648 static int pagetypeinfo_show(struct seq_file *m, void *arg)
649 {
650         pg_data_t *pgdat = (pg_data_t *)arg;
651
652         /* check memoryless node */
653         if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
654                 return 0;
655
656         seq_printf(m, "Page block order: %d\n", pageblock_order);
657         seq_printf(m, "Pages per block:  %lu\n", pageblock_nr_pages);
658         seq_putc(m, '\n');
659         pagetypeinfo_showfree(m, pgdat);
660         pagetypeinfo_showblockcount(m, pgdat);
661
662         return 0;
663 }
664
665 static const struct seq_operations fragmentation_op = {
666         .start  = frag_start,
667         .next   = frag_next,
668         .stop   = frag_stop,
669         .show   = frag_show,
670 };
671
672 static int fragmentation_open(struct inode *inode, struct file *file)
673 {
674         return seq_open(file, &fragmentation_op);
675 }
676
677 static const struct file_operations fragmentation_file_operations = {
678         .open           = fragmentation_open,
679         .read           = seq_read,
680         .llseek         = seq_lseek,
681         .release        = seq_release,
682 };
683
684 static const struct seq_operations pagetypeinfo_op = {
685         .start  = frag_start,
686         .next   = frag_next,
687         .stop   = frag_stop,
688         .show   = pagetypeinfo_show,
689 };
690
691 static int pagetypeinfo_open(struct inode *inode, struct file *file)
692 {
693         return seq_open(file, &pagetypeinfo_op);
694 }
695
696 static const struct file_operations pagetypeinfo_file_ops = {
697         .open           = pagetypeinfo_open,
698         .read           = seq_read,
699         .llseek         = seq_lseek,
700         .release        = seq_release,
701 };
702
703 #ifdef CONFIG_ZONE_DMA
704 #define TEXT_FOR_DMA(xx) xx "_dma",
705 #else
706 #define TEXT_FOR_DMA(xx)
707 #endif
708
709 #ifdef CONFIG_ZONE_DMA32
710 #define TEXT_FOR_DMA32(xx) xx "_dma32",
711 #else
712 #define TEXT_FOR_DMA32(xx)
713 #endif
714
715 #ifdef CONFIG_HIGHMEM
716 #define TEXT_FOR_HIGHMEM(xx) xx "_high",
717 #else
718 #define TEXT_FOR_HIGHMEM(xx)
719 #endif
720
721 #define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
722                                         TEXT_FOR_HIGHMEM(xx) xx "_movable",
723
724 static const char * const vmstat_text[] = {
725         /* Zoned VM counters */
726         "nr_free_pages",
727         "nr_inactive_anon",
728         "nr_active_anon",
729         "nr_inactive_file",
730         "nr_active_file",
731         "nr_unevictable",
732         "nr_mlock",
733         "nr_anon_pages",
734         "nr_mapped",
735         "nr_file_pages",
736         "nr_dirty",
737         "nr_writeback",
738         "nr_slab_reclaimable",
739         "nr_slab_unreclaimable",
740         "nr_page_table_pages",
741         "nr_kernel_stack",
742         "nr_unstable",
743         "nr_bounce",
744         "nr_vmscan_write",
745         "nr_writeback_temp",
746         "nr_isolated_anon",
747         "nr_isolated_file",
748         "nr_shmem",
749         "nr_dirtied",
750         "nr_written",
751         "nr_dirty_threshold",
752         "nr_dirty_background_threshold",
753
754 #ifdef CONFIG_NUMA
755         "numa_hit",
756         "numa_miss",
757         "numa_foreign",
758         "numa_interleave",
759         "numa_local",
760         "numa_other",
761 #endif
762
763 #ifdef CONFIG_VM_EVENT_COUNTERS
764         "pgpgin",
765         "pgpgout",
766         "pswpin",
767         "pswpout",
768
769         TEXTS_FOR_ZONES("pgalloc")
770
771         "pgfree",
772         "pgactivate",
773         "pgdeactivate",
774
775         "pgfault",
776         "pgmajfault",
777
778         TEXTS_FOR_ZONES("pgrefill")
779         TEXTS_FOR_ZONES("pgsteal")
780         TEXTS_FOR_ZONES("pgscan_kswapd")
781         TEXTS_FOR_ZONES("pgscan_direct")
782
783 #ifdef CONFIG_NUMA
784         "zone_reclaim_failed",
785 #endif
786         "pginodesteal",
787         "slabs_scanned",
788         "kswapd_steal",
789         "kswapd_inodesteal",
790         "kswapd_low_wmark_hit_quickly",
791         "kswapd_high_wmark_hit_quickly",
792         "kswapd_skip_congestion_wait",
793         "pageoutrun",
794         "allocstall",
795
796         "pgrotated",
797
798 #ifdef CONFIG_COMPACTION
799         "compact_blocks_moved",
800         "compact_pages_moved",
801         "compact_pagemigrate_failed",
802         "compact_stall",
803         "compact_fail",
804         "compact_success",
805 #endif
806
807 #ifdef CONFIG_HUGETLB_PAGE
808         "htlb_buddy_alloc_success",
809         "htlb_buddy_alloc_fail",
810 #endif
811         "unevictable_pgs_culled",
812         "unevictable_pgs_scanned",
813         "unevictable_pgs_rescued",
814         "unevictable_pgs_mlocked",
815         "unevictable_pgs_munlocked",
816         "unevictable_pgs_cleared",
817         "unevictable_pgs_stranded",
818         "unevictable_pgs_mlockfreed",
819 #endif
820 };
821
822 static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
823                                                         struct zone *zone)
824 {
825         int i;
826         seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
827         seq_printf(m,
828                    "\n  pages free     %lu"
829                    "\n        min      %lu"
830                    "\n        low      %lu"
831                    "\n        high     %lu"
832                    "\n        scanned  %lu"
833                    "\n        spanned  %lu"
834                    "\n        present  %lu",
835                    zone_nr_free_pages(zone),
836                    min_wmark_pages(zone),
837                    low_wmark_pages(zone),
838                    high_wmark_pages(zone),
839                    zone->pages_scanned,
840                    zone->spanned_pages,
841                    zone->present_pages);
842
843         for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
844                 seq_printf(m, "\n    %-12s %lu", vmstat_text[i],
845                                 zone_page_state(zone, i));
846
847         seq_printf(m,
848                    "\n        protection: (%lu",
849                    zone->lowmem_reserve[0]);
850         for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
851                 seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
852         seq_printf(m,
853                    ")"
854                    "\n  pagesets");
855         for_each_online_cpu(i) {
856                 struct per_cpu_pageset *pageset;
857
858                 pageset = per_cpu_ptr(zone->pageset, i);
859                 seq_printf(m,
860                            "\n    cpu: %i"
861                            "\n              count: %i"
862                            "\n              high:  %i"
863                            "\n              batch: %i",
864                            i,
865                            pageset->pcp.count,
866                            pageset->pcp.high,
867                            pageset->pcp.batch);
868 #ifdef CONFIG_SMP
869                 seq_printf(m, "\n  vm stats threshold: %d",
870                                 pageset->stat_threshold);
871 #endif
872         }
873         seq_printf(m,
874                    "\n  all_unreclaimable: %u"
875                    "\n  start_pfn:         %lu"
876                    "\n  inactive_ratio:    %u",
877                    zone->all_unreclaimable,
878                    zone->zone_start_pfn,
879                    zone->inactive_ratio);
880         seq_putc(m, '\n');
881 }
882
883 /*
884  * Output information about zones in @pgdat.
885  */
886 static int zoneinfo_show(struct seq_file *m, void *arg)
887 {
888         pg_data_t *pgdat = (pg_data_t *)arg;
889         walk_zones_in_node(m, pgdat, zoneinfo_show_print);
890         return 0;
891 }
892
893 static const struct seq_operations zoneinfo_op = {
894         .start  = frag_start, /* iterate over all zones. The same as in
895                                * fragmentation. */
896         .next   = frag_next,
897         .stop   = frag_stop,
898         .show   = zoneinfo_show,
899 };
900
901 static int zoneinfo_open(struct inode *inode, struct file *file)
902 {
903         return seq_open(file, &zoneinfo_op);
904 }
905
906 static const struct file_operations proc_zoneinfo_file_operations = {
907         .open           = zoneinfo_open,
908         .read           = seq_read,
909         .llseek         = seq_lseek,
910         .release        = seq_release,
911 };
912
913 enum writeback_stat_item {
914         NR_DIRTY_THRESHOLD,
915         NR_DIRTY_BG_THRESHOLD,
916         NR_VM_WRITEBACK_STAT_ITEMS,
917 };
918
919 static void *vmstat_start(struct seq_file *m, loff_t *pos)
920 {
921         unsigned long *v;
922         int i, stat_items_size;
923
924         if (*pos >= ARRAY_SIZE(vmstat_text))
925                 return NULL;
926         stat_items_size = NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long) +
927                           NR_VM_WRITEBACK_STAT_ITEMS * sizeof(unsigned long);
928
929 #ifdef CONFIG_VM_EVENT_COUNTERS
930         stat_items_size += sizeof(struct vm_event_state);
931 #endif
932
933         v = kmalloc(stat_items_size, GFP_KERNEL);
934         m->private = v;
935         if (!v)
936                 return ERR_PTR(-ENOMEM);
937         for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
938                 v[i] = global_page_state(i);
939         v += NR_VM_ZONE_STAT_ITEMS;
940
941         global_dirty_limits(v + NR_DIRTY_BG_THRESHOLD,
942                             v + NR_DIRTY_THRESHOLD);
943         v += NR_VM_WRITEBACK_STAT_ITEMS;
944
945 #ifdef CONFIG_VM_EVENT_COUNTERS
946         all_vm_events(v);
947         v[PGPGIN] /= 2;         /* sectors -> kbytes */
948         v[PGPGOUT] /= 2;
949 #endif
950         return m->private + *pos;
951 }
952
953 static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
954 {
955         (*pos)++;
956         if (*pos >= ARRAY_SIZE(vmstat_text))
957                 return NULL;
958         return (unsigned long *)m->private + *pos;
959 }
960
961 static int vmstat_show(struct seq_file *m, void *arg)
962 {
963         unsigned long *l = arg;
964         unsigned long off = l - (unsigned long *)m->private;
965
966         seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
967         return 0;
968 }
969
970 static void vmstat_stop(struct seq_file *m, void *arg)
971 {
972         kfree(m->private);
973         m->private = NULL;
974 }
975
976 static const struct seq_operations vmstat_op = {
977         .start  = vmstat_start,
978         .next   = vmstat_next,
979         .stop   = vmstat_stop,
980         .show   = vmstat_show,
981 };
982
983 static int vmstat_open(struct inode *inode, struct file *file)
984 {
985         return seq_open(file, &vmstat_op);
986 }
987
988 static const struct file_operations proc_vmstat_file_operations = {
989         .open           = vmstat_open,
990         .read           = seq_read,
991         .llseek         = seq_lseek,
992         .release        = seq_release,
993 };
994 #endif /* CONFIG_PROC_FS */
995
996 #ifdef CONFIG_SMP
997 static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
998 int sysctl_stat_interval __read_mostly = HZ;
999
1000 static void vmstat_update(struct work_struct *w)
1001 {
1002         refresh_cpu_vm_stats(smp_processor_id());
1003         schedule_delayed_work(&__get_cpu_var(vmstat_work),
1004                 round_jiffies_relative(sysctl_stat_interval));
1005 }
1006
1007 static void __cpuinit start_cpu_timer(int cpu)
1008 {
1009         struct delayed_work *work = &per_cpu(vmstat_work, cpu);
1010
1011         INIT_DELAYED_WORK_DEFERRABLE(work, vmstat_update);
1012         schedule_delayed_work_on(cpu, work, __round_jiffies_relative(HZ, cpu));
1013 }
1014
1015 /*
1016  * Use the cpu notifier to insure that the thresholds are recalculated
1017  * when necessary.
1018  */
1019 static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
1020                 unsigned long action,
1021                 void *hcpu)
1022 {
1023         long cpu = (long)hcpu;
1024
1025         switch (action) {
1026         case CPU_ONLINE:
1027         case CPU_ONLINE_FROZEN:
1028                 refresh_zone_stat_thresholds();
1029                 start_cpu_timer(cpu);
1030                 node_set_state(cpu_to_node(cpu), N_CPU);
1031                 break;
1032         case CPU_DOWN_PREPARE:
1033         case CPU_DOWN_PREPARE_FROZEN:
1034                 cancel_rearming_delayed_work(&per_cpu(vmstat_work, cpu));
1035                 per_cpu(vmstat_work, cpu).work.func = NULL;
1036                 break;
1037         case CPU_DOWN_FAILED:
1038         case CPU_DOWN_FAILED_FROZEN:
1039                 start_cpu_timer(cpu);
1040                 break;
1041         case CPU_DEAD:
1042         case CPU_DEAD_FROZEN:
1043                 refresh_zone_stat_thresholds();
1044                 break;
1045         default:
1046                 break;
1047         }
1048         return NOTIFY_OK;
1049 }
1050
1051 static struct notifier_block __cpuinitdata vmstat_notifier =
1052         { &vmstat_cpuup_callback, NULL, 0 };
1053 #endif
1054
1055 static int __init setup_vmstat(void)
1056 {
1057 #ifdef CONFIG_SMP
1058         int cpu;
1059
1060         refresh_zone_stat_thresholds();
1061         register_cpu_notifier(&vmstat_notifier);
1062
1063         for_each_online_cpu(cpu)
1064                 start_cpu_timer(cpu);
1065 #endif
1066 #ifdef CONFIG_PROC_FS
1067         proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
1068         proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
1069         proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
1070         proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
1071 #endif
1072         return 0;
1073 }
1074 module_init(setup_vmstat)
1075
1076 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
1077 #include <linux/debugfs.h>
1078
1079 static struct dentry *extfrag_debug_root;
1080
1081 /*
1082  * Return an index indicating how much of the available free memory is
1083  * unusable for an allocation of the requested size.
1084  */
1085 static int unusable_free_index(unsigned int order,
1086                                 struct contig_page_info *info)
1087 {
1088         /* No free memory is interpreted as all free memory is unusable */
1089         if (info->free_pages == 0)
1090                 return 1000;
1091
1092         /*
1093          * Index should be a value between 0 and 1. Return a value to 3
1094          * decimal places.
1095          *
1096          * 0 => no fragmentation
1097          * 1 => high fragmentation
1098          */
1099         return div_u64((info->free_pages - (info->free_blocks_suitable << order)) * 1000ULL, info->free_pages);
1100
1101 }
1102
1103 static void unusable_show_print(struct seq_file *m,
1104                                         pg_data_t *pgdat, struct zone *zone)
1105 {
1106         unsigned int order;
1107         int index;
1108         struct contig_page_info info;
1109
1110         seq_printf(m, "Node %d, zone %8s ",
1111                                 pgdat->node_id,
1112                                 zone->name);
1113         for (order = 0; order < MAX_ORDER; ++order) {
1114                 fill_contig_page_info(zone, order, &info);
1115                 index = unusable_free_index(order, &info);
1116                 seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
1117         }
1118
1119         seq_putc(m, '\n');
1120 }
1121
1122 /*
1123  * Display unusable free space index
1124  *
1125  * The unusable free space index measures how much of the available free
1126  * memory cannot be used to satisfy an allocation of a given size and is a
1127  * value between 0 and 1. The higher the value, the more of free memory is
1128  * unusable and by implication, the worse the external fragmentation is. This
1129  * can be expressed as a percentage by multiplying by 100.
1130  */
1131 static int unusable_show(struct seq_file *m, void *arg)
1132 {
1133         pg_data_t *pgdat = (pg_data_t *)arg;
1134
1135         /* check memoryless node */
1136         if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
1137                 return 0;
1138
1139         walk_zones_in_node(m, pgdat, unusable_show_print);
1140
1141         return 0;
1142 }
1143
1144 static const struct seq_operations unusable_op = {
1145         .start  = frag_start,
1146         .next   = frag_next,
1147         .stop   = frag_stop,
1148         .show   = unusable_show,
1149 };
1150
1151 static int unusable_open(struct inode *inode, struct file *file)
1152 {
1153         return seq_open(file, &unusable_op);
1154 }
1155
1156 static const struct file_operations unusable_file_ops = {
1157         .open           = unusable_open,
1158         .read           = seq_read,
1159         .llseek         = seq_lseek,
1160         .release        = seq_release,
1161 };
1162
1163 static void extfrag_show_print(struct seq_file *m,
1164                                         pg_data_t *pgdat, struct zone *zone)
1165 {
1166         unsigned int order;
1167         int index;
1168
1169         /* Alloc on stack as interrupts are disabled for zone walk */
1170         struct contig_page_info info;
1171
1172         seq_printf(m, "Node %d, zone %8s ",
1173                                 pgdat->node_id,
1174                                 zone->name);
1175         for (order = 0; order < MAX_ORDER; ++order) {
1176                 fill_contig_page_info(zone, order, &info);
1177                 index = __fragmentation_index(order, &info);
1178                 seq_printf(m, "%d.%03d ", index / 1000, index % 1000);
1179         }
1180
1181         seq_putc(m, '\n');
1182 }
1183
1184 /*
1185  * Display fragmentation index for orders that allocations would fail for
1186  */
1187 static int extfrag_show(struct seq_file *m, void *arg)
1188 {
1189         pg_data_t *pgdat = (pg_data_t *)arg;
1190
1191         walk_zones_in_node(m, pgdat, extfrag_show_print);
1192
1193         return 0;
1194 }
1195
1196 static const struct seq_operations extfrag_op = {
1197         .start  = frag_start,
1198         .next   = frag_next,
1199         .stop   = frag_stop,
1200         .show   = extfrag_show,
1201 };
1202
1203 static int extfrag_open(struct inode *inode, struct file *file)
1204 {
1205         return seq_open(file, &extfrag_op);
1206 }
1207
1208 static const struct file_operations extfrag_file_ops = {
1209         .open           = extfrag_open,
1210         .read           = seq_read,
1211         .llseek         = seq_lseek,
1212         .release        = seq_release,
1213 };
1214
1215 static int __init extfrag_debug_init(void)
1216 {
1217         extfrag_debug_root = debugfs_create_dir("extfrag", NULL);
1218         if (!extfrag_debug_root)
1219                 return -ENOMEM;
1220
1221         if (!debugfs_create_file("unusable_index", 0444,
1222                         extfrag_debug_root, NULL, &unusable_file_ops))
1223                 return -ENOMEM;
1224
1225         if (!debugfs_create_file("extfrag_index", 0444,
1226                         extfrag_debug_root, NULL, &extfrag_file_ops))
1227                 return -ENOMEM;
1228
1229         return 0;
1230 }
1231
1232 module_init(extfrag_debug_init);
1233 #endif