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