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_FALSE(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
69 extern int sysctl_delayacct(struct ctl_table *table, int write, void *buffer,
70 size_t *lenp, loff_t *ppos);
71
72 extern void __delayacct_tsk_init(struct task_struct *);
73 extern void __delayacct_tsk_exit(struct task_struct *);
74 extern void __delayacct_blkio_start(void);
75 extern void __delayacct_blkio_end(struct task_struct *);
76 extern int delayacct_add_tsk(struct taskstats *, struct task_struct *);
77 extern __u64 __delayacct_blkio_ticks(struct task_struct *);
78 extern void __delayacct_freepages_start(void);
79 extern void __delayacct_freepages_end(void);
80 extern void __delayacct_thrashing_start(void);
81 extern void __delayacct_thrashing_end(void);
82
delayacct_is_task_waiting_on_io(struct task_struct * p)83 static inline int delayacct_is_task_waiting_on_io(struct task_struct *p)
84 {
85 if (p->delays)
86 return (p->delays->flags & DELAYACCT_PF_BLKIO);
87 else
88 return 0;
89 }
90
delayacct_set_flag(struct task_struct * p,int flag)91 static inline void delayacct_set_flag(struct task_struct *p, int flag)
92 {
93 if (p->delays)
94 p->delays->flags |= flag;
95 }
96
delayacct_clear_flag(struct task_struct * p,int flag)97 static inline void delayacct_clear_flag(struct task_struct *p, int flag)
98 {
99 if (p->delays)
100 p->delays->flags &= ~flag;
101 }
102
delayacct_tsk_init(struct task_struct * tsk)103 static inline void delayacct_tsk_init(struct task_struct *tsk)
104 {
105 /* reinitialize in case parent's non-null pointer was dup'ed*/
106 tsk->delays = NULL;
107 if (delayacct_on)
108 __delayacct_tsk_init(tsk);
109 }
110
111 /* Free tsk->delays. Called from bad fork and __put_task_struct
112 * where there's no risk of tsk->delays being accessed elsewhere
113 */
delayacct_tsk_free(struct task_struct * tsk)114 static inline void delayacct_tsk_free(struct task_struct *tsk)
115 {
116 if (tsk->delays)
117 kmem_cache_free(delayacct_cache, tsk->delays);
118 tsk->delays = NULL;
119 }
120
delayacct_blkio_start(void)121 static inline void delayacct_blkio_start(void)
122 {
123 if (!static_branch_unlikely(&delayacct_key))
124 return;
125
126 delayacct_set_flag(current, DELAYACCT_PF_BLKIO);
127 if (current->delays)
128 __delayacct_blkio_start();
129 }
130
delayacct_blkio_end(struct task_struct * p)131 static inline void delayacct_blkio_end(struct task_struct *p)
132 {
133 if (!static_branch_unlikely(&delayacct_key))
134 return;
135
136 if (p->delays)
137 __delayacct_blkio_end(p);
138 delayacct_clear_flag(p, DELAYACCT_PF_BLKIO);
139 }
140
delayacct_blkio_ticks(struct task_struct * tsk)141 static inline __u64 delayacct_blkio_ticks(struct task_struct *tsk)
142 {
143 if (tsk->delays)
144 return __delayacct_blkio_ticks(tsk);
145 return 0;
146 }
147
delayacct_freepages_start(void)148 static inline void delayacct_freepages_start(void)
149 {
150 if (current->delays)
151 __delayacct_freepages_start();
152 }
153
delayacct_freepages_end(void)154 static inline void delayacct_freepages_end(void)
155 {
156 if (current->delays)
157 __delayacct_freepages_end();
158 }
159
delayacct_thrashing_start(void)160 static inline void delayacct_thrashing_start(void)
161 {
162 if (current->delays)
163 __delayacct_thrashing_start();
164 }
165
delayacct_thrashing_end(void)166 static inline void delayacct_thrashing_end(void)
167 {
168 if (current->delays)
169 __delayacct_thrashing_end();
170 }
171
172 #else
173 extern void _trace_android_vh_delayacct_set_flag(struct task_struct *p, int flag);
174 extern void _trace_android_vh_delayacct_clear_flag(struct task_struct *p, int flag);
175 extern void _trace_android_rvh_delayacct_init(void);
176 extern void _trace_android_rvh_delayacct_tsk_init(struct task_struct *tsk);
177 extern void _trace_android_rvh_delayacct_tsk_free(struct task_struct *tsk);
178 extern void _trace_android_vh_delayacct_blkio_start(void);
179 extern void _trace_android_vh_delayacct_blkio_end(struct task_struct *p);
180 extern void _trace_android_vh_delayacct_add_tsk(struct taskstats *d,
181 struct task_struct *tsk,
182 int *ret);
183 extern void _trace_android_vh_delayacct_blkio_ticks(struct task_struct *tsk, __u64 *ret);
184 extern void _trace_android_vh_delayacct_is_task_waiting_on_io(struct task_struct *p, int *ret);
185 extern void _trace_android_vh_delayacct_freepages_start(void);
186 extern void _trace_android_vh_delayacct_freepages_end(void);
187 extern void _trace_android_vh_delayacct_thrashing_start(void);
188 extern void _trace_android_vh_delayacct_thrashing_end(void);
189 extern void set_delayacct_enabled(bool enabled);
190 extern bool get_delayacct_enabled(void);
191
delayacct_set_flag(struct task_struct * p,int flag)192 static inline void delayacct_set_flag(struct task_struct *p, int flag)
193 {
194 if (get_delayacct_enabled())
195 _trace_android_vh_delayacct_set_flag(p, flag);
196 }
delayacct_clear_flag(struct task_struct * p,int flag)197 static inline void delayacct_clear_flag(struct task_struct *p, int flag)
198 {
199 if (get_delayacct_enabled())
200 _trace_android_vh_delayacct_clear_flag(p, flag);
201 }
202
delayacct_init(void)203 static inline void delayacct_init(void)
204 {
205 if (get_delayacct_enabled())
206 _trace_android_rvh_delayacct_init();
207 }
delayacct_tsk_init(struct task_struct * tsk)208 static inline void delayacct_tsk_init(struct task_struct *tsk)
209 {
210 if (get_delayacct_enabled())
211 _trace_android_rvh_delayacct_tsk_init(tsk);
212 }
delayacct_tsk_free(struct task_struct * tsk)213 static inline void delayacct_tsk_free(struct task_struct *tsk)
214 {
215 if (get_delayacct_enabled())
216 _trace_android_rvh_delayacct_tsk_free(tsk);
217 }
delayacct_blkio_start(void)218 static inline void delayacct_blkio_start(void)
219 {
220 if (get_delayacct_enabled())
221 _trace_android_vh_delayacct_blkio_start();
222 }
delayacct_blkio_end(struct task_struct * p)223 static inline void delayacct_blkio_end(struct task_struct *p)
224 {
225 if (get_delayacct_enabled())
226 _trace_android_vh_delayacct_blkio_end(p);
227 }
delayacct_add_tsk(struct taskstats * d,struct task_struct * tsk)228 static inline int delayacct_add_tsk(struct taskstats *d,
229 struct task_struct *tsk)
230 {
231 int ret = 0;
232
233 if (get_delayacct_enabled())
234 _trace_android_vh_delayacct_add_tsk(d, tsk, &ret);
235
236 return ret;
237 }
delayacct_blkio_ticks(struct task_struct * tsk)238 static inline __u64 delayacct_blkio_ticks(struct task_struct *tsk)
239 {
240 __u64 ret = 0;
241
242 if (get_delayacct_enabled())
243 _trace_android_vh_delayacct_blkio_ticks(tsk, &ret);
244
245 return ret;
246 }
delayacct_is_task_waiting_on_io(struct task_struct * p)247 static inline int delayacct_is_task_waiting_on_io(struct task_struct *p)
248 {
249 int ret = 0;
250
251 if (get_delayacct_enabled())
252 _trace_android_vh_delayacct_is_task_waiting_on_io(p, &ret);
253
254 return ret;
255 }
delayacct_freepages_start(void)256 static inline void delayacct_freepages_start(void)
257 {
258 if (get_delayacct_enabled())
259 _trace_android_vh_delayacct_freepages_start();
260 }
delayacct_freepages_end(void)261 static inline void delayacct_freepages_end(void)
262 {
263 if (get_delayacct_enabled())
264 _trace_android_vh_delayacct_freepages_end();
265 }
delayacct_thrashing_start(void)266 static inline void delayacct_thrashing_start(void)
267 {
268 if (get_delayacct_enabled())
269 _trace_android_vh_delayacct_thrashing_start();
270 }
delayacct_thrashing_end(void)271 static inline void delayacct_thrashing_end(void)
272 {
273 if (get_delayacct_enabled())
274 _trace_android_vh_delayacct_thrashing_end();
275 }
276
277 #endif /* CONFIG_TASK_DELAY_ACCT */
278
279 #endif
280