blob: 27725754ac99194215c6dbcf7569200e506a011c [file] [log] [blame]
Thomas Gleixner71700662019-05-19 15:51:55 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Shailabh Nagarca74e922006-07-14 00:24:36 -07002/* delayacct.c - per-task delay accounting
3 *
4 * Copyright (C) Shailabh Nagar, IBM Corp. 2006
Shailabh Nagarca74e922006-07-14 00:24:36 -07005 */
6
7#include <linux/sched.h>
Ingo Molnar9164bb42017-02-04 01:20:53 +01008#include <linux/sched/task.h>
Ingo Molnar32ef5512017-02-05 11:48:36 +01009#include <linux/sched/cputime.h>
Shailabh Nagarca74e922006-07-14 00:24:36 -070010#include <linux/slab.h>
Alexey Dobriyan6952b612009-09-18 23:55:55 +040011#include <linux/taskstats.h>
Shailabh Nagarca74e922006-07-14 00:24:36 -070012#include <linux/time.h>
13#include <linux/sysctl.h>
14#include <linux/delayacct.h>
Glauber Costac9aaa892011-07-11 15:28:14 -040015#include <linux/module.h>
Shailabh Nagarca74e922006-07-14 00:24:36 -070016
Shailabh Nagar163ecdf2006-07-30 03:03:11 -070017int delayacct_on __read_mostly = 1; /* Delay accounting turned on/off */
Glauber Costac9aaa892011-07-11 15:28:14 -040018EXPORT_SYMBOL_GPL(delayacct_on);
Christoph Lametere18b8902006-12-06 20:33:20 -080019struct kmem_cache *delayacct_cache;
Shailabh Nagarca74e922006-07-14 00:24:36 -070020
Shailabh Nagar163ecdf2006-07-30 03:03:11 -070021static int __init delayacct_setup_disable(char *str)
Shailabh Nagarca74e922006-07-14 00:24:36 -070022{
Shailabh Nagar163ecdf2006-07-30 03:03:11 -070023 delayacct_on = 0;
Shailabh Nagarca74e922006-07-14 00:24:36 -070024 return 1;
25}
Shailabh Nagar163ecdf2006-07-30 03:03:11 -070026__setup("nodelayacct", delayacct_setup_disable);
Shailabh Nagarca74e922006-07-14 00:24:36 -070027
28void delayacct_init(void)
29{
Vladimir Davydov5d097052016-01-14 15:18:21 -080030 delayacct_cache = KMEM_CACHE(task_delay_info, SLAB_PANIC|SLAB_ACCOUNT);
Shailabh Nagarca74e922006-07-14 00:24:36 -070031 delayacct_tsk_init(&init_task);
32}
33
34void __delayacct_tsk_init(struct task_struct *tsk)
35{
Christoph Lametere94b1762006-12-06 20:33:17 -080036 tsk->delays = kmem_cache_zalloc(delayacct_cache, GFP_KERNEL);
Shailabh Nagarca74e922006-07-14 00:24:36 -070037 if (tsk->delays)
Sebastian Andrzej Siewior02acc802018-04-23 18:10:23 +020038 raw_spin_lock_init(&tsk->delays->lock);
Shailabh Nagarca74e922006-07-14 00:24:36 -070039}
40
Shailabh Nagarca74e922006-07-14 00:24:36 -070041/*
Thomas Gleixner9667a232014-07-16 21:04:35 +000042 * Finish delay accounting for a statistic using its timestamps (@start),
43 * accumalator (@total) and @count
Shailabh Nagarca74e922006-07-14 00:24:36 -070044 */
Sebastian Andrzej Siewior02acc802018-04-23 18:10:23 +020045static void delayacct_end(raw_spinlock_t *lock, u64 *start, u64 *total,
46 u32 *count)
Shailabh Nagarca74e922006-07-14 00:24:36 -070047{
Thomas Gleixner9667a232014-07-16 21:04:35 +000048 s64 ns = ktime_get_ns() - *start;
Peter Zijlstra64efade2006-11-05 23:52:10 -080049 unsigned long flags;
Shailabh Nagarca74e922006-07-14 00:24:36 -070050
Thomas Gleixner9667a232014-07-16 21:04:35 +000051 if (ns > 0) {
Sebastian Andrzej Siewior02acc802018-04-23 18:10:23 +020052 raw_spin_lock_irqsave(lock, flags);
Thomas Gleixner9667a232014-07-16 21:04:35 +000053 *total += ns;
54 (*count)++;
Sebastian Andrzej Siewior02acc802018-04-23 18:10:23 +020055 raw_spin_unlock_irqrestore(lock, flags);
Thomas Gleixner9667a232014-07-16 21:04:35 +000056 }
Shailabh Nagarca74e922006-07-14 00:24:36 -070057}
58
Shailabh Nagar0ff92242006-07-14 00:24:37 -070059void __delayacct_blkio_start(void)
60{
Thomas Gleixner9667a232014-07-16 21:04:35 +000061 current->delays->blkio_start = ktime_get_ns();
Shailabh Nagar0ff92242006-07-14 00:24:37 -070062}
63
Josh Snyderc96f5472017-12-18 16:15:10 +000064/*
65 * We cannot rely on the `current` macro, as we haven't yet switched back to
66 * the process being woken.
67 */
68void __delayacct_blkio_end(struct task_struct *p)
Shailabh Nagar0ff92242006-07-14 00:24:37 -070069{
Josh Snyderc96f5472017-12-18 16:15:10 +000070 struct task_delay_info *delays = p->delays;
71 u64 *total;
72 u32 *count;
73
74 if (p->delays->flags & DELAYACCT_PF_SWAPIN) {
75 total = &delays->swapin_delay;
76 count = &delays->swapin_count;
77 } else {
78 total = &delays->blkio_delay;
79 count = &delays->blkio_count;
80 }
81
82 delayacct_end(&delays->lock, &delays->blkio_start, total, count);
Shailabh Nagar0ff92242006-07-14 00:24:37 -070083}
Shailabh Nagar6f449932006-07-14 00:24:41 -070084
85int __delayacct_add_tsk(struct taskstats *d, struct task_struct *tsk)
86{
Frederic Weisbeckerdbf3da12017-01-31 04:09:29 +010087 u64 utime, stime, stimescaled, utimescaled;
Thomas Gleixner68f6783d2014-07-16 21:04:37 +000088 unsigned long long t2, t3;
89 unsigned long flags, t1;
90 s64 tmp;
Shailabh Nagar6f449932006-07-14 00:24:41 -070091
Frederic Weisbeckerdbf3da12017-01-31 04:09:29 +010092 task_cputime(tsk, &utime, &stime);
Thomas Gleixner68f6783d2014-07-16 21:04:37 +000093 tmp = (s64)d->cpu_run_real_total;
Frederic Weisbeckerdbf3da12017-01-31 04:09:29 +010094 tmp += utime + stime;
Shailabh Nagar6f449932006-07-14 00:24:41 -070095 d->cpu_run_real_total = (tmp < (s64)d->cpu_run_real_total) ? 0 : tmp;
96
Frederic Weisbeckerdbf3da12017-01-31 04:09:29 +010097 task_cputime_scaled(tsk, &utimescaled, &stimescaled);
Thomas Gleixner68f6783d2014-07-16 21:04:37 +000098 tmp = (s64)d->cpu_scaled_run_real_total;
Frederic Weisbeckerdbf3da12017-01-31 04:09:29 +010099 tmp += utimescaled + stimescaled;
Michael Neulingc66f08b2007-10-18 03:06:34 -0700100 d->cpu_scaled_run_real_total =
101 (tmp < (s64)d->cpu_scaled_run_real_total) ? 0 : tmp;
102
Shailabh Nagar6f449932006-07-14 00:24:41 -0700103 /*
104 * No locking available for sched_info (and too expensive to add one)
105 * Mitigate by taking snapshot of values
106 */
Ingo Molnar2d723762007-10-15 17:00:12 +0200107 t1 = tsk->sched_info.pcount;
Shailabh Nagar6f449932006-07-14 00:24:41 -0700108 t2 = tsk->sched_info.run_delay;
Ken Chen9c2c4802008-12-16 23:41:22 -0800109 t3 = tsk->se.sum_exec_runtime;
Shailabh Nagar6f449932006-07-14 00:24:41 -0700110
111 d->cpu_count += t1;
112
Balbir Singh172ba842007-07-09 18:52:00 +0200113 tmp = (s64)d->cpu_delay_total + t2;
Shailabh Nagar6f449932006-07-14 00:24:41 -0700114 d->cpu_delay_total = (tmp < (s64)d->cpu_delay_total) ? 0 : tmp;
115
Balbir Singh172ba842007-07-09 18:52:00 +0200116 tmp = (s64)d->cpu_run_virtual_total + t3;
Shailabh Nagar6f449932006-07-14 00:24:41 -0700117 d->cpu_run_virtual_total =
118 (tmp < (s64)d->cpu_run_virtual_total) ? 0 : tmp;
119
120 /* zero XXX_total, non-zero XXX_count implies XXX stat overflowed */
121
Sebastian Andrzej Siewior02acc802018-04-23 18:10:23 +0200122 raw_spin_lock_irqsave(&tsk->delays->lock, flags);
Shailabh Nagar6f449932006-07-14 00:24:41 -0700123 tmp = d->blkio_delay_total + tsk->delays->blkio_delay;
124 d->blkio_delay_total = (tmp < d->blkio_delay_total) ? 0 : tmp;
125 tmp = d->swapin_delay_total + tsk->delays->swapin_delay;
126 d->swapin_delay_total = (tmp < d->swapin_delay_total) ? 0 : tmp;
Keika Kobayashi016ae212008-07-25 01:48:53 -0700127 tmp = d->freepages_delay_total + tsk->delays->freepages_delay;
128 d->freepages_delay_total = (tmp < d->freepages_delay_total) ? 0 : tmp;
Johannes Weinerb1d29ba2018-10-26 15:06:08 -0700129 tmp = d->thrashing_delay_total + tsk->delays->thrashing_delay;
130 d->thrashing_delay_total = (tmp < d->thrashing_delay_total) ? 0 : tmp;
Shailabh Nagar6f449932006-07-14 00:24:41 -0700131 d->blkio_count += tsk->delays->blkio_count;
132 d->swapin_count += tsk->delays->swapin_count;
Keika Kobayashi016ae212008-07-25 01:48:53 -0700133 d->freepages_count += tsk->delays->freepages_count;
Johannes Weinerb1d29ba2018-10-26 15:06:08 -0700134 d->thrashing_count += tsk->delays->thrashing_count;
Sebastian Andrzej Siewior02acc802018-04-23 18:10:23 +0200135 raw_spin_unlock_irqrestore(&tsk->delays->lock, flags);
Shailabh Nagar6f449932006-07-14 00:24:41 -0700136
Shailabh Nagar6f449932006-07-14 00:24:41 -0700137 return 0;
138}
Shailabh Nagar25890452006-07-14 00:24:43 -0700139
140__u64 __delayacct_blkio_ticks(struct task_struct *tsk)
141{
142 __u64 ret;
Peter Zijlstra64efade2006-11-05 23:52:10 -0800143 unsigned long flags;
Shailabh Nagar25890452006-07-14 00:24:43 -0700144
Sebastian Andrzej Siewior02acc802018-04-23 18:10:23 +0200145 raw_spin_lock_irqsave(&tsk->delays->lock, flags);
Shailabh Nagar25890452006-07-14 00:24:43 -0700146 ret = nsec_to_clock_t(tsk->delays->blkio_delay +
147 tsk->delays->swapin_delay);
Sebastian Andrzej Siewior02acc802018-04-23 18:10:23 +0200148 raw_spin_unlock_irqrestore(&tsk->delays->lock, flags);
Shailabh Nagar25890452006-07-14 00:24:43 -0700149 return ret;
150}
151
Keika Kobayashi873b4772008-07-25 01:48:52 -0700152void __delayacct_freepages_start(void)
153{
Thomas Gleixner9667a232014-07-16 21:04:35 +0000154 current->delays->freepages_start = ktime_get_ns();
Keika Kobayashi873b4772008-07-25 01:48:52 -0700155}
156
157void __delayacct_freepages_end(void)
158{
Josh Snyderc96f5472017-12-18 16:15:10 +0000159 delayacct_end(
160 &current->delays->lock,
161 &current->delays->freepages_start,
162 &current->delays->freepages_delay,
163 &current->delays->freepages_count);
Keika Kobayashi873b4772008-07-25 01:48:52 -0700164}
165
Johannes Weinerb1d29ba2018-10-26 15:06:08 -0700166void __delayacct_thrashing_start(void)
167{
168 current->delays->thrashing_start = ktime_get_ns();
169}
170
171void __delayacct_thrashing_end(void)
172{
173 delayacct_end(&current->delays->lock,
174 &current->delays->thrashing_start,
175 &current->delays->thrashing_delay,
176 &current->delays->thrashing_count);
177}