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