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