blob: eceaf496210fcf241d55482e9dd317c2a9d5dc56 [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 */
11
Christoph Lameterf6ac2352006-06-30 01:55:32 -070012#include <linux/mm.h>
Christoph Lameter2244b952006-06-30 01:55:33 -070013#include <linux/module.h>
Christoph Lameterdf9ecab2006-08-31 21:27:35 -070014#include <linux/cpu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040015#include <linux/sched.h>
Christoph Lameterf6ac2352006-06-30 01:55:32 -070016
Christoph Lameterf8891e52006-06-30 01:55:45 -070017#ifdef CONFIG_VM_EVENT_COUNTERS
18DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
19EXPORT_PER_CPU_SYMBOL(vm_event_states);
20
21static void sum_vm_events(unsigned long *ret, cpumask_t *cpumask)
22{
23 int cpu = 0;
24 int i;
25
26 memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
27
28 cpu = first_cpu(*cpumask);
29 while (cpu < NR_CPUS) {
30 struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
31
32 cpu = next_cpu(cpu, *cpumask);
33
34 if (cpu < NR_CPUS)
35 prefetch(&per_cpu(vm_event_states, cpu));
36
37
38 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
39 ret[i] += this->event[i];
40 }
41}
42
43/*
44 * Accumulate the vm event counters across all CPUs.
45 * The result is unavoidably approximate - it can change
46 * during and after execution of this function.
47*/
48void all_vm_events(unsigned long *ret)
49{
50 sum_vm_events(ret, &cpu_online_map);
51}
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
141 for_each_zone(zone) {
142
143 if (!zone->present_pages)
144 continue;
145
146 threshold = calculate_threshold(zone);
147
148 for_each_online_cpu(cpu)
149 zone_pcp(zone, cpu)->stat_threshold = threshold;
150 }
Christoph Lameter2244b952006-06-30 01:55:33 -0700151}
152
153/*
154 * For use when we know that interrupts are disabled.
155 */
156void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
157 int delta)
158{
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700159 struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
160 s8 *p = pcp->vm_stat_diff + item;
Christoph Lameter2244b952006-06-30 01:55:33 -0700161 long x;
162
Christoph Lameter2244b952006-06-30 01:55:33 -0700163 x = delta + *p;
164
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700165 if (unlikely(x > pcp->stat_threshold || x < -pcp->stat_threshold)) {
Christoph Lameter2244b952006-06-30 01:55:33 -0700166 zone_page_state_add(x, zone, item);
167 x = 0;
168 }
Christoph Lameter2244b952006-06-30 01:55:33 -0700169 *p = x;
170}
171EXPORT_SYMBOL(__mod_zone_page_state);
172
173/*
174 * For an unknown interrupt state
175 */
176void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
177 int delta)
178{
179 unsigned long flags;
180
181 local_irq_save(flags);
182 __mod_zone_page_state(zone, item, delta);
183 local_irq_restore(flags);
184}
185EXPORT_SYMBOL(mod_zone_page_state);
186
187/*
188 * Optimized increment and decrement functions.
189 *
190 * These are only for a single page and therefore can take a struct page *
191 * argument instead of struct zone *. This allows the inclusion of the code
192 * generated for page_zone(page) into the optimized functions.
193 *
194 * No overflow check is necessary and therefore the differential can be
195 * incremented or decremented in place which may allow the compilers to
196 * generate better code.
Christoph Lameter2244b952006-06-30 01:55:33 -0700197 * The increment or decrement is known and therefore one boundary check can
198 * be omitted.
199 *
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700200 * NOTE: These functions are very performance sensitive. Change only
201 * with care.
202 *
Christoph Lameter2244b952006-06-30 01:55:33 -0700203 * Some processors have inc/dec instructions that are atomic vs an interrupt.
204 * However, the code must first determine the differential location in a zone
205 * based on the processor number and then inc/dec the counter. There is no
206 * guarantee without disabling preemption that the processor will not change
207 * in between and therefore the atomicity vs. interrupt cannot be exploited
208 * in a useful way here.
209 */
Christoph Lameterc8785382007-02-10 01:43:01 -0800210void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
Christoph Lameter2244b952006-06-30 01:55:33 -0700211{
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700212 struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
213 s8 *p = pcp->vm_stat_diff + item;
Christoph Lameter2244b952006-06-30 01:55:33 -0700214
215 (*p)++;
216
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700217 if (unlikely(*p > pcp->stat_threshold)) {
218 int overstep = pcp->stat_threshold / 2;
219
220 zone_page_state_add(*p + overstep, zone, item);
221 *p = -overstep;
Christoph Lameter2244b952006-06-30 01:55:33 -0700222 }
223}
Christoph Lameterca889e62006-06-30 01:55:44 -0700224
225void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
226{
227 __inc_zone_state(page_zone(page), item);
228}
Christoph Lameter2244b952006-06-30 01:55:33 -0700229EXPORT_SYMBOL(__inc_zone_page_state);
230
Christoph Lameterc8785382007-02-10 01:43:01 -0800231void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
Christoph Lameter2244b952006-06-30 01:55:33 -0700232{
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700233 struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
234 s8 *p = pcp->vm_stat_diff + item;
Christoph Lameter2244b952006-06-30 01:55:33 -0700235
236 (*p)--;
237
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700238 if (unlikely(*p < - pcp->stat_threshold)) {
239 int overstep = pcp->stat_threshold / 2;
240
241 zone_page_state_add(*p - overstep, zone, item);
242 *p = overstep;
Christoph Lameter2244b952006-06-30 01:55:33 -0700243 }
244}
Christoph Lameterc8785382007-02-10 01:43:01 -0800245
246void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
247{
248 __dec_zone_state(page_zone(page), item);
249}
Christoph Lameter2244b952006-06-30 01:55:33 -0700250EXPORT_SYMBOL(__dec_zone_page_state);
251
Christoph Lameterca889e62006-06-30 01:55:44 -0700252void inc_zone_state(struct zone *zone, enum zone_stat_item item)
253{
254 unsigned long flags;
255
256 local_irq_save(flags);
257 __inc_zone_state(zone, item);
258 local_irq_restore(flags);
259}
260
Christoph Lameter2244b952006-06-30 01:55:33 -0700261void inc_zone_page_state(struct page *page, enum zone_stat_item item)
262{
263 unsigned long flags;
264 struct zone *zone;
Christoph Lameter2244b952006-06-30 01:55:33 -0700265
266 zone = page_zone(page);
267 local_irq_save(flags);
Christoph Lameterca889e62006-06-30 01:55:44 -0700268 __inc_zone_state(zone, item);
Christoph Lameter2244b952006-06-30 01:55:33 -0700269 local_irq_restore(flags);
270}
271EXPORT_SYMBOL(inc_zone_page_state);
272
273void dec_zone_page_state(struct page *page, enum zone_stat_item item)
274{
275 unsigned long flags;
Christoph Lameter2244b952006-06-30 01:55:33 -0700276
Christoph Lameter2244b952006-06-30 01:55:33 -0700277 local_irq_save(flags);
Christoph Lametera302eb42006-08-31 21:27:34 -0700278 __dec_zone_page_state(page, item);
Christoph Lameter2244b952006-06-30 01:55:33 -0700279 local_irq_restore(flags);
280}
281EXPORT_SYMBOL(dec_zone_page_state);
282
283/*
284 * Update the zone counters for one cpu.
Christoph Lameter4037d452007-05-09 02:35:14 -0700285 *
286 * Note that refresh_cpu_vm_stats strives to only access
287 * node local memory. The per cpu pagesets on remote zones are placed
288 * in the memory local to the processor using that pageset. So the
289 * loop over all zones will access a series of cachelines local to
290 * the processor.
291 *
292 * The call to zone_page_state_add updates the cachelines with the
293 * statistics in the remote zone struct as well as the global cachelines
294 * with the global counters. These could cause remote node cache line
295 * bouncing and will have to be only done when necessary.
Christoph Lameter2244b952006-06-30 01:55:33 -0700296 */
297void refresh_cpu_vm_stats(int cpu)
298{
299 struct zone *zone;
300 int i;
301 unsigned long flags;
302
303 for_each_zone(zone) {
Christoph Lameter4037d452007-05-09 02:35:14 -0700304 struct per_cpu_pageset *p;
Christoph Lameter2244b952006-06-30 01:55:33 -0700305
Christoph Lameter39bbcb82006-09-25 23:31:49 -0700306 if (!populated_zone(zone))
307 continue;
308
Christoph Lameter4037d452007-05-09 02:35:14 -0700309 p = zone_pcp(zone, cpu);
Christoph Lameter2244b952006-06-30 01:55:33 -0700310
311 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
Christoph Lameter4037d452007-05-09 02:35:14 -0700312 if (p->vm_stat_diff[i]) {
Christoph Lameter2244b952006-06-30 01:55:33 -0700313 local_irq_save(flags);
Christoph Lameter4037d452007-05-09 02:35:14 -0700314 zone_page_state_add(p->vm_stat_diff[i],
Christoph Lameter2244b952006-06-30 01:55:33 -0700315 zone, i);
Christoph Lameter4037d452007-05-09 02:35:14 -0700316 p->vm_stat_diff[i] = 0;
317#ifdef CONFIG_NUMA
318 /* 3 seconds idle till flush */
319 p->expire = 3;
320#endif
Christoph Lameter2244b952006-06-30 01:55:33 -0700321 local_irq_restore(flags);
322 }
Christoph Lameter4037d452007-05-09 02:35:14 -0700323#ifdef CONFIG_NUMA
324 /*
325 * Deal with draining the remote pageset of this
326 * processor
327 *
328 * Check if there are pages remaining in this pageset
329 * if not then there is nothing to expire.
330 */
331 if (!p->expire || (!p->pcp[0].count && !p->pcp[1].count))
332 continue;
333
334 /*
335 * We never drain zones local to this processor.
336 */
337 if (zone_to_nid(zone) == numa_node_id()) {
338 p->expire = 0;
339 continue;
340 }
341
342 p->expire--;
343 if (p->expire)
344 continue;
345
346 if (p->pcp[0].count)
347 drain_zone_pages(zone, p->pcp + 0);
348
349 if (p->pcp[1].count)
350 drain_zone_pages(zone, p->pcp + 1);
351#endif
Christoph Lameter2244b952006-06-30 01:55:33 -0700352 }
353}
354
355static void __refresh_cpu_vm_stats(void *dummy)
356{
357 refresh_cpu_vm_stats(smp_processor_id());
358}
359
360/*
361 * Consolidate all counters.
362 *
363 * Note that the result is less inaccurate but still inaccurate
364 * if concurrent processes are allowed to run.
365 */
366void refresh_vm_stats(void)
367{
368 on_each_cpu(__refresh_cpu_vm_stats, NULL, 0, 1);
369}
370EXPORT_SYMBOL(refresh_vm_stats);
371
372#endif
373
Christoph Lameterca889e62006-06-30 01:55:44 -0700374#ifdef CONFIG_NUMA
375/*
376 * zonelist = the list of zones passed to the allocator
377 * z = the zone from which the allocation occurred.
378 *
379 * Must be called with interrupts disabled.
380 */
381void zone_statistics(struct zonelist *zonelist, struct zone *z)
382{
383 if (z->zone_pgdat == zonelist->zones[0]->zone_pgdat) {
384 __inc_zone_state(z, NUMA_HIT);
385 } else {
386 __inc_zone_state(z, NUMA_MISS);
387 __inc_zone_state(zonelist->zones[0], NUMA_FOREIGN);
388 }
Christoph Lameter5d292342006-09-27 01:50:10 -0700389 if (z->node == numa_node_id())
Christoph Lameterca889e62006-06-30 01:55:44 -0700390 __inc_zone_state(z, NUMA_LOCAL);
391 else
392 __inc_zone_state(z, NUMA_OTHER);
393}
394#endif
395
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700396#ifdef CONFIG_PROC_FS
397
398#include <linux/seq_file.h>
399
400static void *frag_start(struct seq_file *m, loff_t *pos)
401{
402 pg_data_t *pgdat;
403 loff_t node = *pos;
404 for (pgdat = first_online_pgdat();
405 pgdat && node;
406 pgdat = next_online_pgdat(pgdat))
407 --node;
408
409 return pgdat;
410}
411
412static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
413{
414 pg_data_t *pgdat = (pg_data_t *)arg;
415
416 (*pos)++;
417 return next_online_pgdat(pgdat);
418}
419
420static void frag_stop(struct seq_file *m, void *arg)
421{
422}
423
424/*
425 * This walks the free areas for each zone.
426 */
427static int frag_show(struct seq_file *m, void *arg)
428{
429 pg_data_t *pgdat = (pg_data_t *)arg;
430 struct zone *zone;
431 struct zone *node_zones = pgdat->node_zones;
432 unsigned long flags;
433 int order;
434
435 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
436 if (!populated_zone(zone))
437 continue;
438
439 spin_lock_irqsave(&zone->lock, flags);
440 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
441 for (order = 0; order < MAX_ORDER; ++order)
442 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
443 spin_unlock_irqrestore(&zone->lock, flags);
444 seq_putc(m, '\n');
445 }
446 return 0;
447}
448
Helge Deller15ad7cd2006-12-06 20:40:36 -0800449const struct seq_operations fragmentation_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700450 .start = frag_start,
451 .next = frag_next,
452 .stop = frag_stop,
453 .show = frag_show,
454};
455
Christoph Lameter4b51d662007-02-10 01:43:10 -0800456#ifdef CONFIG_ZONE_DMA
457#define TEXT_FOR_DMA(xx) xx "_dma",
458#else
459#define TEXT_FOR_DMA(xx)
460#endif
461
Christoph Lameter27bf71c2006-09-25 23:31:15 -0700462#ifdef CONFIG_ZONE_DMA32
463#define TEXT_FOR_DMA32(xx) xx "_dma32",
464#else
465#define TEXT_FOR_DMA32(xx)
466#endif
467
468#ifdef CONFIG_HIGHMEM
469#define TEXT_FOR_HIGHMEM(xx) xx "_high",
470#else
471#define TEXT_FOR_HIGHMEM(xx)
472#endif
473
Christoph Lameter4b51d662007-02-10 01:43:10 -0800474#define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
Christoph Lameter27bf71c2006-09-25 23:31:15 -0700475 TEXT_FOR_HIGHMEM(xx)
476
Helge Deller15ad7cd2006-12-06 20:40:36 -0800477static const char * const vmstat_text[] = {
Christoph Lameter2244b952006-06-30 01:55:33 -0700478 /* Zoned VM counters */
Christoph Lameterd23ad422007-02-10 01:43:02 -0800479 "nr_free_pages",
Christoph Lameterc8785382007-02-10 01:43:01 -0800480 "nr_inactive",
Peter Zijlstra23c1fb52007-07-06 13:35:34 +0200481 "nr_active",
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700482 "nr_anon_pages",
Christoph Lameter65ba55f2006-06-30 01:55:34 -0700483 "nr_mapped",
Christoph Lameter347ce432006-06-30 01:55:35 -0700484 "nr_file_pages",
Christoph Lameter51ed4492007-02-10 01:43:02 -0800485 "nr_dirty",
486 "nr_writeback",
Christoph Lameter972d1a72006-09-25 23:31:51 -0700487 "nr_slab_reclaimable",
488 "nr_slab_unreclaimable",
Christoph Lameterdf849a12006-06-30 01:55:38 -0700489 "nr_page_table_pages",
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700490 "nr_unstable",
Christoph Lameterd2c5e302006-06-30 01:55:41 -0700491 "nr_bounce",
Andrew Mortone129b5c2006-09-27 01:50:00 -0700492 "nr_vmscan_write",
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700493
Christoph Lameterca889e62006-06-30 01:55:44 -0700494#ifdef CONFIG_NUMA
495 "numa_hit",
496 "numa_miss",
497 "numa_foreign",
498 "numa_interleave",
499 "numa_local",
500 "numa_other",
501#endif
502
Christoph Lameterf8891e52006-06-30 01:55:45 -0700503#ifdef CONFIG_VM_EVENT_COUNTERS
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700504 "pgpgin",
505 "pgpgout",
506 "pswpin",
507 "pswpout",
508
Christoph Lameter27bf71c2006-09-25 23:31:15 -0700509 TEXTS_FOR_ZONES("pgalloc")
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700510
511 "pgfree",
512 "pgactivate",
513 "pgdeactivate",
514
515 "pgfault",
516 "pgmajfault",
517
Christoph Lameter27bf71c2006-09-25 23:31:15 -0700518 TEXTS_FOR_ZONES("pgrefill")
519 TEXTS_FOR_ZONES("pgsteal")
520 TEXTS_FOR_ZONES("pgscan_kswapd")
521 TEXTS_FOR_ZONES("pgscan_direct")
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700522
523 "pginodesteal",
524 "slabs_scanned",
525 "kswapd_steal",
526 "kswapd_inodesteal",
527 "pageoutrun",
528 "allocstall",
529
530 "pgrotated",
Christoph Lameterf8891e52006-06-30 01:55:45 -0700531#endif
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700532};
533
534/*
535 * Output information about zones in @pgdat.
536 */
537static int zoneinfo_show(struct seq_file *m, void *arg)
538{
539 pg_data_t *pgdat = arg;
540 struct zone *zone;
541 struct zone *node_zones = pgdat->node_zones;
542 unsigned long flags;
543
544 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
545 int i;
546
547 if (!populated_zone(zone))
548 continue;
549
550 spin_lock_irqsave(&zone->lock, flags);
551 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
552 seq_printf(m,
553 "\n pages free %lu"
554 "\n min %lu"
555 "\n low %lu"
556 "\n high %lu"
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700557 "\n scanned %lu (a: %lu i: %lu)"
558 "\n spanned %lu"
559 "\n present %lu",
Christoph Lameterd23ad422007-02-10 01:43:02 -0800560 zone_page_state(zone, NR_FREE_PAGES),
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700561 zone->pages_min,
562 zone->pages_low,
563 zone->pages_high,
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700564 zone->pages_scanned,
565 zone->nr_scan_active, zone->nr_scan_inactive,
566 zone->spanned_pages,
567 zone->present_pages);
Christoph Lameter2244b952006-06-30 01:55:33 -0700568
569 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
570 seq_printf(m, "\n %-12s %lu", vmstat_text[i],
571 zone_page_state(zone, i));
572
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700573 seq_printf(m,
574 "\n protection: (%lu",
575 zone->lowmem_reserve[0]);
576 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
577 seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
578 seq_printf(m,
579 ")"
580 "\n pagesets");
581 for_each_online_cpu(i) {
582 struct per_cpu_pageset *pageset;
583 int j;
584
585 pageset = zone_pcp(zone, i);
586 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700587 seq_printf(m,
588 "\n cpu: %i pcp: %i"
589 "\n count: %i"
590 "\n high: %i"
591 "\n batch: %i",
592 i, j,
593 pageset->pcp[j].count,
594 pageset->pcp[j].high,
595 pageset->pcp[j].batch);
596 }
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700597#ifdef CONFIG_SMP
598 seq_printf(m, "\n vm stats threshold: %d",
599 pageset->stat_threshold);
600#endif
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700601 }
602 seq_printf(m,
603 "\n all_unreclaimable: %u"
604 "\n prev_priority: %i"
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700605 "\n start_pfn: %lu",
606 zone->all_unreclaimable,
607 zone->prev_priority,
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700608 zone->zone_start_pfn);
609 spin_unlock_irqrestore(&zone->lock, flags);
610 seq_putc(m, '\n');
611 }
612 return 0;
613}
614
Helge Deller15ad7cd2006-12-06 20:40:36 -0800615const struct seq_operations zoneinfo_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700616 .start = frag_start, /* iterate over all zones. The same as in
617 * fragmentation. */
618 .next = frag_next,
619 .stop = frag_stop,
620 .show = zoneinfo_show,
621};
622
623static void *vmstat_start(struct seq_file *m, loff_t *pos)
624{
Christoph Lameter2244b952006-06-30 01:55:33 -0700625 unsigned long *v;
Christoph Lameterf8891e52006-06-30 01:55:45 -0700626#ifdef CONFIG_VM_EVENT_COUNTERS
627 unsigned long *e;
628#endif
Christoph Lameter2244b952006-06-30 01:55:33 -0700629 int i;
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700630
631 if (*pos >= ARRAY_SIZE(vmstat_text))
632 return NULL;
633
Christoph Lameterf8891e52006-06-30 01:55:45 -0700634#ifdef CONFIG_VM_EVENT_COUNTERS
Christoph Lameter2244b952006-06-30 01:55:33 -0700635 v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long)
Christoph Lameterf8891e52006-06-30 01:55:45 -0700636 + sizeof(struct vm_event_state), GFP_KERNEL);
637#else
638 v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long),
639 GFP_KERNEL);
640#endif
Christoph Lameter2244b952006-06-30 01:55:33 -0700641 m->private = v;
642 if (!v)
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700643 return ERR_PTR(-ENOMEM);
Christoph Lameter2244b952006-06-30 01:55:33 -0700644 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
645 v[i] = global_page_state(i);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700646#ifdef CONFIG_VM_EVENT_COUNTERS
647 e = v + NR_VM_ZONE_STAT_ITEMS;
648 all_vm_events(e);
649 e[PGPGIN] /= 2; /* sectors -> kbytes */
650 e[PGPGOUT] /= 2;
651#endif
Christoph Lameter2244b952006-06-30 01:55:33 -0700652 return v + *pos;
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700653}
654
655static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
656{
657 (*pos)++;
658 if (*pos >= ARRAY_SIZE(vmstat_text))
659 return NULL;
660 return (unsigned long *)m->private + *pos;
661}
662
663static int vmstat_show(struct seq_file *m, void *arg)
664{
665 unsigned long *l = arg;
666 unsigned long off = l - (unsigned long *)m->private;
667
668 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
669 return 0;
670}
671
672static void vmstat_stop(struct seq_file *m, void *arg)
673{
674 kfree(m->private);
675 m->private = NULL;
676}
677
Helge Deller15ad7cd2006-12-06 20:40:36 -0800678const struct seq_operations vmstat_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700679 .start = vmstat_start,
680 .next = vmstat_next,
681 .stop = vmstat_stop,
682 .show = vmstat_show,
683};
684
685#endif /* CONFIG_PROC_FS */
686
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700687#ifdef CONFIG_SMP
Christoph Lameterd1187ed2007-05-09 02:35:12 -0700688static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
Christoph Lameter77461ab2007-05-09 02:35:13 -0700689int sysctl_stat_interval __read_mostly = HZ;
Christoph Lameterd1187ed2007-05-09 02:35:12 -0700690
691static void vmstat_update(struct work_struct *w)
692{
693 refresh_cpu_vm_stats(smp_processor_id());
Christoph Lameter77461ab2007-05-09 02:35:13 -0700694 schedule_delayed_work(&__get_cpu_var(vmstat_work),
695 sysctl_stat_interval);
Christoph Lameterd1187ed2007-05-09 02:35:12 -0700696}
697
698static void __devinit start_cpu_timer(int cpu)
699{
700 struct delayed_work *vmstat_work = &per_cpu(vmstat_work, cpu);
701
Christoph Lameter39bf6272007-05-10 22:22:21 -0700702 INIT_DELAYED_WORK_DEFERRABLE(vmstat_work, vmstat_update);
Christoph Lameterd1187ed2007-05-09 02:35:12 -0700703 schedule_delayed_work_on(cpu, vmstat_work, HZ + cpu);
704}
705
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700706/*
707 * Use the cpu notifier to insure that the thresholds are recalculated
708 * when necessary.
709 */
710static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
711 unsigned long action,
712 void *hcpu)
713{
Christoph Lameterd1187ed2007-05-09 02:35:12 -0700714 long cpu = (long)hcpu;
715
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700716 switch (action) {
Christoph Lameterd1187ed2007-05-09 02:35:12 -0700717 case CPU_ONLINE:
718 case CPU_ONLINE_FROZEN:
719 start_cpu_timer(cpu);
720 break;
721 case CPU_DOWN_PREPARE:
722 case CPU_DOWN_PREPARE_FROZEN:
723 cancel_rearming_delayed_work(&per_cpu(vmstat_work, cpu));
724 per_cpu(vmstat_work, cpu).work.func = NULL;
725 break;
726 case CPU_DOWN_FAILED:
727 case CPU_DOWN_FAILED_FROZEN:
728 start_cpu_timer(cpu);
729 break;
Andy Whitcroftce421c72006-12-06 20:33:08 -0800730 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700731 case CPU_DEAD_FROZEN:
Andy Whitcroftce421c72006-12-06 20:33:08 -0800732 refresh_zone_stat_thresholds();
733 break;
734 default:
735 break;
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700736 }
737 return NOTIFY_OK;
738}
739
740static struct notifier_block __cpuinitdata vmstat_notifier =
741 { &vmstat_cpuup_callback, NULL, 0 };
742
743int __init setup_vmstat(void)
744{
Christoph Lameterd1187ed2007-05-09 02:35:12 -0700745 int cpu;
746
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700747 refresh_zone_stat_thresholds();
748 register_cpu_notifier(&vmstat_notifier);
Christoph Lameterd1187ed2007-05-09 02:35:12 -0700749
750 for_each_online_cpu(cpu)
751 start_cpu_timer(cpu);
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700752 return 0;
753}
754module_init(setup_vmstat)
755#endif