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>
15 #include <linux/cpu.h>
17 void __get_zone_counts(unsigned long *active
, unsigned long *inactive
,
18 unsigned long *free
, struct pglist_data
*pgdat
)
20 struct zone
*zones
= pgdat
->node_zones
;
26 for (i
= 0; i
< MAX_NR_ZONES
; i
++) {
27 *active
+= zones
[i
].nr_active
;
28 *inactive
+= zones
[i
].nr_inactive
;
29 *free
+= zones
[i
].free_pages
;
33 void get_zone_counts(unsigned long *active
,
34 unsigned long *inactive
, unsigned long *free
)
36 struct pglist_data
*pgdat
;
41 for_each_online_pgdat(pgdat
) {
42 unsigned long l
, m
, n
;
43 __get_zone_counts(&l
, &m
, &n
, pgdat
);
50 #ifdef CONFIG_VM_EVENT_COUNTERS
51 DEFINE_PER_CPU(struct vm_event_state
, vm_event_states
) = {{0}};
52 EXPORT_PER_CPU_SYMBOL(vm_event_states
);
54 static void sum_vm_events(unsigned long *ret
, cpumask_t
*cpumask
)
59 memset(ret
, 0, NR_VM_EVENT_ITEMS
* sizeof(unsigned long));
61 cpu
= first_cpu(*cpumask
);
62 while (cpu
< NR_CPUS
) {
63 struct vm_event_state
*this = &per_cpu(vm_event_states
, cpu
);
65 cpu
= next_cpu(cpu
, *cpumask
);
68 prefetch(&per_cpu(vm_event_states
, cpu
));
71 for (i
= 0; i
< NR_VM_EVENT_ITEMS
; i
++)
72 ret
[i
] += this->event
[i
];
77 * Accumulate the vm event counters across all CPUs.
78 * The result is unavoidably approximate - it can change
79 * during and after execution of this function.
81 void all_vm_events(unsigned long *ret
)
83 sum_vm_events(ret
, &cpu_online_map
);
85 EXPORT_SYMBOL_GPL(all_vm_events
);
89 * Fold the foreign cpu events into our own.
91 * This is adding to the events on one processor
92 * but keeps the global counts constant.
94 void vm_events_fold_cpu(int cpu
)
96 struct vm_event_state
*fold_state
= &per_cpu(vm_event_states
, cpu
);
99 for (i
= 0; i
< NR_VM_EVENT_ITEMS
; i
++) {
100 count_vm_events(i
, fold_state
->event
[i
]);
101 fold_state
->event
[i
] = 0;
104 #endif /* CONFIG_HOTPLUG */
106 #endif /* CONFIG_VM_EVENT_COUNTERS */
109 * Manage combined zone based / global counters
111 * vm_stat contains the global counters
113 atomic_long_t vm_stat
[NR_VM_ZONE_STAT_ITEMS
];
114 EXPORT_SYMBOL(vm_stat
);
118 static int calculate_threshold(struct zone
*zone
)
121 int mem
; /* memory in 128 MB units */
124 * The threshold scales with the number of processors and the amount
125 * of memory per zone. More memory means that we can defer updates for
126 * longer, more processors could lead to more contention.
127 * fls() is used to have a cheap way of logarithmic scaling.
129 * Some sample thresholds:
131 * Threshold Processors (fls) Zonesize fls(mem+1)
132 * ------------------------------------------------------------------
149 * 125 1024 10 8-16 GB 8
150 * 125 1024 10 16-32 GB 9
153 mem
= zone
->present_pages
>> (27 - PAGE_SHIFT
);
155 threshold
= 2 * fls(num_online_cpus()) * (1 + fls(mem
));
158 * Maximum threshold is 125
160 threshold
= min(125, threshold
);
166 * Refresh the thresholds for each zone.
168 static void refresh_zone_stat_thresholds(void)
174 for_each_zone(zone
) {
176 if (!zone
->present_pages
)
179 threshold
= calculate_threshold(zone
);
181 for_each_online_cpu(cpu
)
182 zone_pcp(zone
, cpu
)->stat_threshold
= threshold
;
187 * For use when we know that interrupts are disabled.
189 void __mod_zone_page_state(struct zone
*zone
, enum zone_stat_item item
,
192 struct per_cpu_pageset
*pcp
= zone_pcp(zone
, smp_processor_id());
193 s8
*p
= pcp
->vm_stat_diff
+ item
;
198 if (unlikely(x
> pcp
->stat_threshold
|| x
< -pcp
->stat_threshold
)) {
199 zone_page_state_add(x
, zone
, item
);
204 EXPORT_SYMBOL(__mod_zone_page_state
);
207 * For an unknown interrupt state
209 void mod_zone_page_state(struct zone
*zone
, enum zone_stat_item item
,
214 local_irq_save(flags
);
215 __mod_zone_page_state(zone
, item
, delta
);
216 local_irq_restore(flags
);
218 EXPORT_SYMBOL(mod_zone_page_state
);
221 * Optimized increment and decrement functions.
223 * These are only for a single page and therefore can take a struct page *
224 * argument instead of struct zone *. This allows the inclusion of the code
225 * generated for page_zone(page) into the optimized functions.
227 * No overflow check is necessary and therefore the differential can be
228 * incremented or decremented in place which may allow the compilers to
229 * generate better code.
230 * The increment or decrement is known and therefore one boundary check can
233 * NOTE: These functions are very performance sensitive. Change only
236 * Some processors have inc/dec instructions that are atomic vs an interrupt.
237 * However, the code must first determine the differential location in a zone
238 * based on the processor number and then inc/dec the counter. There is no
239 * guarantee without disabling preemption that the processor will not change
240 * in between and therefore the atomicity vs. interrupt cannot be exploited
241 * in a useful way here.
243 static void __inc_zone_state(struct zone
*zone
, enum zone_stat_item item
)
245 struct per_cpu_pageset
*pcp
= zone_pcp(zone
, smp_processor_id());
246 s8
*p
= pcp
->vm_stat_diff
+ item
;
250 if (unlikely(*p
> pcp
->stat_threshold
)) {
251 int overstep
= pcp
->stat_threshold
/ 2;
253 zone_page_state_add(*p
+ overstep
, zone
, item
);
258 void __inc_zone_page_state(struct page
*page
, enum zone_stat_item item
)
260 __inc_zone_state(page_zone(page
), item
);
262 EXPORT_SYMBOL(__inc_zone_page_state
);
264 void __dec_zone_page_state(struct page
*page
, enum zone_stat_item item
)
266 struct zone
*zone
= page_zone(page
);
267 struct per_cpu_pageset
*pcp
= zone_pcp(zone
, smp_processor_id());
268 s8
*p
= pcp
->vm_stat_diff
+ item
;
272 if (unlikely(*p
< - pcp
->stat_threshold
)) {
273 int overstep
= pcp
->stat_threshold
/ 2;
275 zone_page_state_add(*p
- overstep
, zone
, item
);
279 EXPORT_SYMBOL(__dec_zone_page_state
);
281 void inc_zone_state(struct zone
*zone
, enum zone_stat_item item
)
285 local_irq_save(flags
);
286 __inc_zone_state(zone
, item
);
287 local_irq_restore(flags
);
290 void inc_zone_page_state(struct page
*page
, enum zone_stat_item item
)
295 zone
= page_zone(page
);
296 local_irq_save(flags
);
297 __inc_zone_state(zone
, item
);
298 local_irq_restore(flags
);
300 EXPORT_SYMBOL(inc_zone_page_state
);
302 void dec_zone_page_state(struct page
*page
, enum zone_stat_item item
)
306 local_irq_save(flags
);
307 __dec_zone_page_state(page
, item
);
308 local_irq_restore(flags
);
310 EXPORT_SYMBOL(dec_zone_page_state
);
313 * Update the zone counters for one cpu.
315 void refresh_cpu_vm_stats(int cpu
)
321 for_each_zone(zone
) {
322 struct per_cpu_pageset
*pcp
;
324 if (!populated_zone(zone
))
327 pcp
= zone_pcp(zone
, cpu
);
329 for (i
= 0; i
< NR_VM_ZONE_STAT_ITEMS
; i
++)
330 if (pcp
->vm_stat_diff
[i
]) {
331 local_irq_save(flags
);
332 zone_page_state_add(pcp
->vm_stat_diff
[i
],
334 pcp
->vm_stat_diff
[i
] = 0;
335 local_irq_restore(flags
);
340 static void __refresh_cpu_vm_stats(void *dummy
)
342 refresh_cpu_vm_stats(smp_processor_id());
346 * Consolidate all counters.
348 * Note that the result is less inaccurate but still inaccurate
349 * if concurrent processes are allowed to run.
351 void refresh_vm_stats(void)
353 on_each_cpu(__refresh_cpu_vm_stats
, NULL
, 0, 1);
355 EXPORT_SYMBOL(refresh_vm_stats
);
361 * zonelist = the list of zones passed to the allocator
362 * z = the zone from which the allocation occurred.
364 * Must be called with interrupts disabled.
366 void zone_statistics(struct zonelist
*zonelist
, struct zone
*z
)
368 if (z
->zone_pgdat
== zonelist
->zones
[0]->zone_pgdat
) {
369 __inc_zone_state(z
, NUMA_HIT
);
371 __inc_zone_state(z
, NUMA_MISS
);
372 __inc_zone_state(zonelist
->zones
[0], NUMA_FOREIGN
);
374 if (z
->zone_pgdat
== NODE_DATA(numa_node_id()))
375 __inc_zone_state(z
, NUMA_LOCAL
);
377 __inc_zone_state(z
, NUMA_OTHER
);
381 #ifdef CONFIG_PROC_FS
383 #include <linux/seq_file.h>
385 static void *frag_start(struct seq_file
*m
, loff_t
*pos
)
389 for (pgdat
= first_online_pgdat();
391 pgdat
= next_online_pgdat(pgdat
))
397 static void *frag_next(struct seq_file
*m
, void *arg
, loff_t
*pos
)
399 pg_data_t
*pgdat
= (pg_data_t
*)arg
;
402 return next_online_pgdat(pgdat
);
405 static void frag_stop(struct seq_file
*m
, void *arg
)
410 * This walks the free areas for each zone.
412 static int frag_show(struct seq_file
*m
, void *arg
)
414 pg_data_t
*pgdat
= (pg_data_t
*)arg
;
416 struct zone
*node_zones
= pgdat
->node_zones
;
420 for (zone
= node_zones
; zone
- node_zones
< MAX_NR_ZONES
; ++zone
) {
421 if (!populated_zone(zone
))
424 spin_lock_irqsave(&zone
->lock
, flags
);
425 seq_printf(m
, "Node %d, zone %8s ", pgdat
->node_id
, zone
->name
);
426 for (order
= 0; order
< MAX_ORDER
; ++order
)
427 seq_printf(m
, "%6lu ", zone
->free_area
[order
].nr_free
);
428 spin_unlock_irqrestore(&zone
->lock
, flags
);
434 struct seq_operations fragmentation_op
= {
441 #ifdef CONFIG_ZONE_DMA32
442 #define TEXT_FOR_DMA32(xx) xx "_dma32",
444 #define TEXT_FOR_DMA32(xx)
447 #ifdef CONFIG_HIGHMEM
448 #define TEXT_FOR_HIGHMEM(xx) xx "_high",
450 #define TEXT_FOR_HIGHMEM(xx)
453 #define TEXTS_FOR_ZONES(xx) xx "_dma", TEXT_FOR_DMA32(xx) xx "_normal", \
456 static char *vmstat_text
[] = {
457 /* Zoned VM counters */
461 "nr_slab_reclaimable",
462 "nr_slab_unreclaimable",
463 "nr_page_table_pages",
478 #ifdef CONFIG_VM_EVENT_COUNTERS
484 TEXTS_FOR_ZONES("pgalloc")
493 TEXTS_FOR_ZONES("pgrefill")
494 TEXTS_FOR_ZONES("pgsteal")
495 TEXTS_FOR_ZONES("pgscan_kswapd")
496 TEXTS_FOR_ZONES("pgscan_direct")
510 * Output information about zones in @pgdat.
512 static int zoneinfo_show(struct seq_file
*m
, void *arg
)
514 pg_data_t
*pgdat
= arg
;
516 struct zone
*node_zones
= pgdat
->node_zones
;
519 for (zone
= node_zones
; zone
- node_zones
< MAX_NR_ZONES
; zone
++) {
522 if (!populated_zone(zone
))
525 spin_lock_irqsave(&zone
->lock
, flags
);
526 seq_printf(m
, "Node %d, zone %8s", pgdat
->node_id
, zone
->name
);
534 "\n scanned %lu (a: %lu i: %lu)"
544 zone
->nr_scan_active
, zone
->nr_scan_inactive
,
546 zone
->present_pages
);
548 for (i
= 0; i
< NR_VM_ZONE_STAT_ITEMS
; i
++)
549 seq_printf(m
, "\n %-12s %lu", vmstat_text
[i
],
550 zone_page_state(zone
, i
));
553 "\n protection: (%lu",
554 zone
->lowmem_reserve
[0]);
555 for (i
= 1; i
< ARRAY_SIZE(zone
->lowmem_reserve
); i
++)
556 seq_printf(m
, ", %lu", zone
->lowmem_reserve
[i
]);
560 for_each_online_cpu(i
) {
561 struct per_cpu_pageset
*pageset
;
564 pageset
= zone_pcp(zone
, i
);
565 for (j
= 0; j
< ARRAY_SIZE(pageset
->pcp
); j
++) {
566 if (pageset
->pcp
[j
].count
)
569 if (j
== ARRAY_SIZE(pageset
->pcp
))
571 for (j
= 0; j
< ARRAY_SIZE(pageset
->pcp
); j
++) {
578 pageset
->pcp
[j
].count
,
579 pageset
->pcp
[j
].high
,
580 pageset
->pcp
[j
].batch
);
583 seq_printf(m
, "\n vm stats threshold: %d",
584 pageset
->stat_threshold
);
588 "\n all_unreclaimable: %u"
589 "\n prev_priority: %i"
590 "\n temp_priority: %i"
592 zone
->all_unreclaimable
,
595 zone
->zone_start_pfn
);
596 spin_unlock_irqrestore(&zone
->lock
, flags
);
602 struct seq_operations zoneinfo_op
= {
603 .start
= frag_start
, /* iterate over all zones. The same as in
607 .show
= zoneinfo_show
,
610 static void *vmstat_start(struct seq_file
*m
, loff_t
*pos
)
613 #ifdef CONFIG_VM_EVENT_COUNTERS
618 if (*pos
>= ARRAY_SIZE(vmstat_text
))
621 #ifdef CONFIG_VM_EVENT_COUNTERS
622 v
= kmalloc(NR_VM_ZONE_STAT_ITEMS
* sizeof(unsigned long)
623 + sizeof(struct vm_event_state
), GFP_KERNEL
);
625 v
= kmalloc(NR_VM_ZONE_STAT_ITEMS
* sizeof(unsigned long),
630 return ERR_PTR(-ENOMEM
);
631 for (i
= 0; i
< NR_VM_ZONE_STAT_ITEMS
; i
++)
632 v
[i
] = global_page_state(i
);
633 #ifdef CONFIG_VM_EVENT_COUNTERS
634 e
= v
+ NR_VM_ZONE_STAT_ITEMS
;
636 e
[PGPGIN
] /= 2; /* sectors -> kbytes */
642 static void *vmstat_next(struct seq_file
*m
, void *arg
, loff_t
*pos
)
645 if (*pos
>= ARRAY_SIZE(vmstat_text
))
647 return (unsigned long *)m
->private + *pos
;
650 static int vmstat_show(struct seq_file
*m
, void *arg
)
652 unsigned long *l
= arg
;
653 unsigned long off
= l
- (unsigned long *)m
->private;
655 seq_printf(m
, "%s %lu\n", vmstat_text
[off
], *l
);
659 static void vmstat_stop(struct seq_file
*m
, void *arg
)
665 struct seq_operations vmstat_op
= {
666 .start
= vmstat_start
,
672 #endif /* CONFIG_PROC_FS */
676 * Use the cpu notifier to insure that the thresholds are recalculated
679 static int __cpuinit
vmstat_cpuup_callback(struct notifier_block
*nfb
,
680 unsigned long action
,
685 case CPU_UP_CANCELED
:
687 refresh_zone_stat_thresholds();
695 static struct notifier_block __cpuinitdata vmstat_notifier
=
696 { &vmstat_cpuup_callback
, NULL
, 0 };
698 int __init
setup_vmstat(void)
700 refresh_zone_stat_thresholds();
701 register_cpu_notifier(&vmstat_notifier
);
704 module_init(setup_vmstat
)