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>
17 * Accumulate the page_state information across all CPUs.
18 * The result is unavoidably approximate - it can change
19 * during and after execution of this function.
21 DEFINE_PER_CPU(struct page_state
, page_states
) = {0};
23 atomic_t nr_pagecache
= ATOMIC_INIT(0);
24 EXPORT_SYMBOL(nr_pagecache
);
26 DEFINE_PER_CPU(long, nr_pagecache_local
) = 0;
29 static void __get_page_state(struct page_state
*ret
, int nr
, cpumask_t
*cpumask
)
33 memset(ret
, 0, nr
* sizeof(unsigned long));
34 cpus_and(*cpumask
, *cpumask
, cpu_online_map
);
36 for_each_cpu_mask(cpu
, *cpumask
) {
42 in
= (unsigned long *)&per_cpu(page_states
, cpu
);
44 next_cpu
= next_cpu(cpu
, *cpumask
);
45 if (likely(next_cpu
< NR_CPUS
))
46 prefetch(&per_cpu(page_states
, next_cpu
));
48 out
= (unsigned long *)ret
;
49 for (off
= 0; off
< nr
; off
++)
54 void get_page_state_node(struct page_state
*ret
, int node
)
57 cpumask_t mask
= node_to_cpumask(node
);
59 nr
= offsetof(struct page_state
, GET_PAGE_STATE_LAST
);
60 nr
/= sizeof(unsigned long);
62 __get_page_state(ret
, nr
+1, &mask
);
65 void get_page_state(struct page_state
*ret
)
68 cpumask_t mask
= CPU_MASK_ALL
;
70 nr
= offsetof(struct page_state
, GET_PAGE_STATE_LAST
);
71 nr
/= sizeof(unsigned long);
73 __get_page_state(ret
, nr
+ 1, &mask
);
76 void get_full_page_state(struct page_state
*ret
)
78 cpumask_t mask
= CPU_MASK_ALL
;
80 __get_page_state(ret
, sizeof(*ret
) / sizeof(unsigned long), &mask
);
83 unsigned long read_page_state_offset(unsigned long offset
)
85 unsigned long ret
= 0;
88 for_each_online_cpu(cpu
) {
91 in
= (unsigned long)&per_cpu(page_states
, cpu
) + offset
;
92 ret
+= *((unsigned long *)in
);
97 void __mod_page_state_offset(unsigned long offset
, unsigned long delta
)
101 ptr
= &__get_cpu_var(page_states
);
102 *(unsigned long *)(ptr
+ offset
) += delta
;
104 EXPORT_SYMBOL(__mod_page_state_offset
);
106 void mod_page_state_offset(unsigned long offset
, unsigned long delta
)
111 local_irq_save(flags
);
112 ptr
= &__get_cpu_var(page_states
);
113 *(unsigned long *)(ptr
+ offset
) += delta
;
114 local_irq_restore(flags
);
116 EXPORT_SYMBOL(mod_page_state_offset
);
118 void __get_zone_counts(unsigned long *active
, unsigned long *inactive
,
119 unsigned long *free
, struct pglist_data
*pgdat
)
121 struct zone
*zones
= pgdat
->node_zones
;
127 for (i
= 0; i
< MAX_NR_ZONES
; i
++) {
128 *active
+= zones
[i
].nr_active
;
129 *inactive
+= zones
[i
].nr_inactive
;
130 *free
+= zones
[i
].free_pages
;
134 void get_zone_counts(unsigned long *active
,
135 unsigned long *inactive
, unsigned long *free
)
137 struct pglist_data
*pgdat
;
142 for_each_online_pgdat(pgdat
) {
143 unsigned long l
, m
, n
;
144 __get_zone_counts(&l
, &m
, &n
, pgdat
);
152 * Manage combined zone based / global counters
154 * vm_stat contains the global counters
156 atomic_long_t vm_stat
[NR_VM_ZONE_STAT_ITEMS
];
157 EXPORT_SYMBOL(vm_stat
);
161 #define STAT_THRESHOLD 32
164 * Determine pointer to currently valid differential byte given a zone and
167 * Preemption must be off
169 static inline s8
*diff_pointer(struct zone
*zone
, enum zone_stat_item item
)
171 return &zone_pcp(zone
, smp_processor_id())->vm_stat_diff
[item
];
175 * For use when we know that interrupts are disabled.
177 void __mod_zone_page_state(struct zone
*zone
, enum zone_stat_item item
,
183 p
= diff_pointer(zone
, item
);
186 if (unlikely(x
> STAT_THRESHOLD
|| x
< -STAT_THRESHOLD
)) {
187 zone_page_state_add(x
, zone
, item
);
193 EXPORT_SYMBOL(__mod_zone_page_state
);
196 * For an unknown interrupt state
198 void mod_zone_page_state(struct zone
*zone
, enum zone_stat_item item
,
203 local_irq_save(flags
);
204 __mod_zone_page_state(zone
, item
, delta
);
205 local_irq_restore(flags
);
207 EXPORT_SYMBOL(mod_zone_page_state
);
210 * Optimized increment and decrement functions.
212 * These are only for a single page and therefore can take a struct page *
213 * argument instead of struct zone *. This allows the inclusion of the code
214 * generated for page_zone(page) into the optimized functions.
216 * No overflow check is necessary and therefore the differential can be
217 * incremented or decremented in place which may allow the compilers to
218 * generate better code.
220 * The increment or decrement is known and therefore one boundary check can
223 * Some processors have inc/dec instructions that are atomic vs an interrupt.
224 * However, the code must first determine the differential location in a zone
225 * based on the processor number and then inc/dec the counter. There is no
226 * guarantee without disabling preemption that the processor will not change
227 * in between and therefore the atomicity vs. interrupt cannot be exploited
228 * in a useful way here.
230 void __inc_zone_page_state(struct page
*page
, enum zone_stat_item item
)
232 struct zone
*zone
= page_zone(page
);
233 s8
*p
= diff_pointer(zone
, item
);
237 if (unlikely(*p
> STAT_THRESHOLD
)) {
238 zone_page_state_add(*p
, zone
, item
);
242 EXPORT_SYMBOL(__inc_zone_page_state
);
244 void __dec_zone_page_state(struct page
*page
, enum zone_stat_item item
)
246 struct zone
*zone
= page_zone(page
);
247 s8
*p
= diff_pointer(zone
, item
);
251 if (unlikely(*p
< -STAT_THRESHOLD
)) {
252 zone_page_state_add(*p
, zone
, item
);
256 EXPORT_SYMBOL(__dec_zone_page_state
);
258 void inc_zone_page_state(struct page
*page
, enum zone_stat_item item
)
264 zone
= page_zone(page
);
265 local_irq_save(flags
);
266 p
= diff_pointer(zone
, item
);
270 if (unlikely(*p
> STAT_THRESHOLD
)) {
271 zone_page_state_add(*p
, zone
, item
);
274 local_irq_restore(flags
);
276 EXPORT_SYMBOL(inc_zone_page_state
);
278 void dec_zone_page_state(struct page
*page
, enum zone_stat_item item
)
284 zone
= page_zone(page
);
285 local_irq_save(flags
);
286 p
= diff_pointer(zone
, item
);
290 if (unlikely(*p
< -STAT_THRESHOLD
)) {
291 zone_page_state_add(*p
, zone
, item
);
294 local_irq_restore(flags
);
296 EXPORT_SYMBOL(dec_zone_page_state
);
299 * Update the zone counters for one cpu.
301 void refresh_cpu_vm_stats(int cpu
)
307 for_each_zone(zone
) {
308 struct per_cpu_pageset
*pcp
;
310 pcp
= zone_pcp(zone
, cpu
);
312 for (i
= 0; i
< NR_VM_ZONE_STAT_ITEMS
; i
++)
313 if (pcp
->vm_stat_diff
[i
]) {
314 local_irq_save(flags
);
315 zone_page_state_add(pcp
->vm_stat_diff
[i
],
317 pcp
->vm_stat_diff
[i
] = 0;
318 local_irq_restore(flags
);
323 static void __refresh_cpu_vm_stats(void *dummy
)
325 refresh_cpu_vm_stats(smp_processor_id());
329 * Consolidate all counters.
331 * Note that the result is less inaccurate but still inaccurate
332 * if concurrent processes are allowed to run.
334 void refresh_vm_stats(void)
336 on_each_cpu(__refresh_cpu_vm_stats
, NULL
, 0, 1);
338 EXPORT_SYMBOL(refresh_vm_stats
);
342 #ifdef CONFIG_PROC_FS
344 #include <linux/seq_file.h>
346 static void *frag_start(struct seq_file
*m
, loff_t
*pos
)
350 for (pgdat
= first_online_pgdat();
352 pgdat
= next_online_pgdat(pgdat
))
358 static void *frag_next(struct seq_file
*m
, void *arg
, loff_t
*pos
)
360 pg_data_t
*pgdat
= (pg_data_t
*)arg
;
363 return next_online_pgdat(pgdat
);
366 static void frag_stop(struct seq_file
*m
, void *arg
)
371 * This walks the free areas for each zone.
373 static int frag_show(struct seq_file
*m
, void *arg
)
375 pg_data_t
*pgdat
= (pg_data_t
*)arg
;
377 struct zone
*node_zones
= pgdat
->node_zones
;
381 for (zone
= node_zones
; zone
- node_zones
< MAX_NR_ZONES
; ++zone
) {
382 if (!populated_zone(zone
))
385 spin_lock_irqsave(&zone
->lock
, flags
);
386 seq_printf(m
, "Node %d, zone %8s ", pgdat
->node_id
, zone
->name
);
387 for (order
= 0; order
< MAX_ORDER
; ++order
)
388 seq_printf(m
, "%6lu ", zone
->free_area
[order
].nr_free
);
389 spin_unlock_irqrestore(&zone
->lock
, flags
);
395 struct seq_operations fragmentation_op
= {
402 static char *vmstat_text
[] = {
403 /* Zoned VM counters */
410 "nr_page_table_pages",
440 "pgscan_kswapd_high",
441 "pgscan_kswapd_normal",
442 "pgscan_kswapd_dma32",
445 "pgscan_direct_high",
446 "pgscan_direct_normal",
447 "pgscan_direct_dma32",
462 * Output information about zones in @pgdat.
464 static int zoneinfo_show(struct seq_file
*m
, void *arg
)
466 pg_data_t
*pgdat
= arg
;
468 struct zone
*node_zones
= pgdat
->node_zones
;
471 for (zone
= node_zones
; zone
- node_zones
< MAX_NR_ZONES
; zone
++) {
474 if (!populated_zone(zone
))
477 spin_lock_irqsave(&zone
->lock
, flags
);
478 seq_printf(m
, "Node %d, zone %8s", pgdat
->node_id
, zone
->name
);
486 "\n scanned %lu (a: %lu i: %lu)"
496 zone
->nr_scan_active
, zone
->nr_scan_inactive
,
498 zone
->present_pages
);
500 for (i
= 0; i
< NR_VM_ZONE_STAT_ITEMS
; i
++)
501 seq_printf(m
, "\n %-12s %lu", vmstat_text
[i
],
502 zone_page_state(zone
, i
));
505 "\n protection: (%lu",
506 zone
->lowmem_reserve
[0]);
507 for (i
= 1; i
< ARRAY_SIZE(zone
->lowmem_reserve
); i
++)
508 seq_printf(m
, ", %lu", zone
->lowmem_reserve
[i
]);
512 for_each_online_cpu(i
) {
513 struct per_cpu_pageset
*pageset
;
516 pageset
= zone_pcp(zone
, i
);
517 for (j
= 0; j
< ARRAY_SIZE(pageset
->pcp
); j
++) {
518 if (pageset
->pcp
[j
].count
)
521 if (j
== ARRAY_SIZE(pageset
->pcp
))
523 for (j
= 0; j
< ARRAY_SIZE(pageset
->pcp
); j
++) {
530 pageset
->pcp
[j
].count
,
531 pageset
->pcp
[j
].high
,
532 pageset
->pcp
[j
].batch
);
538 "\n numa_foreign: %lu"
539 "\n interleave_hit: %lu"
541 "\n other_node: %lu",
544 pageset
->numa_foreign
,
545 pageset
->interleave_hit
,
547 pageset
->other_node
);
551 "\n all_unreclaimable: %u"
552 "\n prev_priority: %i"
553 "\n temp_priority: %i"
555 zone
->all_unreclaimable
,
558 zone
->zone_start_pfn
);
559 spin_unlock_irqrestore(&zone
->lock
, flags
);
565 struct seq_operations zoneinfo_op
= {
566 .start
= frag_start
, /* iterate over all zones. The same as in
570 .show
= zoneinfo_show
,
573 static void *vmstat_start(struct seq_file
*m
, loff_t
*pos
)
576 struct page_state
*ps
;
579 if (*pos
>= ARRAY_SIZE(vmstat_text
))
582 v
= kmalloc(NR_VM_ZONE_STAT_ITEMS
* sizeof(unsigned long)
583 + sizeof(*ps
), GFP_KERNEL
);
586 return ERR_PTR(-ENOMEM
);
587 for (i
= 0; i
< NR_VM_ZONE_STAT_ITEMS
; i
++)
588 v
[i
] = global_page_state(i
);
589 ps
= (struct page_state
*)(v
+ NR_VM_ZONE_STAT_ITEMS
);
590 get_full_page_state(ps
);
591 ps
->pgpgin
/= 2; /* sectors -> kbytes */
596 static void *vmstat_next(struct seq_file
*m
, void *arg
, loff_t
*pos
)
599 if (*pos
>= ARRAY_SIZE(vmstat_text
))
601 return (unsigned long *)m
->private + *pos
;
604 static int vmstat_show(struct seq_file
*m
, void *arg
)
606 unsigned long *l
= arg
;
607 unsigned long off
= l
- (unsigned long *)m
->private;
609 seq_printf(m
, "%s %lu\n", vmstat_text
[off
], *l
);
613 static void vmstat_stop(struct seq_file
*m
, void *arg
)
619 struct seq_operations vmstat_op
= {
620 .start
= vmstat_start
,
626 #endif /* CONFIG_PROC_FS */