Merge branch 'readdir' (readdir speedup and sanity checking)
[linux-block.git] / include / linux / delayacct.h
CommitLineData
c942fddf 1/* SPDX-License-Identifier: GPL-2.0-or-later */
ca74e92b
SN
2/* delayacct.h - per-task delay accounting
3 *
4 * Copyright (C) Shailabh Nagar, IBM Corp. 2006
ca74e92b
SN
5 */
6
7#ifndef _LINUX_DELAYACCT_H
8#define _LINUX_DELAYACCT_H
9
4025819d 10#include <uapi/linux/taskstats.h>
ca74e92b 11
0ff92245
SN
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 */
846c7bb0 18#define DELAYACCT_PF_BLKIO 0x00000002 /* I am waiting on IO */
0ff92245 19
ca74e92b 20#ifdef CONFIG_TASK_DELAY_ACCT
47913d4e 21struct task_delay_info {
02acc80d 22 raw_spinlock_t lock;
47913d4e 23 unsigned int flags; /* Private per-task flags */
ca74e92b 24
47913d4e
IM
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 */
b1d29ba8
JW
50
51 u64 thrashing_start;
52 u64 thrashing_delay; /* wait for thrashing page */
53
47913d4e 54 u32 freepages_count; /* total count of memory reclaim */
b1d29ba8 55 u32 thrashing_count; /* total count of thrash waits */
47913d4e
IM
56};
57#endif
58
59#include <linux/sched.h>
60#include <linux/slab.h>
61
62#ifdef CONFIG_TASK_DELAY_ACCT
ca74e92b 63extern int delayacct_on; /* Delay accounting turned on/off */
e18b890b 64extern struct kmem_cache *delayacct_cache;
ca74e92b
SN
65extern void delayacct_init(void);
66extern void __delayacct_tsk_init(struct task_struct *);
67extern void __delayacct_tsk_exit(struct task_struct *);
0ff92245 68extern void __delayacct_blkio_start(void);
c96f5471 69extern void __delayacct_blkio_end(struct task_struct *);
6f44993f 70extern int __delayacct_add_tsk(struct taskstats *, struct task_struct *);
25890454 71extern __u64 __delayacct_blkio_ticks(struct task_struct *);
873b4771
KK
72extern void __delayacct_freepages_start(void);
73extern void __delayacct_freepages_end(void);
b1d29ba8
JW
74extern void __delayacct_thrashing_start(void);
75extern void __delayacct_thrashing_end(void);
ca74e92b 76
846c7bb0
BS
77static 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
ca74e92b
SN
85static inline void delayacct_set_flag(int flag)
86{
87 if (current->delays)
88 current->delays->flags |= flag;
89}
90
91static inline void delayacct_clear_flag(int flag)
92{
93 if (current->delays)
94 current->delays->flags &= ~flag;
95}
96
97static 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;
163ecdff 101 if (delayacct_on)
ca74e92b
SN
102 __delayacct_tsk_init(tsk);
103}
104
35df17c5
SN
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 */
108static inline void delayacct_tsk_free(struct task_struct *tsk)
ca74e92b
SN
109{
110 if (tsk->delays)
35df17c5
SN
111 kmem_cache_free(delayacct_cache, tsk->delays);
112 tsk->delays = NULL;
ca74e92b
SN
113}
114
0ff92245
SN
115static inline void delayacct_blkio_start(void)
116{
846c7bb0 117 delayacct_set_flag(DELAYACCT_PF_BLKIO);
0ff92245
SN
118 if (current->delays)
119 __delayacct_blkio_start();
120}
121
c96f5471 122static inline void delayacct_blkio_end(struct task_struct *p)
0ff92245 123{
b512719f 124 if (p->delays)
c96f5471 125 __delayacct_blkio_end(p);
846c7bb0 126 delayacct_clear_flag(DELAYACCT_PF_BLKIO);
0ff92245
SN
127}
128
6f44993f
SN
129static inline int delayacct_add_tsk(struct taskstats *d,
130 struct task_struct *tsk)
131{
163ecdff 132 if (!delayacct_on || !tsk->delays)
6f44993f
SN
133 return 0;
134 return __delayacct_add_tsk(d, tsk);
135}
136
25890454
SN
137static 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
873b4771
KK
144static inline void delayacct_freepages_start(void)
145{
146 if (current->delays)
147 __delayacct_freepages_start();
148}
149
150static inline void delayacct_freepages_end(void)
151{
152 if (current->delays)
153 __delayacct_freepages_end();
154}
155
b1d29ba8
JW
156static inline void delayacct_thrashing_start(void)
157{
158 if (current->delays)
159 __delayacct_thrashing_start();
160}
161
162static inline void delayacct_thrashing_end(void)
163{
164 if (current->delays)
165 __delayacct_thrashing_end();
166}
167
ca74e92b
SN
168#else
169static inline void delayacct_set_flag(int flag)
170{}
171static inline void delayacct_clear_flag(int flag)
172{}
173static inline void delayacct_init(void)
174{}
175static inline void delayacct_tsk_init(struct task_struct *tsk)
176{}
35df17c5 177static inline void delayacct_tsk_free(struct task_struct *tsk)
ca74e92b 178{}
0ff92245
SN
179static inline void delayacct_blkio_start(void)
180{}
c96f5471 181static inline void delayacct_blkio_end(struct task_struct *p)
0ff92245 182{}
6f44993f
SN
183static inline int delayacct_add_tsk(struct taskstats *d,
184 struct task_struct *tsk)
185{ return 0; }
25890454
SN
186static inline __u64 delayacct_blkio_ticks(struct task_struct *tsk)
187{ return 0; }
846c7bb0
BS
188static inline int delayacct_is_task_waiting_on_io(struct task_struct *p)
189{ return 0; }
873b4771
KK
190static inline void delayacct_freepages_start(void)
191{}
192static inline void delayacct_freepages_end(void)
193{}
b1d29ba8
JW
194static inline void delayacct_thrashing_start(void)
195{}
196static inline void delayacct_thrashing_end(void)
197{}
873b4771 198
ca74e92b
SN
199#endif /* CONFIG_TASK_DELAY_ACCT */
200
201#endif