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 #ifdef CONFIG_VM_EVENT_COUNTERS
17 DEFINE_PER_CPU(struct vm_event_state
, vm_event_states
) = {{0}};
18 EXPORT_PER_CPU_SYMBOL(vm_event_states
);
20 static void sum_vm_events(unsigned long *ret
, cpumask_t
*cpumask
)
25 memset(ret
, 0, NR_VM_EVENT_ITEMS
* sizeof(unsigned long));
27 cpu
= first_cpu(*cpumask
);
28 while (cpu
< NR_CPUS
) {
29 struct vm_event_state
*this = &per_cpu(vm_event_states
, cpu
);
31 cpu
= next_cpu(cpu
, *cpumask
);
34 prefetch(&per_cpu(vm_event_states
, cpu
));
37 for (i
= 0; i
< NR_VM_EVENT_ITEMS
; i
++)
38 ret
[i
] += this->event
[i
];
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.
47 void all_vm_events(unsigned long *ret
)
49 sum_vm_events(ret
, &cpu_online_map
);
51 EXPORT_SYMBOL_GPL(all_vm_events
);
55 * Fold the foreign cpu events into our own.
57 * This is adding to the events on one processor
58 * but keeps the global counts constant.
60 void vm_events_fold_cpu(int cpu
)
62 struct vm_event_state
*fold_state
= &per_cpu(vm_event_states
, cpu
);
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;
70 #endif /* CONFIG_HOTPLUG */
72 #endif /* CONFIG_VM_EVENT_COUNTERS */
75 * Manage combined zone based / global counters
77 * vm_stat contains the global counters
79 atomic_long_t vm_stat
[NR_VM_ZONE_STAT_ITEMS
];
80 EXPORT_SYMBOL(vm_stat
);
84 static int calculate_threshold(struct zone
*zone
)
87 int mem
; /* memory in 128 MB units */
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.
95 * Some sample thresholds:
97 * Threshold Processors (fls) Zonesize fls(mem+1)
98 * ------------------------------------------------------------------
115 * 125 1024 10 8-16 GB 8
116 * 125 1024 10 16-32 GB 9
119 mem
= zone
->present_pages
>> (27 - PAGE_SHIFT
);
121 threshold
= 2 * fls(num_online_cpus()) * (1 + fls(mem
));
124 * Maximum threshold is 125
126 threshold
= min(125, threshold
);
132 * Refresh the thresholds for each zone.
134 static void refresh_zone_stat_thresholds(void)
140 for_each_zone(zone
) {
142 if (!zone
->present_pages
)
145 threshold
= calculate_threshold(zone
);
147 for_each_online_cpu(cpu
)
148 zone_pcp(zone
, cpu
)->stat_threshold
= threshold
;
153 * For use when we know that interrupts are disabled.
155 void __mod_zone_page_state(struct zone
*zone
, enum zone_stat_item item
,
158 struct per_cpu_pageset
*pcp
= zone_pcp(zone
, smp_processor_id());
159 s8
*p
= pcp
->vm_stat_diff
+ item
;
164 if (unlikely(x
> pcp
->stat_threshold
|| x
< -pcp
->stat_threshold
)) {
165 zone_page_state_add(x
, zone
, item
);
170 EXPORT_SYMBOL(__mod_zone_page_state
);
173 * For an unknown interrupt state
175 void mod_zone_page_state(struct zone
*zone
, enum zone_stat_item item
,
180 local_irq_save(flags
);
181 __mod_zone_page_state(zone
, item
, delta
);
182 local_irq_restore(flags
);
184 EXPORT_SYMBOL(mod_zone_page_state
);
187 * Optimized increment and decrement functions.
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.
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.
196 * The increment or decrement is known and therefore one boundary check can
199 * NOTE: These functions are very performance sensitive. Change only
202 * 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.
209 void __inc_zone_state(struct zone
*zone
, enum zone_stat_item item
)
211 struct per_cpu_pageset
*pcp
= zone_pcp(zone
, smp_processor_id());
212 s8
*p
= pcp
->vm_stat_diff
+ item
;
216 if (unlikely(*p
> pcp
->stat_threshold
)) {
217 int overstep
= pcp
->stat_threshold
/ 2;
219 zone_page_state_add(*p
+ overstep
, zone
, item
);
224 void __inc_zone_page_state(struct page
*page
, enum zone_stat_item item
)
226 __inc_zone_state(page_zone(page
), item
);
228 EXPORT_SYMBOL(__inc_zone_page_state
);
230 void __dec_zone_state(struct zone
*zone
, enum zone_stat_item item
)
232 struct per_cpu_pageset
*pcp
= zone_pcp(zone
, smp_processor_id());
233 s8
*p
= pcp
->vm_stat_diff
+ item
;
237 if (unlikely(*p
< - pcp
->stat_threshold
)) {
238 int overstep
= pcp
->stat_threshold
/ 2;
240 zone_page_state_add(*p
- overstep
, zone
, item
);
245 void __dec_zone_page_state(struct page
*page
, enum zone_stat_item item
)
247 __dec_zone_state(page_zone(page
), item
);
249 EXPORT_SYMBOL(__dec_zone_page_state
);
251 void inc_zone_state(struct zone
*zone
, enum zone_stat_item item
)
255 local_irq_save(flags
);
256 __inc_zone_state(zone
, item
);
257 local_irq_restore(flags
);
260 void inc_zone_page_state(struct page
*page
, enum zone_stat_item item
)
265 zone
= page_zone(page
);
266 local_irq_save(flags
);
267 __inc_zone_state(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
)
276 local_irq_save(flags
);
277 __dec_zone_page_state(page
, item
);
278 local_irq_restore(flags
);
280 EXPORT_SYMBOL(dec_zone_page_state
);
283 * Update the zone counters for one cpu.
285 * Note that refresh_cpu_vm_stats strives to only access
286 * node local memory. The per cpu pagesets on remote zones are placed
287 * in the memory local to the processor using that pageset. So the
288 * loop over all zones will access a series of cachelines local to
291 * The call to zone_page_state_add updates the cachelines with the
292 * statistics in the remote zone struct as well as the global cachelines
293 * with the global counters. These could cause remote node cache line
294 * bouncing and will have to be only done when necessary.
296 void refresh_cpu_vm_stats(int cpu
)
302 for_each_zone(zone
) {
303 struct per_cpu_pageset
*p
;
305 if (!populated_zone(zone
))
308 p
= zone_pcp(zone
, cpu
);
310 for (i
= 0; i
< NR_VM_ZONE_STAT_ITEMS
; i
++)
311 if (p
->vm_stat_diff
[i
]) {
312 local_irq_save(flags
);
313 zone_page_state_add(p
->vm_stat_diff
[i
],
315 p
->vm_stat_diff
[i
] = 0;
317 /* 3 seconds idle till flush */
320 local_irq_restore(flags
);
324 * Deal with draining the remote pageset of this
327 * Check if there are pages remaining in this pageset
328 * if not then there is nothing to expire.
330 if (!p
->expire
|| (!p
->pcp
[0].count
&& !p
->pcp
[1].count
))
334 * We never drain zones local to this processor.
336 if (zone_to_nid(zone
) == numa_node_id()) {
346 drain_zone_pages(zone
, p
->pcp
+ 0);
349 drain_zone_pages(zone
, p
->pcp
+ 1);
354 static void __refresh_cpu_vm_stats(void *dummy
)
356 refresh_cpu_vm_stats(smp_processor_id());
360 * Consolidate all counters.
362 * Note that the result is less inaccurate but still inaccurate
363 * if concurrent processes are allowed to run.
365 void refresh_vm_stats(void)
367 on_each_cpu(__refresh_cpu_vm_stats
, NULL
, 0, 1);
369 EXPORT_SYMBOL(refresh_vm_stats
);
375 * zonelist = the list of zones passed to the allocator
376 * z = the zone from which the allocation occurred.
378 * Must be called with interrupts disabled.
380 void zone_statistics(struct zonelist
*zonelist
, struct zone
*z
)
382 if (z
->zone_pgdat
== zonelist
->zones
[0]->zone_pgdat
) {
383 __inc_zone_state(z
, NUMA_HIT
);
385 __inc_zone_state(z
, NUMA_MISS
);
386 __inc_zone_state(zonelist
->zones
[0], NUMA_FOREIGN
);
388 if (z
->node
== numa_node_id())
389 __inc_zone_state(z
, NUMA_LOCAL
);
391 __inc_zone_state(z
, NUMA_OTHER
);
395 #ifdef CONFIG_PROC_FS
397 #include <linux/seq_file.h>
399 static void *frag_start(struct seq_file
*m
, loff_t
*pos
)
403 for (pgdat
= first_online_pgdat();
405 pgdat
= next_online_pgdat(pgdat
))
411 static void *frag_next(struct seq_file
*m
, void *arg
, loff_t
*pos
)
413 pg_data_t
*pgdat
= (pg_data_t
*)arg
;
416 return next_online_pgdat(pgdat
);
419 static void frag_stop(struct seq_file
*m
, void *arg
)
424 * This walks the free areas for each zone.
426 static int frag_show(struct seq_file
*m
, void *arg
)
428 pg_data_t
*pgdat
= (pg_data_t
*)arg
;
430 struct zone
*node_zones
= pgdat
->node_zones
;
434 for (zone
= node_zones
; zone
- node_zones
< MAX_NR_ZONES
; ++zone
) {
435 if (!populated_zone(zone
))
438 spin_lock_irqsave(&zone
->lock
, flags
);
439 seq_printf(m
, "Node %d, zone %8s ", pgdat
->node_id
, zone
->name
);
440 for (order
= 0; order
< MAX_ORDER
; ++order
)
441 seq_printf(m
, "%6lu ", zone
->free_area
[order
].nr_free
);
442 spin_unlock_irqrestore(&zone
->lock
, flags
);
448 const struct seq_operations fragmentation_op
= {
455 #ifdef CONFIG_ZONE_DMA
456 #define TEXT_FOR_DMA(xx) xx "_dma",
458 #define TEXT_FOR_DMA(xx)
461 #ifdef CONFIG_ZONE_DMA32
462 #define TEXT_FOR_DMA32(xx) xx "_dma32",
464 #define TEXT_FOR_DMA32(xx)
467 #ifdef CONFIG_HIGHMEM
468 #define TEXT_FOR_HIGHMEM(xx) xx "_high",
470 #define TEXT_FOR_HIGHMEM(xx)
473 #define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
476 static const char * const vmstat_text
[] = {
477 /* Zoned VM counters */
486 "nr_slab_reclaimable",
487 "nr_slab_unreclaimable",
488 "nr_page_table_pages",
502 #ifdef CONFIG_VM_EVENT_COUNTERS
508 TEXTS_FOR_ZONES("pgalloc")
517 TEXTS_FOR_ZONES("pgrefill")
518 TEXTS_FOR_ZONES("pgsteal")
519 TEXTS_FOR_ZONES("pgscan_kswapd")
520 TEXTS_FOR_ZONES("pgscan_direct")
534 * Output information about zones in @pgdat.
536 static int zoneinfo_show(struct seq_file
*m
, void *arg
)
538 pg_data_t
*pgdat
= arg
;
540 struct zone
*node_zones
= pgdat
->node_zones
;
543 for (zone
= node_zones
; zone
- node_zones
< MAX_NR_ZONES
; zone
++) {
546 if (!populated_zone(zone
))
549 spin_lock_irqsave(&zone
->lock
, flags
);
550 seq_printf(m
, "Node %d, zone %8s", pgdat
->node_id
, zone
->name
);
556 "\n scanned %lu (a: %lu i: %lu)"
559 zone_page_state(zone
, NR_FREE_PAGES
),
564 zone
->nr_scan_active
, zone
->nr_scan_inactive
,
566 zone
->present_pages
);
568 for (i
= 0; i
< NR_VM_ZONE_STAT_ITEMS
; i
++)
569 seq_printf(m
, "\n %-12s %lu", vmstat_text
[i
],
570 zone_page_state(zone
, i
));
573 "\n protection: (%lu",
574 zone
->lowmem_reserve
[0]);
575 for (i
= 1; i
< ARRAY_SIZE(zone
->lowmem_reserve
); i
++)
576 seq_printf(m
, ", %lu", zone
->lowmem_reserve
[i
]);
580 for_each_online_cpu(i
) {
581 struct per_cpu_pageset
*pageset
;
584 pageset
= zone_pcp(zone
, i
);
585 for (j
= 0; j
< ARRAY_SIZE(pageset
->pcp
); j
++) {
592 pageset
->pcp
[j
].count
,
593 pageset
->pcp
[j
].high
,
594 pageset
->pcp
[j
].batch
);
597 seq_printf(m
, "\n vm stats threshold: %d",
598 pageset
->stat_threshold
);
602 "\n all_unreclaimable: %u"
603 "\n prev_priority: %i"
605 zone
->all_unreclaimable
,
607 zone
->zone_start_pfn
);
608 spin_unlock_irqrestore(&zone
->lock
, flags
);
614 const struct seq_operations zoneinfo_op
= {
615 .start
= frag_start
, /* iterate over all zones. The same as in
619 .show
= zoneinfo_show
,
622 static void *vmstat_start(struct seq_file
*m
, loff_t
*pos
)
625 #ifdef CONFIG_VM_EVENT_COUNTERS
630 if (*pos
>= ARRAY_SIZE(vmstat_text
))
633 #ifdef CONFIG_VM_EVENT_COUNTERS
634 v
= kmalloc(NR_VM_ZONE_STAT_ITEMS
* sizeof(unsigned long)
635 + sizeof(struct vm_event_state
), GFP_KERNEL
);
637 v
= kmalloc(NR_VM_ZONE_STAT_ITEMS
* sizeof(unsigned long),
642 return ERR_PTR(-ENOMEM
);
643 for (i
= 0; i
< NR_VM_ZONE_STAT_ITEMS
; i
++)
644 v
[i
] = global_page_state(i
);
645 #ifdef CONFIG_VM_EVENT_COUNTERS
646 e
= v
+ NR_VM_ZONE_STAT_ITEMS
;
648 e
[PGPGIN
] /= 2; /* sectors -> kbytes */
654 static void *vmstat_next(struct seq_file
*m
, void *arg
, loff_t
*pos
)
657 if (*pos
>= ARRAY_SIZE(vmstat_text
))
659 return (unsigned long *)m
->private + *pos
;
662 static int vmstat_show(struct seq_file
*m
, void *arg
)
664 unsigned long *l
= arg
;
665 unsigned long off
= l
- (unsigned long *)m
->private;
667 seq_printf(m
, "%s %lu\n", vmstat_text
[off
], *l
);
671 static void vmstat_stop(struct seq_file
*m
, void *arg
)
677 const struct seq_operations vmstat_op
= {
678 .start
= vmstat_start
,
684 #endif /* CONFIG_PROC_FS */
687 static DEFINE_PER_CPU(struct delayed_work
, vmstat_work
);
688 int sysctl_stat_interval __read_mostly
= HZ
;
690 static void vmstat_update(struct work_struct
*w
)
692 refresh_cpu_vm_stats(smp_processor_id());
693 schedule_delayed_work(&__get_cpu_var(vmstat_work
),
694 sysctl_stat_interval
);
697 static void __devinit
start_cpu_timer(int cpu
)
699 struct delayed_work
*vmstat_work
= &per_cpu(vmstat_work
, cpu
);
701 INIT_DELAYED_WORK_DEFERRABLE(vmstat_work
, vmstat_update
);
702 schedule_delayed_work_on(cpu
, vmstat_work
, HZ
+ cpu
);
706 * Use the cpu notifier to insure that the thresholds are recalculated
709 static int __cpuinit
vmstat_cpuup_callback(struct notifier_block
*nfb
,
710 unsigned long action
,
713 long cpu
= (long)hcpu
;
717 case CPU_ONLINE_FROZEN
:
718 start_cpu_timer(cpu
);
720 case CPU_DOWN_PREPARE
:
721 case CPU_DOWN_PREPARE_FROZEN
:
722 cancel_rearming_delayed_work(&per_cpu(vmstat_work
, cpu
));
723 per_cpu(vmstat_work
, cpu
).work
.func
= NULL
;
725 case CPU_DOWN_FAILED
:
726 case CPU_DOWN_FAILED_FROZEN
:
727 start_cpu_timer(cpu
);
730 case CPU_DEAD_FROZEN
:
731 refresh_zone_stat_thresholds();
739 static struct notifier_block __cpuinitdata vmstat_notifier
=
740 { &vmstat_cpuup_callback
, NULL
, 0 };
742 int __init
setup_vmstat(void)
746 refresh_zone_stat_thresholds();
747 register_cpu_notifier(&vmstat_notifier
);
749 for_each_online_cpu(cpu
)
750 start_cpu_timer(cpu
);
753 module_init(setup_vmstat
)