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