blob: b3d85f30d42485756a79c831a519b6288000ee48 [file] [log] [blame]
Christoph Lameterf6ac2352006-06-30 01:55:32 -07001#ifndef _LINUX_VMSTAT_H
2#define _LINUX_VMSTAT_H
3
4#include <linux/types.h>
5#include <linux/percpu.h>
Christoph Lameter2244b952006-06-30 01:55:33 -07006#include <linux/mmzone.h>
Andrew Mortonf042e702011-05-26 16:25:24 -07007#include <linux/vm_event_item.h>
Arun Sharma600634972011-07-26 16:09:06 -07008#include <linux/atomic.h>
Christoph Lameterf6ac2352006-06-30 01:55:32 -07009
Adrian Bunkc748e132008-07-23 21:27:03 -070010extern int sysctl_stat_interval;
11
Andrew Morton780a0652007-02-10 01:44:41 -080012#ifdef CONFIG_VM_EVENT_COUNTERS
13/*
14 * Light weight per cpu counter implementation.
15 *
16 * Counters should only be incremented and no critical kernel component
17 * should rely on the counter values.
18 *
19 * Counters are handled completely inline. On many platforms the code
20 * generated will simply be the increment of a global address.
21 */
22
Christoph Lameterf8891e52006-06-30 01:55:45 -070023struct vm_event_state {
24 unsigned long event[NR_VM_EVENT_ITEMS];
25};
Christoph Lameterf6ac2352006-06-30 01:55:32 -070026
Christoph Lameterf8891e52006-06-30 01:55:45 -070027DECLARE_PER_CPU(struct vm_event_state, vm_event_states);
Christoph Lameterf6ac2352006-06-30 01:55:32 -070028
Christoph Lameter293b6a42014-04-07 15:39:43 -070029/*
30 * vm counters are allowed to be racy. Use raw_cpu_ops to avoid the
31 * local_irq_disable overhead.
32 */
Christoph Lameterf8891e52006-06-30 01:55:45 -070033static inline void __count_vm_event(enum vm_event_item item)
34{
Christoph Lameter293b6a42014-04-07 15:39:43 -070035 raw_cpu_inc(vm_event_states.event[item]);
Christoph Lameterf8891e52006-06-30 01:55:45 -070036}
Christoph Lameterf6ac2352006-06-30 01:55:32 -070037
Christoph Lameterf8891e52006-06-30 01:55:45 -070038static inline void count_vm_event(enum vm_event_item item)
39{
Rusty Russelldd17c8f2009-10-29 22:34:15 +090040 this_cpu_inc(vm_event_states.event[item]);
Christoph Lameterf8891e52006-06-30 01:55:45 -070041}
Christoph Lameterf6ac2352006-06-30 01:55:32 -070042
Christoph Lameterf8891e52006-06-30 01:55:45 -070043static inline void __count_vm_events(enum vm_event_item item, long delta)
44{
Christoph Lameter293b6a42014-04-07 15:39:43 -070045 raw_cpu_add(vm_event_states.event[item], delta);
Christoph Lameterf8891e52006-06-30 01:55:45 -070046}
Christoph Lameterf6ac2352006-06-30 01:55:32 -070047
Christoph Lameterf8891e52006-06-30 01:55:45 -070048static inline void count_vm_events(enum vm_event_item item, long delta)
49{
Rusty Russelldd17c8f2009-10-29 22:34:15 +090050 this_cpu_add(vm_event_states.event[item], delta);
Christoph Lameterf8891e52006-06-30 01:55:45 -070051}
Christoph Lameterf6ac2352006-06-30 01:55:32 -070052
Christoph Lameterf8891e52006-06-30 01:55:45 -070053extern void all_vm_events(unsigned long *);
Yijing Wangf1cb0872013-04-29 15:08:14 -070054
Christoph Lameterf8891e52006-06-30 01:55:45 -070055extern void vm_events_fold_cpu(int cpu);
Christoph Lameterf6ac2352006-06-30 01:55:32 -070056
Christoph Lameterf8891e52006-06-30 01:55:45 -070057#else
Christoph Lameterf6ac2352006-06-30 01:55:32 -070058
Christoph Lameterf8891e52006-06-30 01:55:45 -070059/* Disable counters */
Andrew Morton780a0652007-02-10 01:44:41 -080060static inline void count_vm_event(enum vm_event_item item)
61{
62}
63static inline void count_vm_events(enum vm_event_item item, long delta)
64{
65}
66static inline void __count_vm_event(enum vm_event_item item)
67{
68}
69static inline void __count_vm_events(enum vm_event_item item, long delta)
70{
71}
72static inline void all_vm_events(unsigned long *ret)
73{
74}
75static inline void vm_events_fold_cpu(int cpu)
76{
77}
Christoph Lameterf6ac2352006-06-30 01:55:32 -070078
Christoph Lameterf8891e52006-06-30 01:55:45 -070079#endif /* CONFIG_VM_EVENT_COUNTERS */
80
Mel Gorman03c5a6e2012-11-02 14:52:48 +000081#ifdef CONFIG_NUMA_BALANCING
82#define count_vm_numa_event(x) count_vm_event(x)
83#define count_vm_numa_events(x, y) count_vm_events(x, y)
84#else
85#define count_vm_numa_event(x) do {} while (0)
Mel Gorman3c0ff462013-02-22 16:34:29 -080086#define count_vm_numa_events(x, y) do { (void)(y); } while (0)
Mel Gorman03c5a6e2012-11-02 14:52:48 +000087#endif /* CONFIG_NUMA_BALANCING */
88
Mel Gormanec659932014-01-21 14:33:16 -080089#ifdef CONFIG_DEBUG_TLBFLUSH
90#define count_vm_tlb_event(x) count_vm_event(x)
91#define count_vm_tlb_events(x, y) count_vm_events(x, y)
92#else
93#define count_vm_tlb_event(x) do {} while (0)
94#define count_vm_tlb_events(x, y) do { (void)(y); } while (0)
95#endif
96
Davidlohr Bueso4f115142014-06-04 16:06:46 -070097#ifdef CONFIG_DEBUG_VM_VMACACHE
98#define count_vm_vmacache_event(x) count_vm_event(x)
99#else
100#define count_vm_vmacache_event(x) do {} while (0)
101#endif
102
Mel Gorman16709d12016-07-28 15:46:56 -0700103#define __count_zid_vm_events(item, zid, delta) \
104 __count_vm_events(item##_NORMAL - ZONE_NORMAL + zid, delta)
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700105
Christoph Lameter2244b952006-06-30 01:55:33 -0700106/*
Mel Gorman75ef7182016-07-28 15:45:24 -0700107 * Zone and node-based page accounting with per cpu differentials.
Christoph Lameter2244b952006-06-30 01:55:33 -0700108 */
Mel Gorman75ef7182016-07-28 15:45:24 -0700109extern atomic_long_t vm_zone_stat[NR_VM_ZONE_STAT_ITEMS];
110extern atomic_long_t vm_node_stat[NR_VM_NODE_STAT_ITEMS];
Christoph Lameterf6ac2352006-06-30 01:55:32 -0700111
Christoph Lameter2244b952006-06-30 01:55:33 -0700112static inline void zone_page_state_add(long x, struct zone *zone,
113 enum zone_stat_item item)
114{
115 atomic_long_add(x, &zone->vm_stat[item]);
Mel Gorman75ef7182016-07-28 15:45:24 -0700116 atomic_long_add(x, &vm_zone_stat[item]);
117}
118
119static inline void node_page_state_add(long x, struct pglist_data *pgdat,
120 enum node_stat_item item)
121{
122 atomic_long_add(x, &pgdat->vm_stat[item]);
123 atomic_long_add(x, &vm_node_stat[item]);
Christoph Lameter2244b952006-06-30 01:55:33 -0700124}
125
126static inline unsigned long global_page_state(enum zone_stat_item item)
127{
Mel Gorman75ef7182016-07-28 15:45:24 -0700128 long x = atomic_long_read(&vm_zone_stat[item]);
129#ifdef CONFIG_SMP
130 if (x < 0)
131 x = 0;
132#endif
133 return x;
134}
135
136static inline unsigned long global_node_page_state(enum node_stat_item item)
137{
138 long x = atomic_long_read(&vm_node_stat[item]);
Christoph Lameter2244b952006-06-30 01:55:33 -0700139#ifdef CONFIG_SMP
140 if (x < 0)
141 x = 0;
142#endif
143 return x;
144}
145
146static inline unsigned long zone_page_state(struct zone *zone,
147 enum zone_stat_item item)
148{
149 long x = atomic_long_read(&zone->vm_stat[item]);
150#ifdef CONFIG_SMP
151 if (x < 0)
152 x = 0;
153#endif
154 return x;
155}
156
Christoph Lameteraa454842010-09-09 16:38:17 -0700157/*
158 * More accurate version that also considers the currently pending
159 * deltas. For that we need to loop over all cpus to find the current
160 * deltas. There is no synchronization so the result cannot be
161 * exactly accurate either.
162 */
163static inline unsigned long zone_page_state_snapshot(struct zone *zone,
164 enum zone_stat_item item)
165{
166 long x = atomic_long_read(&zone->vm_stat[item]);
167
168#ifdef CONFIG_SMP
169 int cpu;
170 for_each_online_cpu(cpu)
171 x += per_cpu_ptr(zone->pageset, cpu)->vm_stat_diff[item];
172
173 if (x < 0)
174 x = 0;
175#endif
176 return x;
177}
178
Mel Gorman599d0c92016-07-28 15:45:31 -0700179static inline unsigned long node_page_state_snapshot(pg_data_t *pgdat,
180 enum node_stat_item item)
181{
182 long x = atomic_long_read(&pgdat->vm_stat[item]);
183
184#ifdef CONFIG_SMP
185 int cpu;
186 for_each_online_cpu(cpu)
187 x += per_cpu_ptr(pgdat->per_cpu_nodestats, cpu)->vm_node_stat_diff[item];
188
189 if (x < 0)
190 x = 0;
191#endif
192 return x;
193}
194
195
Christoph Lameter2244b952006-06-30 01:55:33 -0700196#ifdef CONFIG_NUMA
Mel Gorman75ef7182016-07-28 15:45:24 -0700197extern unsigned long sum_zone_node_page_state(int node,
198 enum zone_stat_item item);
199extern unsigned long node_page_state(struct pglist_data *pgdat,
200 enum node_stat_item item);
Christoph Lameter2244b952006-06-30 01:55:33 -0700201#else
Mel Gorman75ef7182016-07-28 15:45:24 -0700202#define sum_zone_node_page_state(node, item) global_page_state(item)
203#define node_page_state(node, item) global_node_page_state(item)
Christoph Lameterca889e62006-06-30 01:55:44 -0700204#endif /* CONFIG_NUMA */
Christoph Lameter2244b952006-06-30 01:55:33 -0700205
Christoph Lameter2244b952006-06-30 01:55:33 -0700206#define add_zone_page_state(__z, __i, __d) mod_zone_page_state(__z, __i, __d)
207#define sub_zone_page_state(__z, __i, __d) mod_zone_page_state(__z, __i, -(__d))
Mel Gorman75ef7182016-07-28 15:45:24 -0700208#define add_node_page_state(__p, __i, __d) mod_node_page_state(__p, __i, __d)
209#define sub_node_page_state(__p, __i, __d) mod_node_page_state(__p, __i, -(__d))
Christoph Lameter2244b952006-06-30 01:55:33 -0700210
Christoph Lameter2244b952006-06-30 01:55:33 -0700211#ifdef CONFIG_SMP
Heiko Carstens6cdb18a2015-12-29 14:54:32 -0800212void __mod_zone_page_state(struct zone *, enum zone_stat_item item, long);
Christoph Lameter2244b952006-06-30 01:55:33 -0700213void __inc_zone_page_state(struct page *, enum zone_stat_item);
214void __dec_zone_page_state(struct page *, enum zone_stat_item);
215
Mel Gorman75ef7182016-07-28 15:45:24 -0700216void __mod_node_page_state(struct pglist_data *, enum node_stat_item item, long);
217void __inc_node_page_state(struct page *, enum node_stat_item);
218void __dec_node_page_state(struct page *, enum node_stat_item);
219
Heiko Carstens6cdb18a2015-12-29 14:54:32 -0800220void mod_zone_page_state(struct zone *, enum zone_stat_item, long);
Christoph Lameter2244b952006-06-30 01:55:33 -0700221void inc_zone_page_state(struct page *, enum zone_stat_item);
222void dec_zone_page_state(struct page *, enum zone_stat_item);
223
Mel Gorman75ef7182016-07-28 15:45:24 -0700224void mod_node_page_state(struct pglist_data *, enum node_stat_item, long);
225void inc_node_page_state(struct page *, enum node_stat_item);
226void dec_node_page_state(struct page *, enum node_stat_item);
227
Mel Gorman75ef7182016-07-28 15:45:24 -0700228extern void inc_node_state(struct pglist_data *, enum node_stat_item);
Christoph Lameterc8785382007-02-10 01:43:01 -0800229extern void __inc_zone_state(struct zone *, enum zone_stat_item);
Mel Gorman75ef7182016-07-28 15:45:24 -0700230extern void __inc_node_state(struct pglist_data *, enum node_stat_item);
Christoph Lameterc8785382007-02-10 01:43:01 -0800231extern void dec_zone_state(struct zone *, enum zone_stat_item);
232extern void __dec_zone_state(struct zone *, enum zone_stat_item);
Mel Gorman75ef7182016-07-28 15:45:24 -0700233extern void __dec_node_state(struct pglist_data *, enum node_stat_item);
Christoph Lameter2244b952006-06-30 01:55:33 -0700234
Christoph Lameter0eb77e92016-01-14 15:21:40 -0800235void quiet_vmstat(void);
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700236void cpu_vm_stats_fold(int cpu);
KOSAKI Motohiroa6cccdc2011-05-24 17:11:33 -0700237void refresh_zone_stat_thresholds(void);
Mel Gormanb44129b2011-01-13 15:45:43 -0800238
Hugh Dickins52b6f462016-05-19 17:12:50 -0700239struct ctl_table;
240int vmstat_refresh(struct ctl_table *, int write,
241 void __user *buffer, size_t *lenp, loff_t *ppos);
242
Minchan Kim5a883812012-10-08 16:33:39 -0700243void drain_zonestat(struct zone *zone, struct per_cpu_pageset *);
244
Mel Gormanb44129b2011-01-13 15:45:43 -0800245int calculate_pressure_threshold(struct zone *zone);
246int calculate_normal_threshold(struct zone *zone);
247void set_pgdat_percpu_threshold(pg_data_t *pgdat,
248 int (*calculate_pressure)(struct zone *));
Christoph Lameter2244b952006-06-30 01:55:33 -0700249#else /* CONFIG_SMP */
250
251/*
252 * We do not maintain differentials in a single processor configuration.
253 * The functions directly modify the zone and global counters.
254 */
255static inline void __mod_zone_page_state(struct zone *zone,
Heiko Carstens6cdb18a2015-12-29 14:54:32 -0800256 enum zone_stat_item item, long delta)
Christoph Lameter2244b952006-06-30 01:55:33 -0700257{
258 zone_page_state_add(delta, zone, item);
259}
260
Mel Gorman75ef7182016-07-28 15:45:24 -0700261static inline void __mod_node_page_state(struct pglist_data *pgdat,
262 enum node_stat_item item, int delta)
263{
264 node_page_state_add(delta, pgdat, item);
265}
266
Christoph Lameter7f4599e2006-07-10 04:44:30 -0700267static inline void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
268{
269 atomic_long_inc(&zone->vm_stat[item]);
Mel Gorman75ef7182016-07-28 15:45:24 -0700270 atomic_long_inc(&vm_zone_stat[item]);
271}
272
273static inline void __inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
274{
275 atomic_long_inc(&pgdat->vm_stat[item]);
276 atomic_long_inc(&vm_node_stat[item]);
Christoph Lameter7f4599e2006-07-10 04:44:30 -0700277}
278
Christoph Lameterc8785382007-02-10 01:43:01 -0800279static inline void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
280{
281 atomic_long_dec(&zone->vm_stat[item]);
Mel Gorman75ef7182016-07-28 15:45:24 -0700282 atomic_long_dec(&vm_zone_stat[item]);
283}
284
285static inline void __dec_node_state(struct pglist_data *pgdat, enum node_stat_item item)
286{
287 atomic_long_dec(&pgdat->vm_stat[item]);
288 atomic_long_dec(&vm_node_stat[item]);
Christoph Lameterc8785382007-02-10 01:43:01 -0800289}
290
Johannes Weiner6a3ed212014-04-03 14:47:34 -0700291static inline void __inc_zone_page_state(struct page *page,
292 enum zone_stat_item item)
293{
294 __inc_zone_state(page_zone(page), item);
295}
296
Mel Gorman75ef7182016-07-28 15:45:24 -0700297static inline void __inc_node_page_state(struct page *page,
298 enum node_stat_item item)
299{
300 __inc_node_state(page_pgdat(page), item);
301}
302
303
Christoph Lameter2244b952006-06-30 01:55:33 -0700304static inline void __dec_zone_page_state(struct page *page,
305 enum zone_stat_item item)
306{
Uwe Kleine-König57ce36f2008-02-25 16:45:03 +0100307 __dec_zone_state(page_zone(page), item);
Christoph Lameter2244b952006-06-30 01:55:33 -0700308}
309
Mel Gorman75ef7182016-07-28 15:45:24 -0700310static inline void __dec_node_page_state(struct page *page,
311 enum node_stat_item item)
312{
313 __dec_node_state(page_pgdat(page), item);
314}
315
316
Christoph Lameter2244b952006-06-30 01:55:33 -0700317/*
318 * We only use atomic operations to update counters. So there is no need to
319 * disable interrupts.
320 */
321#define inc_zone_page_state __inc_zone_page_state
322#define dec_zone_page_state __dec_zone_page_state
323#define mod_zone_page_state __mod_zone_page_state
324
Mel Gorman75ef7182016-07-28 15:45:24 -0700325#define inc_node_page_state __inc_node_page_state
326#define dec_node_page_state __dec_node_page_state
327#define mod_node_page_state __mod_node_page_state
328
Johannes Weiner6a3ed212014-04-03 14:47:34 -0700329#define inc_zone_state __inc_zone_state
Mel Gorman75ef7182016-07-28 15:45:24 -0700330#define inc_node_state __inc_node_state
Johannes Weiner6a3ed212014-04-03 14:47:34 -0700331#define dec_zone_state __dec_zone_state
332
Mel Gormanb44129b2011-01-13 15:45:43 -0800333#define set_pgdat_percpu_threshold(pgdat, callback) { }
Mel Gorman88f5acf2011-01-13 15:45:41 -0800334
KOSAKI Motohiroa6cccdc2011-05-24 17:11:33 -0700335static inline void refresh_zone_stat_thresholds(void) { }
Christoph Lameter2bb921e2013-09-11 14:21:30 -0700336static inline void cpu_vm_stats_fold(int cpu) { }
Christoph Lameter0eb77e92016-01-14 15:21:40 -0800337static inline void quiet_vmstat(void) { }
KOSAKI Motohiroa6cccdc2011-05-24 17:11:33 -0700338
Minchan Kim5a883812012-10-08 16:33:39 -0700339static inline void drain_zonestat(struct zone *zone,
340 struct per_cpu_pageset *pset) { }
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700341#endif /* CONFIG_SMP */
342
Bartlomiej Zolnierkiewiczd1ce7492012-10-08 16:32:02 -0700343static inline void __mod_zone_freepage_state(struct zone *zone, int nr_pages,
344 int migratetype)
345{
346 __mod_zone_page_state(zone, NR_FREE_PAGES, nr_pages);
347 if (is_migrate_cma(migratetype))
348 __mod_zone_page_state(zone, NR_FREE_CMA_PAGES, nr_pages);
349}
350
KOSAKI Motohirofa25c502011-05-24 17:11:28 -0700351extern const char * const vmstat_text[];
Christoph Lameter2244b952006-06-30 01:55:33 -0700352
353#endif /* _LINUX_VMSTAT_H */