xref: /freebsd/sys/sys/racct.h (revision c697fb7f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * This software was developed by Edward Tomasz Napierala under sponsorship
8  * from the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD$
32  */
33 
34 /*
35  * Resource accounting.
36  */
37 
38 #ifndef _RACCT_H_
39 #define	_RACCT_H_
40 
41 #include <sys/cdefs.h>
42 #include <sys/types.h>
43 #include <sys/queue.h>
44 #include <sys/stdint.h>
45 #include <sys/sysctl.h>
46 
47 struct buf;
48 struct proc;
49 struct rctl_rule_link;
50 struct ucred;
51 
52 /*
53  * Resources.
54  */
55 #define	RACCT_UNDEFINED		-1
56 #define	RACCT_CPU		0
57 #define	RACCT_DATA		1
58 #define	RACCT_STACK		2
59 #define	RACCT_CORE		3
60 #define	RACCT_RSS		4
61 #define	RACCT_MEMLOCK		5
62 #define	RACCT_NPROC		6
63 #define	RACCT_NOFILE		7
64 #define	RACCT_VMEM		8
65 #define	RACCT_NPTS		9
66 #define	RACCT_SWAP		10
67 #define	RACCT_NTHR		11
68 #define	RACCT_MSGQQUEUED	12
69 #define	RACCT_MSGQSIZE		13
70 #define	RACCT_NMSGQ		14
71 #define	RACCT_NSEM		15
72 #define	RACCT_NSEMOP		16
73 #define	RACCT_NSHM		17
74 #define	RACCT_SHMSIZE		18
75 #define	RACCT_WALLCLOCK		19
76 #define	RACCT_PCTCPU		20
77 #define	RACCT_READBPS		21
78 #define	RACCT_WRITEBPS		22
79 #define	RACCT_READIOPS		23
80 #define	RACCT_WRITEIOPS		24
81 #define	RACCT_MAX		RACCT_WRITEIOPS
82 
83 /*
84  * Resource properties.
85  */
86 #define	RACCT_IN_MILLIONS	0x01
87 #define	RACCT_RECLAIMABLE	0x02
88 #define	RACCT_INHERITABLE	0x04
89 #define	RACCT_DENIABLE		0x08
90 #define	RACCT_SLOPPY		0x10
91 #define	RACCT_DECAYING		0x20
92 
93 extern int racct_types[];
94 extern bool racct_enable;
95 
96 #define ASSERT_RACCT_ENABLED()	KASSERT(racct_enable, \
97 				    ("%s called with !racct_enable", __func__))
98 
99 /*
100  * Amount stored in c_resources[] is 10**6 times bigger than what's
101  * visible to the userland.  It gets fixed up when retrieving resource
102  * usage or adding rules.
103  */
104 #define	RACCT_IS_IN_MILLIONS(X)	\
105     ((X) != RACCT_UNDEFINED && (racct_types[(X)] & RACCT_IN_MILLIONS) != 0)
106 
107 /*
108  * Resource usage can drop, as opposed to only grow.  When the process
109  * terminates, its resource usage is subtracted from the respective
110  * per-credential racct containers.
111  */
112 #define	RACCT_IS_RECLAIMABLE(X)	(racct_types[X] & RACCT_RECLAIMABLE)
113 
114 /*
115  * Children inherit resource usage.
116  */
117 #define	RACCT_IS_INHERITABLE(X)	(racct_types[X] & RACCT_INHERITABLE)
118 
119 /*
120  * racct_{add,set}(9) can actually return an error and not update resource
121  * usage counters.  Note that even when resource is not deniable, allocating
122  * resource might cause signals to be sent by RCTL code.
123  */
124 #define	RACCT_IS_DENIABLE(X)		(racct_types[X] & RACCT_DENIABLE)
125 
126 /*
127  * Per-process resource usage information makes no sense, but per-credential
128  * one does.  This kind of resources are usually allocated for process, but
129  * freed using credentials.
130  */
131 #define	RACCT_IS_SLOPPY(X)		(racct_types[X] & RACCT_SLOPPY)
132 
133 /*
134  * When a process terminates, its resource usage is not automatically
135  * subtracted from per-credential racct containers.  Instead, the resource
136  * usage of per-credential racct containers decays in time.
137  * Resource usage can also drop for such resource.
138  */
139 #define RACCT_IS_DECAYING(X)		(racct_types[X] & RACCT_DECAYING)
140 
141 /*
142  * Resource usage can drop, as opposed to only grow.
143  */
144 #define RACCT_CAN_DROP(X)		(RACCT_IS_RECLAIMABLE(X) | RACCT_IS_DECAYING(X))
145 
146 /*
147  * The 'racct' structure defines resource consumption for a particular
148  * subject, such as process or jail.
149  *
150  * This structure must be filled with zeroes initially.
151  */
152 struct racct {
153 	int64_t				r_resources[RACCT_MAX + 1];
154 	LIST_HEAD(, rctl_rule_link)	r_rule_links;
155 };
156 
157 SYSCTL_DECL(_kern_racct);
158 
159 #ifdef RACCT
160 
161 extern struct mtx racct_lock;
162 
163 #define RACCT_LOCK()		mtx_lock(&racct_lock)
164 #define RACCT_UNLOCK()		mtx_unlock(&racct_lock)
165 #define RACCT_LOCK_ASSERT()	mtx_assert(&racct_lock, MA_OWNED)
166 
167 #define RACCT_ENABLED()		__predict_false(racct_enable)
168 
169 #define	RACCT_PROC_LOCK(p)	do {		\
170 	if (RACCT_ENABLED())			\
171 		PROC_LOCK(p);			\
172 } while (0)
173 #define	RACCT_PROC_UNLOCK(p)	do {		\
174 	if (RACCT_ENABLED())			\
175 		PROC_UNLOCK(p);			\
176 } while (0)
177 
178 int	racct_add(struct proc *p, int resource, uint64_t amount);
179 void	racct_add_cred(struct ucred *cred, int resource, uint64_t amount);
180 void	racct_add_force(struct proc *p, int resource, uint64_t amount);
181 void	racct_add_buf(struct proc *p, const struct buf *bufp, int is_write);
182 int	racct_set(struct proc *p, int resource, uint64_t amount);
183 int	racct_set_unlocked(struct proc *p, int resource, uint64_t amount);
184 void	racct_set_force(struct proc *p, int resource, uint64_t amount);
185 void	racct_sub(struct proc *p, int resource, uint64_t amount);
186 void	racct_sub_cred(struct ucred *cred, int resource, uint64_t amount);
187 uint64_t	racct_get_limit(struct proc *p, int resource);
188 uint64_t	racct_get_available(struct proc *p, int resource);
189 
190 void	racct_create(struct racct **racctp);
191 void	racct_destroy(struct racct **racctp);
192 
193 int	racct_proc_fork(struct proc *parent, struct proc *child);
194 void	racct_proc_fork_done(struct proc *child);
195 void	racct_proc_exit(struct proc *p);
196 
197 void	racct_proc_ucred_changed(struct proc *p, struct ucred *oldcred,
198 	    struct ucred *newcred);
199 void	racct_move(struct racct *dest, struct racct *src);
200 void	racct_proc_throttled(struct proc *p);
201 void	racct_proc_throttle(struct proc *p, int timeout);
202 
203 #else
204 
205 #define	RACCT_PROC_LOCK(p)	do { } while (0)
206 #define	RACCT_PROC_UNLOCK(p)	do { } while (0)
207 
208 static inline int
209 racct_add(struct proc *p, int resource, uint64_t amount)
210 {
211 
212 	return (0);
213 }
214 
215 static inline void
216 racct_add_cred(struct ucred *cred, int resource, uint64_t amount)
217 {
218 }
219 
220 static inline void
221 racct_add_force(struct proc *p, int resource, uint64_t amount)
222 {
223 }
224 
225 static inline int
226 racct_set(struct proc *p, int resource, uint64_t amount)
227 {
228 
229 	return (0);
230 }
231 
232 static inline void
233 racct_set_force(struct proc *p, int resource, uint64_t amount)
234 {
235 }
236 
237 static inline void
238 racct_sub(struct proc *p, int resource, uint64_t amount)
239 {
240 }
241 
242 static inline void
243 racct_sub_cred(struct ucred *cred, int resource, uint64_t amount)
244 {
245 }
246 
247 static inline uint64_t
248 racct_get_limit(struct proc *p, int resource)
249 {
250 
251 	return (UINT64_MAX);
252 }
253 
254 static inline uint64_t
255 racct_get_available(struct proc *p, int resource)
256 {
257 
258 	return (UINT64_MAX);
259 }
260 
261 #define	racct_create(x)
262 #define	racct_destroy(x)
263 
264 static inline int
265 racct_proc_fork(struct proc *parent, struct proc *child)
266 {
267 
268 	return (0);
269 }
270 
271 static inline void
272 racct_proc_fork_done(struct proc *child)
273 {
274 }
275 
276 static inline void
277 racct_proc_exit(struct proc *p)
278 {
279 }
280 
281 #endif
282 
283 #endif /* !_RACCT_H_ */
284