4 * Manages VM statistics
5 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
8 * Copyright (C) 2006 Silicon Graphics, Inc.,
9 * Christoph Lameter <christoph@lameter.com>
13 #include <linux/module.h>
14 #include <linux/cpu.h>
16 void get_zone_counts(unsigned long *active,
17 unsigned long *inactive, unsigned long *free)
19 *active = global_page_state(NR_ACTIVE);
20 *inactive = global_page_state(NR_INACTIVE);
21 *free = global_page_state(NR_FREE_PAGES);
24 #ifdef CONFIG_VM_EVENT_COUNTERS
25 DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
26 EXPORT_PER_CPU_SYMBOL(vm_event_states);
28 static void sum_vm_events(unsigned long *ret, cpumask_t *cpumask)
33 memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
35 cpu = first_cpu(*cpumask);
36 while (cpu < NR_CPUS) {
37 struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
39 cpu = next_cpu(cpu, *cpumask);
42 prefetch(&per_cpu(vm_event_states, cpu));
45 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
46 ret[i] += this->event[i];
51 * Accumulate the vm event counters across all CPUs.
52 * The result is unavoidably approximate - it can change
53 * during and after execution of this function.
55 void all_vm_events(unsigned long *ret)
57 sum_vm_events(ret, &cpu_online_map);
59 EXPORT_SYMBOL_GPL(all_vm_events);
63 * Fold the foreign cpu events into our own.
65 * This is adding to the events on one processor
66 * but keeps the global counts constant.
68 void vm_events_fold_cpu(int cpu)
70 struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu);
73 for (i = 0; i < NR_VM_EVENT_ITEMS; i++) {
74 count_vm_events(i, fold_state->event[i]);
75 fold_state->event[i] = 0;
78 #endif /* CONFIG_HOTPLUG */
80 #endif /* CONFIG_VM_EVENT_COUNTERS */
83 * Manage combined zone based / global counters
85 * vm_stat contains the global counters
87 atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
88 EXPORT_SYMBOL(vm_stat);
92 static int calculate_threshold(struct zone *zone)
95 int mem; /* memory in 128 MB units */
98 * The threshold scales with the number of processors and the amount
99 * of memory per zone. More memory means that we can defer updates for
100 * longer, more processors could lead to more contention.
101 * fls() is used to have a cheap way of logarithmic scaling.
103 * Some sample thresholds:
105 * Threshold Processors (fls) Zonesize fls(mem+1)
106 * ------------------------------------------------------------------
123 * 125 1024 10 8-16 GB 8
124 * 125 1024 10 16-32 GB 9
127 mem = zone->present_pages >> (27 - PAGE_SHIFT);
129 threshold = 2 * fls(num_online_cpus()) * (1 + fls(mem));
132 * Maximum threshold is 125
134 threshold = min(125, threshold);
140 * Refresh the thresholds for each zone.
142 static void refresh_zone_stat_thresholds(void)
148 for_each_zone(zone) {
150 if (!zone->present_pages)
153 threshold = calculate_threshold(zone);
155 for_each_online_cpu(cpu)
156 zone_pcp(zone, cpu)->stat_threshold = threshold;
161 * For use when we know that interrupts are disabled.
163 void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
166 struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
167 s8 *p = pcp->vm_stat_diff + item;
172 if (unlikely(x > pcp->stat_threshold || x < -pcp->stat_threshold)) {
173 zone_page_state_add(x, zone, item);
178 EXPORT_SYMBOL(__mod_zone_page_state);
181 * For an unknown interrupt state
183 void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
188 local_irq_save(flags);
189 __mod_zone_page_state(zone, item, delta);
190 local_irq_restore(flags);
192 EXPORT_SYMBOL(mod_zone_page_state);
195 * Optimized increment and decrement functions.
197 * These are only for a single page and therefore can take a struct page *
198 * argument instead of struct zone *. This allows the inclusion of the code
199 * generated for page_zone(page) into the optimized functions.
201 * No overflow check is necessary and therefore the differential can be
202 * incremented or decremented in place which may allow the compilers to
203 * generate better code.
204 * The increment or decrement is known and therefore one boundary check can
207 * NOTE: These functions are very performance sensitive. Change only
210 * Some processors have inc/dec instructions that are atomic vs an interrupt.
211 * However, the code must first determine the differential location in a zone
212 * based on the processor number and then inc/dec the counter. There is no
213 * guarantee without disabling preemption that the processor will not change
214 * in between and therefore the atomicity vs. interrupt cannot be exploited
215 * in a useful way here.
217 void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
219 struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
220 s8 *p = pcp->vm_stat_diff + item;
224 if (unlikely(*p > pcp->stat_threshold)) {
225 int overstep = pcp->stat_threshold / 2;
227 zone_page_state_add(*p + overstep, zone, item);
232 void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
234 __inc_zone_state(page_zone(page), item);
236 EXPORT_SYMBOL(__inc_zone_page_state);
238 void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
240 struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
241 s8 *p = pcp->vm_stat_diff + item;
245 if (unlikely(*p < - pcp->stat_threshold)) {
246 int overstep = pcp->stat_threshold / 2;
248 zone_page_state_add(*p - overstep, zone, item);
253 void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
255 __dec_zone_state(page_zone(page), item);
257 EXPORT_SYMBOL(__dec_zone_page_state);
259 void inc_zone_state(struct zone *zone, enum zone_stat_item item)
263 local_irq_save(flags);
264 __inc_zone_state(zone, item);
265 local_irq_restore(flags);
268 void inc_zone_page_state(struct page *page, enum zone_stat_item item)
273 zone = page_zone(page);
274 local_irq_save(flags);
275 __inc_zone_state(zone, item);
276 local_irq_restore(flags);
278 EXPORT_SYMBOL(inc_zone_page_state);
280 void dec_zone_page_state(struct page *page, enum zone_stat_item item)
284 local_irq_save(flags);
285 __dec_zone_page_state(page, item);
286 local_irq_restore(flags);
288 EXPORT_SYMBOL(dec_zone_page_state);
291 * Update the zone counters for one cpu.
293 void refresh_cpu_vm_stats(int cpu)
299 for_each_zone(zone) {
300 struct per_cpu_pageset *pcp;
302 if (!populated_zone(zone))
305 pcp = zone_pcp(zone, cpu);
307 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
308 if (pcp->vm_stat_diff[i]) {
309 local_irq_save(flags);
310 zone_page_state_add(pcp->vm_stat_diff[i],
312 pcp->vm_stat_diff[i] = 0;
313 local_irq_restore(flags);
318 static void __refresh_cpu_vm_stats(void *dummy)
320 refresh_cpu_vm_stats(smp_processor_id());
324 * Consolidate all counters.
326 * Note that the result is less inaccurate but still inaccurate
327 * if concurrent processes are allowed to run.
329 void refresh_vm_stats(void)
331 on_each_cpu(__refresh_cpu_vm_stats, NULL, 0, 1);
333 EXPORT_SYMBOL(refresh_vm_stats);
339 * zonelist = the list of zones passed to the allocator
340 * z = the zone from which the allocation occurred.
342 * Must be called with interrupts disabled.
344 void zone_statistics(struct zonelist *zonelist, struct zone *z)
346 if (z->zone_pgdat == zonelist->zones[0]->zone_pgdat) {
347 __inc_zone_state(z, NUMA_HIT);
349 __inc_zone_state(z, NUMA_MISS);
350 __inc_zone_state(zonelist->zones[0], NUMA_FOREIGN);
352 if (z->node == numa_node_id())
353 __inc_zone_state(z, NUMA_LOCAL);
355 __inc_zone_state(z, NUMA_OTHER);
359 #ifdef CONFIG_PROC_FS
361 #include <linux/seq_file.h>
363 static void *frag_start(struct seq_file *m, loff_t *pos)
367 for (pgdat = first_online_pgdat();
369 pgdat = next_online_pgdat(pgdat))
375 static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
377 pg_data_t *pgdat = (pg_data_t *)arg;
380 return next_online_pgdat(pgdat);
383 static void frag_stop(struct seq_file *m, void *arg)
388 * This walks the free areas for each zone.
390 static int frag_show(struct seq_file *m, void *arg)
392 pg_data_t *pgdat = (pg_data_t *)arg;
394 struct zone *node_zones = pgdat->node_zones;
398 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
399 if (!populated_zone(zone))
402 spin_lock_irqsave(&zone->lock, flags);
403 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
404 for (order = 0; order < MAX_ORDER; ++order)
405 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
406 spin_unlock_irqrestore(&zone->lock, flags);
412 const struct seq_operations fragmentation_op = {
419 #ifdef CONFIG_ZONE_DMA32
420 #define TEXT_FOR_DMA32(xx) xx "_dma32",
422 #define TEXT_FOR_DMA32(xx)
425 #ifdef CONFIG_HIGHMEM
426 #define TEXT_FOR_HIGHMEM(xx) xx "_high",
428 #define TEXT_FOR_HIGHMEM(xx)
431 #define TEXTS_FOR_ZONES(xx) xx "_dma", TEXT_FOR_DMA32(xx) xx "_normal", \
434 static const char * const vmstat_text[] = {
435 /* Zoned VM counters */
444 "nr_slab_reclaimable",
445 "nr_slab_unreclaimable",
446 "nr_page_table_pages",
460 #ifdef CONFIG_VM_EVENT_COUNTERS
466 TEXTS_FOR_ZONES("pgalloc")
475 TEXTS_FOR_ZONES("pgrefill")
476 TEXTS_FOR_ZONES("pgsteal")
477 TEXTS_FOR_ZONES("pgscan_kswapd")
478 TEXTS_FOR_ZONES("pgscan_direct")
492 * Output information about zones in @pgdat.
494 static int zoneinfo_show(struct seq_file *m, void *arg)
496 pg_data_t *pgdat = arg;
498 struct zone *node_zones = pgdat->node_zones;
501 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
504 if (!populated_zone(zone))
507 spin_lock_irqsave(&zone->lock, flags);
508 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
514 "\n scanned %lu (a: %lu i: %lu)"
517 zone_page_state(zone, NR_FREE_PAGES),
522 zone->nr_scan_active, zone->nr_scan_inactive,
524 zone->present_pages);
526 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
527 seq_printf(m, "\n %-12s %lu", vmstat_text[i],
528 zone_page_state(zone, i));
531 "\n protection: (%lu",
532 zone->lowmem_reserve[0]);
533 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
534 seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
538 for_each_online_cpu(i) {
539 struct per_cpu_pageset *pageset;
542 pageset = zone_pcp(zone, i);
543 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
550 pageset->pcp[j].count,
551 pageset->pcp[j].high,
552 pageset->pcp[j].batch);
555 seq_printf(m, "\n vm stats threshold: %d",
556 pageset->stat_threshold);
560 "\n all_unreclaimable: %u"
561 "\n prev_priority: %i"
563 zone->all_unreclaimable,
565 zone->zone_start_pfn);
566 spin_unlock_irqrestore(&zone->lock, flags);
572 const struct seq_operations zoneinfo_op = {
573 .start = frag_start, /* iterate over all zones. The same as in
577 .show = zoneinfo_show,
580 static void *vmstat_start(struct seq_file *m, loff_t *pos)
583 #ifdef CONFIG_VM_EVENT_COUNTERS
588 if (*pos >= ARRAY_SIZE(vmstat_text))
591 #ifdef CONFIG_VM_EVENT_COUNTERS
592 v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long)
593 + sizeof(struct vm_event_state), GFP_KERNEL);
595 v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long),
600 return ERR_PTR(-ENOMEM);
601 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
602 v[i] = global_page_state(i);
603 #ifdef CONFIG_VM_EVENT_COUNTERS
604 e = v + NR_VM_ZONE_STAT_ITEMS;
606 e[PGPGIN] /= 2; /* sectors -> kbytes */
612 static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
615 if (*pos >= ARRAY_SIZE(vmstat_text))
617 return (unsigned long *)m->private + *pos;
620 static int vmstat_show(struct seq_file *m, void *arg)
622 unsigned long *l = arg;
623 unsigned long off = l - (unsigned long *)m->private;
625 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
629 static void vmstat_stop(struct seq_file *m, void *arg)
635 const struct seq_operations vmstat_op = {
636 .start = vmstat_start,
642 #endif /* CONFIG_PROC_FS */
646 * Use the cpu notifier to insure that the thresholds are recalculated
649 static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
650 unsigned long action,
655 case CPU_UP_CANCELED:
657 refresh_zone_stat_thresholds();
665 static struct notifier_block __cpuinitdata vmstat_notifier =
666 { &vmstat_cpuup_callback, NULL, 0 };
668 int __init setup_vmstat(void)
670 refresh_zone_stat_thresholds();
671 register_cpu_notifier(&vmstat_notifier);
674 module_init(setup_vmstat)