xref: /linux-6.15/include/linux/delayacct.h (revision 82065b72)
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 (!static_branch_unlikely(&delayacct_key))
135 		return;
136 
137 	if (current->delays)
138 		__delayacct_freepages_start();
139 }
140 
141 static inline void delayacct_freepages_end(void)
142 {
143 	if (!static_branch_unlikely(&delayacct_key))
144 		return;
145 
146 	if (current->delays)
147 		__delayacct_freepages_end();
148 }
149 
150 static inline void delayacct_thrashing_start(void)
151 {
152 	if (!static_branch_unlikely(&delayacct_key))
153 		return;
154 
155 	if (current->delays)
156 		__delayacct_thrashing_start();
157 }
158 
159 static inline void delayacct_thrashing_end(void)
160 {
161 	if (!static_branch_unlikely(&delayacct_key))
162 		return;
163 
164 	if (current->delays)
165 		__delayacct_thrashing_end();
166 }
167 
168 static inline void delayacct_swapin_start(void)
169 {
170 	if (!static_branch_unlikely(&delayacct_key))
171 		return;
172 
173 	if (current->delays)
174 		__delayacct_swapin_start();
175 }
176 
177 static inline void delayacct_swapin_end(void)
178 {
179 	if (!static_branch_unlikely(&delayacct_key))
180 		return;
181 
182 	if (current->delays)
183 		__delayacct_swapin_end();
184 }
185 
186 #else
187 static inline void delayacct_set_flag(struct task_struct *p, int flag)
188 {}
189 static inline void delayacct_clear_flag(struct task_struct *p, int flag)
190 {}
191 static inline void delayacct_init(void)
192 {}
193 static inline void delayacct_tsk_init(struct task_struct *tsk)
194 {}
195 static inline void delayacct_tsk_free(struct task_struct *tsk)
196 {}
197 static inline void delayacct_blkio_start(void)
198 {}
199 static inline void delayacct_blkio_end(struct task_struct *p)
200 {}
201 static inline int delayacct_add_tsk(struct taskstats *d,
202 					struct task_struct *tsk)
203 { return 0; }
204 static inline __u64 delayacct_blkio_ticks(struct task_struct *tsk)
205 { return 0; }
206 static inline int delayacct_is_task_waiting_on_io(struct task_struct *p)
207 { return 0; }
208 static inline void delayacct_freepages_start(void)
209 {}
210 static inline void delayacct_freepages_end(void)
211 {}
212 static inline void delayacct_thrashing_start(void)
213 {}
214 static inline void delayacct_thrashing_end(void)
215 {}
216 static inline void delayacct_swapin_start(void)
217 {}
218 static inline void delayacct_swapin_end(void)
219 {}
220 
221 #endif /* CONFIG_TASK_DELAY_ACCT */
222 
223 #endif
224