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
13struct 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 u64 wpcopy_start;
49 u64 wpcopy_delay; /* wait for write-protect copy */
50
51 u32 freepages_count; /* total count of memory reclaim */
52 u32 thrashing_count; /* total count of thrash waits */
53 u32 compact_count; /* total count of memory compact */
54 u32 wpcopy_count; /* total count of write-protect copy */
55};
56#endif
57
58#include <linux/sched.h>
59#include <linux/slab.h>
60#include <linux/jump_label.h>
61
62#ifdef CONFIG_TASK_DELAY_ACCT
63DECLARE_STATIC_KEY_FALSE(delayacct_key);
64extern int delayacct_on; /* Delay accounting turned on/off */
65extern struct kmem_cache *delayacct_cache;
66extern void delayacct_init(void);
67
68extern void __delayacct_tsk_init(struct task_struct *);
69extern void __delayacct_tsk_exit(struct task_struct *);
70extern void __delayacct_blkio_start(void);
71extern void __delayacct_blkio_end(struct task_struct *);
72extern int delayacct_add_tsk(struct taskstats *, struct task_struct *);
73extern __u64 __delayacct_blkio_ticks(struct task_struct *);
74extern void __delayacct_freepages_start(void);
75extern void __delayacct_freepages_end(void);
76extern void __delayacct_thrashing_start(void);
77extern void __delayacct_thrashing_end(void);
78extern void __delayacct_swapin_start(void);
79extern void __delayacct_swapin_end(void);
80extern void __delayacct_compact_start(void);
81extern void __delayacct_compact_end(void);
82extern void __delayacct_wpcopy_start(void);
83extern void __delayacct_wpcopy_end(void);
84
85static inline void delayacct_tsk_init(struct task_struct *tsk)
86{
87 /* reinitialize in case parent's non-null pointer was dup'ed*/
88 tsk->delays = NULL;
89 if (delayacct_on)
90 __delayacct_tsk_init(tsk);
91}
92
93/* Free tsk->delays. Called from bad fork and __put_task_struct
94 * where there's no risk of tsk->delays being accessed elsewhere
95 */
96static inline void delayacct_tsk_free(struct task_struct *tsk)
97{
98 if (tsk->delays)
99 kmem_cache_free(delayacct_cache, tsk->delays);
100 tsk->delays = NULL;
101}
102
103static inline void delayacct_blkio_start(void)
104{
105 if (!static_branch_unlikely(&delayacct_key))
106 return;
107
108 if (current->delays)
109 __delayacct_blkio_start();
110}
111
112static inline void delayacct_blkio_end(struct task_struct *p)
113{
114 if (!static_branch_unlikely(&delayacct_key))
115 return;
116
117 if (p->delays)
118 __delayacct_blkio_end(p);
119}
120
121static inline __u64 delayacct_blkio_ticks(struct task_struct *tsk)
122{
123 if (tsk->delays)
124 return __delayacct_blkio_ticks(tsk);
125 return 0;
126}
127
128static inline void delayacct_freepages_start(void)
129{
130 if (!static_branch_unlikely(&delayacct_key))
131 return;
132
133 if (current->delays)
134 __delayacct_freepages_start();
135}
136
137static inline void delayacct_freepages_end(void)
138{
139 if (!static_branch_unlikely(&delayacct_key))
140 return;
141
142 if (current->delays)
143 __delayacct_freepages_end();
144}
145
146static inline void delayacct_thrashing_start(void)
147{
148 if (!static_branch_unlikely(&delayacct_key))
149 return;
150
151 if (current->delays)
152 __delayacct_thrashing_start();
153}
154
155static inline void delayacct_thrashing_end(void)
156{
157 if (!static_branch_unlikely(&delayacct_key))
158 return;
159
160 if (current->delays)
161 __delayacct_thrashing_end();
162}
163
164static inline void delayacct_swapin_start(void)
165{
166 if (!static_branch_unlikely(&delayacct_key))
167 return;
168
169 if (current->delays)
170 __delayacct_swapin_start();
171}
172
173static inline void delayacct_swapin_end(void)
174{
175 if (!static_branch_unlikely(&delayacct_key))
176 return;
177
178 if (current->delays)
179 __delayacct_swapin_end();
180}
181
182static inline void delayacct_compact_start(void)
183{
184 if (!static_branch_unlikely(&delayacct_key))
185 return;
186
187 if (current->delays)
188 __delayacct_compact_start();
189}
190
191static inline void delayacct_compact_end(void)
192{
193 if (!static_branch_unlikely(&delayacct_key))
194 return;
195
196 if (current->delays)
197 __delayacct_compact_end();
198}
199
200static inline void delayacct_wpcopy_start(void)
201{
202 if (!static_branch_unlikely(&delayacct_key))
203 return;
204
205 if (current->delays)
206 __delayacct_wpcopy_start();
207}
208
209static inline void delayacct_wpcopy_end(void)
210{
211 if (!static_branch_unlikely(&delayacct_key))
212 return;
213
214 if (current->delays)
215 __delayacct_wpcopy_end();
216}
217
218#else
219static inline void delayacct_init(void)
220{}
221static inline void delayacct_tsk_init(struct task_struct *tsk)
222{}
223static inline void delayacct_tsk_free(struct task_struct *tsk)
224{}
225static inline void delayacct_blkio_start(void)
226{}
227static inline void delayacct_blkio_end(struct task_struct *p)
228{}
229static inline int delayacct_add_tsk(struct taskstats *d,
230 struct task_struct *tsk)
231{ return 0; }
232static inline __u64 delayacct_blkio_ticks(struct task_struct *tsk)
233{ return 0; }
234static inline int delayacct_is_task_waiting_on_io(struct task_struct *p)
235{ return 0; }
236static inline void delayacct_freepages_start(void)
237{}
238static inline void delayacct_freepages_end(void)
239{}
240static inline void delayacct_thrashing_start(void)
241{}
242static inline void delayacct_thrashing_end(void)
243{}
244static inline void delayacct_swapin_start(void)
245{}
246static inline void delayacct_swapin_end(void)
247{}
248static inline void delayacct_compact_start(void)
249{}
250static inline void delayacct_compact_end(void)
251{}
252static inline void delayacct_wpcopy_start(void)
253{}
254static inline void delayacct_wpcopy_end(void)
255{}
256
257#endif /* CONFIG_TASK_DELAY_ACCT */
258
259#endif
260

source code of linux/include/linux/delayacct.h