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>
15 #include <linux/sched.h>
17 #ifdef CONFIG_VM_EVENT_COUNTERS
18 DEFINE_PER_CPU(struct vm_event_state
, vm_event_states
) = {{0}};
19 EXPORT_PER_CPU_SYMBOL(vm_event_states
);
21 static void sum_vm_events(unsigned long *ret
, cpumask_t
*cpumask
)
26 memset(ret
, 0, NR_VM_EVENT_ITEMS
* sizeof(unsigned long));
28 cpu
= first_cpu(*cpumask
);
29 while (cpu
< NR_CPUS
) {
30 struct vm_event_state
*this = &per_cpu(vm_event_states
, cpu
);
32 cpu
= next_cpu(cpu
, *cpumask
);
35 prefetch(&per_cpu(vm_event_states
, cpu
));
38 for (i
= 0; i
< NR_VM_EVENT_ITEMS
; i
++)
39 ret
[i
] += this->event
[i
];
44 * Accumulate the vm event counters across all CPUs.
45 * The result is unavoidably approximate - it can change
46 * during and after execution of this function.
48 void all_vm_events(unsigned long *ret
)
50 sum_vm_events(ret
, &cpu_online_map
);
52 EXPORT_SYMBOL_GPL(all_vm_events
);
56 * Fold the foreign cpu events into our own.
58 * This is adding to the events on one processor
59 * but keeps the global counts constant.
61 void vm_events_fold_cpu(int cpu
)
63 struct vm_event_state
*fold_state
= &per_cpu(vm_event_states
, cpu
);
66 for (i
= 0; i
< NR_VM_EVENT_ITEMS
; i
++) {
67 count_vm_events(i
, fold_state
->event
[i
]);
68 fold_state
->event
[i
] = 0;
71 #endif /* CONFIG_HOTPLUG */
73 #endif /* CONFIG_VM_EVENT_COUNTERS */
76 * Manage combined zone based / global counters
78 * vm_stat contains the global counters
80 atomic_long_t vm_stat
[NR_VM_ZONE_STAT_ITEMS
];
81 EXPORT_SYMBOL(vm_stat
);
85 static int calculate_threshold(struct zone
*zone
)
88 int mem
; /* memory in 128 MB units */
91 * The threshold scales with the number of processors and the amount
92 * of memory per zone. More memory means that we can defer updates for
93 * longer, more processors could lead to more contention.
94 * fls() is used to have a cheap way of logarithmic scaling.
96 * Some sample thresholds:
98 * Threshold Processors (fls) Zonesize fls(mem+1)
99 * ------------------------------------------------------------------
116 * 125 1024 10 8-16 GB 8
117 * 125 1024 10 16-32 GB 9
120 mem
= zone
->present_pages
>> (27 - PAGE_SHIFT
);
122 threshold
= 2 * fls(num_online_cpus()) * (1 + fls(mem
));
125 * Maximum threshold is 125
127 threshold
= min(125, threshold
);
133 * Refresh the thresholds for each zone.
135 static void refresh_zone_stat_thresholds(void)
141 for_each_zone(zone
) {
143 if (!zone
->present_pages
)
146 threshold
= calculate_threshold(zone
);
148 for_each_online_cpu(cpu
)
149 zone_pcp(zone
, cpu
)->stat_threshold
= threshold
;
154 * For use when we know that interrupts are disabled.
156 void __mod_zone_page_state(struct zone
*zone
, enum zone_stat_item item
,
159 struct per_cpu_pageset
*pcp
= zone_pcp(zone
, smp_processor_id());
160 s8
*p
= pcp
->vm_stat_diff
+ item
;
165 if (unlikely(x
> pcp
->stat_threshold
|| x
< -pcp
->stat_threshold
)) {
166 zone_page_state_add(x
, zone
, item
);
171 EXPORT_SYMBOL(__mod_zone_page_state
);
174 * For an unknown interrupt state
176 void mod_zone_page_state(struct zone
*zone
, enum zone_stat_item item
,
181 local_irq_save(flags
);
182 __mod_zone_page_state(zone
, item
, delta
);
183 local_irq_restore(flags
);
185 EXPORT_SYMBOL(mod_zone_page_state
);
188 * Optimized increment and decrement functions.
190 * These are only for a single page and therefore can take a struct page *
191 * argument instead of struct zone *. This allows the inclusion of the code
192 * generated for page_zone(page) into the optimized functions.
194 * No overflow check is necessary and therefore the differential can be
195 * incremented or decremented in place which may allow the compilers to
196 * generate better code.
197 * The increment or decrement is known and therefore one boundary check can
200 * NOTE: These functions are very performance sensitive. Change only
203 * Some processors have inc/dec instructions that are atomic vs an interrupt.
204 * However, the code must first determine the differential location in a zone
205 * based on the processor number and then inc/dec the counter. There is no
206 * guarantee without disabling preemption that the processor will not change
207 * in between and therefore the atomicity vs. interrupt cannot be exploited
208 * in a useful way here.
210 void __inc_zone_state(struct zone
*zone
, enum zone_stat_item item
)
212 struct per_cpu_pageset
*pcp
= zone_pcp(zone
, smp_processor_id());
213 s8
*p
= pcp
->vm_stat_diff
+ item
;
217 if (unlikely(*p
> pcp
->stat_threshold
)) {
218 int overstep
= pcp
->stat_threshold
/ 2;
220 zone_page_state_add(*p
+ overstep
, zone
, item
);
225 void __inc_zone_page_state(struct page
*page
, enum zone_stat_item item
)
227 __inc_zone_state(page_zone(page
), item
);
229 EXPORT_SYMBOL(__inc_zone_page_state
);
231 void __dec_zone_state(struct zone
*zone
, enum zone_stat_item item
)
233 struct per_cpu_pageset
*pcp
= zone_pcp(zone
, smp_processor_id());
234 s8
*p
= pcp
->vm_stat_diff
+ item
;
238 if (unlikely(*p
< - pcp
->stat_threshold
)) {
239 int overstep
= pcp
->stat_threshold
/ 2;
241 zone_page_state_add(*p
- overstep
, zone
, item
);
246 void __dec_zone_page_state(struct page
*page
, enum zone_stat_item item
)
248 __dec_zone_state(page_zone(page
), item
);
250 EXPORT_SYMBOL(__dec_zone_page_state
);
252 void inc_zone_state(struct zone
*zone
, enum zone_stat_item item
)
256 local_irq_save(flags
);
257 __inc_zone_state(zone
, item
);
258 local_irq_restore(flags
);
261 void inc_zone_page_state(struct page
*page
, enum zone_stat_item item
)
266 zone
= page_zone(page
);
267 local_irq_save(flags
);
268 __inc_zone_state(zone
, item
);
269 local_irq_restore(flags
);
271 EXPORT_SYMBOL(inc_zone_page_state
);
273 void dec_zone_page_state(struct page
*page
, enum zone_stat_item item
)
277 local_irq_save(flags
);
278 __dec_zone_page_state(page
, item
);
279 local_irq_restore(flags
);
281 EXPORT_SYMBOL(dec_zone_page_state
);
284 * Update the zone counters for one cpu.
286 * Note that refresh_cpu_vm_stats strives to only access
287 * node local memory. The per cpu pagesets on remote zones are placed
288 * in the memory local to the processor using that pageset. So the
289 * loop over all zones will access a series of cachelines local to
292 * The call to zone_page_state_add updates the cachelines with the
293 * statistics in the remote zone struct as well as the global cachelines
294 * with the global counters. These could cause remote node cache line
295 * bouncing and will have to be only done when necessary.
297 void refresh_cpu_vm_stats(int cpu
)
303 for_each_zone(zone
) {
304 struct per_cpu_pageset
*p
;
306 if (!populated_zone(zone
))
309 p
= zone_pcp(zone
, cpu
);
311 for (i
= 0; i
< NR_VM_ZONE_STAT_ITEMS
; i
++)
312 if (p
->vm_stat_diff
[i
]) {
313 local_irq_save(flags
);
314 zone_page_state_add(p
->vm_stat_diff
[i
],
316 p
->vm_stat_diff
[i
] = 0;
318 /* 3 seconds idle till flush */
321 local_irq_restore(flags
);
325 * Deal with draining the remote pageset of this
328 * Check if there are pages remaining in this pageset
329 * if not then there is nothing to expire.
331 if (!p
->expire
|| (!p
->pcp
[0].count
&& !p
->pcp
[1].count
))
335 * We never drain zones local to this processor.
337 if (zone_to_nid(zone
) == numa_node_id()) {
347 drain_zone_pages(zone
, p
->pcp
+ 0);
350 drain_zone_pages(zone
, p
->pcp
+ 1);
355 static void __refresh_cpu_vm_stats(void *dummy
)
357 refresh_cpu_vm_stats(smp_processor_id());
361 * Consolidate all counters.
363 * Note that the result is less inaccurate but still inaccurate
364 * if concurrent processes are allowed to run.
366 void refresh_vm_stats(void)
368 on_each_cpu(__refresh_cpu_vm_stats
, NULL
, 0, 1);
370 EXPORT_SYMBOL(refresh_vm_stats
);
376 * zonelist = the list of zones passed to the allocator
377 * z = the zone from which the allocation occurred.
379 * Must be called with interrupts disabled.
381 void zone_statistics(struct zonelist
*zonelist
, struct zone
*z
)
383 if (z
->zone_pgdat
== zonelist
->zones
[0]->zone_pgdat
) {
384 __inc_zone_state(z
, NUMA_HIT
);
386 __inc_zone_state(z
, NUMA_MISS
);
387 __inc_zone_state(zonelist
->zones
[0], NUMA_FOREIGN
);
389 if (z
->node
== numa_node_id())
390 __inc_zone_state(z
, NUMA_LOCAL
);
392 __inc_zone_state(z
, NUMA_OTHER
);
396 #ifdef CONFIG_PROC_FS
398 #include <linux/seq_file.h>
400 static void *frag_start(struct seq_file
*m
, loff_t
*pos
)
404 for (pgdat
= first_online_pgdat();
406 pgdat
= next_online_pgdat(pgdat
))
412 static void *frag_next(struct seq_file
*m
, void *arg
, loff_t
*pos
)
414 pg_data_t
*pgdat
= (pg_data_t
*)arg
;
417 return next_online_pgdat(pgdat
);
420 static void frag_stop(struct seq_file
*m
, void *arg
)
425 * This walks the free areas for each zone.
427 static int frag_show(struct seq_file
*m
, void *arg
)
429 pg_data_t
*pgdat
= (pg_data_t
*)arg
;
431 struct zone
*node_zones
= pgdat
->node_zones
;
435 for (zone
= node_zones
; zone
- node_zones
< MAX_NR_ZONES
; ++zone
) {
436 if (!populated_zone(zone
))
439 spin_lock_irqsave(&zone
->lock
, flags
);
440 seq_printf(m
, "Node %d, zone %8s ", pgdat
->node_id
, zone
->name
);
441 for (order
= 0; order
< MAX_ORDER
; ++order
)
442 seq_printf(m
, "%6lu ", zone
->free_area
[order
].nr_free
);
443 spin_unlock_irqrestore(&zone
->lock
, flags
);
449 const struct seq_operations fragmentation_op
= {
456 #ifdef CONFIG_ZONE_DMA
457 #define TEXT_FOR_DMA(xx) xx "_dma",
459 #define TEXT_FOR_DMA(xx)
462 #ifdef CONFIG_ZONE_DMA32
463 #define TEXT_FOR_DMA32(xx) xx "_dma32",
465 #define TEXT_FOR_DMA32(xx)
468 #ifdef CONFIG_HIGHMEM
469 #define TEXT_FOR_HIGHMEM(xx) xx "_high",
471 #define TEXT_FOR_HIGHMEM(xx)
474 #define TEXTS_FOR_ZONES(xx) TEXT_FOR_DMA(xx) TEXT_FOR_DMA32(xx) xx "_normal", \
477 static const char * const vmstat_text
[] = {
478 /* Zoned VM counters */
487 "nr_slab_reclaimable",
488 "nr_slab_unreclaimable",
489 "nr_page_table_pages",
503 #ifdef CONFIG_VM_EVENT_COUNTERS
509 TEXTS_FOR_ZONES("pgalloc")
518 TEXTS_FOR_ZONES("pgrefill")
519 TEXTS_FOR_ZONES("pgsteal")
520 TEXTS_FOR_ZONES("pgscan_kswapd")
521 TEXTS_FOR_ZONES("pgscan_direct")
535 * Output information about zones in @pgdat.
537 static int zoneinfo_show(struct seq_file
*m
, void *arg
)
539 pg_data_t
*pgdat
= arg
;
541 struct zone
*node_zones
= pgdat
->node_zones
;
544 for (zone
= node_zones
; zone
- node_zones
< MAX_NR_ZONES
; zone
++) {
547 if (!populated_zone(zone
))
550 spin_lock_irqsave(&zone
->lock
, flags
);
551 seq_printf(m
, "Node %d, zone %8s", pgdat
->node_id
, zone
->name
);
557 "\n scanned %lu (a: %lu i: %lu)"
560 zone_page_state(zone
, NR_FREE_PAGES
),
565 zone
->nr_scan_active
, zone
->nr_scan_inactive
,
567 zone
->present_pages
);
569 for (i
= 0; i
< NR_VM_ZONE_STAT_ITEMS
; i
++)
570 seq_printf(m
, "\n %-12s %lu", vmstat_text
[i
],
571 zone_page_state(zone
, i
));
574 "\n protection: (%lu",
575 zone
->lowmem_reserve
[0]);
576 for (i
= 1; i
< ARRAY_SIZE(zone
->lowmem_reserve
); i
++)
577 seq_printf(m
, ", %lu", zone
->lowmem_reserve
[i
]);
581 for_each_online_cpu(i
) {
582 struct per_cpu_pageset
*pageset
;
585 pageset
= zone_pcp(zone
, i
);
586 for (j
= 0; j
< ARRAY_SIZE(pageset
->pcp
); j
++) {
593 pageset
->pcp
[j
].count
,
594 pageset
->pcp
[j
].high
,
595 pageset
->pcp
[j
].batch
);
598 seq_printf(m
, "\n vm stats threshold: %d",
599 pageset
->stat_threshold
);
603 "\n all_unreclaimable: %u"
604 "\n prev_priority: %i"
606 zone
->all_unreclaimable
,
608 zone
->zone_start_pfn
);
609 spin_unlock_irqrestore(&zone
->lock
, flags
);
615 const struct seq_operations zoneinfo_op
= {
616 .start
= frag_start
, /* iterate over all zones. The same as in
620 .show
= zoneinfo_show
,
623 static void *vmstat_start(struct seq_file
*m
, loff_t
*pos
)
626 #ifdef CONFIG_VM_EVENT_COUNTERS
631 if (*pos
>= ARRAY_SIZE(vmstat_text
))
634 #ifdef CONFIG_VM_EVENT_COUNTERS
635 v
= kmalloc(NR_VM_ZONE_STAT_ITEMS
* sizeof(unsigned long)
636 + sizeof(struct vm_event_state
), GFP_KERNEL
);
638 v
= kmalloc(NR_VM_ZONE_STAT_ITEMS
* sizeof(unsigned long),
643 return ERR_PTR(-ENOMEM
);
644 for (i
= 0; i
< NR_VM_ZONE_STAT_ITEMS
; i
++)
645 v
[i
] = global_page_state(i
);
646 #ifdef CONFIG_VM_EVENT_COUNTERS
647 e
= v
+ NR_VM_ZONE_STAT_ITEMS
;
649 e
[PGPGIN
] /= 2; /* sectors -> kbytes */
655 static void *vmstat_next(struct seq_file
*m
, void *arg
, loff_t
*pos
)
658 if (*pos
>= ARRAY_SIZE(vmstat_text
))
660 return (unsigned long *)m
->private + *pos
;
663 static int vmstat_show(struct seq_file
*m
, void *arg
)
665 unsigned long *l
= arg
;
666 unsigned long off
= l
- (unsigned long *)m
->private;
668 seq_printf(m
, "%s %lu\n", vmstat_text
[off
], *l
);
672 static void vmstat_stop(struct seq_file
*m
, void *arg
)
678 const struct seq_operations vmstat_op
= {
679 .start
= vmstat_start
,
685 #endif /* CONFIG_PROC_FS */
688 static DEFINE_PER_CPU(struct delayed_work
, vmstat_work
);
689 int sysctl_stat_interval __read_mostly
= HZ
;
691 static void vmstat_update(struct work_struct
*w
)
693 refresh_cpu_vm_stats(smp_processor_id());
694 schedule_delayed_work(&__get_cpu_var(vmstat_work
),
695 sysctl_stat_interval
);
698 static void __devinit
start_cpu_timer(int cpu
)
700 struct delayed_work
*vmstat_work
= &per_cpu(vmstat_work
, cpu
);
702 INIT_DELAYED_WORK_DEFERRABLE(vmstat_work
, vmstat_update
);
703 schedule_delayed_work_on(cpu
, vmstat_work
, HZ
+ cpu
);
707 * Use the cpu notifier to insure that the thresholds are recalculated
710 static int __cpuinit
vmstat_cpuup_callback(struct notifier_block
*nfb
,
711 unsigned long action
,
714 long cpu
= (long)hcpu
;
718 case CPU_ONLINE_FROZEN
:
719 start_cpu_timer(cpu
);
721 case CPU_DOWN_PREPARE
:
722 case CPU_DOWN_PREPARE_FROZEN
:
723 cancel_rearming_delayed_work(&per_cpu(vmstat_work
, cpu
));
724 per_cpu(vmstat_work
, cpu
).work
.func
= NULL
;
726 case CPU_DOWN_FAILED
:
727 case CPU_DOWN_FAILED_FROZEN
:
728 start_cpu_timer(cpu
);
731 case CPU_DEAD_FROZEN
:
732 refresh_zone_stat_thresholds();
740 static struct notifier_block __cpuinitdata vmstat_notifier
=
741 { &vmstat_cpuup_callback
, NULL
, 0 };
743 int __init
setup_vmstat(void)
747 refresh_zone_stat_thresholds();
748 register_cpu_notifier(&vmstat_notifier
);
750 for_each_online_cpu(cpu
)
751 start_cpu_timer(cpu
);
754 module_init(setup_vmstat
)