xref: /linux-6.15/include/linux/delayacct.h (revision 1193829d)
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 	u32 freepages_count;	/* total count of memory reclaim */
46 	u32 thrashing_count;	/* total count of thrash waits */
47 };
48 #endif
49 
50 #include <linux/sched.h>
51 #include <linux/slab.h>
52 #include <linux/jump_label.h>
53 
54 #ifdef CONFIG_TASK_DELAY_ACCT
55 DECLARE_STATIC_KEY_FALSE(delayacct_key);
56 extern int delayacct_on;	/* Delay accounting turned on/off */
57 extern struct kmem_cache *delayacct_cache;
58 extern void delayacct_init(void);
59 
60 extern int sysctl_delayacct(struct ctl_table *table, int write, void *buffer,
61 			    size_t *lenp, loff_t *ppos);
62 
63 extern void __delayacct_tsk_init(struct task_struct *);
64 extern void __delayacct_tsk_exit(struct task_struct *);
65 extern void __delayacct_blkio_start(void);
66 extern void __delayacct_blkio_end(struct task_struct *);
67 extern int delayacct_add_tsk(struct taskstats *, struct task_struct *);
68 extern __u64 __delayacct_blkio_ticks(struct task_struct *);
69 extern void __delayacct_freepages_start(void);
70 extern void __delayacct_freepages_end(void);
71 extern void __delayacct_thrashing_start(void);
72 extern void __delayacct_thrashing_end(void);
73 extern void __delayacct_swapin_start(void);
74 extern void __delayacct_swapin_end(void);
75 
76 static inline void delayacct_tsk_init(struct task_struct *tsk)
77 {
78 	/* reinitialize in case parent's non-null pointer was dup'ed*/
79 	tsk->delays = NULL;
80 	if (delayacct_on)
81 		__delayacct_tsk_init(tsk);
82 }
83 
84 /* Free tsk->delays. Called from bad fork and __put_task_struct
85  * where there's no risk of tsk->delays being accessed elsewhere
86  */
87 static inline void delayacct_tsk_free(struct task_struct *tsk)
88 {
89 	if (tsk->delays)
90 		kmem_cache_free(delayacct_cache, tsk->delays);
91 	tsk->delays = NULL;
92 }
93 
94 static inline void delayacct_blkio_start(void)
95 {
96 	if (!static_branch_unlikely(&delayacct_key))
97 		return;
98 
99 	if (current->delays)
100 		__delayacct_blkio_start();
101 }
102 
103 static inline void delayacct_blkio_end(struct task_struct *p)
104 {
105 	if (!static_branch_unlikely(&delayacct_key))
106 		return;
107 
108 	if (p->delays)
109 		__delayacct_blkio_end(p);
110 }
111 
112 static inline __u64 delayacct_blkio_ticks(struct task_struct *tsk)
113 {
114 	if (tsk->delays)
115 		return __delayacct_blkio_ticks(tsk);
116 	return 0;
117 }
118 
119 static inline void delayacct_freepages_start(void)
120 {
121 	if (!static_branch_unlikely(&delayacct_key))
122 		return;
123 
124 	if (current->delays)
125 		__delayacct_freepages_start();
126 }
127 
128 static inline void delayacct_freepages_end(void)
129 {
130 	if (!static_branch_unlikely(&delayacct_key))
131 		return;
132 
133 	if (current->delays)
134 		__delayacct_freepages_end();
135 }
136 
137 static inline void delayacct_thrashing_start(void)
138 {
139 	if (!static_branch_unlikely(&delayacct_key))
140 		return;
141 
142 	if (current->delays)
143 		__delayacct_thrashing_start();
144 }
145 
146 static inline void delayacct_thrashing_end(void)
147 {
148 	if (!static_branch_unlikely(&delayacct_key))
149 		return;
150 
151 	if (current->delays)
152 		__delayacct_thrashing_end();
153 }
154 
155 static inline void delayacct_swapin_start(void)
156 {
157 	if (!static_branch_unlikely(&delayacct_key))
158 		return;
159 
160 	if (current->delays)
161 		__delayacct_swapin_start();
162 }
163 
164 static inline void delayacct_swapin_end(void)
165 {
166 	if (!static_branch_unlikely(&delayacct_key))
167 		return;
168 
169 	if (current->delays)
170 		__delayacct_swapin_end();
171 }
172 
173 #else
174 static inline void delayacct_init(void)
175 {}
176 static inline void delayacct_tsk_init(struct task_struct *tsk)
177 {}
178 static inline void delayacct_tsk_free(struct task_struct *tsk)
179 {}
180 static inline void delayacct_blkio_start(void)
181 {}
182 static inline void delayacct_blkio_end(struct task_struct *p)
183 {}
184 static inline int delayacct_add_tsk(struct taskstats *d,
185 					struct task_struct *tsk)
186 { return 0; }
187 static inline __u64 delayacct_blkio_ticks(struct task_struct *tsk)
188 { return 0; }
189 static inline int delayacct_is_task_waiting_on_io(struct task_struct *p)
190 { return 0; }
191 static inline void delayacct_freepages_start(void)
192 {}
193 static inline void delayacct_freepages_end(void)
194 {}
195 static inline void delayacct_thrashing_start(void)
196 {}
197 static inline void delayacct_thrashing_end(void)
198 {}
199 static inline void delayacct_swapin_start(void)
200 {}
201 static inline void delayacct_swapin_end(void)
202 {}
203 
204 #endif /* CONFIG_TASK_DELAY_ACCT */
205 
206 #endif
207