1220137Strasz/*-
2220137Strasz * Copyright (c) 2010 The FreeBSD Foundation
3220137Strasz * All rights reserved.
4220137Strasz *
5220137Strasz * This software was developed by Edward Tomasz Napierala under sponsorship
6220137Strasz * from the FreeBSD Foundation.
7220137Strasz *
8220137Strasz * Redistribution and use in source and binary forms, with or without
9220137Strasz * modification, are permitted provided that the following conditions
10220137Strasz * are met:
11220137Strasz * 1. Redistributions of source code must retain the above copyright
12220137Strasz *    notice, this list of conditions and the following disclaimer.
13220137Strasz * 2. Redistributions in binary form must reproduce the above copyright
14220137Strasz *    notice, this list of conditions and the following disclaimer in the
15220137Strasz *    documentation and/or other materials provided with the distribution.
16220137Strasz *
17220137Strasz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18220137Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19220137Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20220137Strasz * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21220137Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22220137Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23220137Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24220137Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25220137Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26220137Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27220137Strasz * SUCH DAMAGE.
28220137Strasz *
29220137Strasz * $FreeBSD$
30220137Strasz */
31220137Strasz
32220137Strasz/*
33220137Strasz * Resource accounting.
34220137Strasz */
35220137Strasz
36220137Strasz#ifndef _RACCT_H_
37220137Strasz#define	_RACCT_H_
38220137Strasz
39220137Strasz#include <sys/cdefs.h>
40220137Strasz#include <sys/queue.h>
41220137Strasz#include <sys/types.h>
42220137Strasz
43220137Straszstruct proc;
44220137Straszstruct rctl_rule_link;
45220137Straszstruct ucred;
46220137Strasz
47220137Strasz/*
48220137Strasz * Resources.
49220137Strasz */
50220137Strasz#define	RACCT_UNDEFINED		-1
51220137Strasz#define	RACCT_CPU		0
52222380Strasz#define	RACCT_DATA		1
53222380Strasz#define	RACCT_STACK		2
54222380Strasz#define	RACCT_CORE		3
55222380Strasz#define	RACCT_RSS		4
56222380Strasz#define	RACCT_MEMLOCK		5
57222380Strasz#define	RACCT_NPROC		6
58222380Strasz#define	RACCT_NOFILE		7
59222380Strasz#define	RACCT_VMEM		8
60222380Strasz#define	RACCT_NPTS		9
61222380Strasz#define	RACCT_SWAP		10
62222380Strasz#define	RACCT_NTHR		11
63222380Strasz#define	RACCT_MSGQQUEUED	12
64222380Strasz#define	RACCT_MSGQSIZE		13
65222380Strasz#define	RACCT_NMSGQ		14
66222380Strasz#define	RACCT_NSEM		15
67222380Strasz#define	RACCT_NSEMOP		16
68222380Strasz#define	RACCT_NSHM		17
69222380Strasz#define	RACCT_SHMSIZE		18
70222380Strasz#define	RACCT_WALLCLOCK		19
71242139Strasz#define	RACCT_PCTCPU		20
72242139Strasz#define	RACCT_MAX		RACCT_PCTCPU
73220137Strasz
74220137Strasz/*
75220137Strasz * Resource properties.
76220137Strasz */
77224036Strasz#define	RACCT_IN_MILLIONS	0x01
78220137Strasz#define	RACCT_RECLAIMABLE	0x02
79220137Strasz#define	RACCT_INHERITABLE	0x04
80220137Strasz#define	RACCT_DENIABLE		0x08
81220137Strasz#define	RACCT_SLOPPY		0x10
82242139Strasz#define	RACCT_DECAYING		0x20
83220137Strasz
84220137Straszextern int racct_types[];
85284665Straszextern int racct_enable;
86220137Strasz
87284665Strasz#define ASSERT_RACCT_ENABLED()	KASSERT(racct_enable, \
88284665Strasz				    ("%s called with !racct_enable", __func__))
89284665Strasz
90220137Strasz/*
91224036Strasz * Amount stored in c_resources[] is 10**6 times bigger than what's
92220137Strasz * visible to the userland.  It gets fixed up when retrieving resource
93220137Strasz * usage or adding rules.
94220137Strasz */
95299616Sngie#define	RACCT_IS_IN_MILLIONS(X)	\
96299616Sngie    ((X) != RACCT_UNDEFINED && (racct_types[(X)] & RACCT_IN_MILLIONS) != 0)
97220137Strasz
98220137Strasz/*
99242139Strasz * Resource usage can drop, as opposed to only grow.  When the process
100242139Strasz * terminates, its resource usage is freed from the respective
101242139Strasz * per-credential racct containers.
102220137Strasz */
103223844Strasz#define	RACCT_IS_RECLAIMABLE(X)	(racct_types[X] & RACCT_RECLAIMABLE)
104220137Strasz
105220137Strasz/*
106220137Strasz * Children inherit resource usage.
107220137Strasz */
108223844Strasz#define	RACCT_IS_INHERITABLE(X)	(racct_types[X] & RACCT_INHERITABLE)
109220137Strasz
110220137Strasz/*
111220137Strasz * racct_{add,set}(9) can actually return an error and not update resource
112220137Strasz * usage counters.  Note that even when resource is not deniable, allocating
113220137Strasz * resource might cause signals to be sent by RCTL code.
114220137Strasz */
115223844Strasz#define	RACCT_IS_DENIABLE(X)		(racct_types[X] & RACCT_DENIABLE)
116220137Strasz
117220137Strasz/*
118220137Strasz * Per-process resource usage information makes no sense, but per-credential
119220137Strasz * one does.  This kind of resources are usually allocated for process, but
120220137Strasz * freed using credentials.
121220137Strasz */
122223844Strasz#define	RACCT_IS_SLOPPY(X)		(racct_types[X] & RACCT_SLOPPY)
123220137Strasz
124220137Strasz/*
125242139Strasz * When a process terminates, its resource usage is not automatically
126242139Strasz * subtracted from per-credential racct containers.  Instead, the resource
127242139Strasz * usage of per-credential racct containers decays in time.
128242139Strasz * Resource usage can olso drop for such resource.
129242139Strasz * So far, the only such resource is RACCT_PCTCPU.
130242139Strasz */
131242139Strasz#define RACCT_IS_DECAYING(X)		(racct_types[X] & RACCT_DECAYING)
132242139Strasz
133242139Strasz/*
134242139Strasz * Resource usage can drop, as opposed to only grow.
135242139Strasz */
136242139Strasz#define RACCT_CAN_DROP(X)		(RACCT_IS_RECLAIMABLE(X) | RACCT_IS_DECAYING(X))
137242139Strasz
138242139Strasz/*
139220137Strasz * The 'racct' structure defines resource consumption for a particular
140220137Strasz * subject, such as process or jail.
141220137Strasz *
142220137Strasz * This structure must be filled with zeroes initially.
143220137Strasz */
144220137Straszstruct racct {
145220137Strasz	int64_t				r_resources[RACCT_MAX + 1];
146220137Strasz	LIST_HEAD(, rctl_rule_link)	r_rule_links;
147220137Strasz};
148220137Strasz
149220137Straszint	racct_add(struct proc *p, int resource, uint64_t amount);
150220137Straszvoid	racct_add_cred(struct ucred *cred, int resource, uint64_t amount);
151220137Straszvoid	racct_add_force(struct proc *p, int resource, uint64_t amount);
152220137Straszint	racct_set(struct proc *p, int resource, uint64_t amount);
153220137Straszvoid	racct_set_force(struct proc *p, int resource, uint64_t amount);
154220137Straszvoid	racct_sub(struct proc *p, int resource, uint64_t amount);
155220137Straszvoid	racct_sub_cred(struct ucred *cred, int resource, uint64_t amount);
156220137Straszuint64_t	racct_get_limit(struct proc *p, int resource);
157220137Straszuint64_t	racct_get_available(struct proc *p, int resource);
158220137Strasz
159220137Straszvoid	racct_create(struct racct **racctp);
160220137Straszvoid	racct_destroy(struct racct **racctp);
161220137Strasz
162220137Straszint	racct_proc_fork(struct proc *parent, struct proc *child);
163225940Straszvoid	racct_proc_fork_done(struct proc *child);
164220137Straszvoid	racct_proc_exit(struct proc *p);
165220137Strasz
166220137Straszvoid	racct_proc_ucred_changed(struct proc *p, struct ucred *oldcred,
167220137Strasz	    struct ucred *newcred);
168232598Straszvoid	racct_move(struct racct *dest, struct racct *src);
169220137Strasz
170220137Strasz#endif /* !_RACCT_H_ */
171