]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/racct.h
Merge ACPICA 20141107 and 20150204.
[FreeBSD/FreeBSD.git] / sys / sys / racct.h
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/stdint.h>
41 #include <sys/queue.h>
42 #include <sys/types.h>
43
44 struct proc;
45 struct rctl_rule_link;
46 struct ucred;
47
48 /*
49  * Resources.
50  */
51 #define RACCT_UNDEFINED         -1
52 #define RACCT_CPU               0
53 #define RACCT_DATA              1
54 #define RACCT_STACK             2
55 #define RACCT_CORE              3
56 #define RACCT_RSS               4
57 #define RACCT_MEMLOCK           5
58 #define RACCT_NPROC             6
59 #define RACCT_NOFILE            7
60 #define RACCT_VMEM              8
61 #define RACCT_NPTS              9
62 #define RACCT_SWAP              10
63 #define RACCT_NTHR              11
64 #define RACCT_MSGQQUEUED        12
65 #define RACCT_MSGQSIZE          13
66 #define RACCT_NMSGQ             14
67 #define RACCT_NSEM              15
68 #define RACCT_NSEMOP            16
69 #define RACCT_NSHM              17
70 #define RACCT_SHMSIZE           18
71 #define RACCT_WALLCLOCK         19
72 #define RACCT_PCTCPU            20
73 #define RACCT_MAX               RACCT_PCTCPU
74
75 /*
76  * Resource properties.
77  */
78 #define RACCT_IN_MILLIONS       0x01
79 #define RACCT_RECLAIMABLE       0x02
80 #define RACCT_INHERITABLE       0x04
81 #define RACCT_DENIABLE          0x08
82 #define RACCT_SLOPPY            0x10
83 #define RACCT_DECAYING          0x20
84
85 extern int racct_types[];
86
87 /*
88  * Amount stored in c_resources[] is 10**6 times bigger than what's
89  * visible to the userland.  It gets fixed up when retrieving resource
90  * usage or adding rules.
91  */
92 #define RACCT_IS_IN_MILLIONS(X) (racct_types[X] & RACCT_IN_MILLIONS)
93
94 /*
95  * Resource usage can drop, as opposed to only grow.  When the process
96  * terminates, its resource usage is freed from the respective
97  * per-credential racct containers.
98  */
99 #define RACCT_IS_RECLAIMABLE(X) (racct_types[X] & RACCT_RECLAIMABLE)
100
101 /*
102  * Children inherit resource usage.
103  */
104 #define RACCT_IS_INHERITABLE(X) (racct_types[X] & RACCT_INHERITABLE)
105
106 /*
107  * racct_{add,set}(9) can actually return an error and not update resource
108  * usage counters.  Note that even when resource is not deniable, allocating
109  * resource might cause signals to be sent by RCTL code.
110  */
111 #define RACCT_IS_DENIABLE(X)            (racct_types[X] & RACCT_DENIABLE)
112
113 /*
114  * Per-process resource usage information makes no sense, but per-credential
115  * one does.  This kind of resources are usually allocated for process, but
116  * freed using credentials.
117  */
118 #define RACCT_IS_SLOPPY(X)              (racct_types[X] & RACCT_SLOPPY)
119
120 /*
121  * When a process terminates, its resource usage is not automatically
122  * subtracted from per-credential racct containers.  Instead, the resource
123  * usage of per-credential racct containers decays in time.
124  * Resource usage can olso drop for such resource.
125  * So far, the only such resource is RACCT_PCTCPU.
126  */
127 #define RACCT_IS_DECAYING(X)            (racct_types[X] & RACCT_DECAYING)
128
129 /*
130  * Resource usage can drop, as opposed to only grow.
131  */
132 #define RACCT_CAN_DROP(X)               (RACCT_IS_RECLAIMABLE(X) | RACCT_IS_DECAYING(X))
133
134 /*
135  * The 'racct' structure defines resource consumption for a particular
136  * subject, such as process or jail.
137  *
138  * This structure must be filled with zeroes initially.
139  */
140 struct racct {
141         int64_t                         r_resources[RACCT_MAX + 1];
142         LIST_HEAD(, rctl_rule_link)     r_rule_links;
143 };
144
145 #ifdef RACCT
146
147 int     racct_add(struct proc *p, int resource, uint64_t amount);
148 void    racct_add_cred(struct ucred *cred, int resource, uint64_t amount);
149 void    racct_add_force(struct proc *p, int resource, uint64_t amount);
150 int     racct_set(struct proc *p, int resource, uint64_t amount);
151 void    racct_set_force(struct proc *p, int resource, uint64_t amount);
152 void    racct_sub(struct proc *p, int resource, uint64_t amount);
153 void    racct_sub_cred(struct ucred *cred, int resource, uint64_t amount);
154 uint64_t        racct_get_limit(struct proc *p, int resource);
155 uint64_t        racct_get_available(struct proc *p, int resource);
156
157 void    racct_create(struct racct **racctp);
158 void    racct_destroy(struct racct **racctp);
159
160 int     racct_proc_fork(struct proc *parent, struct proc *child);
161 void    racct_proc_fork_done(struct proc *child);
162 void    racct_proc_exit(struct proc *p);
163
164 void    racct_proc_ucred_changed(struct proc *p, struct ucred *oldcred,
165             struct ucred *newcred);
166 void    racct_move(struct racct *dest, struct racct *src);
167
168 #else
169
170 static inline int
171 racct_add(struct proc *p, int resource, uint64_t amount)
172 {
173
174         return (0);
175 }
176
177 static inline void
178 racct_add_cred(struct ucred *cred, int resource, uint64_t amount)
179 {
180 }
181
182 static inline void
183 racct_add_force(struct proc *p, int resource, uint64_t amount)
184 {
185 }
186
187 static inline int
188 racct_set(struct proc *p, int resource, uint64_t amount)
189 {
190
191         return (0);
192 }
193
194 static inline void
195 racct_set_force(struct proc *p, int resource, uint64_t amount)
196 {
197 }
198
199 static inline void
200 racct_sub(struct proc *p, int resource, uint64_t amount)
201 {
202 }
203
204 static inline void
205 racct_sub_cred(struct ucred *cred, int resource, uint64_t amount)
206 {
207 }
208
209 static inline uint64_t
210 racct_get_limit(struct proc *p, int resource)
211 {
212
213         return (UINT64_MAX);
214 }
215
216 static inline uint64_t
217 racct_get_available(struct proc *p, int resource)
218 {
219
220         return (UINT64_MAX);
221 }
222
223 #define racct_create(x)
224 #define racct_destroy(x)
225
226 static inline int
227 racct_proc_fork(struct proc *parent, struct proc *child)
228 {
229
230         return (0);
231 }
232
233 static inline void
234 racct_proc_fork_done(struct proc *child)
235 {
236 }
237
238 static inline void
239 racct_proc_exit(struct proc *p)
240 {
241 }
242
243 #endif
244
245 #endif /* !_RACCT_H_ */