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>
12 #include <linux/config.h>
14 #include <linux/module.h>
16 void __get_zone_counts(unsigned long *active, unsigned long *inactive,
17 unsigned long *free, struct pglist_data *pgdat)
19 struct zone *zones = pgdat->node_zones;
25 for (i = 0; i < MAX_NR_ZONES; i++) {
26 *active += zones[i].nr_active;
27 *inactive += zones[i].nr_inactive;
28 *free += zones[i].free_pages;
32 void get_zone_counts(unsigned long *active,
33 unsigned long *inactive, unsigned long *free)
35 struct pglist_data *pgdat;
40 for_each_online_pgdat(pgdat) {
41 unsigned long l, m, n;
42 __get_zone_counts(&l, &m, &n, pgdat);
49 #ifdef CONFIG_VM_EVENT_COUNTERS
50 DEFINE_PER_CPU(struct vm_event_state, vm_event_states) = {{0}};
51 EXPORT_PER_CPU_SYMBOL(vm_event_states);
53 static void sum_vm_events(unsigned long *ret, cpumask_t *cpumask)
58 memset(ret, 0, NR_VM_EVENT_ITEMS * sizeof(unsigned long));
60 cpu = first_cpu(*cpumask);
61 while (cpu < NR_CPUS) {
62 struct vm_event_state *this = &per_cpu(vm_event_states, cpu);
64 cpu = next_cpu(cpu, *cpumask);
67 prefetch(&per_cpu(vm_event_states, cpu));
70 for (i = 0; i < NR_VM_EVENT_ITEMS; i++)
71 ret[i] += this->event[i];
76 * Accumulate the vm event counters across all CPUs.
77 * The result is unavoidably approximate - it can change
78 * during and after execution of this function.
80 void all_vm_events(unsigned long *ret)
82 sum_vm_events(ret, &cpu_online_map);
87 * Fold the foreign cpu events into our own.
89 * This is adding to the events on one processor
90 * but keeps the global counts constant.
92 void vm_events_fold_cpu(int cpu)
94 struct vm_event_state *fold_state = &per_cpu(vm_event_states, cpu);
97 for (i = 0; i < NR_VM_EVENT_ITEMS; i++) {
98 count_vm_events(i, fold_state->event[i]);
99 fold_state->event[i] = 0;
102 #endif /* CONFIG_HOTPLUG */
104 #endif /* CONFIG_VM_EVENT_COUNTERS */
107 * Manage combined zone based / global counters
109 * vm_stat contains the global counters
111 atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
112 EXPORT_SYMBOL(vm_stat);
116 #define STAT_THRESHOLD 32
119 * Determine pointer to currently valid differential byte given a zone and
122 * Preemption must be off
124 static inline s8 *diff_pointer(struct zone *zone, enum zone_stat_item item)
126 return &zone_pcp(zone, smp_processor_id())->vm_stat_diff[item];
130 * For use when we know that interrupts are disabled.
132 void __mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
138 p = diff_pointer(zone, item);
141 if (unlikely(x > STAT_THRESHOLD || x < -STAT_THRESHOLD)) {
142 zone_page_state_add(x, zone, item);
148 EXPORT_SYMBOL(__mod_zone_page_state);
151 * For an unknown interrupt state
153 void mod_zone_page_state(struct zone *zone, enum zone_stat_item item,
158 local_irq_save(flags);
159 __mod_zone_page_state(zone, item, delta);
160 local_irq_restore(flags);
162 EXPORT_SYMBOL(mod_zone_page_state);
165 * Optimized increment and decrement functions.
167 * These are only for a single page and therefore can take a struct page *
168 * argument instead of struct zone *. This allows the inclusion of the code
169 * generated for page_zone(page) into the optimized functions.
171 * No overflow check is necessary and therefore the differential can be
172 * incremented or decremented in place which may allow the compilers to
173 * generate better code.
175 * The increment or decrement is known and therefore one boundary check can
178 * Some processors have inc/dec instructions that are atomic vs an interrupt.
179 * However, the code must first determine the differential location in a zone
180 * based on the processor number and then inc/dec the counter. There is no
181 * guarantee without disabling preemption that the processor will not change
182 * in between and therefore the atomicity vs. interrupt cannot be exploited
183 * in a useful way here.
185 static void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
187 s8 *p = diff_pointer(zone, item);
191 if (unlikely(*p > STAT_THRESHOLD)) {
192 zone_page_state_add(*p, zone, item);
197 void __inc_zone_page_state(struct page *page, enum zone_stat_item item)
199 __inc_zone_state(page_zone(page), item);
201 EXPORT_SYMBOL(__inc_zone_page_state);
203 void __dec_zone_page_state(struct page *page, enum zone_stat_item item)
205 struct zone *zone = page_zone(page);
206 s8 *p = diff_pointer(zone, item);
210 if (unlikely(*p < -STAT_THRESHOLD)) {
211 zone_page_state_add(*p, zone, item);
215 EXPORT_SYMBOL(__dec_zone_page_state);
217 void inc_zone_state(struct zone *zone, enum zone_stat_item item)
221 local_irq_save(flags);
222 __inc_zone_state(zone, item);
223 local_irq_restore(flags);
226 void inc_zone_page_state(struct page *page, enum zone_stat_item item)
231 zone = page_zone(page);
232 local_irq_save(flags);
233 __inc_zone_state(zone, item);
234 local_irq_restore(flags);
236 EXPORT_SYMBOL(inc_zone_page_state);
238 void dec_zone_page_state(struct page *page, enum zone_stat_item item)
244 zone = page_zone(page);
245 local_irq_save(flags);
246 p = diff_pointer(zone, item);
250 if (unlikely(*p < -STAT_THRESHOLD)) {
251 zone_page_state_add(*p, zone, item);
254 local_irq_restore(flags);
256 EXPORT_SYMBOL(dec_zone_page_state);
259 * Update the zone counters for one cpu.
261 void refresh_cpu_vm_stats(int cpu)
267 for_each_zone(zone) {
268 struct per_cpu_pageset *pcp;
270 pcp = zone_pcp(zone, cpu);
272 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
273 if (pcp->vm_stat_diff[i]) {
274 local_irq_save(flags);
275 zone_page_state_add(pcp->vm_stat_diff[i],
277 pcp->vm_stat_diff[i] = 0;
278 local_irq_restore(flags);
283 static void __refresh_cpu_vm_stats(void *dummy)
285 refresh_cpu_vm_stats(smp_processor_id());
289 * Consolidate all counters.
291 * Note that the result is less inaccurate but still inaccurate
292 * if concurrent processes are allowed to run.
294 void refresh_vm_stats(void)
296 on_each_cpu(__refresh_cpu_vm_stats, NULL, 0, 1);
298 EXPORT_SYMBOL(refresh_vm_stats);
304 * zonelist = the list of zones passed to the allocator
305 * z = the zone from which the allocation occurred.
307 * Must be called with interrupts disabled.
309 void zone_statistics(struct zonelist *zonelist, struct zone *z)
311 if (z->zone_pgdat == zonelist->zones[0]->zone_pgdat) {
312 __inc_zone_state(z, NUMA_HIT);
314 __inc_zone_state(z, NUMA_MISS);
315 __inc_zone_state(zonelist->zones[0], NUMA_FOREIGN);
317 if (z->zone_pgdat == NODE_DATA(numa_node_id()))
318 __inc_zone_state(z, NUMA_LOCAL);
320 __inc_zone_state(z, NUMA_OTHER);
324 #ifdef CONFIG_PROC_FS
326 #include <linux/seq_file.h>
328 static void *frag_start(struct seq_file *m, loff_t *pos)
332 for (pgdat = first_online_pgdat();
334 pgdat = next_online_pgdat(pgdat))
340 static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
342 pg_data_t *pgdat = (pg_data_t *)arg;
345 return next_online_pgdat(pgdat);
348 static void frag_stop(struct seq_file *m, void *arg)
353 * This walks the free areas for each zone.
355 static int frag_show(struct seq_file *m, void *arg)
357 pg_data_t *pgdat = (pg_data_t *)arg;
359 struct zone *node_zones = pgdat->node_zones;
363 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
364 if (!populated_zone(zone))
367 spin_lock_irqsave(&zone->lock, flags);
368 seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
369 for (order = 0; order < MAX_ORDER; ++order)
370 seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
371 spin_unlock_irqrestore(&zone->lock, flags);
377 struct seq_operations fragmentation_op = {
384 static char *vmstat_text[] = {
385 /* Zoned VM counters */
390 "nr_page_table_pages",
405 #ifdef CONFIG_VM_EVENT_COUNTERS
434 "pgscan_kswapd_dma32",
435 "pgscan_kswapd_normal",
436 "pgscan_kswapd_high",
439 "pgscan_direct_dma32",
440 "pgscan_direct_normal",
441 "pgscan_direct_high",
455 * Output information about zones in @pgdat.
457 static int zoneinfo_show(struct seq_file *m, void *arg)
459 pg_data_t *pgdat = arg;
461 struct zone *node_zones = pgdat->node_zones;
464 for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
467 if (!populated_zone(zone))
470 spin_lock_irqsave(&zone->lock, flags);
471 seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
479 "\n scanned %lu (a: %lu i: %lu)"
489 zone->nr_scan_active, zone->nr_scan_inactive,
491 zone->present_pages);
493 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
494 seq_printf(m, "\n %-12s %lu", vmstat_text[i],
495 zone_page_state(zone, i));
498 "\n protection: (%lu",
499 zone->lowmem_reserve[0]);
500 for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
501 seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
505 for_each_online_cpu(i) {
506 struct per_cpu_pageset *pageset;
509 pageset = zone_pcp(zone, i);
510 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
511 if (pageset->pcp[j].count)
514 if (j == ARRAY_SIZE(pageset->pcp))
516 for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
523 pageset->pcp[j].count,
524 pageset->pcp[j].high,
525 pageset->pcp[j].batch);
529 "\n all_unreclaimable: %u"
530 "\n prev_priority: %i"
531 "\n temp_priority: %i"
533 zone->all_unreclaimable,
536 zone->zone_start_pfn);
537 spin_unlock_irqrestore(&zone->lock, flags);
543 struct seq_operations zoneinfo_op = {
544 .start = frag_start, /* iterate over all zones. The same as in
548 .show = zoneinfo_show,
551 static void *vmstat_start(struct seq_file *m, loff_t *pos)
554 #ifdef CONFIG_VM_EVENT_COUNTERS
559 if (*pos >= ARRAY_SIZE(vmstat_text))
562 #ifdef CONFIG_VM_EVENT_COUNTERS
563 v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long)
564 + sizeof(struct vm_event_state), GFP_KERNEL);
566 v = kmalloc(NR_VM_ZONE_STAT_ITEMS * sizeof(unsigned long),
571 return ERR_PTR(-ENOMEM);
572 for (i = 0; i < NR_VM_ZONE_STAT_ITEMS; i++)
573 v[i] = global_page_state(i);
574 #ifdef CONFIG_VM_EVENT_COUNTERS
575 e = v + NR_VM_ZONE_STAT_ITEMS;
577 e[PGPGIN] /= 2; /* sectors -> kbytes */
583 static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
586 if (*pos >= ARRAY_SIZE(vmstat_text))
588 return (unsigned long *)m->private + *pos;
591 static int vmstat_show(struct seq_file *m, void *arg)
593 unsigned long *l = arg;
594 unsigned long off = l - (unsigned long *)m->private;
596 seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
600 static void vmstat_stop(struct seq_file *m, void *arg)
606 struct seq_operations vmstat_op = {
607 .start = vmstat_start,
613 #endif /* CONFIG_PROC_FS */