blob: baa4ab387db773c03200ec67ef94ab82b472de5a [file] [log] [blame]
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001/*
2 * linux/mm/vmstat.c
3 *
4 * Manages VM statistics
5 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
Christoph Lameter2244b952006-06-30 01:55:33 -07006 *
7 * zoned VM statistics
8 * Copyright (C) 2006 Silicon Graphics, Inc.,
9 * Christoph Lameter <christoph@lameter.com>
Christoph Lameterf6ac2352006-06-30 01:55:32 -070010 */
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +040011#include <linux/fs.h>
Christoph Lameterf6ac2352006-06-30 01:55:32 -070012#include <linux/mm.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040013#include <linux/err.h>
Christoph Lameter2244b952006-06-30 01:55:33 -070014#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Christoph Lameterdf9ecab2006-08-31 21:27:35 -070016#include <linux/cpu.h>
Adrian Bunkc748e132008-07-23 21:27:03 -070017#include <linux/vmstat.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040018#include <linux/sched.h>
Mel Gormanf1a5ab12010-05-24 14:32:26 -070019#include <linux/math64.h>
Michael Rubin79da8262010-10-26 14:21:36 -070020#include <linux/writeback.h>
Christoph Lameterf6ac2352006-06-30 01:55:32 -070021
Christoph Lameterf8891e52006-06-30 01:55:45 -070022#ifdef CONFIG_VM_EVENT_COUNTERS
23DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
24EXPORT_PER_CPU_SYMBOL(vm_event_states);
25
Minchan Kim31f961a2010-08-09 17:18:59 -070026static void sum_vm_events(unsigned long *ret)
Christoph Lameterf8891e52006-06-30 01:55:45 -070027{
Christoph Lameter9eccf2a2008-02-04 22:29:22 -080028 int cpu;
Christoph Lameterf8891e52006-06-30 01:55:45 -070029 int i;
30
31 memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
32
Minchan Kim31f961a2010-08-09 17:18:59 -070033 for_each_online_cpu(cpu) {
Christoph Lameterf8891e52006-06-30 01:55:45 -070034 struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
35
Christoph Lameterf8891e52006-06-30 01:55:45 -070036 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{
KOSAKI Motohirob5be1132008-05-12 14:02:06 -070048 get_online_cpus();
Minchan Kim31f961a2010-08-09 17:18:59 -070049 sum_vm_events(ret);
KOSAKI Motohirob5be1132008-05-12 14:02:06 -070050 put_online_cpus();
Christoph Lameterf8891e52006-06-30 01:55:45 -070051}
Heiko Carstens32dd66f2006-07-10 04:44:31 -070052EXPORT_SYMBOL_GPL(all_vm_events);
Christoph Lameterf8891e52006-06-30 01:55:45 -070053
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
Christoph Lameter2244b952006-06-30 01:55:33 -070075/*
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
Christoph Lameterdf9ecab2006-08-31 21:27:35 -070085static 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}
Christoph Lameter2244b952006-06-30 01:55:33 -0700131
132/*
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700133 * Refresh the thresholds for each zone.
Christoph Lameter2244b952006-06-30 01:55:33 -0700134 */
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700135static void refresh_zone_stat_thresholds(void)
Christoph Lameter2244b952006-06-30 01:55:33 -0700136{
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700137 struct zone *zone;
138 int cpu;
139 int threshold;
140
KOSAKI Motohiroee99c712009-03-31 15:19:31 -0700141 for_each_populated_zone(zone) {
Christoph Lameteraa454842010-09-09 16:38:17 -0700142 unsigned long max_drift, tolerate_drift;
143
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700144 threshold = calculate_threshold(zone);
145
146 for_each_online_cpu(cpu)
Christoph Lameter99dcc3e2010-01-05 15:34:51 +0900147 per_cpu_ptr(zone->pageset, cpu)->stat_threshold
148 = threshold;
Christoph Lameteraa454842010-09-09 16:38:17 -0700149
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;
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700160 }
Christoph Lameter2244b952006-06-30 01:55:33 -0700161}
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{
Christoph Lameter99dcc3e2010-01-05 15:34:51 +0900169 struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
170
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700171 s8 *p = pcp->vm_stat_diff + item;
Christoph Lameter2244b952006-06-30 01:55:33 -0700172 long x;
173
Christoph Lameter2244b952006-06-30 01:55:33 -0700174 x = delta + *p;
175
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700176 if (unlikely(x > pcp->stat_threshold || x < -pcp->stat_threshold)) {
Christoph Lameter2244b952006-06-30 01:55:33 -0700177 zone_page_state_add(x, zone, item);
178 x = 0;
179 }
Christoph Lameter2244b952006-06-30 01:55:33 -0700180 *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.
Christoph Lameter2244b952006-06-30 01:55:33 -0700208 * The increment or decrement is known and therefore one boundary check can
209 * be omitted.
210 *
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700211 * NOTE: These functions are very performance sensitive. Change only
212 * with care.
213 *
Christoph Lameter2244b952006-06-30 01:55:33 -0700214 * 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 */
Christoph Lameterc8785382007-02-10 01:43:01 -0800221void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
Christoph Lameter2244b952006-06-30 01:55:33 -0700222{
Christoph Lameter99dcc3e2010-01-05 15:34:51 +0900223 struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700224 s8 *p = pcp->vm_stat_diff + item;
Christoph Lameter2244b952006-06-30 01:55:33 -0700225
226 (*p)++;
227
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700228 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;
Christoph Lameter2244b952006-06-30 01:55:33 -0700233 }
234}
Christoph Lameterca889e62006-06-30 01:55:44 -0700235
236void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
237{
238 __inc_zone_state(page_zone(page), item);
239}
Christoph Lameter2244b952006-06-30 01:55:33 -0700240EXPORT_SYMBOL(__inc_zone_page_state);
241
Christoph Lameterc8785382007-02-10 01:43:01 -0800242void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
Christoph Lameter2244b952006-06-30 01:55:33 -0700243{
Christoph Lameter99dcc3e2010-01-05 15:34:51 +0900244 struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700245 s8 *p = pcp->vm_stat_diff + item;
Christoph Lameter2244b952006-06-30 01:55:33 -0700246
247 (*p)--;
248
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700249 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;
Christoph Lameter2244b952006-06-30 01:55:33 -0700254 }
255}
Christoph Lameterc8785382007-02-10 01:43:01 -0800256
257void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
258{
259 __dec_zone_state(page_zone(page), item);
260}
Christoph Lameter2244b952006-06-30 01:55:33 -0700261EXPORT_SYMBOL(__dec_zone_page_state);
262
Christoph Lameterca889e62006-06-30 01:55:44 -0700263void 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
Christoph Lameter2244b952006-06-30 01:55:33 -0700272void inc_zone_page_state(struct page *page, enum zone_stat_item item)
273{
274 unsigned long flags;
275 struct zone *zone;
Christoph Lameter2244b952006-06-30 01:55:33 -0700276
277 zone = page_zone(page);
278 local_irq_save(flags);
Christoph Lameterca889e62006-06-30 01:55:44 -0700279 __inc_zone_state(zone, item);
Christoph Lameter2244b952006-06-30 01:55:33 -0700280 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;
Christoph Lameter2244b952006-06-30 01:55:33 -0700287
Christoph Lameter2244b952006-06-30 01:55:33 -0700288 local_irq_save(flags);
Christoph Lametera302eb42006-08-31 21:27:34 -0700289 __dec_zone_page_state(page, item);
Christoph Lameter2244b952006-06-30 01:55:33 -0700290 local_irq_restore(flags);
291}
292EXPORT_SYMBOL(dec_zone_page_state);
293
294/*
295 * Update the zone counters for one cpu.
Christoph Lameter4037d452007-05-09 02:35:14 -0700296 *
Christoph Lametera7f75e22008-02-04 22:29:16 -0800297 * 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 *
Christoph Lameter4037d452007-05-09 02:35:14 -0700301 * 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.
Christoph Lameter2244b952006-06-30 01:55:33 -0700311 */
312void refresh_cpu_vm_stats(int cpu)
313{
314 struct zone *zone;
315 int i;
Christoph Lametera7f75e22008-02-04 22:29:16 -0800316 int global_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
Christoph Lameter2244b952006-06-30 01:55:33 -0700317
KOSAKI Motohiroee99c712009-03-31 15:19:31 -0700318 for_each_populated_zone(zone) {
Christoph Lameter4037d452007-05-09 02:35:14 -0700319 struct per_cpu_pageset *p;
Christoph Lameter2244b952006-06-30 01:55:33 -0700320
Christoph Lameter99dcc3e2010-01-05 15:34:51 +0900321 p = per_cpu_ptr(zone->pageset, cpu);
Christoph Lameter2244b952006-06-30 01:55:33 -0700322
323 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
Christoph Lameter4037d452007-05-09 02:35:14 -0700324 if (p->vm_stat_diff[i]) {
Christoph Lametera7f75e22008-02-04 22:29:16 -0800325 unsigned long flags;
326 int v;
327
Christoph Lameter2244b952006-06-30 01:55:33 -0700328 local_irq_save(flags);
Christoph Lametera7f75e22008-02-04 22:29:16 -0800329 v = p->vm_stat_diff[i];
Christoph Lameter4037d452007-05-09 02:35:14 -0700330 p->vm_stat_diff[i] = 0;
Christoph Lametera7f75e22008-02-04 22:29:16 -0800331 local_irq_restore(flags);
332 atomic_long_add(v, &zone->vm_stat[i]);
333 global_diff[i] += v;
Christoph Lameter4037d452007-05-09 02:35:14 -0700334#ifdef CONFIG_NUMA
335 /* 3 seconds idle till flush */
336 p->expire = 3;
337#endif
Christoph Lameter2244b952006-06-30 01:55:33 -0700338 }
Dimitri Sivanich468fd622008-04-28 02:13:37 -0700339 cond_resched();
Christoph Lameter4037d452007-05-09 02:35:14 -0700340#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 */
Christoph Lameter3dfa5722008-02-04 22:29:19 -0800348 if (!p->expire || !p->pcp.count)
Christoph Lameter4037d452007-05-09 02:35:14 -0700349 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
Christoph Lameter3dfa5722008-02-04 22:29:19 -0800363 if (p->pcp.count)
364 drain_zone_pages(zone, &p->pcp);
Christoph Lameter4037d452007-05-09 02:35:14 -0700365#endif
Christoph Lameter2244b952006-06-30 01:55:33 -0700366 }
Christoph Lametera7f75e22008-02-04 22:29:16 -0800367
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]);
Christoph Lameter2244b952006-06-30 01:55:33 -0700371}
372
Christoph Lameter2244b952006-06-30 01:55:33 -0700373#endif
374
Christoph Lameterca889e62006-06-30 01:55:44 -0700375#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 */
Mel Gorman18ea7e72008-04-28 02:12:14 -0700382void zone_statistics(struct zone *preferred_zone, struct zone *z)
Christoph Lameterca889e62006-06-30 01:55:44 -0700383{
Mel Gorman18ea7e72008-04-28 02:12:14 -0700384 if (z->zone_pgdat == preferred_zone->zone_pgdat) {
Christoph Lameterca889e62006-06-30 01:55:44 -0700385 __inc_zone_state(z, NUMA_HIT);
386 } else {
387 __inc_zone_state(z, NUMA_MISS);
Mel Gorman18ea7e72008-04-28 02:12:14 -0700388 __inc_zone_state(preferred_zone, NUMA_FOREIGN);
Christoph Lameterca889e62006-06-30 01:55:44 -0700389 }
Christoph Lameter5d292342006-09-27 01:50:10 -0700390 if (z->node == numa_node_id())
Christoph Lameterca889e62006-06-30 01:55:44 -0700391 __inc_zone_state(z, NUMA_LOCAL);
392 else
393 __inc_zone_state(z, NUMA_OTHER);
394}
395#endif
396
Mel Gormand7a57522010-05-24 14:32:25 -0700397#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}
Mel Gormanf1a5ab12010-05-24 14:32:26 -0700438
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 */
Mel Gorman56de7262010-05-24 14:32:30 -0700446static int __fragmentation_index(unsigned int order, struct contig_page_info *info)
Mel Gormanf1a5ab12010-05-24 14:32:26 -0700447{
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}
Mel Gorman56de7262010-05-24 14:32:30 -0700465
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}
Mel Gormand7a57522010-05-24 14:32:25 -0700474#endif
475
476#if defined(CONFIG_PROC_FS) || defined(CONFIG_COMPACTION)
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +0400477#include <linux/proc_fs.h>
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700478#include <linux/seq_file.h>
479
Mel Gorman467c9962007-10-16 01:26:02 -0700480static char * const migratetype_names[MIGRATE_TYPES] = {
481 "Unmovable",
482 "Reclaimable",
483 "Movable",
484 "Reserve",
KOSAKI Motohiro91446b02008-04-15 14:34:42 -0700485 "Isolate",
Mel Gorman467c9962007-10-16 01:26:02 -0700486};
487
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700488static 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
Mel Gorman467c9962007-10-16 01:26:02 -0700512/* 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 *))
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700515{
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700516 struct zone *zone;
517 struct zone *node_zones = pgdat->node_zones;
518 unsigned long flags;
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700519
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);
Mel Gorman467c9962007-10-16 01:26:02 -0700525 print(m, pgdat, zone);
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700526 spin_unlock_irqrestore(&zone->lock, flags);
Mel Gorman467c9962007-10-16 01:26:02 -0700527 }
528}
Mel Gormand7a57522010-05-24 14:32:25 -0700529#endif
Mel Gorman467c9962007-10-16 01:26:02 -0700530
Mel Gormand7a57522010-05-24 14:32:25 -0700531#ifdef CONFIG_PROC_FS
Mel Gorman467c9962007-10-16 01:26:02 -0700532static 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 }
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700574 seq_putc(m, '\n');
575 }
Mel Gorman467c9962007-10-16 01:26:02 -0700576}
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);
Mel Gormaneb335752009-05-13 17:34:48 +0100611
612 /* Watch for unexpected holes punched in the memmap */
613 if (!memmap_valid_within(pfn, page, zone))
Mel Gormane80d6a22008-08-14 11:10:14 +0100614 continue;
Mel Gormaneb335752009-05-13 17:34:48 +0100615
Mel Gorman467c9962007-10-16 01:26:02 -0700616 mtype = get_pageblock_migratetype(page);
617
Mel Gormane80d6a22008-08-14 11:10:14 +0100618 if (mtype < MIGRATE_TYPES)
619 count[mtype]++;
Mel Gorman467c9962007-10-16 01:26:02 -0700620 }
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
KOSAKI Motohiro41b25a32008-04-30 00:52:13 -0700652 /* check memoryless node */
653 if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
654 return 0;
655
Mel Gorman467c9962007-10-16 01:26:02 -0700656 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
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700662 return 0;
663}
664
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +0400665static const struct seq_operations fragmentation_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700666 .start = frag_start,
667 .next = frag_next,
668 .stop = frag_stop,
669 .show = frag_show,
670};
671
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +0400672static 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
Alexey Dobriyan74e2e8e2008-10-06 04:15:36 +0400684static const struct seq_operations pagetypeinfo_op = {
Mel Gorman467c9962007-10-16 01:26:02 -0700685 .start = frag_start,
686 .next = frag_next,
687 .stop = frag_stop,
688 .show = pagetypeinfo_show,
689};
690
Alexey Dobriyan74e2e8e2008-10-06 04:15:36 +0400691static 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
Christoph Lameter4b51d662007-02-10 01:43:10 -0800703#ifdef CONFIG_ZONE_DMA
704#define TEXT_FOR_DMA(xx) xx "_dma",
705#else
706#define TEXT_FOR_DMA(xx)
707#endif
708
Christoph Lameter27bf71c2006-09-25 23:31:15 -0700709#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
Christoph Lameter4b51d662007-02-10 01:43:10 -0800721#define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
Mel Gorman2a1e2742007-07-17 04:03:12 -0700722 TEXT_FOR_HIGHMEM(xx) xx "_movable",
Christoph Lameter27bf71c2006-09-25 23:31:15 -0700723
Helge Deller15ad7cd2006-12-06 20:40:36 -0800724static const char * const vmstat_text[] = {
Christoph Lameter2244b952006-06-30 01:55:33 -0700725 /* Zoned VM counters */
Christoph Lameterd23ad422007-02-10 01:43:02 -0800726 "nr_free_pages",
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700727 "nr_inactive_anon",
728 "nr_active_anon",
729 "nr_inactive_file",
730 "nr_active_file",
Lee Schermerhorn7b854122008-10-18 20:26:40 -0700731 "nr_unevictable",
Nick Piggin5344b7e2008-10-18 20:26:51 -0700732 "nr_mlock",
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700733 "nr_anon_pages",
Christoph Lameter65ba55f2006-06-30 01:55:34 -0700734 "nr_mapped",
Christoph Lameter347ce432006-06-30 01:55:35 -0700735 "nr_file_pages",
Christoph Lameter51ed4492007-02-10 01:43:02 -0800736 "nr_dirty",
737 "nr_writeback",
Christoph Lameter972d1a72006-09-25 23:31:51 -0700738 "nr_slab_reclaimable",
739 "nr_slab_unreclaimable",
Christoph Lameterdf849a12006-06-30 01:55:38 -0700740 "nr_page_table_pages",
KOSAKI Motohiroc6a7f572009-09-21 17:01:32 -0700741 "nr_kernel_stack",
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700742 "nr_unstable",
Christoph Lameterd2c5e302006-06-30 01:55:41 -0700743 "nr_bounce",
Andrew Mortone129b5c2006-09-27 01:50:00 -0700744 "nr_vmscan_write",
Miklos Szeredifc3ba692008-04-30 00:54:38 -0700745 "nr_writeback_temp",
KOSAKI Motohiroa7312862009-09-21 17:01:37 -0700746 "nr_isolated_anon",
747 "nr_isolated_file",
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700748 "nr_shmem",
Michael Rubinea941f02010-10-26 14:21:35 -0700749 "nr_dirtied",
750 "nr_written",
Michael Rubin79da8262010-10-26 14:21:36 -0700751 "nr_dirty_threshold",
752 "nr_dirty_background_threshold",
Michael Rubinea941f02010-10-26 14:21:35 -0700753
Christoph Lameterca889e62006-06-30 01:55:44 -0700754#ifdef CONFIG_NUMA
755 "numa_hit",
756 "numa_miss",
757 "numa_foreign",
758 "numa_interleave",
759 "numa_local",
760 "numa_other",
761#endif
762
Christoph Lameterf8891e52006-06-30 01:55:45 -0700763#ifdef CONFIG_VM_EVENT_COUNTERS
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700764 "pgpgin",
765 "pgpgout",
766 "pswpin",
767 "pswpout",
768
Christoph Lameter27bf71c2006-09-25 23:31:15 -0700769 TEXTS_FOR_ZONES("pgalloc")
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700770
771 "pgfree",
772 "pgactivate",
773 "pgdeactivate",
774
775 "pgfault",
776 "pgmajfault",
777
Christoph Lameter27bf71c2006-09-25 23:31:15 -0700778 TEXTS_FOR_ZONES("pgrefill")
779 TEXTS_FOR_ZONES("pgsteal")
780 TEXTS_FOR_ZONES("pgscan_kswapd")
781 TEXTS_FOR_ZONES("pgscan_direct")
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700782
Mel Gorman24cf725182009-06-16 15:33:23 -0700783#ifdef CONFIG_NUMA
784 "zone_reclaim_failed",
785#endif
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700786 "pginodesteal",
787 "slabs_scanned",
788 "kswapd_steal",
789 "kswapd_inodesteal",
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -0800790 "kswapd_low_wmark_hit_quickly",
791 "kswapd_high_wmark_hit_quickly",
792 "kswapd_skip_congestion_wait",
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700793 "pageoutrun",
794 "allocstall",
795
796 "pgrotated",
Mel Gorman748446b2010-05-24 14:32:27 -0700797
798#ifdef CONFIG_COMPACTION
799 "compact_blocks_moved",
800 "compact_pages_moved",
801 "compact_pagemigrate_failed",
Mel Gorman56de7262010-05-24 14:32:30 -0700802 "compact_stall",
803 "compact_fail",
804 "compact_success",
Mel Gorman748446b2010-05-24 14:32:27 -0700805#endif
806
Adam Litke3b116302008-04-28 02:13:06 -0700807#ifdef CONFIG_HUGETLB_PAGE
808 "htlb_buddy_alloc_success",
809 "htlb_buddy_alloc_fail",
810#endif
Lee Schermerhornbbfd28e2008-10-18 20:26:40 -0700811 "unevictable_pgs_culled",
812 "unevictable_pgs_scanned",
813 "unevictable_pgs_rescued",
Nick Piggin5344b7e2008-10-18 20:26:51 -0700814 "unevictable_pgs_mlocked",
815 "unevictable_pgs_munlocked",
816 "unevictable_pgs_cleared",
817 "unevictable_pgs_stranded",
Lee Schermerhorn985737c2008-10-18 20:26:53 -0700818 "unevictable_pgs_mlockfreed",
Lee Schermerhornbbfd28e2008-10-18 20:26:40 -0700819#endif
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700820};
821
Mel Gorman467c9962007-10-16 01:26:02 -0700822static 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"
Wu Fengguang08d9ae72009-06-16 15:32:30 -0700832 "\n scanned %lu"
Mel Gorman467c9962007-10-16 01:26:02 -0700833 "\n spanned %lu"
834 "\n present %lu",
Christoph Lameteraa454842010-09-09 16:38:17 -0700835 zone_nr_free_pages(zone),
Mel Gorman41858962009-06-16 15:32:12 -0700836 min_wmark_pages(zone),
837 low_wmark_pages(zone),
838 high_wmark_pages(zone),
Mel Gorman467c9962007-10-16 01:26:02 -0700839 zone->pages_scanned,
Mel Gorman467c9962007-10-16 01:26:02 -0700840 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;
Mel Gorman467c9962007-10-16 01:26:02 -0700857
Christoph Lameter99dcc3e2010-01-05 15:34:51 +0900858 pageset = per_cpu_ptr(zone->pageset, i);
Christoph Lameter3dfa5722008-02-04 22:29:19 -0800859 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);
Mel Gorman467c9962007-10-16 01:26:02 -0700868#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"
Rik van Riel556adec2008-10-18 20:26:34 -0700875 "\n start_pfn: %lu"
876 "\n inactive_ratio: %u",
KOSAKI Motohiro93e4a892010-03-05 13:41:55 -0800877 zone->all_unreclaimable,
Rik van Riel556adec2008-10-18 20:26:34 -0700878 zone->zone_start_pfn,
879 zone->inactive_ratio);
Mel Gorman467c9962007-10-16 01:26:02 -0700880 seq_putc(m, '\n');
881}
882
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700883/*
884 * Output information about zones in @pgdat.
885 */
886static int zoneinfo_show(struct seq_file *m, void *arg)
887{
Mel Gorman467c9962007-10-16 01:26:02 -0700888 pg_data_t *pgdat = (pg_data_t *)arg;
889 walk_zones_in_node(m, pgdat, zoneinfo_show_print);
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700890 return 0;
891}
892
Alexey Dobriyan5c9fe622008-10-06 04:19:42 +0400893static const struct seq_operations zoneinfo_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700894 .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
Alexey Dobriyan5c9fe622008-10-06 04:19:42 +0400901static 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
Michael Rubin79da8262010-10-26 14:21:36 -0700913enum writeback_stat_item {
914 NR_DIRTY_THRESHOLD,
915 NR_DIRTY_BG_THRESHOLD,
916 NR_VM_WRITEBACK_STAT_ITEMS,
917};
918
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700919static void *vmstat_start(struct seq_file *m, loff_t *pos)
920{
Christoph Lameter2244b952006-06-30 01:55:33 -0700921 unsigned long *v;
Michael Rubin79da8262010-10-26 14:21:36 -0700922 int i, stat_items_size;
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700923
924 if (*pos >= ARRAY_SIZE(vmstat_text))
925 return NULL;
Michael Rubin79da8262010-10-26 14:21:36 -0700926 stat_items_size = NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long) +
927 NR_VM_WRITEBACK_STAT_ITEMS * sizeof(unsigned long);
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700928
Christoph Lameterf8891e52006-06-30 01:55:45 -0700929#ifdef CONFIG_VM_EVENT_COUNTERS
Michael Rubin79da8262010-10-26 14:21:36 -0700930 stat_items_size += sizeof(struct vm_event_state);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700931#endif
Michael Rubin79da8262010-10-26 14:21:36 -0700932
933 v = kmalloc(stat_items_size, GFP_KERNEL);
Christoph Lameter2244b952006-06-30 01:55:33 -0700934 m->private = v;
935 if (!v)
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700936 return ERR_PTR(-ENOMEM);
Christoph Lameter2244b952006-06-30 01:55:33 -0700937 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
938 v[i] = global_page_state(i);
Michael Rubin79da8262010-10-26 14:21:36 -0700939 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
Christoph Lameterf8891e52006-06-30 01:55:45 -0700945#ifdef CONFIG_VM_EVENT_COUNTERS
Michael Rubin79da8262010-10-26 14:21:36 -0700946 all_vm_events(v);
947 v[PGPGIN] /= 2; /* sectors -> kbytes */
948 v[PGPGOUT] /= 2;
Christoph Lameterf8891e52006-06-30 01:55:45 -0700949#endif
Michael Rubin79da8262010-10-26 14:21:36 -0700950 return m->private + *pos;
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700951}
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
Alexey Dobriyanb6aa44a2008-10-06 04:17:48 +0400976static const struct seq_operations vmstat_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700977 .start = vmstat_start,
978 .next = vmstat_next,
979 .stop = vmstat_stop,
980 .show = vmstat_show,
981};
982
Alexey Dobriyanb6aa44a2008-10-06 04:17:48 +0400983static 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};
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700994#endif /* CONFIG_PROC_FS */
995
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700996#ifdef CONFIG_SMP
Christoph Lameterd1187ed2007-05-09 02:35:12 -0700997static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
Christoph Lameter77461ab2007-05-09 02:35:13 -0700998int sysctl_stat_interval __read_mostly = HZ;
Christoph Lameterd1187ed2007-05-09 02:35:12 -0700999
1000static void vmstat_update(struct work_struct *w)
1001{
1002 refresh_cpu_vm_stats(smp_processor_id());
Christoph Lameter77461ab2007-05-09 02:35:13 -07001003 schedule_delayed_work(&__get_cpu_var(vmstat_work),
Anton Blanchard98f4ebb2009-04-02 16:56:39 -07001004 round_jiffies_relative(sysctl_stat_interval));
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001005}
1006
Randy Dunlap42614fc2007-11-14 17:00:12 -08001007static void __cpuinit start_cpu_timer(int cpu)
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001008{
Tejun Heo1871e522009-10-29 22:34:13 +09001009 struct delayed_work *work = &per_cpu(vmstat_work, cpu);
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001010
Tejun Heo1871e522009-10-29 22:34:13 +09001011 INIT_DELAYED_WORK_DEFERRABLE(work, vmstat_update);
1012 schedule_delayed_work_on(cpu, work, __round_jiffies_relative(HZ, cpu));
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001013}
1014
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001015/*
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{
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001023 long cpu = (long)hcpu;
1024
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001025 switch (action) {
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001026 case CPU_ONLINE:
1027 case CPU_ONLINE_FROZEN:
KAMEZAWA Hiroyuki5ee28a42010-09-09 16:38:14 -07001028 refresh_zone_stat_thresholds();
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001029 start_cpu_timer(cpu);
Christoph Lameterad596922010-01-05 15:34:51 +09001030 node_set_state(cpu_to_node(cpu), N_CPU);
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001031 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;
Andy Whitcroftce421c72006-12-06 20:33:08 -08001041 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001042 case CPU_DEAD_FROZEN:
Andy Whitcroftce421c72006-12-06 20:33:08 -08001043 refresh_zone_stat_thresholds();
1044 break;
1045 default:
1046 break;
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001047 }
1048 return NOTIFY_OK;
1049}
1050
1051static struct notifier_block __cpuinitdata vmstat_notifier =
1052 { &vmstat_cpuup_callback, NULL, 0 };
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001053#endif
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001054
Adrian Bunke2fc88d2007-10-16 01:26:27 -07001055static int __init setup_vmstat(void)
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001056{
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001057#ifdef CONFIG_SMP
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001058 int cpu;
1059
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001060 refresh_zone_stat_thresholds();
1061 register_cpu_notifier(&vmstat_notifier);
Christoph Lameterd1187ed2007-05-09 02:35:12 -07001062
1063 for_each_online_cpu(cpu)
1064 start_cpu_timer(cpu);
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001065#endif
1066#ifdef CONFIG_PROC_FS
1067 proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
Alexey Dobriyan74e2e8e2008-10-06 04:15:36 +04001068 proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
Alexey Dobriyanb6aa44a2008-10-06 04:17:48 +04001069 proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
Alexey Dobriyan5c9fe622008-10-06 04:19:42 +04001070 proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +04001071#endif
Christoph Lameterdf9ecab2006-08-31 21:27:35 -07001072 return 0;
1073}
1074module_init(setup_vmstat)
Mel Gormand7a57522010-05-24 14:32:25 -07001075
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
Mel Gormanf1a5ab12010-05-24 14:32:26 -07001163static 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);
Mel Gorman56de7262010-05-24 14:32:30 -07001177 index = __fragmentation_index(order, &info);
Mel Gormanf1a5ab12010-05-24 14:32:26 -07001178 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
Mel Gormand7a57522010-05-24 14:32:25 -07001215static 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
Mel Gormanf1a5ab12010-05-24 14:32:26 -07001225 if (!debugfs_create_file("extfrag_index", 0444,
1226 extfrag_debug_root, NULL, &extfrag_file_ops))
1227 return -ENOMEM;
1228
Mel Gormand7a57522010-05-24 14:32:25 -07001229 return 0;
1230}
1231
1232module_init(extfrag_debug_init);
1233#endif