Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 2 | /* delayacct.h - per-task delay accounting |
| 3 | * |
| 4 | * Copyright (C) Shailabh Nagar, IBM Corp. 2006 |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef _LINUX_DELAYACCT_H |
| 8 | #define _LINUX_DELAYACCT_H |
| 9 | |
Ingo Molnar | 4025819 | 2017-02-01 11:58:42 +0100 | [diff] [blame] | 10 | #include <uapi/linux/taskstats.h> |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 11 | |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 12 | /* |
| 13 | * Per-task flags relevant to delay accounting |
| 14 | * maintained privately to avoid exhausting similar flags in sched.h:PF_* |
| 15 | * Used to set current->delays->flags |
| 16 | */ |
| 17 | #define DELAYACCT_PF_SWAPIN 0x00000001 /* I am doing a swapin */ |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 18 | #define DELAYACCT_PF_BLKIO 0x00000002 /* I am waiting on IO */ |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 19 | |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 20 | #ifdef CONFIG_TASK_DELAY_ACCT |
Ingo Molnar | 47913d4 | 2017-02-01 18:00:26 +0100 | [diff] [blame] | 21 | struct task_delay_info { |
Sebastian Andrzej Siewior | 02acc80 | 2018-04-23 18:10:23 +0200 | [diff] [blame] | 22 | raw_spinlock_t lock; |
Ingo Molnar | 47913d4 | 2017-02-01 18:00:26 +0100 | [diff] [blame] | 23 | unsigned int flags; /* Private per-task flags */ |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 24 | |
Ingo Molnar | 47913d4 | 2017-02-01 18:00:26 +0100 | [diff] [blame] | 25 | /* For each stat XXX, add following, aligned appropriately |
| 26 | * |
| 27 | * struct timespec XXX_start, XXX_end; |
| 28 | * u64 XXX_delay; |
| 29 | * u32 XXX_count; |
| 30 | * |
| 31 | * Atomicity of updates to XXX_delay, XXX_count protected by |
| 32 | * single lock above (split into XXX_lock if contention is an issue). |
| 33 | */ |
| 34 | |
| 35 | /* |
| 36 | * XXX_count is incremented on every XXX operation, the delay |
| 37 | * associated with the operation is added to XXX_delay. |
| 38 | * XXX_delay contains the accumulated delay time in nanoseconds. |
| 39 | */ |
| 40 | u64 blkio_start; /* Shared by blkio, swapin */ |
| 41 | u64 blkio_delay; /* wait for sync block io completion */ |
| 42 | u64 swapin_delay; /* wait for swapin block io completion */ |
| 43 | u32 blkio_count; /* total count of the number of sync block */ |
| 44 | /* io operations performed */ |
| 45 | u32 swapin_count; /* total count of the number of swapin block */ |
| 46 | /* io operations performed */ |
| 47 | |
| 48 | u64 freepages_start; |
| 49 | u64 freepages_delay; /* wait for memory reclaim */ |
Johannes Weiner | b1d29ba | 2018-10-26 15:06:08 -0700 | [diff] [blame] | 50 | |
| 51 | u64 thrashing_start; |
| 52 | u64 thrashing_delay; /* wait for thrashing page */ |
| 53 | |
Ingo Molnar | 47913d4 | 2017-02-01 18:00:26 +0100 | [diff] [blame] | 54 | u32 freepages_count; /* total count of memory reclaim */ |
Johannes Weiner | b1d29ba | 2018-10-26 15:06:08 -0700 | [diff] [blame] | 55 | u32 thrashing_count; /* total count of thrash waits */ |
Ingo Molnar | 47913d4 | 2017-02-01 18:00:26 +0100 | [diff] [blame] | 56 | }; |
| 57 | #endif |
| 58 | |
| 59 | #include <linux/sched.h> |
| 60 | #include <linux/slab.h> |
| 61 | |
| 62 | #ifdef CONFIG_TASK_DELAY_ACCT |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 63 | extern int delayacct_on; /* Delay accounting turned on/off */ |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 64 | extern struct kmem_cache *delayacct_cache; |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 65 | extern void delayacct_init(void); |
| 66 | extern void __delayacct_tsk_init(struct task_struct *); |
| 67 | extern void __delayacct_tsk_exit(struct task_struct *); |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 68 | extern void __delayacct_blkio_start(void); |
Josh Snyder | c96f547 | 2017-12-18 16:15:10 +0000 | [diff] [blame] | 69 | extern void __delayacct_blkio_end(struct task_struct *); |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 70 | extern int __delayacct_add_tsk(struct taskstats *, struct task_struct *); |
Shailabh Nagar | 2589045 | 2006-07-14 00:24:43 -0700 | [diff] [blame] | 71 | extern __u64 __delayacct_blkio_ticks(struct task_struct *); |
Keika Kobayashi | 873b477 | 2008-07-25 01:48:52 -0700 | [diff] [blame] | 72 | extern void __delayacct_freepages_start(void); |
| 73 | extern void __delayacct_freepages_end(void); |
Johannes Weiner | b1d29ba | 2018-10-26 15:06:08 -0700 | [diff] [blame] | 74 | extern void __delayacct_thrashing_start(void); |
| 75 | extern void __delayacct_thrashing_end(void); |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 76 | |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 77 | static inline int delayacct_is_task_waiting_on_io(struct task_struct *p) |
| 78 | { |
| 79 | if (p->delays) |
| 80 | return (p->delays->flags & DELAYACCT_PF_BLKIO); |
| 81 | else |
| 82 | return 0; |
| 83 | } |
| 84 | |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 85 | static inline void delayacct_set_flag(int flag) |
| 86 | { |
| 87 | if (current->delays) |
| 88 | current->delays->flags |= flag; |
| 89 | } |
| 90 | |
| 91 | static inline void delayacct_clear_flag(int flag) |
| 92 | { |
| 93 | if (current->delays) |
| 94 | current->delays->flags &= ~flag; |
| 95 | } |
| 96 | |
| 97 | static inline void delayacct_tsk_init(struct task_struct *tsk) |
| 98 | { |
| 99 | /* reinitialize in case parent's non-null pointer was dup'ed*/ |
| 100 | tsk->delays = NULL; |
Shailabh Nagar | 163ecdf | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 101 | if (delayacct_on) |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 102 | __delayacct_tsk_init(tsk); |
| 103 | } |
| 104 | |
Shailabh Nagar | 35df17c | 2006-08-31 21:27:38 -0700 | [diff] [blame] | 105 | /* Free tsk->delays. Called from bad fork and __put_task_struct |
| 106 | * where there's no risk of tsk->delays being accessed elsewhere |
| 107 | */ |
| 108 | static inline void delayacct_tsk_free(struct task_struct *tsk) |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 109 | { |
| 110 | if (tsk->delays) |
Shailabh Nagar | 35df17c | 2006-08-31 21:27:38 -0700 | [diff] [blame] | 111 | kmem_cache_free(delayacct_cache, tsk->delays); |
| 112 | tsk->delays = NULL; |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 115 | static inline void delayacct_blkio_start(void) |
| 116 | { |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 117 | delayacct_set_flag(DELAYACCT_PF_BLKIO); |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 118 | if (current->delays) |
| 119 | __delayacct_blkio_start(); |
| 120 | } |
| 121 | |
Josh Snyder | c96f547 | 2017-12-18 16:15:10 +0000 | [diff] [blame] | 122 | static inline void delayacct_blkio_end(struct task_struct *p) |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 123 | { |
Tejun Heo | b512719 | 2018-07-26 16:37:08 -0700 | [diff] [blame] | 124 | if (p->delays) |
Josh Snyder | c96f547 | 2017-12-18 16:15:10 +0000 | [diff] [blame] | 125 | __delayacct_blkio_end(p); |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 126 | delayacct_clear_flag(DELAYACCT_PF_BLKIO); |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 129 | static inline int delayacct_add_tsk(struct taskstats *d, |
| 130 | struct task_struct *tsk) |
| 131 | { |
Shailabh Nagar | 163ecdf | 2006-07-30 03:03:11 -0700 | [diff] [blame] | 132 | if (!delayacct_on || !tsk->delays) |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 133 | return 0; |
| 134 | return __delayacct_add_tsk(d, tsk); |
| 135 | } |
| 136 | |
Shailabh Nagar | 2589045 | 2006-07-14 00:24:43 -0700 | [diff] [blame] | 137 | static inline __u64 delayacct_blkio_ticks(struct task_struct *tsk) |
| 138 | { |
| 139 | if (tsk->delays) |
| 140 | return __delayacct_blkio_ticks(tsk); |
| 141 | return 0; |
| 142 | } |
| 143 | |
Keika Kobayashi | 873b477 | 2008-07-25 01:48:52 -0700 | [diff] [blame] | 144 | static inline void delayacct_freepages_start(void) |
| 145 | { |
| 146 | if (current->delays) |
| 147 | __delayacct_freepages_start(); |
| 148 | } |
| 149 | |
| 150 | static inline void delayacct_freepages_end(void) |
| 151 | { |
| 152 | if (current->delays) |
| 153 | __delayacct_freepages_end(); |
| 154 | } |
| 155 | |
Johannes Weiner | b1d29ba | 2018-10-26 15:06:08 -0700 | [diff] [blame] | 156 | static inline void delayacct_thrashing_start(void) |
| 157 | { |
| 158 | if (current->delays) |
| 159 | __delayacct_thrashing_start(); |
| 160 | } |
| 161 | |
| 162 | static inline void delayacct_thrashing_end(void) |
| 163 | { |
| 164 | if (current->delays) |
| 165 | __delayacct_thrashing_end(); |
| 166 | } |
| 167 | |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 168 | #else |
| 169 | static inline void delayacct_set_flag(int flag) |
| 170 | {} |
| 171 | static inline void delayacct_clear_flag(int flag) |
| 172 | {} |
| 173 | static inline void delayacct_init(void) |
| 174 | {} |
| 175 | static inline void delayacct_tsk_init(struct task_struct *tsk) |
| 176 | {} |
Shailabh Nagar | 35df17c | 2006-08-31 21:27:38 -0700 | [diff] [blame] | 177 | static inline void delayacct_tsk_free(struct task_struct *tsk) |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 178 | {} |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 179 | static inline void delayacct_blkio_start(void) |
| 180 | {} |
Josh Snyder | c96f547 | 2017-12-18 16:15:10 +0000 | [diff] [blame] | 181 | static inline void delayacct_blkio_end(struct task_struct *p) |
Shailabh Nagar | 0ff9224 | 2006-07-14 00:24:37 -0700 | [diff] [blame] | 182 | {} |
Shailabh Nagar | 6f44993 | 2006-07-14 00:24:41 -0700 | [diff] [blame] | 183 | static inline int delayacct_add_tsk(struct taskstats *d, |
| 184 | struct task_struct *tsk) |
| 185 | { return 0; } |
Shailabh Nagar | 2589045 | 2006-07-14 00:24:43 -0700 | [diff] [blame] | 186 | static inline __u64 delayacct_blkio_ticks(struct task_struct *tsk) |
| 187 | { return 0; } |
Balbir Singh | 846c7bb | 2007-10-18 23:39:44 -0700 | [diff] [blame] | 188 | static inline int delayacct_is_task_waiting_on_io(struct task_struct *p) |
| 189 | { return 0; } |
Keika Kobayashi | 873b477 | 2008-07-25 01:48:52 -0700 | [diff] [blame] | 190 | static inline void delayacct_freepages_start(void) |
| 191 | {} |
| 192 | static inline void delayacct_freepages_end(void) |
| 193 | {} |
Johannes Weiner | b1d29ba | 2018-10-26 15:06:08 -0700 | [diff] [blame] | 194 | static inline void delayacct_thrashing_start(void) |
| 195 | {} |
| 196 | static inline void delayacct_thrashing_end(void) |
| 197 | {} |
Keika Kobayashi | 873b477 | 2008-07-25 01:48:52 -0700 | [diff] [blame] | 198 | |
Shailabh Nagar | ca74e92 | 2006-07-14 00:24:36 -0700 | [diff] [blame] | 199 | #endif /* CONFIG_TASK_DELAY_ACCT */ |
| 200 | |
| 201 | #endif |