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, unsigned long *inactive,
17 unsigned long *free, struct pglist_data *pgdat)
19 *active = node_page_state(pgdat->node_id, NR_ACTIVE);
20 *inactive = node_page_state(pgdat->node_id, NR_INACTIVE);
21 *free = node_page_state(pgdat->node_id, NR_FREE_PAGES);
24 void get_zone_counts(unsigned long *active,
25 unsigned long *inactive, unsigned long *free)
27 *active = global_page_state(NR_ACTIVE);
28 *inactive = global_page_state(NR_INACTIVE);
29 *free = global_page_state(NR_FREE_PAGES);
32 #ifdef CONFIG_VM_EVENT_COUNTERS
33 DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
34 EXPORT_PER_CPU_SYMBOL(vm_event_states);
36 static void sum_vm_events(unsigned long *ret, cpumask_t *cpumask)
41 memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
43 cpu = first_cpu(*cpumask);
44 while (cpu < NR_CPUS) {
45 struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
47 cpu = next_cpu(cpu, *cpumask);
50 prefetch(&per_cpu(vm_event_states, cpu));
53 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
54 ret[i] += this->event[i];
59 * Accumulate the vm event counters across all CPUs.
60 * The result is unavoidably approximate - it can change
61 * during and after execution of this function.
63 void all_vm_events(unsigned long *ret)
65 sum_vm_events(ret, &cpu_online_map);
67 EXPORT_SYMBOL_GPL(all_vm_events);
71 * Fold the foreign cpu events into our own.
73 * This is adding to the events on one processor
74 * but keeps the global counts constant.
76 void vm_events_fold_cpu(int cpu)
78 struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu);
81 for (i = 0; i < NR_VM_EVENT_ITEMS; i++) {
82 count_vm_events(i, fold_state->event[i]);
83 fold_state->event[i] = 0;
86 #endif /* CONFIG_HOTPLUG */
88 #endif /* CONFIG_VM_EVENT_COUNTERS */
91 * Manage combined zone based / global counters
93 * vm_stat contains the global counters
95 atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
96 EXPORT_SYMBOL(vm_stat);
100 static int calculate_threshold(struct zone *zone)
103 int mem; /* memory in 128 MB units */
106 * The threshold scales with the number of processors and the amount
107 * of memory per zone. More memory means that we can defer updates for
108 * longer, more processors could lead to more contention.
109 * fls() is used to have a cheap way of logarithmic scaling.
111 * Some sample thresholds:
113 * Threshold Processors (fls) Zonesize fls(mem+1)
114 * ------------------------------------------------------------------
131 * 125 1024 10 8-16 GB 8
132 * 125 1024 10 16-32 GB 9
135 mem = zone->present_pages >> (27 - PAGE_SHIFT);
137 threshold = 2 * fls(num_online_cpus()) * (1 + fls(mem));
140 * Maximum threshold is 125
142 threshold = min(125, threshold);
148 * Refresh the thresholds for each zone.
150 static void refresh_zone_stat_thresholds(void)
156 for_each_zone(zone) {
158 if (!zone->present_pages)
161 threshold = calculate_threshold(zone);
163 for_each_online_cpu(cpu)
164 zone_pcp(zone, cpu)->stat_threshold = threshold;
169 * For use when we know that interrupts are disabled.
171 void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
174 struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
175 s8 *p = pcp->vm_stat_diff + item;
180 if (unlikely(x > pcp->stat_threshold || x < -pcp->stat_threshold)) {
181 zone_page_state_add(x, zone, item);
186 EXPORT_SYMBOL(__mod_zone_page_state);
189 * For an unknown interrupt state
191 void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
196 local_irq_save(flags);
197 __mod_zone_page_state(zone, item, delta);
198 local_irq_restore(flags);
200 EXPORT_SYMBOL(mod_zone_page_state);
203 * Optimized increment and decrement functions.
205 * These are only for a single page and therefore can take a struct page *
206 * argument instead of struct zone *. This allows the inclusion of the code
207 * generated for page_zone(page) into the optimized functions.
209 * No overflow check is necessary and therefore the differential can be
210 * incremented or decremented in place which may allow the compilers to
211 * generate better code.
212 * The increment or decrement is known and therefore one boundary check can
215 * NOTE: These functions are very performance sensitive. Change only
218 * Some processors have inc/dec instructions that are atomic vs an interrupt.
219 * However, the code must first determine the differential location in a zone
220 * based on the processor number and then inc/dec the counter. There is no
221 * guarantee without disabling preemption that the processor will not change
222 * in between and therefore the atomicity vs. interrupt cannot be exploited
223 * in a useful way here.
225 void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
227 struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
228 s8 *p = pcp->vm_stat_diff + item;
232 if (unlikely(*p > pcp->stat_threshold)) {
233 int overstep = pcp->stat_threshold / 2;
235 zone_page_state_add(*p + overstep, zone, item);
240 void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
242 __inc_zone_state(page_zone(page), item);
244 EXPORT_SYMBOL(__inc_zone_page_state);
246 void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
248 struct per_cpu_pageset *pcp = zone_pcp(zone, smp_processor_id());
249 s8 *p = pcp->vm_stat_diff + item;
253 if (unlikely(*p < - pcp->stat_threshold)) {
254 int overstep = pcp->stat_threshold / 2;
256 zone_page_state_add(*p - overstep, zone, item);
261 void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
263 __dec_zone_state(page_zone(page), item);
265 EXPORT_SYMBOL(__dec_zone_page_state);
267 void inc_zone_state(struct zone *zone, enum zone_stat_item item)
271 local_irq_save(flags);
272 __inc_zone_state(zone, item);
273 local_irq_restore(flags);
276 void inc_zone_page_state(struct page *page, enum zone_stat_item item)
281 zone = page_zone(page);
282 local_irq_save(flags);
283 __inc_zone_state(zone, item);
284 local_irq_restore(flags);
286 EXPORT_SYMBOL(inc_zone_page_state);
288 void dec_zone_page_state(struct page *page, enum zone_stat_item item)
292 local_irq_save(flags);
293 __dec_zone_page_state(page, item);
294 local_irq_restore(flags);
296 EXPORT_SYMBOL(dec_zone_page_state);
299 * Update the zone counters for one cpu.
301 void refresh_cpu_vm_stats(int cpu)
307 for_each_zone(zone) {
308 struct per_cpu_pageset *pcp;
310 if (!populated_zone(zone))
313 pcp = zone_pcp(zone, cpu);
315 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
316 if (pcp->vm_stat_diff[i]) {
317 local_irq_save(flags);
318 zone_page_state_add(pcp->vm_stat_diff[i],
320 pcp->vm_stat_diff[i] = 0;
321 local_irq_restore(flags);
326 static void __refresh_cpu_vm_stats(void *dummy)
328 refresh_cpu_vm_stats(smp_processor_id());
332 * Consolidate all counters.
334 * Note that the result is less inaccurate but still inaccurate
335 * if concurrent processes are allowed to run.
337 void refresh_vm_stats(void)
339 on_each_cpu(__refresh_cpu_vm_stats, NULL, 0, 1);
341 EXPORT_SYMBOL(refresh_vm_stats);
347 * zonelist = the list of zones passed to the allocator
348 * z = the zone from which the allocation occurred.
350 * Must be called with interrupts disabled.
352 void zone_statistics(struct zonelist *zonelist, struct zone *z)
354 if (z->zone_pgdat == zonelist->zones[0]->zone_pgdat) {
355 __inc_zone_state(z, NUMA_HIT);
357 __inc_zone_state(z, NUMA_MISS);
358 __inc_zone_state(zonelist->zones[0], NUMA_FOREIGN);
360 if (z->node == numa_node_id())
361 __inc_zone_state(z, NUMA_LOCAL);
363 __inc_zone_state(z, NUMA_OTHER);
367 #ifdef CONFIG_PROC_FS
369 #include <linux/seq_file.h>
371 static void *frag_start(struct seq_file *m, loff_t *pos)
375 for (pgdat = first_online_pgdat();
377 pgdat = next_online_pgdat(pgdat))
383 static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
385 pg_data_t *pgdat = (pg_data_t *)arg;
388 return next_online_pgdat(pgdat);
391 static void frag_stop(struct seq_file *m, void *arg)
396 * This walks the free areas for each zone.
398 static int frag_show(struct seq_file *m, void *arg)
400 pg_data_t *pgdat = (pg_data_t *)arg;
402 struct zone *node_zones = pgdat->node_zones;
406 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
407 if (!populated_zone(zone))
410 spin_lock_irqsave(&zone->lock, flags);
411 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
412 for (order = 0; order < MAX_ORDER; ++order)
413 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
414 spin_unlock_irqrestore(&zone->lock, flags);
420 const struct seq_operations fragmentation_op = {
427 #ifdef CONFIG_ZONE_DMA32
428 #define TEXT_FOR_DMA32(xx) xx "_dma32",
430 #define TEXT_FOR_DMA32(xx)
433 #ifdef CONFIG_HIGHMEM
434 #define TEXT_FOR_HIGHMEM(xx) xx "_high",
436 #define TEXT_FOR_HIGHMEM(xx)
439 #define TEXTS_FOR_ZONES(xx) xx "_dma", TEXT_FOR_DMA32(xx) xx "_normal", \
442 static const char * const vmstat_text[] = {
443 /* Zoned VM counters */
450 "nr_slab_reclaimable",
451 "nr_slab_unreclaimable",
452 "nr_page_table_pages",
468 #ifdef CONFIG_VM_EVENT_COUNTERS
474 TEXTS_FOR_ZONES("pgalloc")
483 TEXTS_FOR_ZONES("pgrefill")
484 TEXTS_FOR_ZONES("pgsteal")
485 TEXTS_FOR_ZONES("pgscan_kswapd")
486 TEXTS_FOR_ZONES("pgscan_direct")
500 * Output information about zones in @pgdat.
502 static int zoneinfo_show(struct seq_file *m, void *arg)
504 pg_data_t *pgdat = arg;
506 struct zone *node_zones = pgdat->node_zones;
509 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
512 if (!populated_zone(zone))
515 spin_lock_irqsave(&zone->lock, flags);
516 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
522 "\n scanned %lu (a: %lu i: %lu)"
525 zone_page_state(zone, NR_FREE_PAGES),
530 zone->nr_scan_active, zone->nr_scan_inactive,
532 zone->present_pages);
534 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
535 seq_printf(m, "\n %-12s %lu", vmstat_text[i],
536 zone_page_state(zone, i));
539 "\n protection: (%lu",
540 zone->lowmem_reserve[0]);
541 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
542 seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
546 for_each_online_cpu(i) {
547 struct per_cpu_pageset *pageset;
550 pageset = zone_pcp(zone, i);
551 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
558 pageset->pcp[j].count,
559 pageset->pcp[j].high,
560 pageset->pcp[j].batch);
563 seq_printf(m, "\n vm stats threshold: %d",
564 pageset->stat_threshold);
568 "\n all_unreclaimable: %u"
569 "\n prev_priority: %i"
571 zone->all_unreclaimable,
573 zone->zone_start_pfn);
574 spin_unlock_irqrestore(&zone->lock, flags);
580 const struct seq_operations zoneinfo_op = {
581 .start = frag_start, /* iterate over all zones. The same as in
585 .show = zoneinfo_show,
588 static void *vmstat_start(struct seq_file *m, loff_t *pos)
591 #ifdef CONFIG_VM_EVENT_COUNTERS
596 if (*pos >= ARRAY_SIZE(vmstat_text))
599 #ifdef CONFIG_VM_EVENT_COUNTERS
600 v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long)
601 + sizeof(struct vm_event_state), GFP_KERNEL);
603 v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long),
608 return ERR_PTR(-ENOMEM);
609 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
610 v[i] = global_page_state(i);
611 #ifdef CONFIG_VM_EVENT_COUNTERS
612 e = v + NR_VM_ZONE_STAT_ITEMS;
614 e[PGPGIN] /= 2; /* sectors -> kbytes */
620 static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
623 if (*pos >= ARRAY_SIZE(vmstat_text))
625 return (unsigned long *)m->private + *pos;
628 static int vmstat_show(struct seq_file *m, void *arg)
630 unsigned long *l = arg;
631 unsigned long off = l - (unsigned long *)m->private;
633 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
637 static void vmstat_stop(struct seq_file *m, void *arg)
643 const struct seq_operations vmstat_op = {
644 .start = vmstat_start,
650 #endif /* CONFIG_PROC_FS */
654 * Use the cpu notifier to insure that the thresholds are recalculated
657 static int __cpuinit vmstat_cpuup_callback(struct notifier_block *nfb,
658 unsigned long action,
663 case CPU_UP_CANCELED:
665 refresh_zone_stat_thresholds();
673 static struct notifier_block __cpuinitdata vmstat_notifier =
674 { &vmstat_cpuup_callback, NULL, 0 };
676 int __init setup_vmstat(void)
678 refresh_zone_stat_thresholds();
679 register_cpu_notifier(&vmstat_notifier);
682 module_init(setup_vmstat)