4 * Manages VM statistics
5 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
8 * Copyright (C) 2006 Silicon Graphics, Inc.,
12 #include <linux/config.h>
14 #include <linux/module.h>
17 * Accumulate the page_state information across all CPUs.
18 * The result is unavoidably approximate - it can change
19 * during and after execution of this function.
21 DEFINE_PER_CPU(struct page_state, page_states) = {0};
23 static void __get_page_state(struct page_state *ret, int nr, cpumask_t *cpumask)
27 memset(ret, 0, nr * sizeof(unsigned long));
28 cpus_and(*cpumask, *cpumask, cpu_online_map);
30 for_each_cpu_mask(cpu, *cpumask) {
36 in = (unsigned long *)&per_cpu(page_states, cpu);
38 next_cpu = next_cpu(cpu, *cpumask);
39 if (likely(next_cpu < NR_CPUS))
40 prefetch(&per_cpu(page_states, next_cpu));
42 out = (unsigned long *)ret;
43 for (off = 0; off < nr; off++)
48 void get_page_state_node(struct page_state *ret, int node)
51 cpumask_t mask = node_to_cpumask(node);
53 nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
54 nr /= sizeof(unsigned long);
56 __get_page_state(ret, nr+1, &mask);
59 void get_page_state(struct page_state *ret)
62 cpumask_t mask = CPU_MASK_ALL;
64 nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
65 nr /= sizeof(unsigned long);
67 __get_page_state(ret, nr + 1, &mask);
70 void get_full_page_state(struct page_state *ret)
72 cpumask_t mask = CPU_MASK_ALL;
74 __get_page_state(ret, sizeof(*ret) / sizeof(unsigned long), &mask);
77 unsigned long read_page_state_offset(unsigned long offset)
79 unsigned long ret = 0;
82 for_each_online_cpu(cpu) {
85 in = (unsigned long)&per_cpu(page_states, cpu) + offset;
86 ret += *((unsigned long *)in);
91 void __mod_page_state_offset(unsigned long offset, unsigned long delta)
95 ptr = &__get_cpu_var(page_states);
96 *(unsigned long *)(ptr + offset) += delta;
98 EXPORT_SYMBOL(__mod_page_state_offset);
100 void mod_page_state_offset(unsigned long offset, unsigned long delta)
105 local_irq_save(flags);
106 ptr = &__get_cpu_var(page_states);
107 *(unsigned long *)(ptr + offset) += delta;
108 local_irq_restore(flags);
110 EXPORT_SYMBOL(mod_page_state_offset);
112 void __get_zone_counts(unsigned long *active, unsigned long *inactive,
113 unsigned long *free, struct pglist_data *pgdat)
115 struct zone *zones = pgdat->node_zones;
121 for (i = 0; i < MAX_NR_ZONES; i++) {
122 *active += zones[i].nr_active;
123 *inactive += zones[i].nr_inactive;
124 *free += zones[i].free_pages;
128 void get_zone_counts(unsigned long *active,
129 unsigned long *inactive, unsigned long *free)
131 struct pglist_data *pgdat;
136 for_each_online_pgdat(pgdat) {
137 unsigned long l, m, n;
138 __get_zone_counts(&l, &m, &n, pgdat);
146 * Manage combined zone based / global counters
148 * vm_stat contains the global counters
150 atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
151 EXPORT_SYMBOL(vm_stat);
155 #define STAT_THRESHOLD 32
158 * Determine pointer to currently valid differential byte given a zone and
161 * Preemption must be off
163 static inline s8 *diff_pointer(struct zone *zone, enum zone_stat_item item)
165 return &zone_pcp(zone, smp_processor_id())->vm_stat_diff[item];
169 * For use when we know that interrupts are disabled.
171 void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
177 p = diff_pointer(zone, item);
180 if (unlikely(x > STAT_THRESHOLD || x < -STAT_THRESHOLD)) {
181 zone_page_state_add(x, zone, item);
187 EXPORT_SYMBOL(__mod_zone_page_state);
190 * For an unknown interrupt state
192 void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
197 local_irq_save(flags);
198 __mod_zone_page_state(zone, item, delta);
199 local_irq_restore(flags);
201 EXPORT_SYMBOL(mod_zone_page_state);
204 * Optimized increment and decrement functions.
206 * These are only for a single page and therefore can take a struct page *
207 * argument instead of struct zone *. This allows the inclusion of the code
208 * generated for page_zone(page) into the optimized functions.
210 * No overflow check is necessary and therefore the differential can be
211 * incremented or decremented in place which may allow the compilers to
212 * generate better code.
214 * The increment or decrement is known and therefore one boundary check can
217 * Some processors have inc/dec instructions that are atomic vs an interrupt.
218 * However, the code must first determine the differential location in a zone
219 * based on the processor number and then inc/dec the counter. There is no
220 * guarantee without disabling preemption that the processor will not change
221 * in between and therefore the atomicity vs. interrupt cannot be exploited
222 * in a useful way here.
224 void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
226 struct zone *zone = page_zone(page);
227 s8 *p = diff_pointer(zone, item);
231 if (unlikely(*p > STAT_THRESHOLD)) {
232 zone_page_state_add(*p, zone, item);
236 EXPORT_SYMBOL(__inc_zone_page_state);
238 void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
240 struct zone *zone = page_zone(page);
241 s8 *p = diff_pointer(zone, item);
245 if (unlikely(*p < -STAT_THRESHOLD)) {
246 zone_page_state_add(*p, zone, item);
250 EXPORT_SYMBOL(__dec_zone_page_state);
252 void inc_zone_page_state(struct page *page, enum zone_stat_item item)
258 zone = page_zone(page);
259 local_irq_save(flags);
260 p = diff_pointer(zone, item);
264 if (unlikely(*p > STAT_THRESHOLD)) {
265 zone_page_state_add(*p, zone, item);
268 local_irq_restore(flags);
270 EXPORT_SYMBOL(inc_zone_page_state);
272 void dec_zone_page_state(struct page *page, enum zone_stat_item item)
278 zone = page_zone(page);
279 local_irq_save(flags);
280 p = diff_pointer(zone, item);
284 if (unlikely(*p < -STAT_THRESHOLD)) {
285 zone_page_state_add(*p, zone, item);
288 local_irq_restore(flags);
290 EXPORT_SYMBOL(dec_zone_page_state);
293 * Update the zone counters for one cpu.
295 void refresh_cpu_vm_stats(int cpu)
301 for_each_zone(zone) {
302 struct per_cpu_pageset *pcp;
304 pcp = zone_pcp(zone, cpu);
306 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
307 if (pcp->vm_stat_diff[i]) {
308 local_irq_save(flags);
309 zone_page_state_add(pcp->vm_stat_diff[i],
311 pcp->vm_stat_diff[i] = 0;
312 local_irq_restore(flags);
317 static void __refresh_cpu_vm_stats(void *dummy)
319 refresh_cpu_vm_stats(smp_processor_id());
323 * Consolidate all counters.
325 * Note that the result is less inaccurate but still inaccurate
326 * if concurrent processes are allowed to run.
328 void refresh_vm_stats(void)
330 on_each_cpu(__refresh_cpu_vm_stats, NULL, 0, 1);
332 EXPORT_SYMBOL(refresh_vm_stats);
336 #ifdef CONFIG_PROC_FS
338 #include <linux/seq_file.h>
340 static void *frag_start(struct seq_file *m, loff_t *pos)
344 for (pgdat = first_online_pgdat();
346 pgdat = next_online_pgdat(pgdat))
352 static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
354 pg_data_t *pgdat = (pg_data_t *)arg;
357 return next_online_pgdat(pgdat);
360 static void frag_stop(struct seq_file *m, void *arg)
365 * This walks the free areas for each zone.
367 static int frag_show(struct seq_file *m, void *arg)
369 pg_data_t *pgdat = (pg_data_t *)arg;
371 struct zone *node_zones = pgdat->node_zones;
375 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
376 if (!populated_zone(zone))
379 spin_lock_irqsave(&zone->lock, flags);
380 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
381 for (order = 0; order < MAX_ORDER; ++order)
382 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
383 spin_unlock_irqrestore(&zone->lock, flags);
389 struct seq_operations fragmentation_op = {
396 static char *vmstat_text[] = {
397 /* Zoned VM counters */
407 "nr_page_table_pages",
436 "pgscan_kswapd_high",
437 "pgscan_kswapd_normal",
438 "pgscan_kswapd_dma32",
441 "pgscan_direct_high",
442 "pgscan_direct_normal",
443 "pgscan_direct_dma32",
458 * Output information about zones in @pgdat.
460 static int zoneinfo_show(struct seq_file *m, void *arg)
462 pg_data_t *pgdat = arg;
464 struct zone *node_zones = pgdat->node_zones;
467 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
470 if (!populated_zone(zone))
473 spin_lock_irqsave(&zone->lock, flags);
474 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
482 "\n scanned %lu (a: %lu i: %lu)"
492 zone->nr_scan_active, zone->nr_scan_inactive,
494 zone->present_pages);
496 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
497 seq_printf(m, "\n %-12s %lu", vmstat_text[i],
498 zone_page_state(zone, i));
501 "\n protection: (%lu",
502 zone->lowmem_reserve[0]);
503 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
504 seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
508 for_each_online_cpu(i) {
509 struct per_cpu_pageset *pageset;
512 pageset = zone_pcp(zone, i);
513 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
514 if (pageset->pcp[j].count)
517 if (j == ARRAY_SIZE(pageset->pcp))
519 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
526 pageset->pcp[j].count,
527 pageset->pcp[j].high,
528 pageset->pcp[j].batch);
534 "\n numa_foreign: %lu"
535 "\n interleave_hit: %lu"
537 "\n other_node: %lu",
540 pageset->numa_foreign,
541 pageset->interleave_hit,
543 pageset->other_node);
547 "\n all_unreclaimable: %u"
548 "\n prev_priority: %i"
549 "\n temp_priority: %i"
551 zone->all_unreclaimable,
554 zone->zone_start_pfn);
555 spin_unlock_irqrestore(&zone->lock, flags);
561 struct seq_operations zoneinfo_op = {
562 .start = frag_start, /* iterate over all zones. The same as in
566 .show = zoneinfo_show,
569 static void *vmstat_start(struct seq_file *m, loff_t *pos)
572 struct page_state *ps;
575 if (*pos >= ARRAY_SIZE(vmstat_text))
578 v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long)
579 + sizeof(*ps), GFP_KERNEL);
582 return ERR_PTR(-ENOMEM);
583 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
584 v[i] = global_page_state(i);
585 ps = (struct page_state *)(v + NR_VM_ZONE_STAT_ITEMS);
586 get_full_page_state(ps);
587 ps->pgpgin /= 2; /* sectors -> kbytes */
592 static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
595 if (*pos >= ARRAY_SIZE(vmstat_text))
597 return (unsigned long *)m->private + *pos;
600 static int vmstat_show(struct seq_file *m, void *arg)
602 unsigned long *l = arg;
603 unsigned long off = l - (unsigned long *)m->private;
605 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
609 static void vmstat_stop(struct seq_file *m, void *arg)
615 struct seq_operations vmstat_op = {
616 .start = vmstat_start,
622 #endif /* CONFIG_PROC_FS */