1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* delayacct.h - per-task delay accounting 3 * 4 * Copyright (C) Shailabh Nagar, IBM Corp. 2006 5 */ 6 7 #ifndef _LINUX_DELAYACCT_H 8 #define _LINUX_DELAYACCT_H 9 10 #include <uapi/linux/taskstats.h> 11 12 /* 13 * Per-task flags relevant to delay accounting 14 * maintained privately to avoid exhausting similar flags in sched.h:PF_* 15 * Used to set current->delays->flags 16 */ 17 #define DELAYACCT_PF_SWAPIN 0x00000001 /* I am doing a swapin */ 18 #define DELAYACCT_PF_BLKIO 0x00000002 /* I am waiting on IO */ 19 20 #ifdef CONFIG_TASK_DELAY_ACCT 21 struct task_delay_info { 22 raw_spinlock_t lock; 23 unsigned int flags; /* Private per-task flags */ 24 25 /* For each stat XXX, add following, aligned appropriately 26 * 27 * struct timespec XXX_start, XXX_end; 28 * u64 XXX_delay; 29 * u32 XXX_count; 30 * 31 * Atomicity of updates to XXX_delay, XXX_count protected by 32 * single lock above (split into XXX_lock if contention is an issue). 33 */ 34 35 /* 36 * XXX_count is incremented on every XXX operation, the delay 37 * associated with the operation is added to XXX_delay. 38 * XXX_delay contains the accumulated delay time in nanoseconds. 39 */ 40 u64 blkio_start; /* Shared by blkio, swapin */ 41 u64 blkio_delay; /* wait for sync block io completion */ 42 u64 swapin_delay; /* wait for swapin block io completion */ 43 u32 blkio_count; /* total count of the number of sync block */ 44 /* io operations performed */ 45 u32 swapin_count; /* total count of the number of swapin block */ 46 /* io operations performed */ 47 48 u64 freepages_start; 49 u64 freepages_delay; /* wait for memory reclaim */ 50 51 u64 thrashing_start; 52 u64 thrashing_delay; /* wait for thrashing page */ 53 54 u32 freepages_count; /* total count of memory reclaim */ 55 u32 thrashing_count; /* total count of thrash waits */ 56 }; 57 #endif 58 59 #include <linux/sched.h> 60 #include <linux/slab.h> 61 #include <linux/jump_label.h> 62 63 #ifdef CONFIG_TASK_DELAY_ACCT 64 DECLARE_STATIC_KEY_TRUE(delayacct_key); 65 extern int delayacct_on; /* Delay accounting turned on/off */ 66 extern struct kmem_cache *delayacct_cache; 67 extern void delayacct_init(void); 68 extern void __delayacct_tsk_init(struct task_struct *); 69 extern void __delayacct_tsk_exit(struct task_struct *); 70 extern void __delayacct_blkio_start(void); 71 extern void __delayacct_blkio_end(struct task_struct *); 72 extern int __delayacct_add_tsk(struct taskstats *, struct task_struct *); 73 extern __u64 __delayacct_blkio_ticks(struct task_struct *); 74 extern void __delayacct_freepages_start(void); 75 extern void __delayacct_freepages_end(void); 76 extern void __delayacct_thrashing_start(void); 77 extern void __delayacct_thrashing_end(void); 78 79 static inline int delayacct_is_task_waiting_on_io(struct task_struct *p) 80 { 81 if (p->delays) 82 return (p->delays->flags & DELAYACCT_PF_BLKIO); 83 else 84 return 0; 85 } 86 87 static inline void delayacct_set_flag(struct task_struct *p, int flag) 88 { 89 if (p->delays) 90 p->delays->flags |= flag; 91 } 92 93 static inline void delayacct_clear_flag(struct task_struct *p, int flag) 94 { 95 if (p->delays) 96 p->delays->flags &= ~flag; 97 } 98 99 static inline void delayacct_tsk_init(struct task_struct *tsk) 100 { 101 /* reinitialize in case parent's non-null pointer was dup'ed*/ 102 tsk->delays = NULL; 103 if (delayacct_on) 104 __delayacct_tsk_init(tsk); 105 } 106 107 /* Free tsk->delays. Called from bad fork and __put_task_struct 108 * where there's no risk of tsk->delays being accessed elsewhere 109 */ 110 static inline void delayacct_tsk_free(struct task_struct *tsk) 111 { 112 if (tsk->delays) 113 kmem_cache_free(delayacct_cache, tsk->delays); 114 tsk->delays = NULL; 115 } 116 117 static inline void delayacct_blkio_start(void) 118 { 119 if (!static_branch_likely(&delayacct_key)) 120 return; 121 122 delayacct_set_flag(current, DELAYACCT_PF_BLKIO); 123 if (current->delays) 124 __delayacct_blkio_start(); 125 } 126 127 static inline void delayacct_blkio_end(struct task_struct *p) 128 { 129 if (!static_branch_likely(&delayacct_key)) 130 return; 131 132 if (p->delays) 133 __delayacct_blkio_end(p); 134 delayacct_clear_flag(p, DELAYACCT_PF_BLKIO); 135 } 136 137 static inline int delayacct_add_tsk(struct taskstats *d, 138 struct task_struct *tsk) 139 { 140 if (!delayacct_on || !tsk->delays) 141 return 0; 142 return __delayacct_add_tsk(d, tsk); 143 } 144 145 static inline __u64 delayacct_blkio_ticks(struct task_struct *tsk) 146 { 147 if (tsk->delays) 148 return __delayacct_blkio_ticks(tsk); 149 return 0; 150 } 151 152 static inline void delayacct_freepages_start(void) 153 { 154 if (current->delays) 155 __delayacct_freepages_start(); 156 } 157 158 static inline void delayacct_freepages_end(void) 159 { 160 if (current->delays) 161 __delayacct_freepages_end(); 162 } 163 164 static inline void delayacct_thrashing_start(void) 165 { 166 if (current->delays) 167 __delayacct_thrashing_start(); 168 } 169 170 static inline void delayacct_thrashing_end(void) 171 { 172 if (current->delays) 173 __delayacct_thrashing_end(); 174 } 175 176 #else 177 static inline void delayacct_set_flag(struct task_struct *p, int flag) 178 {} 179 static inline void delayacct_clear_flag(struct task_struct *p, int flag) 180 {} 181 static inline void delayacct_init(void) 182 {} 183 static inline void delayacct_tsk_init(struct task_struct *tsk) 184 {} 185 static inline void delayacct_tsk_free(struct task_struct *tsk) 186 {} 187 static inline void delayacct_blkio_start(void) 188 {} 189 static inline void delayacct_blkio_end(struct task_struct *p) 190 {} 191 static inline int delayacct_add_tsk(struct taskstats *d, 192 struct task_struct *tsk) 193 { return 0; } 194 static inline __u64 delayacct_blkio_ticks(struct task_struct *tsk) 195 { return 0; } 196 static inline int delayacct_is_task_waiting_on_io(struct task_struct *p) 197 { return 0; } 198 static inline void delayacct_freepages_start(void) 199 {} 200 static inline void delayacct_freepages_end(void) 201 {} 202 static inline void delayacct_thrashing_start(void) 203 {} 204 static inline void delayacct_thrashing_end(void) 205 {} 206 207 #endif /* CONFIG_TASK_DELAY_ACCT */ 208 209 #endif 210