1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_VMSTAT_H
3 #define _LINUX_VMSTAT_H
5 #include <linux/types.h>
6 #include <linux/percpu.h>
7 #include <linux/mmzone.h>
8 #include <linux/vm_event_item.h>
9 #include <linux/atomic.h>
10 #include <linux/static_key.h>
12 extern int sysctl_stat_interval;
15 #define ENABLE_NUMA_STAT 1
16 #define DISABLE_NUMA_STAT 0
17 extern int sysctl_vm_numa_stat;
18 DECLARE_STATIC_KEY_TRUE(vm_numa_stat_key);
19 int sysctl_vm_numa_stat_handler(struct ctl_table *table, int write,
20 void *buffer, size_t *length, loff_t *ppos);
25 unsigned nr_unqueued_dirty;
26 unsigned nr_congested;
27 unsigned nr_writeback;
28 unsigned nr_immediate;
30 unsigned nr_activate[2];
32 unsigned nr_unmap_fail;
33 unsigned nr_lazyfree_fail;
36 enum writeback_stat_item {
38 NR_DIRTY_BG_THRESHOLD,
39 NR_VM_WRITEBACK_STAT_ITEMS,
42 #ifdef CONFIG_VM_EVENT_COUNTERS
44 * Light weight per cpu counter implementation.
46 * Counters should only be incremented and no critical kernel component
47 * should rely on the counter values.
49 * Counters are handled completely inline. On many platforms the code
50 * generated will simply be the increment of a global address.
53 struct vm_event_state {
54 unsigned long event[NR_VM_EVENT_ITEMS];
57 DECLARE_PER_CPU(struct vm_event_state, vm_event_states);
60 * vm counters are allowed to be racy. Use raw_cpu_ops to avoid the
61 * local_irq_disable overhead.
63 static inline void __count_vm_event(enum vm_event_item item)
65 raw_cpu_inc(vm_event_states.event[item]);
68 static inline void count_vm_event(enum vm_event_item item)
70 this_cpu_inc(vm_event_states.event[item]);
73 static inline void __count_vm_events(enum vm_event_item item, long delta)
75 raw_cpu_add(vm_event_states.event[item], delta);
78 static inline void count_vm_events(enum vm_event_item item, long delta)
80 this_cpu_add(vm_event_states.event[item], delta);
83 extern void all_vm_events(unsigned long *);
85 extern void vm_events_fold_cpu(int cpu);
89 /* Disable counters */
90 static inline void count_vm_event(enum vm_event_item item)
93 static inline void count_vm_events(enum vm_event_item item, long delta)
96 static inline void __count_vm_event(enum vm_event_item item)
99 static inline void __count_vm_events(enum vm_event_item item, long delta)
102 static inline void all_vm_events(unsigned long *ret)
105 static inline void vm_events_fold_cpu(int cpu)
109 #endif /* CONFIG_VM_EVENT_COUNTERS */
111 #ifdef CONFIG_NUMA_BALANCING
112 #define count_vm_numa_event(x) count_vm_event(x)
113 #define count_vm_numa_events(x, y) count_vm_events(x, y)
115 #define count_vm_numa_event(x) do {} while (0)
116 #define count_vm_numa_events(x, y) do { (void)(y); } while (0)
117 #endif /* CONFIG_NUMA_BALANCING */
119 #ifdef CONFIG_DEBUG_TLBFLUSH
120 #define count_vm_tlb_event(x) count_vm_event(x)
121 #define count_vm_tlb_events(x, y) count_vm_events(x, y)
123 #define count_vm_tlb_event(x) do {} while (0)
124 #define count_vm_tlb_events(x, y) do { (void)(y); } while (0)
127 #ifdef CONFIG_DEBUG_VM_VMACACHE
128 #define count_vm_vmacache_event(x) count_vm_event(x)
130 #define count_vm_vmacache_event(x) do {} while (0)
133 #define __count_zid_vm_events(item, zid, delta) \
134 __count_vm_events(item##_NORMAL - ZONE_NORMAL + zid, delta)
137 * Zone and node-based page accounting with per cpu differentials.
139 extern atomic_long_t vm_zone_stat[NR_VM_ZONE_STAT_ITEMS];
140 extern atomic_long_t vm_numa_stat[NR_VM_NUMA_STAT_ITEMS];
141 extern atomic_long_t vm_node_stat[NR_VM_NODE_STAT_ITEMS];
144 static inline void zone_numa_state_add(long x, struct zone *zone,
145 enum numa_stat_item item)
147 atomic_long_add(x, &zone->vm_numa_stat[item]);
148 atomic_long_add(x, &vm_numa_stat[item]);
151 static inline unsigned long global_numa_state(enum numa_stat_item item)
153 long x = atomic_long_read(&vm_numa_stat[item]);
158 static inline unsigned long zone_numa_state_snapshot(struct zone *zone,
159 enum numa_stat_item item)
161 long x = atomic_long_read(&zone->vm_numa_stat[item]);
164 for_each_online_cpu(cpu)
165 x += per_cpu_ptr(zone->pageset, cpu)->vm_numa_stat_diff[item];
169 #endif /* CONFIG_NUMA */
171 static inline void zone_page_state_add(long x, struct zone *zone,
172 enum zone_stat_item item)
174 atomic_long_add(x, &zone->vm_stat[item]);
175 atomic_long_add(x, &vm_zone_stat[item]);
178 static inline void node_page_state_add(long x, struct pglist_data *pgdat,
179 enum node_stat_item item)
181 atomic_long_add(x, &pgdat->vm_stat[item]);
182 atomic_long_add(x, &vm_node_stat[item]);
185 static inline unsigned long global_zone_page_state(enum zone_stat_item item)
187 long x = atomic_long_read(&vm_zone_stat[item]);
195 static inline unsigned long global_node_page_state(enum node_stat_item item)
197 long x = atomic_long_read(&vm_node_stat[item]);
205 static inline unsigned long zone_page_state(struct zone *zone,
206 enum zone_stat_item item)
208 long x = atomic_long_read(&zone->vm_stat[item]);
217 * More accurate version that also considers the currently pending
218 * deltas. For that we need to loop over all cpus to find the current
219 * deltas. There is no synchronization so the result cannot be
220 * exactly accurate either.
222 static inline unsigned long zone_page_state_snapshot(struct zone *zone,
223 enum zone_stat_item item)
225 long x = atomic_long_read(&zone->vm_stat[item]);
229 for_each_online_cpu(cpu)
230 x += per_cpu_ptr(zone->pageset, cpu)->vm_stat_diff[item];
239 extern void __inc_numa_state(struct zone *zone, enum numa_stat_item item);
240 extern unsigned long sum_zone_node_page_state(int node,
241 enum zone_stat_item item);
242 extern unsigned long sum_zone_numa_state(int node, enum numa_stat_item item);
243 extern unsigned long node_page_state(struct pglist_data *pgdat,
244 enum node_stat_item item);
246 #define sum_zone_node_page_state(node, item) global_zone_page_state(item)
247 #define node_page_state(node, item) global_node_page_state(item)
248 #endif /* CONFIG_NUMA */
251 void __mod_zone_page_state(struct zone *, enum zone_stat_item item, long);
252 void __inc_zone_page_state(struct page *, enum zone_stat_item);
253 void __dec_zone_page_state(struct page *, enum zone_stat_item);
255 void __mod_node_page_state(struct pglist_data *, enum node_stat_item item, long);
256 void __inc_node_page_state(struct page *, enum node_stat_item);
257 void __dec_node_page_state(struct page *, enum node_stat_item);
259 void mod_zone_page_state(struct zone *, enum zone_stat_item, long);
260 void inc_zone_page_state(struct page *, enum zone_stat_item);
261 void dec_zone_page_state(struct page *, enum zone_stat_item);
263 void mod_node_page_state(struct pglist_data *, enum node_stat_item, long);
264 void inc_node_page_state(struct page *, enum node_stat_item);
265 void dec_node_page_state(struct page *, enum node_stat_item);
267 extern void inc_node_state(struct pglist_data *, enum node_stat_item);
268 extern void __inc_zone_state(struct zone *, enum zone_stat_item);
269 extern void __inc_node_state(struct pglist_data *, enum node_stat_item);
270 extern void dec_zone_state(struct zone *, enum zone_stat_item);
271 extern void __dec_zone_state(struct zone *, enum zone_stat_item);
272 extern void __dec_node_state(struct pglist_data *, enum node_stat_item);
274 void quiet_vmstat(void);
275 void cpu_vm_stats_fold(int cpu);
276 void refresh_zone_stat_thresholds(void);
279 int vmstat_refresh(struct ctl_table *, int write, void *buffer, size_t *lenp,
282 void drain_zonestat(struct zone *zone, struct per_cpu_pageset *);
284 int calculate_pressure_threshold(struct zone *zone);
285 int calculate_normal_threshold(struct zone *zone);
286 void set_pgdat_percpu_threshold(pg_data_t *pgdat,
287 int (*calculate_pressure)(struct zone *));
288 #else /* CONFIG_SMP */
291 * We do not maintain differentials in a single processor configuration.
292 * The functions directly modify the zone and global counters.
294 static inline void __mod_zone_page_state(struct zone *zone,
295 enum zone_stat_item item, long delta)
297 zone_page_state_add(delta, zone, item);
300 static inline void __mod_node_page_state(struct pglist_data *pgdat,
301 enum node_stat_item item, int delta)
303 node_page_state_add(delta, pgdat, item);
306 static inline void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
308 atomic_long_inc(&zone->vm_stat[item]);
309 atomic_long_inc(&vm_zone_stat[item]);
312 static inline void __inc_node_state(struct pglist_data *pgdat, enum node_stat_item item)
314 atomic_long_inc(&pgdat->vm_stat[item]);
315 atomic_long_inc(&vm_node_stat[item]);
318 static inline void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
320 atomic_long_dec(&zone->vm_stat[item]);
321 atomic_long_dec(&vm_zone_stat[item]);
324 static inline void __dec_node_state(struct pglist_data *pgdat, enum node_stat_item item)
326 atomic_long_dec(&pgdat->vm_stat[item]);
327 atomic_long_dec(&vm_node_stat[item]);
330 static inline void __inc_zone_page_state(struct page *page,
331 enum zone_stat_item item)
333 __inc_zone_state(page_zone(page), item);
336 static inline void __inc_node_page_state(struct page *page,
337 enum node_stat_item item)
339 __inc_node_state(page_pgdat(page), item);
343 static inline void __dec_zone_page_state(struct page *page,
344 enum zone_stat_item item)
346 __dec_zone_state(page_zone(page), item);
349 static inline void __dec_node_page_state(struct page *page,
350 enum node_stat_item item)
352 __dec_node_state(page_pgdat(page), item);
357 * We only use atomic operations to update counters. So there is no need to
358 * disable interrupts.
360 #define inc_zone_page_state __inc_zone_page_state
361 #define dec_zone_page_state __dec_zone_page_state
362 #define mod_zone_page_state __mod_zone_page_state
364 #define inc_node_page_state __inc_node_page_state
365 #define dec_node_page_state __dec_node_page_state
366 #define mod_node_page_state __mod_node_page_state
368 #define inc_zone_state __inc_zone_state
369 #define inc_node_state __inc_node_state
370 #define dec_zone_state __dec_zone_state
372 #define set_pgdat_percpu_threshold(pgdat, callback) { }
374 static inline void refresh_zone_stat_thresholds(void) { }
375 static inline void cpu_vm_stats_fold(int cpu) { }
376 static inline void quiet_vmstat(void) { }
378 static inline void drain_zonestat(struct zone *zone,
379 struct per_cpu_pageset *pset) { }
380 #endif /* CONFIG_SMP */
382 static inline void __mod_zone_freepage_state(struct zone *zone, int nr_pages,
385 __mod_zone_page_state(zone, NR_FREE_PAGES, nr_pages);
386 if (is_migrate_cma(migratetype))
387 __mod_zone_page_state(zone, NR_FREE_CMA_PAGES, nr_pages);
390 extern const char * const vmstat_text[];
392 static inline const char *zone_stat_name(enum zone_stat_item item)
394 return vmstat_text[item];
398 static inline const char *numa_stat_name(enum numa_stat_item item)
400 return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
403 #endif /* CONFIG_NUMA */
405 static inline const char *node_stat_name(enum node_stat_item item)
407 return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
408 NR_VM_NUMA_STAT_ITEMS +
412 static inline const char *lru_list_name(enum lru_list lru)
414 return node_stat_name(NR_LRU_BASE + lru) + 3; // skip "nr_"
417 static inline const char *writeback_stat_name(enum writeback_stat_item item)
419 return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
420 NR_VM_NUMA_STAT_ITEMS +
421 NR_VM_NODE_STAT_ITEMS +
425 #if defined(CONFIG_VM_EVENT_COUNTERS) || defined(CONFIG_MEMCG)
426 static inline const char *vm_event_name(enum vm_event_item item)
428 return vmstat_text[NR_VM_ZONE_STAT_ITEMS +
429 NR_VM_NUMA_STAT_ITEMS +
430 NR_VM_NODE_STAT_ITEMS +
431 NR_VM_WRITEBACK_STAT_ITEMS +
434 #endif /* CONFIG_VM_EVENT_COUNTERS || CONFIG_MEMCG */
436 #endif /* _LINUX_VMSTAT_H */