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 struct zone
*zones
= pgdat
->node_zones
;
22 *active
= node_page_state(pgdat
->node_id
, NR_ACTIVE
);
23 *inactive
= node_page_state(pgdat
->node_id
, NR_INACTIVE
);
25 for (i
= 0; i
< MAX_NR_ZONES
; i
++) {
26 *free
+= zones
[i
].free_pages
;
30 void get_zone_counts(unsigned long *active
,
31 unsigned long *inactive
, unsigned long *free
)
33 struct pglist_data
*pgdat
;
35 *active
= global_page_state(NR_ACTIVE
);
36 *inactive
= global_page_state(NR_INACTIVE
);
38 for_each_online_pgdat(pgdat
) {
39 unsigned long l
, m
, n
;
40 __get_zone_counts(&l
, &m
, &n
, pgdat
);
45 #ifdef CONFIG_VM_EVENT_COUNTERS
46 DEFINE_PER_CPU(struct vm_event_state
, vm_event_states
) = {{0}};
47 EXPORT_PER_CPU_SYMBOL(vm_event_states
);
49 static void sum_vm_events(unsigned long *ret
, cpumask_t
*cpumask
)
54 memset(ret
, 0, NR_VM_EVENT_ITEMS
* sizeof(unsigned long));
56 cpu
= first_cpu(*cpumask
);
57 while (cpu
< NR_CPUS
) {
58 struct vm_event_state
*this = &per_cpu(vm_event_states
, cpu
);
60 cpu
= next_cpu(cpu
, *cpumask
);
63 prefetch(&per_cpu(vm_event_states
, cpu
));
66 for (i
= 0; i
< NR_VM_EVENT_ITEMS
; i
++)
67 ret
[i
] += this->event
[i
];
72 * Accumulate the vm event counters across all CPUs.
73 * The result is unavoidably approximate - it can change
74 * during and after execution of this function.
76 void all_vm_events(unsigned long *ret
)
78 sum_vm_events(ret
, &cpu_online_map
);
80 EXPORT_SYMBOL_GPL(all_vm_events
);
84 * Fold the foreign cpu events into our own.
86 * This is adding to the events on one processor
87 * but keeps the global counts constant.
89 void vm_events_fold_cpu(int cpu
)
91 struct vm_event_state
*fold_state
= &per_cpu(vm_event_states
, cpu
);
94 for (i
= 0; i
< NR_VM_EVENT_ITEMS
; i
++) {
95 count_vm_events(i
, fold_state
->event
[i
]);
96 fold_state
->event
[i
] = 0;
99 #endif /* CONFIG_HOTPLUG */
101 #endif /* CONFIG_VM_EVENT_COUNTERS */
104 * Manage combined zone based / global counters
106 * vm_stat contains the global counters
108 atomic_long_t vm_stat
[NR_VM_ZONE_STAT_ITEMS
];
109 EXPORT_SYMBOL(vm_stat
);
113 static int calculate_threshold(struct zone
*zone
)
116 int mem
; /* memory in 128 MB units */
119 * The threshold scales with the number of processors and the amount
120 * of memory per zone. More memory means that we can defer updates for
121 * longer, more processors could lead to more contention.
122 * fls() is used to have a cheap way of logarithmic scaling.
124 * Some sample thresholds:
126 * Threshold Processors (fls) Zonesize fls(mem+1)
127 * ------------------------------------------------------------------
144 * 125 1024 10 8-16 GB 8
145 * 125 1024 10 16-32 GB 9
148 mem
= zone
->present_pages
>> (27 - PAGE_SHIFT
);
150 threshold
= 2 * fls(num_online_cpus()) * (1 + fls(mem
));
153 * Maximum threshold is 125
155 threshold
= min(125, threshold
);
161 * Refresh the thresholds for each zone.
163 static void refresh_zone_stat_thresholds(void)
169 for_each_zone(zone
) {
171 if (!zone
->present_pages
)
174 threshold
= calculate_threshold(zone
);
176 for_each_online_cpu(cpu
)
177 zone_pcp(zone
, cpu
)->stat_threshold
= threshold
;
182 * For use when we know that interrupts are disabled.
184 void __mod_zone_page_state(struct zone
*zone
, enum zone_stat_item item
,
187 struct per_cpu_pageset
*pcp
= zone_pcp(zone
, smp_processor_id());
188 s8
*p
= pcp
->vm_stat_diff
+ item
;
193 if (unlikely(x
> pcp
->stat_threshold
|| x
< -pcp
->stat_threshold
)) {
194 zone_page_state_add(x
, zone
, item
);
199 EXPORT_SYMBOL(__mod_zone_page_state
);
202 * For an unknown interrupt state
204 void mod_zone_page_state(struct zone
*zone
, enum zone_stat_item item
,
209 local_irq_save(flags
);
210 __mod_zone_page_state(zone
, item
, delta
);
211 local_irq_restore(flags
);
213 EXPORT_SYMBOL(mod_zone_page_state
);
216 * Optimized increment and decrement functions.
218 * These are only for a single page and therefore can take a struct page *
219 * argument instead of struct zone *. This allows the inclusion of the code
220 * generated for page_zone(page) into the optimized functions.
222 * No overflow check is necessary and therefore the differential can be
223 * incremented or decremented in place which may allow the compilers to
224 * generate better code.
225 * The increment or decrement is known and therefore one boundary check can
228 * NOTE: These functions are very performance sensitive. Change only
231 * Some processors have inc/dec instructions that are atomic vs an interrupt.
232 * However, the code must first determine the differential location in a zone
233 * based on the processor number and then inc/dec the counter. There is no
234 * guarantee without disabling preemption that the processor will not change
235 * in between and therefore the atomicity vs. interrupt cannot be exploited
236 * in a useful way here.
238 void __inc_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 __inc_zone_page_state(struct page
*page
, enum zone_stat_item item
)
255 __inc_zone_state(page_zone(page
), item
);
257 EXPORT_SYMBOL(__inc_zone_page_state
);
259 void __dec_zone_state(struct zone
*zone
, enum zone_stat_item item
)
261 struct per_cpu_pageset
*pcp
= zone_pcp(zone
, smp_processor_id());
262 s8
*p
= pcp
->vm_stat_diff
+ item
;
266 if (unlikely(*p
< - pcp
->stat_threshold
)) {
267 int overstep
= pcp
->stat_threshold
/ 2;
269 zone_page_state_add(*p
- overstep
, zone
, item
);
274 void __dec_zone_page_state(struct page
*page
, enum zone_stat_item item
)
276 __dec_zone_state(page_zone(page
), item
);
278 EXPORT_SYMBOL(__dec_zone_page_state
);
280 void inc_zone_state(struct zone
*zone
, enum zone_stat_item item
)
284 local_irq_save(flags
);
285 __inc_zone_state(zone
, item
);
286 local_irq_restore(flags
);
289 void inc_zone_page_state(struct page
*page
, enum zone_stat_item item
)
294 zone
= page_zone(page
);
295 local_irq_save(flags
);
296 __inc_zone_state(zone
, item
);
297 local_irq_restore(flags
);
299 EXPORT_SYMBOL(inc_zone_page_state
);
301 void dec_zone_page_state(struct page
*page
, enum zone_stat_item item
)
305 local_irq_save(flags
);
306 __dec_zone_page_state(page
, item
);
307 local_irq_restore(flags
);
309 EXPORT_SYMBOL(dec_zone_page_state
);
312 * Update the zone counters for one cpu.
314 void refresh_cpu_vm_stats(int cpu
)
320 for_each_zone(zone
) {
321 struct per_cpu_pageset
*pcp
;
323 if (!populated_zone(zone
))
326 pcp
= zone_pcp(zone
, cpu
);
328 for (i
= 0; i
< NR_VM_ZONE_STAT_ITEMS
; i
++)
329 if (pcp
->vm_stat_diff
[i
]) {
330 local_irq_save(flags
);
331 zone_page_state_add(pcp
->vm_stat_diff
[i
],
333 pcp
->vm_stat_diff
[i
] = 0;
334 local_irq_restore(flags
);
339 static void __refresh_cpu_vm_stats(void *dummy
)
341 refresh_cpu_vm_stats(smp_processor_id());
345 * Consolidate all counters.
347 * Note that the result is less inaccurate but still inaccurate
348 * if concurrent processes are allowed to run.
350 void refresh_vm_stats(void)
352 on_each_cpu(__refresh_cpu_vm_stats
, NULL
, 0, 1);
354 EXPORT_SYMBOL(refresh_vm_stats
);
360 * zonelist = the list of zones passed to the allocator
361 * z = the zone from which the allocation occurred.
363 * Must be called with interrupts disabled.
365 void zone_statistics(struct zonelist
*zonelist
, struct zone
*z
)
367 if (z
->zone_pgdat
== zonelist
->zones
[0]->zone_pgdat
) {
368 __inc_zone_state(z
, NUMA_HIT
);
370 __inc_zone_state(z
, NUMA_MISS
);
371 __inc_zone_state(zonelist
->zones
[0], NUMA_FOREIGN
);
373 if (z
->node
== numa_node_id())
374 __inc_zone_state(z
, NUMA_LOCAL
);
376 __inc_zone_state(z
, NUMA_OTHER
);
380 #ifdef CONFIG_PROC_FS
382 #include <linux/seq_file.h>
384 static void *frag_start(struct seq_file
*m
, loff_t
*pos
)
388 for (pgdat
= first_online_pgdat();
390 pgdat
= next_online_pgdat(pgdat
))
396 static void *frag_next(struct seq_file
*m
, void *arg
, loff_t
*pos
)
398 pg_data_t
*pgdat
= (pg_data_t
*)arg
;
401 return next_online_pgdat(pgdat
);
404 static void frag_stop(struct seq_file
*m
, void *arg
)
409 * This walks the free areas for each zone.
411 static int frag_show(struct seq_file
*m
, void *arg
)
413 pg_data_t
*pgdat
= (pg_data_t
*)arg
;
415 struct zone
*node_zones
= pgdat
->node_zones
;
419 for (zone
= node_zones
; zone
- node_zones
< MAX_NR_ZONES
; ++zone
) {
420 if (!populated_zone(zone
))
423 spin_lock_irqsave(&zone
->lock
, flags
);
424 seq_printf(m
, "Node %d, zone %8s ", pgdat
->node_id
, zone
->name
);
425 for (order
= 0; order
< MAX_ORDER
; ++order
)
426 seq_printf(m
, "%6lu ", zone
->free_area
[order
].nr_free
);
427 spin_unlock_irqrestore(&zone
->lock
, flags
);
433 const struct seq_operations fragmentation_op
= {
440 #ifdef CONFIG_ZONE_DMA32
441 #define TEXT_FOR_DMA32(xx) xx "_dma32",
443 #define TEXT_FOR_DMA32(xx)
446 #ifdef CONFIG_HIGHMEM
447 #define TEXT_FOR_HIGHMEM(xx) xx "_high",
449 #define TEXT_FOR_HIGHMEM(xx)
452 #define TEXTS_FOR_ZONES(xx) xx "_dma", TEXT_FOR_DMA32(xx) xx "_normal", \
455 static const char * const vmstat_text
[] = {
456 /* Zoned VM counters */
462 "nr_slab_reclaimable",
463 "nr_slab_unreclaimable",
464 "nr_page_table_pages",
480 #ifdef CONFIG_VM_EVENT_COUNTERS
486 TEXTS_FOR_ZONES("pgalloc")
495 TEXTS_FOR_ZONES("pgrefill")
496 TEXTS_FOR_ZONES("pgsteal")
497 TEXTS_FOR_ZONES("pgscan_kswapd")
498 TEXTS_FOR_ZONES("pgscan_direct")
512 * Output information about zones in @pgdat.
514 static int zoneinfo_show(struct seq_file
*m
, void *arg
)
516 pg_data_t
*pgdat
= arg
;
518 struct zone
*node_zones
= pgdat
->node_zones
;
521 for (zone
= node_zones
; zone
- node_zones
< MAX_NR_ZONES
; zone
++) {
524 if (!populated_zone(zone
))
527 spin_lock_irqsave(&zone
->lock
, flags
);
528 seq_printf(m
, "Node %d, zone %8s", pgdat
->node_id
, zone
->name
);
534 "\n scanned %lu (a: %lu i: %lu)"
542 zone
->nr_scan_active
, zone
->nr_scan_inactive
,
544 zone
->present_pages
);
546 for (i
= 0; i
< NR_VM_ZONE_STAT_ITEMS
; i
++)
547 seq_printf(m
, "\n %-12s %lu", vmstat_text
[i
],
548 zone_page_state(zone
, i
));
551 "\n protection: (%lu",
552 zone
->lowmem_reserve
[0]);
553 for (i
= 1; i
< ARRAY_SIZE(zone
->lowmem_reserve
); i
++)
554 seq_printf(m
, ", %lu", zone
->lowmem_reserve
[i
]);
558 for_each_online_cpu(i
) {
559 struct per_cpu_pageset
*pageset
;
562 pageset
= zone_pcp(zone
, i
);
563 for (j
= 0; j
< ARRAY_SIZE(pageset
->pcp
); j
++) {
570 pageset
->pcp
[j
].count
,
571 pageset
->pcp
[j
].high
,
572 pageset
->pcp
[j
].batch
);
575 seq_printf(m
, "\n vm stats threshold: %d",
576 pageset
->stat_threshold
);
580 "\n all_unreclaimable: %u"
581 "\n prev_priority: %i"
583 zone
->all_unreclaimable
,
585 zone
->zone_start_pfn
);
586 spin_unlock_irqrestore(&zone
->lock
, flags
);
592 const struct seq_operations zoneinfo_op
= {
593 .start
= frag_start
, /* iterate over all zones. The same as in
597 .show
= zoneinfo_show
,
600 static void *vmstat_start(struct seq_file
*m
, loff_t
*pos
)
603 #ifdef CONFIG_VM_EVENT_COUNTERS
608 if (*pos
>= ARRAY_SIZE(vmstat_text
))
611 #ifdef CONFIG_VM_EVENT_COUNTERS
612 v
= kmalloc(NR_VM_ZONE_STAT_ITEMS
* sizeof(unsigned long)
613 + sizeof(struct vm_event_state
), GFP_KERNEL
);
615 v
= kmalloc(NR_VM_ZONE_STAT_ITEMS
* sizeof(unsigned long),
620 return ERR_PTR(-ENOMEM
);
621 for (i
= 0; i
< NR_VM_ZONE_STAT_ITEMS
; i
++)
622 v
[i
] = global_page_state(i
);
623 #ifdef CONFIG_VM_EVENT_COUNTERS
624 e
= v
+ NR_VM_ZONE_STAT_ITEMS
;
626 e
[PGPGIN
] /= 2; /* sectors -> kbytes */
632 static void *vmstat_next(struct seq_file
*m
, void *arg
, loff_t
*pos
)
635 if (*pos
>= ARRAY_SIZE(vmstat_text
))
637 return (unsigned long *)m
->private + *pos
;
640 static int vmstat_show(struct seq_file
*m
, void *arg
)
642 unsigned long *l
= arg
;
643 unsigned long off
= l
- (unsigned long *)m
->private;
645 seq_printf(m
, "%s %lu\n", vmstat_text
[off
], *l
);
649 static void vmstat_stop(struct seq_file
*m
, void *arg
)
655 const struct seq_operations vmstat_op
= {
656 .start
= vmstat_start
,
662 #endif /* CONFIG_PROC_FS */
666 * Use the cpu notifier to insure that the thresholds are recalculated
669 static int __cpuinit
vmstat_cpuup_callback(struct notifier_block
*nfb
,
670 unsigned long action
,
675 case CPU_UP_CANCELED
:
677 refresh_zone_stat_thresholds();
685 static struct notifier_block __cpuinitdata vmstat_notifier
=
686 { &vmstat_cpuup_callback
, NULL
, 0 };
688 int __init
setup_vmstat(void)
690 refresh_zone_stat_thresholds();
691 register_cpu_notifier(&vmstat_notifier
);
694 module_init(setup_vmstat
)