blob: 6c488d6ac425d948b7b77d78a6cac899be16af6a [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>
Christoph Lameterf6ac2352006-06-30 01:55:32 -070015
Christoph Lameterf8891e52006-06-30 01:55:45 -070016#ifdef CONFIG_VM_EVENT_COUNTERS
17DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
18EXPORT_PER_CPU_SYMBOL(vm_event_states);
19
20static void sum_vm_events(unsigned long *ret, cpumask_t *cpumask)
21{
22 int cpu = 0;
23 int i;
24
25 memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
26
27 cpu = first_cpu(*cpumask);
28 while (cpu < NR_CPUS) {
29 struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
30
31 cpu = next_cpu(cpu, *cpumask);
32
33 if (cpu < NR_CPUS)
34 prefetch(&per_cpu(vm_event_states, cpu));
35
36
37 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
38 ret[i] += this->event[i];
39 }
40}
41
42/*
43 * Accumulate the vm event counters across all CPUs.
44 * The result is unavoidably approximate - it can change
45 * during and after execution of this function.
46*/
47void all_vm_events(unsigned long *ret)
48{
49 sum_vm_events(ret, &cpu_online_map);
50}
Heiko Carstens32dd66f2006-07-10 04:44:31 -070051EXPORT_SYMBOL_GPL(all_vm_events);
Christoph Lameterf8891e52006-06-30 01:55:45 -070052
53#ifdef CONFIG_HOTPLUG
54/*
55 * Fold the foreign cpu events into our own.
56 *
57 * This is adding to the events on one processor
58 * but keeps the global counts constant.
59 */
60void vm_events_fold_cpu(int cpu)
61{
62 struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu);
63 int i;
64
65 for (i = 0; i < NR_VM_EVENT_ITEMS; i++) {
66 count_vm_events(i, fold_state->event[i]);
67 fold_state->event[i] = 0;
68 }
69}
70#endif /* CONFIG_HOTPLUG */
71
72#endif /* CONFIG_VM_EVENT_COUNTERS */
73
Christoph Lameter2244b952006-06-30 01:55:33 -070074/*
75 * Manage combined zone based / global counters
76 *
77 * vm_stat contains the global counters
78 */
79atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
80EXPORT_SYMBOL(vm_stat);
81
82#ifdef CONFIG_SMP
83
Christoph Lameterdf9ecab2006-08-31 21:27:35 -070084static int calculate_threshold(struct zone *zone)
85{
86 int threshold;
87 int mem; /* memory in 128 MB units */
88
89 /*
90 * The threshold scales with the number of processors and the amount
91 * of memory per zone. More memory means that we can defer updates for
92 * longer, more processors could lead to more contention.
93 * fls() is used to have a cheap way of logarithmic scaling.
94 *
95 * Some sample thresholds:
96 *
97 * Threshold Processors (fls) Zonesize fls(mem+1)
98 * ------------------------------------------------------------------
99 * 8 1 1 0.9-1 GB 4
100 * 16 2 2 0.9-1 GB 4
101 * 20 2 2 1-2 GB 5
102 * 24 2 2 2-4 GB 6
103 * 28 2 2 4-8 GB 7
104 * 32 2 2 8-16 GB 8
105 * 4 2 2 <128M 1
106 * 30 4 3 2-4 GB 5
107 * 48 4 3 8-16 GB 8
108 * 32 8 4 1-2 GB 4
109 * 32 8 4 0.9-1GB 4
110 * 10 16 5 <128M 1
111 * 40 16 5 900M 4
112 * 70 64 7 2-4 GB 5
113 * 84 64 7 4-8 GB 6
114 * 108 512 9 4-8 GB 6
115 * 125 1024 10 8-16 GB 8
116 * 125 1024 10 16-32 GB 9
117 */
118
119 mem = zone->present_pages >> (27 - PAGE_SHIFT);
120
121 threshold = 2 * fls(num_online_cpus()) * (1 + fls(mem));
122
123 /*
124 * Maximum threshold is 125
125 */
126 threshold = min(125, threshold);
127
128 return threshold;
129}
Christoph Lameter2244b952006-06-30 01:55:33 -0700130
131/*
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700132 * Refresh the thresholds for each zone.
Christoph Lameter2244b952006-06-30 01:55:33 -0700133 */
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700134static void refresh_zone_stat_thresholds(void)
Christoph Lameter2244b952006-06-30 01:55:33 -0700135{
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700136 struct zone *zone;
137 int cpu;
138 int threshold;
139
140 for_each_zone(zone) {
141
142 if (!zone->present_pages)
143 continue;
144
145 threshold = calculate_threshold(zone);
146
147 for_each_online_cpu(cpu)
148 zone_pcp(zone, cpu)->stat_threshold = threshold;
149 }
Christoph Lameter2244b952006-06-30 01:55:33 -0700150}
151
152/*
153 * For use when we know that interrupts are disabled.
154 */
155void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
156 int delta)
157{
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700158 struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
159 s8 *p = pcp->vm_stat_diff + item;
Christoph Lameter2244b952006-06-30 01:55:33 -0700160 long x;
161
Christoph Lameter2244b952006-06-30 01:55:33 -0700162 x = delta + *p;
163
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700164 if (unlikely(x > pcp->stat_threshold || x < -pcp->stat_threshold)) {
Christoph Lameter2244b952006-06-30 01:55:33 -0700165 zone_page_state_add(x, zone, item);
166 x = 0;
167 }
Christoph Lameter2244b952006-06-30 01:55:33 -0700168 *p = x;
169}
170EXPORT_SYMBOL(__mod_zone_page_state);
171
172/*
173 * For an unknown interrupt state
174 */
175void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
176 int delta)
177{
178 unsigned long flags;
179
180 local_irq_save(flags);
181 __mod_zone_page_state(zone, item, delta);
182 local_irq_restore(flags);
183}
184EXPORT_SYMBOL(mod_zone_page_state);
185
186/*
187 * Optimized increment and decrement functions.
188 *
189 * These are only for a single page and therefore can take a struct page *
190 * argument instead of struct zone *. This allows the inclusion of the code
191 * generated for page_zone(page) into the optimized functions.
192 *
193 * No overflow check is necessary and therefore the differential can be
194 * incremented or decremented in place which may allow the compilers to
195 * generate better code.
Christoph Lameter2244b952006-06-30 01:55:33 -0700196 * The increment or decrement is known and therefore one boundary check can
197 * be omitted.
198 *
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700199 * NOTE: These functions are very performance sensitive. Change only
200 * with care.
201 *
Christoph Lameter2244b952006-06-30 01:55:33 -0700202 * Some processors have inc/dec instructions that are atomic vs an interrupt.
203 * However, the code must first determine the differential location in a zone
204 * based on the processor number and then inc/dec the counter. There is no
205 * guarantee without disabling preemption that the processor will not change
206 * in between and therefore the atomicity vs. interrupt cannot be exploited
207 * in a useful way here.
208 */
Christoph Lameterc8785382007-02-10 01:43:01 -0800209void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
Christoph Lameter2244b952006-06-30 01:55:33 -0700210{
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700211 struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
212 s8 *p = pcp->vm_stat_diff + item;
Christoph Lameter2244b952006-06-30 01:55:33 -0700213
214 (*p)++;
215
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700216 if (unlikely(*p > pcp->stat_threshold)) {
217 int overstep = pcp->stat_threshold / 2;
218
219 zone_page_state_add(*p + overstep, zone, item);
220 *p = -overstep;
Christoph Lameter2244b952006-06-30 01:55:33 -0700221 }
222}
Christoph Lameterca889e62006-06-30 01:55:44 -0700223
224void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
225{
226 __inc_zone_state(page_zone(page), item);
227}
Christoph Lameter2244b952006-06-30 01:55:33 -0700228EXPORT_SYMBOL(__inc_zone_page_state);
229
Christoph Lameterc8785382007-02-10 01:43:01 -0800230void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
Christoph Lameter2244b952006-06-30 01:55:33 -0700231{
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700232 struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
233 s8 *p = pcp->vm_stat_diff + item;
Christoph Lameter2244b952006-06-30 01:55:33 -0700234
235 (*p)--;
236
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700237 if (unlikely(*p < - pcp->stat_threshold)) {
238 int overstep = pcp->stat_threshold / 2;
239
240 zone_page_state_add(*p - overstep, zone, item);
241 *p = overstep;
Christoph Lameter2244b952006-06-30 01:55:33 -0700242 }
243}
Christoph Lameterc8785382007-02-10 01:43:01 -0800244
245void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
246{
247 __dec_zone_state(page_zone(page), item);
248}
Christoph Lameter2244b952006-06-30 01:55:33 -0700249EXPORT_SYMBOL(__dec_zone_page_state);
250
Christoph Lameterca889e62006-06-30 01:55:44 -0700251void inc_zone_state(struct zone *zone, enum zone_stat_item item)
252{
253 unsigned long flags;
254
255 local_irq_save(flags);
256 __inc_zone_state(zone, item);
257 local_irq_restore(flags);
258}
259
Christoph Lameter2244b952006-06-30 01:55:33 -0700260void inc_zone_page_state(struct page *page, enum zone_stat_item item)
261{
262 unsigned long flags;
263 struct zone *zone;
Christoph Lameter2244b952006-06-30 01:55:33 -0700264
265 zone = page_zone(page);
266 local_irq_save(flags);
Christoph Lameterca889e62006-06-30 01:55:44 -0700267 __inc_zone_state(zone, item);
Christoph Lameter2244b952006-06-30 01:55:33 -0700268 local_irq_restore(flags);
269}
270EXPORT_SYMBOL(inc_zone_page_state);
271
272void dec_zone_page_state(struct page *page, enum zone_stat_item item)
273{
274 unsigned long flags;
Christoph Lameter2244b952006-06-30 01:55:33 -0700275
Christoph Lameter2244b952006-06-30 01:55:33 -0700276 local_irq_save(flags);
Christoph Lametera302eb42006-08-31 21:27:34 -0700277 __dec_zone_page_state(page, item);
Christoph Lameter2244b952006-06-30 01:55:33 -0700278 local_irq_restore(flags);
279}
280EXPORT_SYMBOL(dec_zone_page_state);
281
282/*
283 * Update the zone counters for one cpu.
284 */
285void refresh_cpu_vm_stats(int cpu)
286{
287 struct zone *zone;
288 int i;
289 unsigned long flags;
290
291 for_each_zone(zone) {
292 struct per_cpu_pageset *pcp;
293
Christoph Lameter39bbcb82006-09-25 23:31:49 -0700294 if (!populated_zone(zone))
295 continue;
296
Christoph Lameter2244b952006-06-30 01:55:33 -0700297 pcp = zone_pcp(zone, cpu);
298
299 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
300 if (pcp->vm_stat_diff[i]) {
301 local_irq_save(flags);
302 zone_page_state_add(pcp->vm_stat_diff[i],
303 zone, i);
304 pcp->vm_stat_diff[i] = 0;
305 local_irq_restore(flags);
306 }
307 }
308}
309
310static void __refresh_cpu_vm_stats(void *dummy)
311{
312 refresh_cpu_vm_stats(smp_processor_id());
313}
314
315/*
316 * Consolidate all counters.
317 *
318 * Note that the result is less inaccurate but still inaccurate
319 * if concurrent processes are allowed to run.
320 */
321void refresh_vm_stats(void)
322{
323 on_each_cpu(__refresh_cpu_vm_stats, NULL, 0, 1);
324}
325EXPORT_SYMBOL(refresh_vm_stats);
326
327#endif
328
Christoph Lameterca889e62006-06-30 01:55:44 -0700329#ifdef CONFIG_NUMA
330/*
331 * zonelist = the list of zones passed to the allocator
332 * z = the zone from which the allocation occurred.
333 *
334 * Must be called with interrupts disabled.
335 */
336void zone_statistics(struct zonelist *zonelist, struct zone *z)
337{
338 if (z->zone_pgdat == zonelist->zones[0]->zone_pgdat) {
339 __inc_zone_state(z, NUMA_HIT);
340 } else {
341 __inc_zone_state(z, NUMA_MISS);
342 __inc_zone_state(zonelist->zones[0], NUMA_FOREIGN);
343 }
Christoph Lameter5d292342006-09-27 01:50:10 -0700344 if (z->node == numa_node_id())
Christoph Lameterca889e62006-06-30 01:55:44 -0700345 __inc_zone_state(z, NUMA_LOCAL);
346 else
347 __inc_zone_state(z, NUMA_OTHER);
348}
349#endif
350
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700351#ifdef CONFIG_PROC_FS
352
353#include <linux/seq_file.h>
354
355static void *frag_start(struct seq_file *m, loff_t *pos)
356{
357 pg_data_t *pgdat;
358 loff_t node = *pos;
359 for (pgdat = first_online_pgdat();
360 pgdat && node;
361 pgdat = next_online_pgdat(pgdat))
362 --node;
363
364 return pgdat;
365}
366
367static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
368{
369 pg_data_t *pgdat = (pg_data_t *)arg;
370
371 (*pos)++;
372 return next_online_pgdat(pgdat);
373}
374
375static void frag_stop(struct seq_file *m, void *arg)
376{
377}
378
379/*
380 * This walks the free areas for each zone.
381 */
382static int frag_show(struct seq_file *m, void *arg)
383{
384 pg_data_t *pgdat = (pg_data_t *)arg;
385 struct zone *zone;
386 struct zone *node_zones = pgdat->node_zones;
387 unsigned long flags;
388 int order;
389
390 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
391 if (!populated_zone(zone))
392 continue;
393
394 spin_lock_irqsave(&zone->lock, flags);
395 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
396 for (order = 0; order < MAX_ORDER; ++order)
397 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
398 spin_unlock_irqrestore(&zone->lock, flags);
399 seq_putc(m, '\n');
400 }
401 return 0;
402}
403
Helge Deller15ad7cd2006-12-06 20:40:36 -0800404const struct seq_operations fragmentation_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700405 .start = frag_start,
406 .next = frag_next,
407 .stop = frag_stop,
408 .show = frag_show,
409};
410
Christoph Lameter4b51d662007-02-10 01:43:10 -0800411#ifdef CONFIG_ZONE_DMA
412#define TEXT_FOR_DMA(xx) xx "_dma",
413#else
414#define TEXT_FOR_DMA(xx)
415#endif
416
Christoph Lameter27bf71c2006-09-25 23:31:15 -0700417#ifdef CONFIG_ZONE_DMA32
418#define TEXT_FOR_DMA32(xx) xx "_dma32",
419#else
420#define TEXT_FOR_DMA32(xx)
421#endif
422
423#ifdef CONFIG_HIGHMEM
424#define TEXT_FOR_HIGHMEM(xx) xx "_high",
425#else
426#define TEXT_FOR_HIGHMEM(xx)
427#endif
428
Christoph Lameter4b51d662007-02-10 01:43:10 -0800429#define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
Christoph Lameter27bf71c2006-09-25 23:31:15 -0700430 TEXT_FOR_HIGHMEM(xx)
431
Helge Deller15ad7cd2006-12-06 20:40:36 -0800432static const char * const vmstat_text[] = {
Christoph Lameter2244b952006-06-30 01:55:33 -0700433 /* Zoned VM counters */
Christoph Lameterd23ad422007-02-10 01:43:02 -0800434 "nr_free_pages",
Christoph Lameterc8785382007-02-10 01:43:01 -0800435 "nr_active",
436 "nr_inactive",
Christoph Lameterf3dbd342006-06-30 01:55:36 -0700437 "nr_anon_pages",
Christoph Lameter65ba55f2006-06-30 01:55:34 -0700438 "nr_mapped",
Christoph Lameter347ce432006-06-30 01:55:35 -0700439 "nr_file_pages",
Christoph Lameter51ed4492007-02-10 01:43:02 -0800440 "nr_dirty",
441 "nr_writeback",
Christoph Lameter972d1a72006-09-25 23:31:51 -0700442 "nr_slab_reclaimable",
443 "nr_slab_unreclaimable",
Christoph Lameterdf849a12006-06-30 01:55:38 -0700444 "nr_page_table_pages",
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700445 "nr_unstable",
Christoph Lameterd2c5e302006-06-30 01:55:41 -0700446 "nr_bounce",
Andrew Mortone129b5c2006-09-27 01:50:00 -0700447 "nr_vmscan_write",
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700448
Christoph Lameterca889e62006-06-30 01:55:44 -0700449#ifdef CONFIG_NUMA
450 "numa_hit",
451 "numa_miss",
452 "numa_foreign",
453 "numa_interleave",
454 "numa_local",
455 "numa_other",
456#endif
457
Christoph Lameterf8891e52006-06-30 01:55:45 -0700458#ifdef CONFIG_VM_EVENT_COUNTERS
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700459 "pgpgin",
460 "pgpgout",
461 "pswpin",
462 "pswpout",
463
Christoph Lameter27bf71c2006-09-25 23:31:15 -0700464 TEXTS_FOR_ZONES("pgalloc")
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700465
466 "pgfree",
467 "pgactivate",
468 "pgdeactivate",
469
470 "pgfault",
471 "pgmajfault",
472
Christoph Lameter27bf71c2006-09-25 23:31:15 -0700473 TEXTS_FOR_ZONES("pgrefill")
474 TEXTS_FOR_ZONES("pgsteal")
475 TEXTS_FOR_ZONES("pgscan_kswapd")
476 TEXTS_FOR_ZONES("pgscan_direct")
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700477
478 "pginodesteal",
479 "slabs_scanned",
480 "kswapd_steal",
481 "kswapd_inodesteal",
482 "pageoutrun",
483 "allocstall",
484
485 "pgrotated",
Christoph Lameterf8891e52006-06-30 01:55:45 -0700486#endif
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700487};
488
489/*
490 * Output information about zones in @pgdat.
491 */
492static int zoneinfo_show(struct seq_file *m, void *arg)
493{
494 pg_data_t *pgdat = arg;
495 struct zone *zone;
496 struct zone *node_zones = pgdat->node_zones;
497 unsigned long flags;
498
499 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
500 int i;
501
502 if (!populated_zone(zone))
503 continue;
504
505 spin_lock_irqsave(&zone->lock, flags);
506 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
507 seq_printf(m,
508 "\n pages free %lu"
509 "\n min %lu"
510 "\n low %lu"
511 "\n high %lu"
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700512 "\n scanned %lu (a: %lu i: %lu)"
513 "\n spanned %lu"
514 "\n present %lu",
Christoph Lameterd23ad422007-02-10 01:43:02 -0800515 zone_page_state(zone, NR_FREE_PAGES),
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700516 zone->pages_min,
517 zone->pages_low,
518 zone->pages_high,
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700519 zone->pages_scanned,
520 zone->nr_scan_active, zone->nr_scan_inactive,
521 zone->spanned_pages,
522 zone->present_pages);
Christoph Lameter2244b952006-06-30 01:55:33 -0700523
524 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
525 seq_printf(m, "\n %-12s %lu", vmstat_text[i],
526 zone_page_state(zone, i));
527
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700528 seq_printf(m,
529 "\n protection: (%lu",
530 zone->lowmem_reserve[0]);
531 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
532 seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
533 seq_printf(m,
534 ")"
535 "\n pagesets");
536 for_each_online_cpu(i) {
537 struct per_cpu_pageset *pageset;
538 int j;
539
540 pageset = zone_pcp(zone, i);
541 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700542 seq_printf(m,
543 "\n cpu: %i pcp: %i"
544 "\n count: %i"
545 "\n high: %i"
546 "\n batch: %i",
547 i, j,
548 pageset->pcp[j].count,
549 pageset->pcp[j].high,
550 pageset->pcp[j].batch);
551 }
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700552#ifdef CONFIG_SMP
553 seq_printf(m, "\n vm stats threshold: %d",
554 pageset->stat_threshold);
555#endif
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700556 }
557 seq_printf(m,
558 "\n all_unreclaimable: %u"
559 "\n prev_priority: %i"
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700560 "\n start_pfn: %lu",
561 zone->all_unreclaimable,
562 zone->prev_priority,
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700563 zone->zone_start_pfn);
564 spin_unlock_irqrestore(&zone->lock, flags);
565 seq_putc(m, '\n');
566 }
567 return 0;
568}
569
Helge Deller15ad7cd2006-12-06 20:40:36 -0800570const struct seq_operations zoneinfo_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700571 .start = frag_start, /* iterate over all zones. The same as in
572 * fragmentation. */
573 .next = frag_next,
574 .stop = frag_stop,
575 .show = zoneinfo_show,
576};
577
578static void *vmstat_start(struct seq_file *m, loff_t *pos)
579{
Christoph Lameter2244b952006-06-30 01:55:33 -0700580 unsigned long *v;
Christoph Lameterf8891e52006-06-30 01:55:45 -0700581#ifdef CONFIG_VM_EVENT_COUNTERS
582 unsigned long *e;
583#endif
Christoph Lameter2244b952006-06-30 01:55:33 -0700584 int i;
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700585
586 if (*pos >= ARRAY_SIZE(vmstat_text))
587 return NULL;
588
Christoph Lameterf8891e52006-06-30 01:55:45 -0700589#ifdef CONFIG_VM_EVENT_COUNTERS
Christoph Lameter2244b952006-06-30 01:55:33 -0700590 v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long)
Christoph Lameterf8891e52006-06-30 01:55:45 -0700591 + sizeof(struct vm_event_state), GFP_KERNEL);
592#else
593 v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long),
594 GFP_KERNEL);
595#endif
Christoph Lameter2244b952006-06-30 01:55:33 -0700596 m->private = v;
597 if (!v)
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700598 return ERR_PTR(-ENOMEM);
Christoph Lameter2244b952006-06-30 01:55:33 -0700599 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
600 v[i] = global_page_state(i);
Christoph Lameterf8891e52006-06-30 01:55:45 -0700601#ifdef CONFIG_VM_EVENT_COUNTERS
602 e = v + NR_VM_ZONE_STAT_ITEMS;
603 all_vm_events(e);
604 e[PGPGIN] /= 2; /* sectors -> kbytes */
605 e[PGPGOUT] /= 2;
606#endif
Christoph Lameter2244b952006-06-30 01:55:33 -0700607 return v + *pos;
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700608}
609
610static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
611{
612 (*pos)++;
613 if (*pos >= ARRAY_SIZE(vmstat_text))
614 return NULL;
615 return (unsigned long *)m->private + *pos;
616}
617
618static int vmstat_show(struct seq_file *m, void *arg)
619{
620 unsigned long *l = arg;
621 unsigned long off = l - (unsigned long *)m->private;
622
623 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
624 return 0;
625}
626
627static void vmstat_stop(struct seq_file *m, void *arg)
628{
629 kfree(m->private);
630 m->private = NULL;
631}
632
Helge Deller15ad7cd2006-12-06 20:40:36 -0800633const struct seq_operations vmstat_op = {
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700634 .start = vmstat_start,
635 .next = vmstat_next,
636 .stop = vmstat_stop,
637 .show = vmstat_show,
638};
639
640#endif /* CONFIG_PROC_FS */
641
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700642#ifdef CONFIG_SMP
643/*
644 * Use the cpu notifier to insure that the thresholds are recalculated
645 * when necessary.
646 */
647static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
648 unsigned long action,
649 void *hcpu)
650{
651 switch (action) {
Andy Whitcroftce421c72006-12-06 20:33:08 -0800652 case CPU_UP_PREPARE:
653 case CPU_UP_CANCELED:
654 case CPU_DEAD:
655 refresh_zone_stat_thresholds();
656 break;
657 default:
658 break;
Christoph Lameterdf9ecab2006-08-31 21:27:35 -0700659 }
660 return NOTIFY_OK;
661}
662
663static struct notifier_block __cpuinitdata vmstat_notifier =
664 { &vmstat_cpuup_callback, NULL, 0 };
665
666int __init setup_vmstat(void)
667{
668 refresh_zone_stat_thresholds();
669 register_cpu_notifier(&vmstat_notifier);
670 return 0;
671}
672module_init(setup_vmstat)
673#endif