2 * Copyright (c) 2010 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
33 * Resource accounting.
39 #include <sys/cdefs.h>
40 #include <sys/queue.h>
41 #include <sys/types.h>
44 struct rctl_rule_link;
50 #define RACCT_UNDEFINED -1
56 #define RACCT_MEMLOCK 5
58 #define RACCT_NOFILE 7
63 #define RACCT_MSGQQUEUED 12
64 #define RACCT_MSGQSIZE 13
65 #define RACCT_NMSGQ 14
67 #define RACCT_NSEMOP 16
69 #define RACCT_SHMSIZE 18
70 #define RACCT_WALLCLOCK 19
71 #define RACCT_PCTCPU 20
72 #define RACCT_MAX RACCT_PCTCPU
75 * Resource properties.
77 #define RACCT_IN_MILLIONS 0x01
78 #define RACCT_RECLAIMABLE 0x02
79 #define RACCT_INHERITABLE 0x04
80 #define RACCT_DENIABLE 0x08
81 #define RACCT_SLOPPY 0x10
82 #define RACCT_DECAYING 0x20
84 extern int racct_types[];
87 * Amount stored in c_resources[] is 10**6 times bigger than what's
88 * visible to the userland. It gets fixed up when retrieving resource
89 * usage or adding rules.
91 #define RACCT_IS_IN_MILLIONS(X) (racct_types[X] & RACCT_IN_MILLIONS)
94 * Resource usage can drop, as opposed to only grow. When the process
95 * terminates, its resource usage is freed from the respective
96 * per-credential racct containers.
98 #define RACCT_IS_RECLAIMABLE(X) (racct_types[X] & RACCT_RECLAIMABLE)
101 * Children inherit resource usage.
103 #define RACCT_IS_INHERITABLE(X) (racct_types[X] & RACCT_INHERITABLE)
106 * racct_{add,set}(9) can actually return an error and not update resource
107 * usage counters. Note that even when resource is not deniable, allocating
108 * resource might cause signals to be sent by RCTL code.
110 #define RACCT_IS_DENIABLE(X) (racct_types[X] & RACCT_DENIABLE)
113 * Per-process resource usage information makes no sense, but per-credential
114 * one does. This kind of resources are usually allocated for process, but
115 * freed using credentials.
117 #define RACCT_IS_SLOPPY(X) (racct_types[X] & RACCT_SLOPPY)
120 * When a process terminates, its resource usage is not automatically
121 * subtracted from per-credential racct containers. Instead, the resource
122 * usage of per-credential racct containers decays in time.
123 * Resource usage can olso drop for such resource.
124 * So far, the only such resource is RACCT_PCTCPU.
126 #define RACCT_IS_DECAYING(X) (racct_types[X] & RACCT_DECAYING)
129 * Resource usage can drop, as opposed to only grow.
131 #define RACCT_CAN_DROP(X) (RACCT_IS_RECLAIMABLE(X) | RACCT_IS_DECAYING(X))
134 * The 'racct' structure defines resource consumption for a particular
135 * subject, such as process or jail.
137 * This structure must be filled with zeroes initially.
140 int64_t r_resources[RACCT_MAX + 1];
141 LIST_HEAD(, rctl_rule_link) r_rule_links;
144 int racct_add(struct proc *p, int resource, uint64_t amount);
145 void racct_add_cred(struct ucred *cred, int resource, uint64_t amount);
146 void racct_add_force(struct proc *p, int resource, uint64_t amount);
147 int racct_set(struct proc *p, int resource, uint64_t amount);
148 void racct_set_force(struct proc *p, int resource, uint64_t amount);
149 void racct_sub(struct proc *p, int resource, uint64_t amount);
150 void racct_sub_cred(struct ucred *cred, int resource, uint64_t amount);
151 uint64_t racct_get_limit(struct proc *p, int resource);
152 uint64_t racct_get_available(struct proc *p, int resource);
154 void racct_create(struct racct **racctp);
155 void racct_destroy(struct racct **racctp);
157 int racct_proc_fork(struct proc *parent, struct proc *child);
158 void racct_proc_fork_done(struct proc *child);
159 void racct_proc_exit(struct proc *p);
161 void racct_proc_ucred_changed(struct proc *p, struct ucred *oldcred,
162 struct ucred *newcred);
163 void racct_move(struct racct *dest, struct racct *src);
165 #endif /* !_RACCT_H_ */