vmstat.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #ifndef _LINUX_VMSTAT_H
  2. #define _LINUX_VMSTAT_H
  3. #include <linux/types.h>
  4. #include <linux/percpu.h>
  5. #include <linux/mm.h>
  6. #include <linux/mmzone.h>
  7. #include <linux/vm_event_item.h>
  8. #include <linux/atomic.h>
  9. extern int sysctl_stat_interval;
  10. #ifdef CONFIG_VM_EVENT_COUNTERS
  11. /*
  12. * Light weight per cpu counter implementation.
  13. *
  14. * Counters should only be incremented and no critical kernel component
  15. * should rely on the counter values.
  16. *
  17. * Counters are handled completely inline. On many platforms the code
  18. * generated will simply be the increment of a global address.
  19. */
  20. struct vm_event_state {
  21. unsigned long event[NR_VM_EVENT_ITEMS];
  22. };
  23. DECLARE_PER_CPU(struct vm_event_state, vm_event_states);
  24. /*
  25. * vm counters are allowed to be racy. Use raw_cpu_ops to avoid the
  26. * local_irq_disable overhead.
  27. */
  28. static inline void __count_vm_event(enum vm_event_item item)
  29. {
  30. raw_cpu_inc(vm_event_states.event[item]);
  31. }
  32. static inline void count_vm_event(enum vm_event_item item)
  33. {
  34. this_cpu_inc(vm_event_states.event[item]);
  35. }
  36. static inline void __count_vm_events(enum vm_event_item item, long delta)
  37. {
  38. raw_cpu_add(vm_event_states.event[item], delta);
  39. }
  40. static inline void count_vm_events(enum vm_event_item item, long delta)
  41. {
  42. this_cpu_add(vm_event_states.event[item], delta);
  43. }
  44. extern void all_vm_events(unsigned long *);
  45. extern void vm_events_fold_cpu(int cpu);
  46. #else
  47. /* Disable counters */
  48. static inline void count_vm_event(enum vm_event_item item)
  49. {
  50. }
  51. static inline void count_vm_events(enum vm_event_item item, long delta)
  52. {
  53. }
  54. static inline void __count_vm_event(enum vm_event_item item)
  55. {
  56. }
  57. static inline void __count_vm_events(enum vm_event_item item, long delta)
  58. {
  59. }
  60. static inline void all_vm_events(unsigned long *ret)
  61. {
  62. }
  63. static inline void vm_events_fold_cpu(int cpu)
  64. {
  65. }
  66. #endif /* CONFIG_VM_EVENT_COUNTERS */
  67. #ifdef CONFIG_NUMA_BALANCING
  68. #define count_vm_numa_event(x) count_vm_event(x)
  69. #define count_vm_numa_events(x, y) count_vm_events(x, y)
  70. #else
  71. #define count_vm_numa_event(x) do {} while (0)
  72. #define count_vm_numa_events(x, y) do { (void)(y); } while (0)
  73. #endif /* CONFIG_NUMA_BALANCING */
  74. #ifdef CONFIG_DEBUG_TLBFLUSH
  75. #define count_vm_tlb_event(x) count_vm_event(x)
  76. #define count_vm_tlb_events(x, y) count_vm_events(x, y)
  77. #else
  78. #define count_vm_tlb_event(x) do {} while (0)
  79. #define count_vm_tlb_events(x, y) do { (void)(y); } while (0)
  80. #endif
  81. #ifdef CONFIG_DEBUG_VM_VMACACHE
  82. #define count_vm_vmacache_event(x) count_vm_event(x)
  83. #else
  84. #define count_vm_vmacache_event(x) do {} while (0)
  85. #endif
  86. #define __count_zone_vm_events(item, zone, delta) \
  87. __count_vm_events(item##_NORMAL - ZONE_NORMAL + \
  88. zone_idx(zone), delta)
  89. /*
  90. * Zone based page accounting with per cpu differentials.
  91. */
  92. extern atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
  93. static inline void zone_page_state_add(long x, struct zone *zone,
  94. enum zone_stat_item item)
  95. {
  96. atomic_long_add(x, &zone->vm_stat[item]);
  97. atomic_long_add(x, &vm_stat[item]);
  98. }
  99. static inline unsigned long global_page_state(enum zone_stat_item item)
  100. {
  101. long x = atomic_long_read(&vm_stat[item]);
  102. #ifdef CONFIG_SMP
  103. if (x < 0)
  104. x = 0;
  105. #endif
  106. return x;
  107. }
  108. static inline unsigned long zone_page_state(struct zone *zone,
  109. enum zone_stat_item item)
  110. {
  111. long x = atomic_long_read(&zone->vm_stat[item]);
  112. #ifdef CONFIG_SMP
  113. if (x < 0)
  114. x = 0;
  115. #endif
  116. return x;
  117. }
  118. /*
  119. * More accurate version that also considers the currently pending
  120. * deltas. For that we need to loop over all cpus to find the current
  121. * deltas. There is no synchronization so the result cannot be
  122. * exactly accurate either.
  123. */
  124. static inline unsigned long zone_page_state_snapshot(struct zone *zone,
  125. enum zone_stat_item item)
  126. {
  127. long x = atomic_long_read(&zone->vm_stat[item]);
  128. #ifdef CONFIG_SMP
  129. int cpu;
  130. for_each_online_cpu(cpu)
  131. x += per_cpu_ptr(zone->pageset, cpu)->vm_stat_diff[item];
  132. if (x < 0)
  133. x = 0;
  134. #endif
  135. return x;
  136. }
  137. #ifdef CONFIG_NUMA
  138. /*
  139. * Determine the per node value of a stat item. This function
  140. * is called frequently in a NUMA machine, so try to be as
  141. * frugal as possible.
  142. */
  143. static inline unsigned long node_page_state(int node,
  144. enum zone_stat_item item)
  145. {
  146. struct zone *zones = NODE_DATA(node)->node_zones;
  147. return
  148. #ifdef CONFIG_ZONE_DMA
  149. zone_page_state(&zones[ZONE_DMA], item) +
  150. #endif
  151. #ifdef CONFIG_ZONE_DMA32
  152. zone_page_state(&zones[ZONE_DMA32], item) +
  153. #endif
  154. #ifdef CONFIG_HIGHMEM
  155. zone_page_state(&zones[ZONE_HIGHMEM], item) +
  156. #endif
  157. zone_page_state(&zones[ZONE_NORMAL], item) +
  158. zone_page_state(&zones[ZONE_MOVABLE], item);
  159. }
  160. extern void zone_statistics(struct zone *, struct zone *, gfp_t gfp);
  161. #else
  162. #define node_page_state(node, item) global_page_state(item)
  163. #define zone_statistics(_zl, _z, gfp) do { } while (0)
  164. #endif /* CONFIG_NUMA */
  165. #define add_zone_page_state(__z, __i, __d) mod_zone_page_state(__z, __i, __d)
  166. #define sub_zone_page_state(__z, __i, __d) mod_zone_page_state(__z, __i, -(__d))
  167. #ifdef CONFIG_SMP
  168. void __mod_zone_page_state(struct zone *, enum zone_stat_item item, int);
  169. void __inc_zone_page_state(struct page *, enum zone_stat_item);
  170. void __dec_zone_page_state(struct page *, enum zone_stat_item);
  171. void mod_zone_page_state(struct zone *, enum zone_stat_item, int);
  172. void inc_zone_page_state(struct page *, enum zone_stat_item);
  173. void dec_zone_page_state(struct page *, enum zone_stat_item);
  174. extern void inc_zone_state(struct zone *, enum zone_stat_item);
  175. extern void __inc_zone_state(struct zone *, enum zone_stat_item);
  176. extern void dec_zone_state(struct zone *, enum zone_stat_item);
  177. extern void __dec_zone_state(struct zone *, enum zone_stat_item);
  178. void cpu_vm_stats_fold(int cpu);
  179. void refresh_zone_stat_thresholds(void);
  180. void drain_zonestat(struct zone *zone, struct per_cpu_pageset *);
  181. int calculate_pressure_threshold(struct zone *zone);
  182. int calculate_normal_threshold(struct zone *zone);
  183. void set_pgdat_percpu_threshold(pg_data_t *pgdat,
  184. int (*calculate_pressure)(struct zone *));
  185. #else /* CONFIG_SMP */
  186. /*
  187. * We do not maintain differentials in a single processor configuration.
  188. * The functions directly modify the zone and global counters.
  189. */
  190. static inline void __mod_zone_page_state(struct zone *zone,
  191. enum zone_stat_item item, int delta)
  192. {
  193. zone_page_state_add(delta, zone, item);
  194. }
  195. static inline void __inc_zone_state(struct zone *zone, enum zone_stat_item item)
  196. {
  197. atomic_long_inc(&zone->vm_stat[item]);
  198. atomic_long_inc(&vm_stat[item]);
  199. }
  200. static inline void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
  201. {
  202. atomic_long_dec(&zone->vm_stat[item]);
  203. atomic_long_dec(&vm_stat[item]);
  204. }
  205. static inline void __inc_zone_page_state(struct page *page,
  206. enum zone_stat_item item)
  207. {
  208. __inc_zone_state(page_zone(page), item);
  209. }
  210. static inline void __dec_zone_page_state(struct page *page,
  211. enum zone_stat_item item)
  212. {
  213. __dec_zone_state(page_zone(page), item);
  214. }
  215. /*
  216. * We only use atomic operations to update counters. So there is no need to
  217. * disable interrupts.
  218. */
  219. #define inc_zone_page_state __inc_zone_page_state
  220. #define dec_zone_page_state __dec_zone_page_state
  221. #define mod_zone_page_state __mod_zone_page_state
  222. #define inc_zone_state __inc_zone_state
  223. #define dec_zone_state __dec_zone_state
  224. #define set_pgdat_percpu_threshold(pgdat, callback) { }
  225. static inline void refresh_cpu_vm_stats(int cpu) { }
  226. static inline void refresh_zone_stat_thresholds(void) { }
  227. static inline void cpu_vm_stats_fold(int cpu) { }
  228. static inline void drain_zonestat(struct zone *zone,
  229. struct per_cpu_pageset *pset) { }
  230. #endif /* CONFIG_SMP */
  231. static inline void __mod_zone_freepage_state(struct zone *zone, int nr_pages,
  232. int migratetype)
  233. {
  234. __mod_zone_page_state(zone, NR_FREE_PAGES, nr_pages);
  235. if (is_migrate_cma(migratetype))
  236. __mod_zone_page_state(zone, NR_FREE_CMA_PAGES, nr_pages);
  237. }
  238. extern const char * const vmstat_text[];
  239. #endif /* _LINUX_VMSTAT_H */