1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* delayacct.c - per-task delay accounting
4 * Copyright (C) Shailabh Nagar, IBM Corp. 2006
7 #include <linux/sched.h>
8 #include <linux/sched/task.h>
9 #include <linux/sched/cputime.h>
10 #include <linux/sched/clock.h>
11 #include <linux/slab.h>
12 #include <linux/taskstats.h>
13 #include <linux/sysctl.h>
14 #include <linux/delayacct.h>
15 #include <linux/module.h>
17 DEFINE_STATIC_KEY_FALSE(delayacct_key
);
18 int delayacct_on __read_mostly
; /* Delay accounting turned on/off */
19 struct kmem_cache
*delayacct_cache
;
21 static void set_delayacct(bool enabled
)
24 static_branch_enable(&delayacct_key
);
28 static_branch_disable(&delayacct_key
);
32 static int __init
delayacct_setup_enable(char *str
)
37 __setup("delayacct", delayacct_setup_enable
);
39 void delayacct_init(void)
41 delayacct_cache
= KMEM_CACHE(task_delay_info
, SLAB_PANIC
|SLAB_ACCOUNT
);
42 delayacct_tsk_init(&init_task
);
43 set_delayacct(delayacct_on
);
46 #ifdef CONFIG_PROC_SYSCTL
47 static int sysctl_delayacct(const struct ctl_table
*table
, int write
, void *buffer
,
48 size_t *lenp
, loff_t
*ppos
)
50 int state
= delayacct_on
;
54 if (write
&& !capable(CAP_SYS_ADMIN
))
59 err
= proc_dointvec_minmax(&t
, write
, buffer
, lenp
, ppos
);
67 static struct ctl_table kern_delayacct_table
[] = {
69 .procname
= "task_delayacct",
71 .maxlen
= sizeof(unsigned int),
73 .proc_handler
= sysctl_delayacct
,
74 .extra1
= SYSCTL_ZERO
,
79 static __init
int kernel_delayacct_sysctls_init(void)
81 register_sysctl_init("kernel", kern_delayacct_table
);
84 late_initcall(kernel_delayacct_sysctls_init
);
87 void __delayacct_tsk_init(struct task_struct
*tsk
)
89 tsk
->delays
= kmem_cache_zalloc(delayacct_cache
, GFP_KERNEL
);
91 raw_spin_lock_init(&tsk
->delays
->lock
);
95 * Finish delay accounting for a statistic using its timestamps (@start),
96 * accumalator (@total) and @count
98 static void delayacct_end(raw_spinlock_t
*lock
, u64
*start
, u64
*total
, u32
*count
)
100 s64 ns
= local_clock() - *start
;
104 raw_spin_lock_irqsave(lock
, flags
);
107 raw_spin_unlock_irqrestore(lock
, flags
);
111 void __delayacct_blkio_start(void)
113 current
->delays
->blkio_start
= local_clock();
117 * We cannot rely on the `current` macro, as we haven't yet switched back to
118 * the process being woken.
120 void __delayacct_blkio_end(struct task_struct
*p
)
122 delayacct_end(&p
->delays
->lock
,
123 &p
->delays
->blkio_start
,
124 &p
->delays
->blkio_delay
,
125 &p
->delays
->blkio_count
);
128 int delayacct_add_tsk(struct taskstats
*d
, struct task_struct
*tsk
)
130 u64 utime
, stime
, stimescaled
, utimescaled
;
131 unsigned long long t2
, t3
;
132 unsigned long flags
, t1
;
135 task_cputime(tsk
, &utime
, &stime
);
136 tmp
= (s64
)d
->cpu_run_real_total
;
137 tmp
+= utime
+ stime
;
138 d
->cpu_run_real_total
= (tmp
< (s64
)d
->cpu_run_real_total
) ? 0 : tmp
;
140 task_cputime_scaled(tsk
, &utimescaled
, &stimescaled
);
141 tmp
= (s64
)d
->cpu_scaled_run_real_total
;
142 tmp
+= utimescaled
+ stimescaled
;
143 d
->cpu_scaled_run_real_total
=
144 (tmp
< (s64
)d
->cpu_scaled_run_real_total
) ? 0 : tmp
;
147 * No locking available for sched_info (and too expensive to add one)
148 * Mitigate by taking snapshot of values
150 t1
= tsk
->sched_info
.pcount
;
151 t2
= tsk
->sched_info
.run_delay
;
152 t3
= tsk
->se
.sum_exec_runtime
;
156 tmp
= (s64
)d
->cpu_delay_total
+ t2
;
157 d
->cpu_delay_total
= (tmp
< (s64
)d
->cpu_delay_total
) ? 0 : tmp
;
159 tmp
= (s64
)d
->cpu_run_virtual_total
+ t3
;
160 d
->cpu_run_virtual_total
=
161 (tmp
< (s64
)d
->cpu_run_virtual_total
) ? 0 : tmp
;
166 /* zero XXX_total, non-zero XXX_count implies XXX stat overflowed */
168 raw_spin_lock_irqsave(&tsk
->delays
->lock
, flags
);
169 tmp
= d
->blkio_delay_total
+ tsk
->delays
->blkio_delay
;
170 d
->blkio_delay_total
= (tmp
< d
->blkio_delay_total
) ? 0 : tmp
;
171 tmp
= d
->swapin_delay_total
+ tsk
->delays
->swapin_delay
;
172 d
->swapin_delay_total
= (tmp
< d
->swapin_delay_total
) ? 0 : tmp
;
173 tmp
= d
->freepages_delay_total
+ tsk
->delays
->freepages_delay
;
174 d
->freepages_delay_total
= (tmp
< d
->freepages_delay_total
) ? 0 : tmp
;
175 tmp
= d
->thrashing_delay_total
+ tsk
->delays
->thrashing_delay
;
176 d
->thrashing_delay_total
= (tmp
< d
->thrashing_delay_total
) ? 0 : tmp
;
177 tmp
= d
->compact_delay_total
+ tsk
->delays
->compact_delay
;
178 d
->compact_delay_total
= (tmp
< d
->compact_delay_total
) ? 0 : tmp
;
179 tmp
= d
->wpcopy_delay_total
+ tsk
->delays
->wpcopy_delay
;
180 d
->wpcopy_delay_total
= (tmp
< d
->wpcopy_delay_total
) ? 0 : tmp
;
181 tmp
= d
->irq_delay_total
+ tsk
->delays
->irq_delay
;
182 d
->irq_delay_total
= (tmp
< d
->irq_delay_total
) ? 0 : tmp
;
183 d
->blkio_count
+= tsk
->delays
->blkio_count
;
184 d
->swapin_count
+= tsk
->delays
->swapin_count
;
185 d
->freepages_count
+= tsk
->delays
->freepages_count
;
186 d
->thrashing_count
+= tsk
->delays
->thrashing_count
;
187 d
->compact_count
+= tsk
->delays
->compact_count
;
188 d
->wpcopy_count
+= tsk
->delays
->wpcopy_count
;
189 d
->irq_count
+= tsk
->delays
->irq_count
;
190 raw_spin_unlock_irqrestore(&tsk
->delays
->lock
, flags
);
195 __u64
__delayacct_blkio_ticks(struct task_struct
*tsk
)
200 raw_spin_lock_irqsave(&tsk
->delays
->lock
, flags
);
201 ret
= nsec_to_clock_t(tsk
->delays
->blkio_delay
);
202 raw_spin_unlock_irqrestore(&tsk
->delays
->lock
, flags
);
206 void __delayacct_freepages_start(void)
208 current
->delays
->freepages_start
= local_clock();
211 void __delayacct_freepages_end(void)
213 delayacct_end(¤t
->delays
->lock
,
214 ¤t
->delays
->freepages_start
,
215 ¤t
->delays
->freepages_delay
,
216 ¤t
->delays
->freepages_count
);
219 void __delayacct_thrashing_start(bool *in_thrashing
)
221 *in_thrashing
= !!current
->in_thrashing
;
225 current
->in_thrashing
= 1;
226 current
->delays
->thrashing_start
= local_clock();
229 void __delayacct_thrashing_end(bool *in_thrashing
)
234 current
->in_thrashing
= 0;
235 delayacct_end(¤t
->delays
->lock
,
236 ¤t
->delays
->thrashing_start
,
237 ¤t
->delays
->thrashing_delay
,
238 ¤t
->delays
->thrashing_count
);
241 void __delayacct_swapin_start(void)
243 current
->delays
->swapin_start
= local_clock();
246 void __delayacct_swapin_end(void)
248 delayacct_end(¤t
->delays
->lock
,
249 ¤t
->delays
->swapin_start
,
250 ¤t
->delays
->swapin_delay
,
251 ¤t
->delays
->swapin_count
);
254 void __delayacct_compact_start(void)
256 current
->delays
->compact_start
= local_clock();
259 void __delayacct_compact_end(void)
261 delayacct_end(¤t
->delays
->lock
,
262 ¤t
->delays
->compact_start
,
263 ¤t
->delays
->compact_delay
,
264 ¤t
->delays
->compact_count
);
267 void __delayacct_wpcopy_start(void)
269 current
->delays
->wpcopy_start
= local_clock();
272 void __delayacct_wpcopy_end(void)
274 delayacct_end(¤t
->delays
->lock
,
275 ¤t
->delays
->wpcopy_start
,
276 ¤t
->delays
->wpcopy_delay
,
277 ¤t
->delays
->wpcopy_count
);
280 void __delayacct_irq(struct task_struct
*task
, u32 delta
)
284 raw_spin_lock_irqsave(&task
->delays
->lock
, flags
);
285 task
->delays
->irq_delay
+= delta
;
286 task
->delays
->irq_count
++;
287 raw_spin_unlock_irqrestore(&task
->delays
->lock
, flags
);