blob: 2d3bdcccf5eb6491538ea4d7e8a47dd7ad76f94c [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Shailabh Nagarca74e922006-07-14 00:24:36 -07002/* delayacct.h - per-task delay accounting
3 *
4 * Copyright (C) Shailabh Nagar, IBM Corp. 2006
Shailabh Nagarca74e922006-07-14 00:24:36 -07005 */
6
7#ifndef _LINUX_DELAYACCT_H
8#define _LINUX_DELAYACCT_H
9
Ingo Molnar40258192017-02-01 11:58:42 +010010#include <uapi/linux/taskstats.h>
Shailabh Nagarca74e922006-07-14 00:24:36 -070011
Shailabh Nagar0ff92242006-07-14 00:24:37 -070012/*
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 Singh846c7bb2007-10-18 23:39:44 -070018#define DELAYACCT_PF_BLKIO 0x00000002 /* I am waiting on IO */
Shailabh Nagar0ff92242006-07-14 00:24:37 -070019
Shailabh Nagarca74e922006-07-14 00:24:36 -070020#ifdef CONFIG_TASK_DELAY_ACCT
Ingo Molnar47913d42017-02-01 18:00:26 +010021struct task_delay_info {
Sebastian Andrzej Siewior02acc802018-04-23 18:10:23 +020022 raw_spinlock_t lock;
Ingo Molnar47913d42017-02-01 18:00:26 +010023 unsigned int flags; /* Private per-task flags */
Shailabh Nagarca74e922006-07-14 00:24:36 -070024
Ingo Molnar47913d42017-02-01 18:00:26 +010025 /* 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 Weinerb1d29ba2018-10-26 15:06:08 -070050
51 u64 thrashing_start;
52 u64 thrashing_delay; /* wait for thrashing page */
53
Ingo Molnar47913d42017-02-01 18:00:26 +010054 u32 freepages_count; /* total count of memory reclaim */
Johannes Weinerb1d29ba2018-10-26 15:06:08 -070055 u32 thrashing_count; /* total count of thrash waits */
Ingo Molnar47913d42017-02-01 18:00:26 +010056};
57#endif
58
59#include <linux/sched.h>
60#include <linux/slab.h>
61
62#ifdef CONFIG_TASK_DELAY_ACCT
Shailabh Nagarca74e922006-07-14 00:24:36 -070063extern int delayacct_on; /* Delay accounting turned on/off */
Christoph Lametere18b8902006-12-06 20:33:20 -080064extern struct kmem_cache *delayacct_cache;
Shailabh Nagarca74e922006-07-14 00:24:36 -070065extern void delayacct_init(void);
66extern void __delayacct_tsk_init(struct task_struct *);
67extern void __delayacct_tsk_exit(struct task_struct *);
Shailabh Nagar0ff92242006-07-14 00:24:37 -070068extern void __delayacct_blkio_start(void);
Josh Snyderc96f5472017-12-18 16:15:10 +000069extern void __delayacct_blkio_end(struct task_struct *);
Shailabh Nagar6f449932006-07-14 00:24:41 -070070extern int __delayacct_add_tsk(struct taskstats *, struct task_struct *);
Shailabh Nagar25890452006-07-14 00:24:43 -070071extern __u64 __delayacct_blkio_ticks(struct task_struct *);
Keika Kobayashi873b4772008-07-25 01:48:52 -070072extern void __delayacct_freepages_start(void);
73extern void __delayacct_freepages_end(void);
Johannes Weinerb1d29ba2018-10-26 15:06:08 -070074extern void __delayacct_thrashing_start(void);
75extern void __delayacct_thrashing_end(void);
Shailabh Nagarca74e922006-07-14 00:24:36 -070076
Balbir Singh846c7bb2007-10-18 23:39:44 -070077static 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 Nagarca74e922006-07-14 00:24:36 -070085static 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;
Shailabh Nagar163ecdf2006-07-30 03:03:11 -0700101 if (delayacct_on)
Shailabh Nagarca74e922006-07-14 00:24:36 -0700102 __delayacct_tsk_init(tsk);
103}
104
Shailabh Nagar35df17c2006-08-31 21:27:38 -0700105/* 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)
Shailabh Nagarca74e922006-07-14 00:24:36 -0700109{
110 if (tsk->delays)
Shailabh Nagar35df17c2006-08-31 21:27:38 -0700111 kmem_cache_free(delayacct_cache, tsk->delays);
112 tsk->delays = NULL;
Shailabh Nagarca74e922006-07-14 00:24:36 -0700113}
114
Shailabh Nagar0ff92242006-07-14 00:24:37 -0700115static inline void delayacct_blkio_start(void)
116{
Balbir Singh846c7bb2007-10-18 23:39:44 -0700117 delayacct_set_flag(DELAYACCT_PF_BLKIO);
Shailabh Nagar0ff92242006-07-14 00:24:37 -0700118 if (current->delays)
119 __delayacct_blkio_start();
120}
121
Josh Snyderc96f5472017-12-18 16:15:10 +0000122static inline void delayacct_blkio_end(struct task_struct *p)
Shailabh Nagar0ff92242006-07-14 00:24:37 -0700123{
Tejun Heob5127192018-07-26 16:37:08 -0700124 if (p->delays)
Josh Snyderc96f5472017-12-18 16:15:10 +0000125 __delayacct_blkio_end(p);
Balbir Singh846c7bb2007-10-18 23:39:44 -0700126 delayacct_clear_flag(DELAYACCT_PF_BLKIO);
Shailabh Nagar0ff92242006-07-14 00:24:37 -0700127}
128
Shailabh Nagar6f449932006-07-14 00:24:41 -0700129static inline int delayacct_add_tsk(struct taskstats *d,
130 struct task_struct *tsk)
131{
Shailabh Nagar163ecdf2006-07-30 03:03:11 -0700132 if (!delayacct_on || !tsk->delays)
Shailabh Nagar6f449932006-07-14 00:24:41 -0700133 return 0;
134 return __delayacct_add_tsk(d, tsk);
135}
136
Shailabh Nagar25890452006-07-14 00:24:43 -0700137static 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 Kobayashi873b4772008-07-25 01:48:52 -0700144static 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
Johannes Weinerb1d29ba2018-10-26 15:06:08 -0700156static 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
Shailabh Nagarca74e922006-07-14 00:24:36 -0700168#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{}
Shailabh Nagar35df17c2006-08-31 21:27:38 -0700177static inline void delayacct_tsk_free(struct task_struct *tsk)
Shailabh Nagarca74e922006-07-14 00:24:36 -0700178{}
Shailabh Nagar0ff92242006-07-14 00:24:37 -0700179static inline void delayacct_blkio_start(void)
180{}
Josh Snyderc96f5472017-12-18 16:15:10 +0000181static inline void delayacct_blkio_end(struct task_struct *p)
Shailabh Nagar0ff92242006-07-14 00:24:37 -0700182{}
Shailabh Nagar6f449932006-07-14 00:24:41 -0700183static inline int delayacct_add_tsk(struct taskstats *d,
184 struct task_struct *tsk)
185{ return 0; }
Shailabh Nagar25890452006-07-14 00:24:43 -0700186static inline __u64 delayacct_blkio_ticks(struct task_struct *tsk)
187{ return 0; }
Balbir Singh846c7bb2007-10-18 23:39:44 -0700188static inline int delayacct_is_task_waiting_on_io(struct task_struct *p)
189{ return 0; }
Keika Kobayashi873b4772008-07-25 01:48:52 -0700190static inline void delayacct_freepages_start(void)
191{}
192static inline void delayacct_freepages_end(void)
193{}
Johannes Weinerb1d29ba2018-10-26 15:06:08 -0700194static inline void delayacct_thrashing_start(void)
195{}
196static inline void delayacct_thrashing_end(void)
197{}
Keika Kobayashi873b4772008-07-25 01:48:52 -0700198
Shailabh Nagarca74e922006-07-14 00:24:36 -0700199#endif /* CONFIG_TASK_DELAY_ACCT */
200
201#endif