mm: declare some external symbols
[linux-2.6-block.git] / mm / vmstat.c
CommitLineData
f6ac2354
CL
1/*
2 * linux/mm/vmstat.c
3 *
4 * Manages VM statistics
5 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
2244b95a
CL
6 *
7 * zoned VM statistics
8 * Copyright (C) 2006 Silicon Graphics, Inc.,
9 * Christoph Lameter <christoph@lameter.com>
f6ac2354 10 */
8f32f7e5 11#include <linux/fs.h>
f6ac2354 12#include <linux/mm.h>
4e950f6f 13#include <linux/err.h>
2244b95a 14#include <linux/module.h>
5a0e3ad6 15#include <linux/slab.h>
df9ecaba 16#include <linux/cpu.h>
c748e134 17#include <linux/vmstat.h>
e8edc6e0 18#include <linux/sched.h>
f1a5ab12 19#include <linux/math64.h>
79da826a 20#include <linux/writeback.h>
f6ac2354 21
f8891e5e
CL
22#ifdef CONFIG_VM_EVENT_COUNTERS
23DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
24EXPORT_PER_CPU_SYMBOL(vm_event_states);
25
31f961a8 26static void sum_vm_events(unsigned long *ret)
f8891e5e 27{
9eccf2a8 28 int cpu;
f8891e5e
CL
29 int i;
30
31 memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
32
31f961a8 33 for_each_online_cpu(cpu) {
f8891e5e
CL
34 struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
35
f8891e5e
CL
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*/
46void all_vm_events(unsigned long *ret)
47{
b5be1132 48 get_online_cpus();
31f961a8 49 sum_vm_events(ret);
b5be1132 50 put_online_cpus();
f8891e5e 51}
32dd66fc 52EXPORT_SYMBOL_GPL(all_vm_events);
f8891e5e
CL
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 */
61void 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
2244b95a
CL
75/*
76 * Manage combined zone based / global counters
77 *
78 * vm_stat contains the global counters
79 */
80atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
81EXPORT_SYMBOL(vm_stat);
82
83#ifdef CONFIG_SMP
84
df9ecaba
CL
85static 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}
2244b95a
CL
131
132/*
df9ecaba 133 * Refresh the thresholds for each zone.
2244b95a 134 */
df9ecaba 135static void refresh_zone_stat_thresholds(void)
2244b95a 136{
df9ecaba
CL
137 struct zone *zone;
138 int cpu;
139 int threshold;
140
ee99c71c 141 for_each_populated_zone(zone) {
aa454840
CL
142 unsigned long max_drift, tolerate_drift;
143
df9ecaba
CL
144 threshold = calculate_threshold(zone);
145
146 for_each_online_cpu(cpu)
99dcc3e5
CL
147 per_cpu_ptr(zone->pageset, cpu)->stat_threshold
148 = threshold;
aa454840
CL
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;
df9ecaba 160 }
2244b95a
CL
161}
162
163/*
164 * For use when we know that interrupts are disabled.
165 */
166void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
167 int delta)
168{
99dcc3e5
CL
169 struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
170
df9ecaba 171 s8 *p = pcp->vm_stat_diff + item;
2244b95a
CL
172 long x;
173
2244b95a
CL
174 x = delta + *p;
175
df9ecaba 176 if (unlikely(x > pcp->stat_threshold || x < -pcp->stat_threshold)) {
2244b95a
CL
177 zone_page_state_add(x, zone, item);
178 x = 0;
179 }
2244b95a
CL
180 *p = x;
181}
182EXPORT_SYMBOL(__mod_zone_page_state);
183
184/*
185 * For an unknown interrupt state
186 */
187void 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}
196EXPORT_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.
2244b95a
CL
208 * The increment or decrement is known and therefore one boundary check can
209 * be omitted.
210 *
df9ecaba
CL
211 * NOTE: These functions are very performance sensitive. Change only
212 * with care.
213 *
2244b95a
CL
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 */
c8785385 221void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
2244b95a 222{
99dcc3e5 223 struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
df9ecaba 224 s8 *p = pcp->vm_stat_diff + item;
2244b95a
CL
225
226 (*p)++;
227
df9ecaba
CL
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;
2244b95a
CL
233 }
234}
ca889e6c
CL
235
236void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
237{
238 __inc_zone_state(page_zone(page), item);
239}
2244b95a
CL
240EXPORT_SYMBOL(__inc_zone_page_state);
241
c8785385 242void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
2244b95a 243{
99dcc3e5 244 struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
df9ecaba 245 s8 *p = pcp->vm_stat_diff + item;
2244b95a
CL
246
247 (*p)--;
248
df9ecaba
CL
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;
2244b95a
CL
254 }
255}
c8785385
CL
256
257void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
258{
259 __dec_zone_state(page_zone(page), item);
260}
2244b95a
CL
261EXPORT_SYMBOL(__dec_zone_page_state);
262
ca889e6c
CL
263void 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
2244b95a
CL
272void inc_zone_page_state(struct page *page, enum zone_stat_item item)
273{
274 unsigned long flags;
275 struct zone *zone;
2244b95a
CL
276
277 zone = page_zone(page);
278 local_irq_save(flags);
ca889e6c 279 __inc_zone_state(zone, item);
2244b95a
CL
280 local_irq_restore(flags);
281}
282EXPORT_SYMBOL(inc_zone_page_state);
283
284void dec_zone_page_state(struct page *page, enum zone_stat_item item)
285{
286 unsigned long flags;
2244b95a 287
2244b95a 288 local_irq_save(flags);
a302eb4e 289 __dec_zone_page_state(page, item);
2244b95a
CL
290 local_irq_restore(flags);
291}
292EXPORT_SYMBOL(dec_zone_page_state);
293
294/*
295 * Update the zone counters for one cpu.
4037d452 296 *
a7f75e25
CL
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 *
4037d452
CL
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.
2244b95a
CL
311 */
312void refresh_cpu_vm_stats(int cpu)
313{
314 struct zone *zone;
315 int i;
a7f75e25 316 int global_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
2244b95a 317
ee99c71c 318 for_each_populated_zone(zone) {
4037d452 319 struct per_cpu_pageset *p;
2244b95a 320
99dcc3e5 321 p = per_cpu_ptr(zone->pageset, cpu);
2244b95a
CL
322
323 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
4037d452 324 if (p->vm_stat_diff[i]) {
a7f75e25
CL
325 unsigned long flags;
326 int v;
327
2244b95a 328 local_irq_save(flags);
a7f75e25 329 v = p->vm_stat_diff[i];
4037d452 330 p->vm_stat_diff[i] = 0;
a7f75e25
CL
331 local_irq_restore(flags);
332 atomic_long_add(v, &zone->vm_stat[i]);
333 global_diff[i] += v;
4037d452
CL
334#ifdef CONFIG_NUMA
335 /* 3 seconds idle till flush */
336 p->expire = 3;
337#endif
2244b95a 338 }
468fd62e 339 cond_resched();
4037d452
CL
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 */
3dfa5721 348 if (!p->expire || !p->pcp.count)
4037d452
CL
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
3dfa5721
CL
363 if (p->pcp.count)
364 drain_zone_pages(zone, &p->pcp);
4037d452 365#endif
2244b95a 366 }
a7f75e25
CL
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]);
2244b95a
CL
371}
372
2244b95a
CL
373#endif
374
ca889e6c
CL
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 */
18ea7e71 382void zone_statistics(struct zone *preferred_zone, struct zone *z)
ca889e6c 383{
18ea7e71 384 if (z->zone_pgdat == preferred_zone->zone_pgdat) {
ca889e6c
CL
385 __inc_zone_state(z, NUMA_HIT);
386 } else {
387 __inc_zone_state(z, NUMA_MISS);
18ea7e71 388 __inc_zone_state(preferred_zone, NUMA_FOREIGN);
ca889e6c 389 }
5d292343 390 if (z->node == numa_node_id())
ca889e6c
CL
391 __inc_zone_state(z, NUMA_LOCAL);
392 else
393 __inc_zone_state(z, NUMA_OTHER);
394}
395#endif
396
d7a5752c
MG
397#ifdef CONFIG_COMPACTION
398struct 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 */
412static 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}
f1a5ab12
MG
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 */
56de7263 446static int __fragmentation_index(unsigned int order, struct contig_page_info *info)
f1a5ab12
MG
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}
56de7263
MG
465
466/* Same as __fragmentation index but allocs contig_page_info on stack */
467int 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}
d7a5752c
MG
474#endif
475
476#if defined(CONFIG_PROC_FS) || defined(CONFIG_COMPACTION)
8f32f7e5 477#include <linux/proc_fs.h>
f6ac2354
CL
478#include <linux/seq_file.h>
479
467c996c
MG
480static char * const migratetype_names[MIGRATE_TYPES] = {
481 "Unmovable",
482 "Reclaimable",
483 "Movable",
484 "Reserve",
91446b06 485 "Isolate",
467c996c
MG
486};
487
f6ac2354
CL
488static 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
500static 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
508static void frag_stop(struct seq_file *m, void *arg)
509{
510}
511
467c996c
MG
512/* Walk all the zones in a node and print using a callback */
513static 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 *))
f6ac2354 515{
f6ac2354
CL
516 struct zone *zone;
517 struct zone *node_zones = pgdat->node_zones;
518 unsigned long flags;
f6ac2354
CL
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);
467c996c 525 print(m, pgdat, zone);
f6ac2354 526 spin_unlock_irqrestore(&zone->lock, flags);
467c996c
MG
527 }
528}
d7a5752c 529#endif
467c996c 530
d7a5752c 531#ifdef CONFIG_PROC_FS
467c996c
MG
532static 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 */
546static 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
553static 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 }
f6ac2354
CL
574 seq_putc(m, '\n');
575 }
467c996c
MG
576}
577
578/* Print out the free pages at each order for each migatetype */
579static 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
595static 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);
eb33575c
MG
611
612 /* Watch for unexpected holes punched in the memmap */
613 if (!memmap_valid_within(pfn, page, zone))
e80d6a24 614 continue;
eb33575c 615
467c996c
MG
616 mtype = get_pageblock_migratetype(page);
617
e80d6a24
MG
618 if (mtype < MIGRATE_TYPES)
619 count[mtype]++;
467c996c
MG
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 */
630static 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 */
648static int pagetypeinfo_show(struct seq_file *m, void *arg)
649{
650 pg_data_t *pgdat = (pg_data_t *)arg;
651
41b25a37
KM
652 /* check memoryless node */
653 if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
654 return 0;
655
467c996c
MG
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
f6ac2354
CL
662 return 0;
663}
664
8f32f7e5 665static const struct seq_operations fragmentation_op = {
f6ac2354
CL
666 .start = frag_start,
667 .next = frag_next,
668 .stop = frag_stop,
669 .show = frag_show,
670};
671
8f32f7e5
AD
672static int fragmentation_open(struct inode *inode, struct file *file)
673{
674 return seq_open(file, &fragmentation_op);
675}
676
677static 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
74e2e8e8 684static const struct seq_operations pagetypeinfo_op = {
467c996c
MG
685 .start = frag_start,
686 .next = frag_next,
687 .stop = frag_stop,
688 .show = pagetypeinfo_show,
689};
690
74e2e8e8
AD
691static int pagetypeinfo_open(struct inode *inode, struct file *file)
692{
693 return seq_open(file, &pagetypeinfo_op);
694}
695
696static 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
4b51d669
CL
703#ifdef CONFIG_ZONE_DMA
704#define TEXT_FOR_DMA(xx) xx "_dma",
705#else
706#define TEXT_FOR_DMA(xx)
707#endif
708
27bf71c2
CL
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
4b51d669 721#define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
2a1e274a 722 TEXT_FOR_HIGHMEM(xx) xx "_movable",
27bf71c2 723
15ad7cdc 724static const char * const vmstat_text[] = {
2244b95a 725 /* Zoned VM counters */
d23ad423 726 "nr_free_pages",
4f98a2fe
RR
727 "nr_inactive_anon",
728 "nr_active_anon",
729 "nr_inactive_file",
730 "nr_active_file",
7b854121 731 "nr_unevictable",
5344b7e6 732 "nr_mlock",
f3dbd344 733 "nr_anon_pages",
65ba55f5 734 "nr_mapped",
347ce434 735 "nr_file_pages",
51ed4491
CL
736 "nr_dirty",
737 "nr_writeback",
972d1a7b
CL
738 "nr_slab_reclaimable",
739 "nr_slab_unreclaimable",
df849a15 740 "nr_page_table_pages",
c6a7f572 741 "nr_kernel_stack",
f6ac2354 742 "nr_unstable",
d2c5e30c 743 "nr_bounce",
e129b5c2 744 "nr_vmscan_write",
fc3ba692 745 "nr_writeback_temp",
a731286d
KM
746 "nr_isolated_anon",
747 "nr_isolated_file",
4b02108a 748 "nr_shmem",
ea941f0e
MR
749 "nr_dirtied",
750 "nr_written",
79da826a
MR
751 "nr_dirty_threshold",
752 "nr_dirty_background_threshold",
ea941f0e 753
ca889e6c
CL
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
f8891e5e 763#ifdef CONFIG_VM_EVENT_COUNTERS
f6ac2354
CL
764 "pgpgin",
765 "pgpgout",
766 "pswpin",
767 "pswpout",
768
27bf71c2 769 TEXTS_FOR_ZONES("pgalloc")
f6ac2354
CL
770
771 "pgfree",
772 "pgactivate",
773 "pgdeactivate",
774
775 "pgfault",
776 "pgmajfault",
777
27bf71c2
CL
778 TEXTS_FOR_ZONES("pgrefill")
779 TEXTS_FOR_ZONES("pgsteal")
780 TEXTS_FOR_ZONES("pgscan_kswapd")
781 TEXTS_FOR_ZONES("pgscan_direct")
f6ac2354 782
24cf7251
MG
783#ifdef CONFIG_NUMA
784 "zone_reclaim_failed",
785#endif
f6ac2354
CL
786 "pginodesteal",
787 "slabs_scanned",
788 "kswapd_steal",
789 "kswapd_inodesteal",
bb3ab596
KM
790 "kswapd_low_wmark_hit_quickly",
791 "kswapd_high_wmark_hit_quickly",
792 "kswapd_skip_congestion_wait",
f6ac2354
CL
793 "pageoutrun",
794 "allocstall",
795
796 "pgrotated",
748446bb
MG
797
798#ifdef CONFIG_COMPACTION
799 "compact_blocks_moved",
800 "compact_pages_moved",
801 "compact_pagemigrate_failed",
56de7263
MG
802 "compact_stall",
803 "compact_fail",
804 "compact_success",
748446bb
MG
805#endif
806
3b116300
AL
807#ifdef CONFIG_HUGETLB_PAGE
808 "htlb_buddy_alloc_success",
809 "htlb_buddy_alloc_fail",
810#endif
bbfd28ee
LS
811 "unevictable_pgs_culled",
812 "unevictable_pgs_scanned",
813 "unevictable_pgs_rescued",
5344b7e6
NP
814 "unevictable_pgs_mlocked",
815 "unevictable_pgs_munlocked",
816 "unevictable_pgs_cleared",
817 "unevictable_pgs_stranded",
985737cf 818 "unevictable_pgs_mlockfreed",
bbfd28ee 819#endif
f6ac2354
CL
820};
821
467c996c
MG
822static void zoneinfo_show_print(struct seq_file *m, pg_data_t *pgdat,
823 struct zone *zone)
f6ac2354 824{
467c996c
MG
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"
08d9ae7c 832 "\n scanned %lu"
467c996c
MG
833 "\n spanned %lu"
834 "\n present %lu",
aa454840 835 zone_nr_free_pages(zone),
41858966
MG
836 min_wmark_pages(zone),
837 low_wmark_pages(zone),
838 high_wmark_pages(zone),
467c996c 839 zone->pages_scanned,
467c996c
MG
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;
467c996c 857
99dcc3e5 858 pageset = per_cpu_ptr(zone->pageset, i);
3dfa5721
CL
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);
df9ecaba 868#ifdef CONFIG_SMP
467c996c
MG
869 seq_printf(m, "\n vm stats threshold: %d",
870 pageset->stat_threshold);
df9ecaba 871#endif
f6ac2354 872 }
467c996c
MG
873 seq_printf(m,
874 "\n all_unreclaimable: %u"
556adecb
RR
875 "\n start_pfn: %lu"
876 "\n inactive_ratio: %u",
93e4a89a 877 zone->all_unreclaimable,
556adecb
RR
878 zone->zone_start_pfn,
879 zone->inactive_ratio);
467c996c
MG
880 seq_putc(m, '\n');
881}
882
883/*
884 * Output information about zones in @pgdat.
885 */
886static 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);
f6ac2354
CL
890 return 0;
891}
892
5c9fe628 893static const struct seq_operations zoneinfo_op = {
f6ac2354
CL
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
5c9fe628
AD
901static int zoneinfo_open(struct inode *inode, struct file *file)
902{
903 return seq_open(file, &zoneinfo_op);
904}
905
906static 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
79da826a
MR
913enum writeback_stat_item {
914 NR_DIRTY_THRESHOLD,
915 NR_DIRTY_BG_THRESHOLD,
916 NR_VM_WRITEBACK_STAT_ITEMS,
917};
918
f6ac2354
CL
919static void *vmstat_start(struct seq_file *m, loff_t *pos)
920{
2244b95a 921 unsigned long *v;
79da826a 922 int i, stat_items_size;
f6ac2354
CL
923
924 if (*pos >= ARRAY_SIZE(vmstat_text))
925 return NULL;
79da826a
MR
926 stat_items_size = NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long) +
927 NR_VM_WRITEBACK_STAT_ITEMS * sizeof(unsigned long);
f6ac2354 928
f8891e5e 929#ifdef CONFIG_VM_EVENT_COUNTERS
79da826a 930 stat_items_size += sizeof(struct vm_event_state);
f8891e5e 931#endif
79da826a
MR
932
933 v = kmalloc(stat_items_size, GFP_KERNEL);
2244b95a
CL
934 m->private = v;
935 if (!v)
f6ac2354 936 return ERR_PTR(-ENOMEM);
2244b95a
CL
937 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
938 v[i] = global_page_state(i);
79da826a
MR
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
f8891e5e 945#ifdef CONFIG_VM_EVENT_COUNTERS
79da826a
MR
946 all_vm_events(v);
947 v[PGPGIN] /= 2; /* sectors -> kbytes */
948 v[PGPGOUT] /= 2;
f8891e5e 949#endif
79da826a 950 return m->private + *pos;
f6ac2354
CL
951}
952
953static 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
961static 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
970static void vmstat_stop(struct seq_file *m, void *arg)
971{
972 kfree(m->private);
973 m->private = NULL;
974}
975
b6aa44ab 976static const struct seq_operations vmstat_op = {
f6ac2354
CL
977 .start = vmstat_start,
978 .next = vmstat_next,
979 .stop = vmstat_stop,
980 .show = vmstat_show,
981};
982
b6aa44ab
AD
983static int vmstat_open(struct inode *inode, struct file *file)
984{
985 return seq_open(file, &vmstat_op);
986}
987
988static 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};
f6ac2354
CL
994#endif /* CONFIG_PROC_FS */
995
df9ecaba 996#ifdef CONFIG_SMP
d1187ed2 997static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
77461ab3 998int sysctl_stat_interval __read_mostly = HZ;
d1187ed2
CL
999
1000static void vmstat_update(struct work_struct *w)
1001{
1002 refresh_cpu_vm_stats(smp_processor_id());
77461ab3 1003 schedule_delayed_work(&__get_cpu_var(vmstat_work),
98f4ebb2 1004 round_jiffies_relative(sysctl_stat_interval));
d1187ed2
CL
1005}
1006
42614fcd 1007static void __cpuinit start_cpu_timer(int cpu)
d1187ed2 1008{
1871e52c 1009 struct delayed_work *work = &per_cpu(vmstat_work, cpu);
d1187ed2 1010
1871e52c
TH
1011 INIT_DELAYED_WORK_DEFERRABLE(work, vmstat_update);
1012 schedule_delayed_work_on(cpu, work, __round_jiffies_relative(HZ, cpu));
d1187ed2
CL
1013}
1014
df9ecaba
CL
1015/*
1016 * Use the cpu notifier to insure that the thresholds are recalculated
1017 * when necessary.
1018 */
1019static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
1020 unsigned long action,
1021 void *hcpu)
1022{
d1187ed2
CL
1023 long cpu = (long)hcpu;
1024
df9ecaba 1025 switch (action) {
d1187ed2
CL
1026 case CPU_ONLINE:
1027 case CPU_ONLINE_FROZEN:
5ee28a44 1028 refresh_zone_stat_thresholds();
d1187ed2 1029 start_cpu_timer(cpu);
ad596925 1030 node_set_state(cpu_to_node(cpu), N_CPU);
d1187ed2
CL
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;
ce421c79 1041 case CPU_DEAD:
8bb78442 1042 case CPU_DEAD_FROZEN:
ce421c79
AW
1043 refresh_zone_stat_thresholds();
1044 break;
1045 default:
1046 break;
df9ecaba
CL
1047 }
1048 return NOTIFY_OK;
1049}
1050
1051static struct notifier_block __cpuinitdata vmstat_notifier =
1052 { &vmstat_cpuup_callback, NULL, 0 };
8f32f7e5 1053#endif
df9ecaba 1054
e2fc88d0 1055static int __init setup_vmstat(void)
df9ecaba 1056{
8f32f7e5 1057#ifdef CONFIG_SMP
d1187ed2
CL
1058 int cpu;
1059
df9ecaba
CL
1060 refresh_zone_stat_thresholds();
1061 register_cpu_notifier(&vmstat_notifier);
d1187ed2
CL
1062
1063 for_each_online_cpu(cpu)
1064 start_cpu_timer(cpu);
8f32f7e5
AD
1065#endif
1066#ifdef CONFIG_PROC_FS
1067 proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
74e2e8e8 1068 proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
b6aa44ab 1069 proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
5c9fe628 1070 proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
8f32f7e5 1071#endif
df9ecaba
CL
1072 return 0;
1073}
1074module_init(setup_vmstat)
d7a5752c
MG
1075
1076#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
1077#include <linux/debugfs.h>
1078
1079static 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 */
1085static 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
1103static 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 */
1131static 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
1144static const struct seq_operations unusable_op = {
1145 .start = frag_start,
1146 .next = frag_next,
1147 .stop = frag_stop,
1148 .show = unusable_show,
1149};
1150
1151static int unusable_open(struct inode *inode, struct file *file)
1152{
1153 return seq_open(file, &unusable_op);
1154}
1155
1156static 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
f1a5ab12
MG
1163static 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);
56de7263 1177 index = __fragmentation_index(order, &info);
f1a5ab12
MG
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 */
1187static 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
1196static const struct seq_operations extfrag_op = {
1197 .start = frag_start,
1198 .next = frag_next,
1199 .stop = frag_stop,
1200 .show = extfrag_show,
1201};
1202
1203static int extfrag_open(struct inode *inode, struct file *file)
1204{
1205 return seq_open(file, &extfrag_op);
1206}
1207
1208static 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
d7a5752c
MG
1215static 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
f1a5ab12
MG
1225 if (!debugfs_create_file("extfrag_index", 0444,
1226 extfrag_debug_root, NULL, &extfrag_file_ops))
1227 return -ENOMEM;
1228
d7a5752c
MG
1229 return 0;
1230}
1231
1232module_init(extfrag_debug_init);
1233#endif