blob: d3e0fa169f05bbedc7a0cd222a4e55f3fac1cfca [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>
Christoph Lameterf6ac2352006-06-30 01:55:32 -070019
Christoph Lameterf8891e52006-06-30 01:55:45 -070020#ifdef CONFIG_VM_EVENT_COUNTERS
21DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
22EXPORT_PER_CPU_SYMBOL(vm_event_states);
23
Rusty Russell174596a2009-01-01 10:12:29 +103024static void sum_vm_events(unsigned long *ret, const struct cpumask *cpumask)
Christoph Lameterf8891e52006-06-30 01:55:45 -070025{
Christoph Lameter9eccf2a2008-02-04 22:29:22 -080026 int cpu;
Christoph Lameterf8891e52006-06-30 01:55:45 -070027 int i;
28
29 memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
30
Rusty Russellaa85ea52009-03-30 22:05:15 -060031 for_each_cpu(cpu, cpumask) {
Christoph Lameterf8891e52006-06-30 01:55:45 -070032 struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
33
Christoph Lameterf8891e52006-06-30 01:55:45 -070034 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*/
44void all_vm_events(unsigned long *ret)
45{
KOSAKI Motohirob5be1132008-05-12 14:02:06 -070046 get_online_cpus();
Rusty Russell174596a2009-01-01 10:12:29 +103047 sum_vm_events(ret, cpu_online_mask);
KOSAKI Motohirob5be1132008-05-12 14:02:06 -070048 put_online_cpus();
Christoph Lameterf8891e52006-06-30 01:55:45 -070049}
Heiko Carstens32dd66f2006-07-10 04:44:31 -070050EXPORT_SYMBOL_GPL(all_vm_events);
Christoph Lameterf8891e52006-06-30 01:55:45 -070051
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 */
59void 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
Christoph Lameter2244b952006-06-30 01:55:33 -070073/*
74 * Manage combined zone based / global counters
75 *
76 * vm_stat contains the global counters
77 */
78atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
79EXPORT_SYMBOL(vm_stat);
80
81#ifdef CONFIG_SMP
82
Christoph Lameterdf9ecab2006-08-31 21:27:35 -070083static 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}
Christoph Lameter2244b952006-06-30 01:55:33 -0700129
130/*
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700131 * Refresh the thresholds for each zone.
Christoph Lameter2244b952006-06-30 01:55:33 -0700132 */
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700133static void refresh_zone_stat_thresholds(void)
Christoph Lameter2244b952006-06-30 01:55:33 -0700134{
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700135 struct zone *zone;
136 int cpu;
137 int threshold;
138
KOSAKI Motohiroee99c712009-03-31 15:19:31 -0700139 for_each_populated_zone(zone) {
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700140 threshold = calculate_threshold(zone);
141
142 for_each_online_cpu(cpu)
Christoph Lameter99dcc3e2010-01-05 15:34:51 +0900143 per_cpu_ptr(zone->pageset, cpu)->stat_threshold
144 = threshold;
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700145 }
Christoph Lameter2244b952006-06-30 01:55:33 -0700146}
147
148/*
149 * For use when we know that interrupts are disabled.
150 */
151void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
152 int delta)
153{
Christoph Lameter99dcc3e2010-01-05 15:34:51 +0900154 struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
155
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700156 s8 *p = pcp->vm_stat_diff + item;
Christoph Lameter2244b952006-06-30 01:55:33 -0700157 long x;
158
Christoph Lameter2244b952006-06-30 01:55:33 -0700159 x = delta + *p;
160
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700161 if (unlikely(x > pcp->stat_threshold || x < -pcp->stat_threshold)) {
Christoph Lameter2244b952006-06-30 01:55:33 -0700162 zone_page_state_add(x, zone, item);
163 x = 0;
164 }
Christoph Lameter2244b952006-06-30 01:55:33 -0700165 *p = x;
166}
167EXPORT_SYMBOL(__mod_zone_page_state);
168
169/*
170 * For an unknown interrupt state
171 */
172void 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}
181EXPORT_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.
Christoph Lameter2244b952006-06-30 01:55:33 -0700193 * The increment or decrement is known and therefore one boundary check can
194 * be omitted.
195 *
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700196 * NOTE: These functions are very performance sensitive. Change only
197 * with care.
198 *
Christoph Lameter2244b952006-06-30 01:55:33 -0700199 * 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 */
Christoph Lameterc8785382007-02-10 01:43:01 -0800206void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
Christoph Lameter2244b952006-06-30 01:55:33 -0700207{
Christoph Lameter99dcc3e2010-01-05 15:34:51 +0900208 struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700209 s8 *p = pcp->vm_stat_diff + item;
Christoph Lameter2244b952006-06-30 01:55:33 -0700210
211 (*p)++;
212
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700213 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;
Christoph Lameter2244b952006-06-30 01:55:33 -0700218 }
219}
Christoph Lameterca889e62006-06-30 01:55:44 -0700220
221void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
222{
223 __inc_zone_state(page_zone(page), item);
224}
Christoph Lameter2244b952006-06-30 01:55:33 -0700225EXPORT_SYMBOL(__inc_zone_page_state);
226
Christoph Lameterc8785382007-02-10 01:43:01 -0800227void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
Christoph Lameter2244b952006-06-30 01:55:33 -0700228{
Christoph Lameter99dcc3e2010-01-05 15:34:51 +0900229 struct per_cpu_pageset *pcp = this_cpu_ptr(zone->pageset);
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700230 s8 *p = pcp->vm_stat_diff + item;
Christoph Lameter2244b952006-06-30 01:55:33 -0700231
232 (*p)--;
233
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700234 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;
Christoph Lameter2244b952006-06-30 01:55:33 -0700239 }
240}
Christoph Lameterc8785382007-02-10 01:43:01 -0800241
242void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
243{
244 __dec_zone_state(page_zone(page), item);
245}
Christoph Lameter2244b952006-06-30 01:55:33 -0700246EXPORT_SYMBOL(__dec_zone_page_state);
247
Christoph Lameterca889e62006-06-30 01:55:44 -0700248void 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
Christoph Lameter2244b952006-06-30 01:55:33 -0700257void inc_zone_page_state(struct page *page, enum zone_stat_item item)
258{
259 unsigned long flags;
260 struct zone *zone;
Christoph Lameter2244b952006-06-30 01:55:33 -0700261
262 zone = page_zone(page);
263 local_irq_save(flags);
Christoph Lameterca889e62006-06-30 01:55:44 -0700264 __inc_zone_state(zone, item);
Christoph Lameter2244b952006-06-30 01:55:33 -0700265 local_irq_restore(flags);
266}
267EXPORT_SYMBOL(inc_zone_page_state);
268
269void dec_zone_page_state(struct page *page, enum zone_stat_item item)
270{
271 unsigned long flags;
Christoph Lameter2244b952006-06-30 01:55:33 -0700272
Christoph Lameter2244b952006-06-30 01:55:33 -0700273 local_irq_save(flags);
Christoph Lametera302eb42006-08-31 21:27:34 -0700274 __dec_zone_page_state(page, item);
Christoph Lameter2244b952006-06-30 01:55:33 -0700275 local_irq_restore(flags);
276}
277EXPORT_SYMBOL(dec_zone_page_state);
278
279/*
280 * Update the zone counters for one cpu.
Christoph Lameter4037d452007-05-09 02:35:14 -0700281 *
Christoph Lametera7f75e22008-02-04 22:29:16 -0800282 * 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 *
Christoph Lameter4037d452007-05-09 02:35:14 -0700286 * 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.
Christoph Lameter2244b952006-06-30 01:55:33 -0700296 */
297void refresh_cpu_vm_stats(int cpu)
298{
299 struct zone *zone;
300 int i;
Christoph Lametera7f75e22008-02-04 22:29:16 -0800301 int global_diff[NR_VM_ZONE_STAT_ITEMS] = { 0, };
Christoph Lameter2244b952006-06-30 01:55:33 -0700302
KOSAKI Motohiroee99c712009-03-31 15:19:31 -0700303 for_each_populated_zone(zone) {
Christoph Lameter4037d452007-05-09 02:35:14 -0700304 struct per_cpu_pageset *p;
Christoph Lameter2244b952006-06-30 01:55:33 -0700305
Christoph Lameter99dcc3e2010-01-05 15:34:51 +0900306 p = per_cpu_ptr(zone->pageset, cpu);
Christoph Lameter2244b952006-06-30 01:55:33 -0700307
308 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
Christoph Lameter4037d452007-05-09 02:35:14 -0700309 if (p->vm_stat_diff[i]) {
Christoph Lametera7f75e22008-02-04 22:29:16 -0800310 unsigned long flags;
311 int v;
312
Christoph Lameter2244b952006-06-30 01:55:33 -0700313 local_irq_save(flags);
Christoph Lametera7f75e22008-02-04 22:29:16 -0800314 v = p->vm_stat_diff[i];
Christoph Lameter4037d452007-05-09 02:35:14 -0700315 p->vm_stat_diff[i] = 0;
Christoph Lametera7f75e22008-02-04 22:29:16 -0800316 local_irq_restore(flags);
317 atomic_long_add(v, &zone->vm_stat[i]);
318 global_diff[i] += v;
Christoph Lameter4037d452007-05-09 02:35:14 -0700319#ifdef CONFIG_NUMA
320 /* 3 seconds idle till flush */
321 p->expire = 3;
322#endif
Christoph Lameter2244b952006-06-30 01:55:33 -0700323 }
Dimitri Sivanich468fd622008-04-28 02:13:37 -0700324 cond_resched();
Christoph Lameter4037d452007-05-09 02:35:14 -0700325#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 */
Christoph Lameter3dfa5722008-02-04 22:29:19 -0800333 if (!p->expire || !p->pcp.count)
Christoph Lameter4037d452007-05-09 02:35:14 -0700334 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
Christoph Lameter3dfa5722008-02-04 22:29:19 -0800348 if (p->pcp.count)
349 drain_zone_pages(zone, &p->pcp);
Christoph Lameter4037d452007-05-09 02:35:14 -0700350#endif
Christoph Lameter2244b952006-06-30 01:55:33 -0700351 }
Christoph Lametera7f75e22008-02-04 22:29:16 -0800352
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]);
Christoph Lameter2244b952006-06-30 01:55:33 -0700356}
357
Christoph Lameter2244b952006-06-30 01:55:33 -0700358#endif
359
Christoph Lameterca889e62006-06-30 01:55:44 -0700360#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 */
Mel Gorman18ea7e72008-04-28 02:12:14 -0700367void zone_statistics(struct zone *preferred_zone, struct zone *z)
Christoph Lameterca889e62006-06-30 01:55:44 -0700368{
Mel Gorman18ea7e72008-04-28 02:12:14 -0700369 if (z->zone_pgdat == preferred_zone->zone_pgdat) {
Christoph Lameterca889e62006-06-30 01:55:44 -0700370 __inc_zone_state(z, NUMA_HIT);
371 } else {
372 __inc_zone_state(z, NUMA_MISS);
Mel Gorman18ea7e72008-04-28 02:12:14 -0700373 __inc_zone_state(preferred_zone, NUMA_FOREIGN);
Christoph Lameterca889e62006-06-30 01:55:44 -0700374 }
Christoph Lameter5d292342006-09-27 01:50:10 -0700375 if (z->node == numa_node_id())
Christoph Lameterca889e62006-06-30 01:55:44 -0700376 __inc_zone_state(z, NUMA_LOCAL);
377 else
378 __inc_zone_state(z, NUMA_OTHER);
379}
380#endif
381
Mel Gormand7a57522010-05-24 14:32:25 -0700382#ifdef CONFIG_COMPACTION
383struct 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 */
397static 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)
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +0400426#include <linux/proc_fs.h>
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700427#include <linux/seq_file.h>
428
Mel Gorman467c9962007-10-16 01:26:02 -0700429static char * const migratetype_names[MIGRATE_TYPES] = {
430 "Unmovable",
431 "Reclaimable",
432 "Movable",
433 "Reserve",
KOSAKI Motohiro91446b02008-04-15 14:34:42 -0700434 "Isolate",
Mel Gorman467c9962007-10-16 01:26:02 -0700435};
436
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700437static 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
449static 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
457static void frag_stop(struct seq_file *m, void *arg)
458{
459}
460
Mel Gorman467c9962007-10-16 01:26:02 -0700461/* Walk all the zones in a node and print using a callback */
462static 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 *))
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700464{
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700465 struct zone *zone;
466 struct zone *node_zones = pgdat->node_zones;
467 unsigned long flags;
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700468
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);
Mel Gorman467c9962007-10-16 01:26:02 -0700474 print(m, pgdat, zone);
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700475 spin_unlock_irqrestore(&zone->lock, flags);
Mel Gorman467c9962007-10-16 01:26:02 -0700476 }
477}
Mel Gormand7a57522010-05-24 14:32:25 -0700478#endif
Mel Gorman467c9962007-10-16 01:26:02 -0700479
Mel Gormand7a57522010-05-24 14:32:25 -0700480#ifdef CONFIG_PROC_FS
Mel Gorman467c9962007-10-16 01:26:02 -0700481static 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 */
495static 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
502static 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 }
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700523 seq_putc(m, '\n');
524 }
Mel Gorman467c9962007-10-16 01:26:02 -0700525}
526
527/* Print out the free pages at each order for each migatetype */
528static 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
544static 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);
Mel Gormaneb335752009-05-13 17:34:48 +0100560
561 /* Watch for unexpected holes punched in the memmap */
562 if (!memmap_valid_within(pfn, page, zone))
Mel Gormane80d6a22008-08-14 11:10:14 +0100563 continue;
Mel Gormaneb335752009-05-13 17:34:48 +0100564
Mel Gorman467c9962007-10-16 01:26:02 -0700565 mtype = get_pageblock_migratetype(page);
566
Mel Gormane80d6a22008-08-14 11:10:14 +0100567 if (mtype < MIGRATE_TYPES)
568 count[mtype]++;
Mel Gorman467c9962007-10-16 01:26:02 -0700569 }
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 */
579static 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 */
597static int pagetypeinfo_show(struct seq_file *m, void *arg)
598{
599 pg_data_t *pgdat = (pg_data_t *)arg;
600
KOSAKI Motohiro41b25a32008-04-30 00:52:13 -0700601 /* check memoryless node */
602 if (!node_state(pgdat->node_id, N_HIGH_MEMORY))
603 return 0;
604
Mel Gorman467c9962007-10-16 01:26:02 -0700605 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
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700611 return 0;
612}
613
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +0400614static const struct seq_operations fragmentation_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700615 .start = frag_start,
616 .next = frag_next,
617 .stop = frag_stop,
618 .show = frag_show,
619};
620
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +0400621static int fragmentation_open(struct inode *inode, struct file *file)
622{
623 return seq_open(file, &fragmentation_op);
624}
625
626static 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
Alexey Dobriyan74e2e8e2008-10-06 04:15:36 +0400633static const struct seq_operations pagetypeinfo_op = {
Mel Gorman467c9962007-10-16 01:26:02 -0700634 .start = frag_start,
635 .next = frag_next,
636 .stop = frag_stop,
637 .show = pagetypeinfo_show,
638};
639
Alexey Dobriyan74e2e8e2008-10-06 04:15:36 +0400640static int pagetypeinfo_open(struct inode *inode, struct file *file)
641{
642 return seq_open(file, &pagetypeinfo_op);
643}
644
645static 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
Christoph Lameter4b51d662007-02-10 01:43:10 -0800652#ifdef CONFIG_ZONE_DMA
653#define TEXT_FOR_DMA(xx) xx "_dma",
654#else
655#define TEXT_FOR_DMA(xx)
656#endif
657
Christoph Lameter27bf71c2006-09-25 23:31:15 -0700658#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
Christoph Lameter4b51d662007-02-10 01:43:10 -0800670#define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
Mel Gorman2a1e2742007-07-17 04:03:12 -0700671 TEXT_FOR_HIGHMEM(xx) xx "_movable",
Christoph Lameter27bf71c2006-09-25 23:31:15 -0700672
Helge Deller15ad7cd2006-12-06 20:40:36 -0800673static const char * const vmstat_text[] = {
Christoph Lameter2244b952006-06-30 01:55:33 -0700674 /* Zoned VM counters */
Christoph Lameterd23ad422007-02-10 01:43:02 -0800675 "nr_free_pages",
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700676 "nr_inactive_anon",
677 "nr_active_anon",
678 "nr_inactive_file",
679 "nr_active_file",
Lee Schermerhorn7b854122008-10-18 20:26:40 -0700680 "nr_unevictable",
Nick Piggin5344b7e2008-10-18 20:26:51 -0700681 "nr_mlock",
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700682 "nr_anon_pages",
Christoph Lameter65ba55f2006-06-30 01:55:34 -0700683 "nr_mapped",
Christoph Lameter347ce432006-06-30 01:55:35 -0700684 "nr_file_pages",
Christoph Lameter51ed4492007-02-10 01:43:02 -0800685 "nr_dirty",
686 "nr_writeback",
Christoph Lameter972d1a72006-09-25 23:31:51 -0700687 "nr_slab_reclaimable",
688 "nr_slab_unreclaimable",
Christoph Lameterdf849a12006-06-30 01:55:38 -0700689 "nr_page_table_pages",
KOSAKI Motohiroc6a7f572009-09-21 17:01:32 -0700690 "nr_kernel_stack",
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700691 "nr_unstable",
Christoph Lameterd2c5e302006-06-30 01:55:41 -0700692 "nr_bounce",
Andrew Mortone129b5c2006-09-27 01:50:00 -0700693 "nr_vmscan_write",
Miklos Szeredifc3ba692008-04-30 00:54:38 -0700694 "nr_writeback_temp",
KOSAKI Motohiroa7312862009-09-21 17:01:37 -0700695 "nr_isolated_anon",
696 "nr_isolated_file",
KOSAKI Motohiro4b021082009-09-21 17:01:33 -0700697 "nr_shmem",
Christoph Lameterca889e62006-06-30 01:55:44 -0700698#ifdef CONFIG_NUMA
699 "numa_hit",
700 "numa_miss",
701 "numa_foreign",
702 "numa_interleave",
703 "numa_local",
704 "numa_other",
705#endif
706
Christoph Lameterf8891e52006-06-30 01:55:45 -0700707#ifdef CONFIG_VM_EVENT_COUNTERS
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700708 "pgpgin",
709 "pgpgout",
710 "pswpin",
711 "pswpout",
712
Christoph Lameter27bf71c2006-09-25 23:31:15 -0700713 TEXTS_FOR_ZONES("pgalloc")
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700714
715 "pgfree",
716 "pgactivate",
717 "pgdeactivate",
718
719 "pgfault",
720 "pgmajfault",
721
Christoph Lameter27bf71c2006-09-25 23:31:15 -0700722 TEXTS_FOR_ZONES("pgrefill")
723 TEXTS_FOR_ZONES("pgsteal")
724 TEXTS_FOR_ZONES("pgscan_kswapd")
725 TEXTS_FOR_ZONES("pgscan_direct")
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700726
Mel Gorman24cf725182009-06-16 15:33:23 -0700727#ifdef CONFIG_NUMA
728 "zone_reclaim_failed",
729#endif
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700730 "pginodesteal",
731 "slabs_scanned",
732 "kswapd_steal",
733 "kswapd_inodesteal",
KOSAKI Motohirobb3ab592009-12-14 17:58:55 -0800734 "kswapd_low_wmark_hit_quickly",
735 "kswapd_high_wmark_hit_quickly",
736 "kswapd_skip_congestion_wait",
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700737 "pageoutrun",
738 "allocstall",
739
740 "pgrotated",
Adam Litke3b116302008-04-28 02:13:06 -0700741#ifdef CONFIG_HUGETLB_PAGE
742 "htlb_buddy_alloc_success",
743 "htlb_buddy_alloc_fail",
744#endif
Lee Schermerhornbbfd28e2008-10-18 20:26:40 -0700745 "unevictable_pgs_culled",
746 "unevictable_pgs_scanned",
747 "unevictable_pgs_rescued",
Nick Piggin5344b7e2008-10-18 20:26:51 -0700748 "unevictable_pgs_mlocked",
749 "unevictable_pgs_munlocked",
750 "unevictable_pgs_cleared",
751 "unevictable_pgs_stranded",
Lee Schermerhorn985737c2008-10-18 20:26:53 -0700752 "unevictable_pgs_mlockfreed",
Lee Schermerhornbbfd28e2008-10-18 20:26:40 -0700753#endif
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700754};
755
Mel Gorman467c9962007-10-16 01:26:02 -0700756static 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"
Wu Fengguang08d9ae72009-06-16 15:32:30 -0700766 "\n scanned %lu"
Mel Gorman467c9962007-10-16 01:26:02 -0700767 "\n spanned %lu"
768 "\n present %lu",
769 zone_page_state(zone, NR_FREE_PAGES),
Mel Gorman41858962009-06-16 15:32:12 -0700770 min_wmark_pages(zone),
771 low_wmark_pages(zone),
772 high_wmark_pages(zone),
Mel Gorman467c9962007-10-16 01:26:02 -0700773 zone->pages_scanned,
Mel Gorman467c9962007-10-16 01:26:02 -0700774 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;
Mel Gorman467c9962007-10-16 01:26:02 -0700791
Christoph Lameter99dcc3e2010-01-05 15:34:51 +0900792 pageset = per_cpu_ptr(zone->pageset, i);
Christoph Lameter3dfa5722008-02-04 22:29:19 -0800793 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);
Mel Gorman467c9962007-10-16 01:26:02 -0700802#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"
Rik van Riel556adec2008-10-18 20:26:34 -0700810 "\n start_pfn: %lu"
811 "\n inactive_ratio: %u",
KOSAKI Motohiro93e4a892010-03-05 13:41:55 -0800812 zone->all_unreclaimable,
Mel Gorman467c9962007-10-16 01:26:02 -0700813 zone->prev_priority,
Rik van Riel556adec2008-10-18 20:26:34 -0700814 zone->zone_start_pfn,
815 zone->inactive_ratio);
Mel Gorman467c9962007-10-16 01:26:02 -0700816 seq_putc(m, '\n');
817}
818
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700819/*
820 * Output information about zones in @pgdat.
821 */
822static int zoneinfo_show(struct seq_file *m, void *arg)
823{
Mel Gorman467c9962007-10-16 01:26:02 -0700824 pg_data_t *pgdat = (pg_data_t *)arg;
825 walk_zones_in_node(m, pgdat, zoneinfo_show_print);
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700826 return 0;
827}
828
Alexey Dobriyan5c9fe622008-10-06 04:19:42 +0400829static const struct seq_operations zoneinfo_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700830 .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
Alexey Dobriyan5c9fe622008-10-06 04:19:42 +0400837static int zoneinfo_open(struct inode *inode, struct file *file)
838{
839 return seq_open(file, &zoneinfo_op);
840}
841
842static 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
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700849static void *vmstat_start(struct seq_file *m, loff_t *pos)
850{
Christoph Lameter2244b952006-06-30 01:55:33 -0700851 unsigned long *v;
Christoph Lameterf8891e52006-06-30 01:55:45 -0700852#ifdef CONFIG_VM_EVENT_COUNTERS
853 unsigned long *e;
854#endif
Christoph Lameter2244b952006-06-30 01:55:33 -0700855 int i;
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700856
857 if (*pos >= ARRAY_SIZE(vmstat_text))
858 return NULL;
859
Christoph Lameterf8891e52006-06-30 01:55:45 -0700860#ifdef CONFIG_VM_EVENT_COUNTERS
Christoph Lameter2244b952006-06-30 01:55:33 -0700861 v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long)
Christoph Lameterf8891e52006-06-30 01:55:45 -0700862 + 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
Christoph Lameter2244b952006-06-30 01:55:33 -0700867 m->private = v;
868 if (!v)
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700869 return ERR_PTR(-ENOMEM);
Christoph Lameter2244b952006-06-30 01:55:33 -0700870 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
871 v[i] = global_page_state(i);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700872#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
Christoph Lameter2244b952006-06-30 01:55:33 -0700878 return v + *pos;
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700879}
880
881static 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
889static 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
898static void vmstat_stop(struct seq_file *m, void *arg)
899{
900 kfree(m->private);
901 m->private = NULL;
902}
903
Alexey Dobriyanb6aa44a2008-10-06 04:17:48 +0400904static const struct seq_operations vmstat_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700905 .start = vmstat_start,
906 .next = vmstat_next,
907 .stop = vmstat_stop,
908 .show = vmstat_show,
909};
910
Alexey Dobriyanb6aa44a2008-10-06 04:17:48 +0400911static int vmstat_open(struct inode *inode, struct file *file)
912{
913 return seq_open(file, &vmstat_op);
914}
915
916static 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};
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700922#endif /* CONFIG_PROC_FS */
923
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700924#ifdef CONFIG_SMP
Christoph Lameterd1187ed2007-05-09 02:35:12 -0700925static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
Christoph Lameter77461ab2007-05-09 02:35:13 -0700926int sysctl_stat_interval __read_mostly = HZ;
Christoph Lameterd1187ed2007-05-09 02:35:12 -0700927
928static void vmstat_update(struct work_struct *w)
929{
930 refresh_cpu_vm_stats(smp_processor_id());
Christoph Lameter77461ab2007-05-09 02:35:13 -0700931 schedule_delayed_work(&__get_cpu_var(vmstat_work),
Anton Blanchard98f4ebb2009-04-02 16:56:39 -0700932 round_jiffies_relative(sysctl_stat_interval));
Christoph Lameterd1187ed2007-05-09 02:35:12 -0700933}
934
Randy Dunlap42614fc2007-11-14 17:00:12 -0800935static void __cpuinit start_cpu_timer(int cpu)
Christoph Lameterd1187ed2007-05-09 02:35:12 -0700936{
Tejun Heo1871e522009-10-29 22:34:13 +0900937 struct delayed_work *work = &per_cpu(vmstat_work, cpu);
Christoph Lameterd1187ed2007-05-09 02:35:12 -0700938
Tejun Heo1871e522009-10-29 22:34:13 +0900939 INIT_DELAYED_WORK_DEFERRABLE(work, vmstat_update);
940 schedule_delayed_work_on(cpu, work, __round_jiffies_relative(HZ, cpu));
Christoph Lameterd1187ed2007-05-09 02:35:12 -0700941}
942
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700943/*
944 * Use the cpu notifier to insure that the thresholds are recalculated
945 * when necessary.
946 */
947static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
948 unsigned long action,
949 void *hcpu)
950{
Christoph Lameterd1187ed2007-05-09 02:35:12 -0700951 long cpu = (long)hcpu;
952
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700953 switch (action) {
Christoph Lameterd1187ed2007-05-09 02:35:12 -0700954 case CPU_ONLINE:
955 case CPU_ONLINE_FROZEN:
956 start_cpu_timer(cpu);
Christoph Lameterad596922010-01-05 15:34:51 +0900957 node_set_state(cpu_to_node(cpu), N_CPU);
Christoph Lameterd1187ed2007-05-09 02:35:12 -0700958 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;
Andy Whitcroftce421c72006-12-06 20:33:08 -0800968 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700969 case CPU_DEAD_FROZEN:
Andy Whitcroftce421c72006-12-06 20:33:08 -0800970 refresh_zone_stat_thresholds();
971 break;
972 default:
973 break;
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700974 }
975 return NOTIFY_OK;
976}
977
978static struct notifier_block __cpuinitdata vmstat_notifier =
979 { &vmstat_cpuup_callback, NULL, 0 };
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +0400980#endif
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700981
Adrian Bunke2fc88d2007-10-16 01:26:27 -0700982static int __init setup_vmstat(void)
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700983{
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +0400984#ifdef CONFIG_SMP
Christoph Lameterd1187ed2007-05-09 02:35:12 -0700985 int cpu;
986
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700987 refresh_zone_stat_thresholds();
988 register_cpu_notifier(&vmstat_notifier);
Christoph Lameterd1187ed2007-05-09 02:35:12 -0700989
990 for_each_online_cpu(cpu)
991 start_cpu_timer(cpu);
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +0400992#endif
993#ifdef CONFIG_PROC_FS
994 proc_create("buddyinfo", S_IRUGO, NULL, &fragmentation_file_operations);
Alexey Dobriyan74e2e8e2008-10-06 04:15:36 +0400995 proc_create("pagetypeinfo", S_IRUGO, NULL, &pagetypeinfo_file_ops);
Alexey Dobriyanb6aa44a2008-10-06 04:17:48 +0400996 proc_create("vmstat", S_IRUGO, NULL, &proc_vmstat_file_operations);
Alexey Dobriyan5c9fe622008-10-06 04:19:42 +0400997 proc_create("zoneinfo", S_IRUGO, NULL, &proc_zoneinfo_file_operations);
Alexey Dobriyan8f32f7e2008-10-06 04:13:52 +0400998#endif
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700999 return 0;
1000}
1001module_init(setup_vmstat)
Mel Gormand7a57522010-05-24 14:32:25 -07001002
1003#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_COMPACTION)
1004#include <linux/debugfs.h>
1005
1006static 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 */
1012static 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
1030static 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 */
1058static 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
1071static const struct seq_operations unusable_op = {
1072 .start = frag_start,
1073 .next = frag_next,
1074 .stop = frag_stop,
1075 .show = unusable_show,
1076};
1077
1078static int unusable_open(struct inode *inode, struct file *file)
1079{
1080 return seq_open(file, &unusable_op);
1081}
1082
1083static 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
1090static 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
1103module_init(extfrag_debug_init);
1104#endif