resourcevar.h revision 210224
11539Srgrimes/*-
21539Srgrimes * Copyright (c) 1991, 1993
31539Srgrimes *	The Regents of the University of California.  All rights reserved.
41539Srgrimes *
51539Srgrimes * Redistribution and use in source and binary forms, with or without
61539Srgrimes * modification, are permitted provided that the following conditions
71539Srgrimes * are met:
81539Srgrimes * 1. Redistributions of source code must retain the above copyright
91539Srgrimes *    notice, this list of conditions and the following disclaimer.
101539Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111539Srgrimes *    notice, this list of conditions and the following disclaimer in the
121539Srgrimes *    documentation and/or other materials provided with the distribution.
131539Srgrimes * 4. Neither the name of the University nor the names of its contributors
141539Srgrimes *    may be used to endorse or promote products derived from this software
151539Srgrimes *    without specific prior written permission.
161539Srgrimes *
171539Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181539Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191539Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201539Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211539Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221539Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231539Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241539Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251539Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261539Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271539Srgrimes * SUCH DAMAGE.
281539Srgrimes *
291539Srgrimes *	@(#)resourcevar.h	8.4 (Berkeley) 1/9/95
301539Srgrimes * $FreeBSD: head/sys/sys/resourcevar.h 210224 2010-07-18 19:29:12Z trasz $
311539Srgrimes */
321539Srgrimes
3323657Speter#ifndef	_SYS_RESOURCEVAR_H_
3455031Sbde#define	_SYS_RESOURCEVAR_H_
351539Srgrimes
361539Srgrimes#include <sys/resource.h>
371539Srgrimes#include <sys/queue.h>
387865Sbde#ifdef _KERNEL
391539Srgrimes#include <sys/_lock.h>
4033861Sbde#include <sys/_mutex.h>
41123257Smarcel#endif
42102227Smike
4333861Sbde/*
44103728Swollman * Kernel per-process accounting / statistics
45102227Smike * (not necessarily resident except when running).
46102227Smike *
47102227Smike * Locking key:
4815483Sbde *      b - created at fork, never changes
4915483Sbde *      c - locked by proc mtx
5015483Sbde *      j - locked by proc slock
51102227Smike *      k - only accessed by curthread
52102227Smike */
53102227Smikestruct pstats {
541539Srgrimes#define	pstat_startzero	p_cru
551539Srgrimes	struct	rusage p_cru;		/* Stats for reaped children. */
5699640Sobrien	struct	itimerval p_timer[3];	/* (j) Virtual-time timers. */
57102227Smike#define	pstat_endzero	pstat_startcopy
58102227Smike
59102227Smike#define	pstat_startcopy	p_prof
601539Srgrimes	struct uprof {			/* Profile arguments. */
6199640Sobrien		caddr_t	pr_base;	/* (c + j) Buffer base. */
621539Srgrimes		u_long	pr_size;	/* (c + j) Buffer size. */
631539Srgrimes		u_long	pr_off;		/* (c + j) PC offset. */
64103766Sbde		u_long	pr_scale;	/* (c + j) PC scaling. */
65103766Sbde	} p_prof;
661539Srgrimes#define	pstat_endcopy	p_start
671539Srgrimes	struct	timeval p_start;	/* (b) Starting time. */
681539Srgrimes};
69103766Sbde
70103766Sbde#ifdef _KERNEL
711539Srgrimes
721539Srgrimes/*
731539Srgrimes * Kernel shareable process resource limits.  Because this structure
741539Srgrimes * is moderately large but changes infrequently, it is normally
751539Srgrimes * shared copy-on-write after forks.
761539Srgrimes */
771539Srgrimesstruct plimit {
781539Srgrimes	struct	rlimit pl_rlimit[RLIM_NLIMITS];
791539Srgrimes	int	pl_refcnt;		/* number of references */
801539Srgrimes};
811539Srgrimes
8293032Simp/*-
83153684Sphk * Per uid resource consumption.  This structure is used to track
8493032Simp * the total resource consumption (process count, socket buffer size,
8593032Simp * etc) for the uid and impose limits.
8693032Simp *
8793032Simp * Locking guide:
8893032Simp * (a) Constant from inception
8993032Simp * (b) Lockless, updated using atomics
9093032Simp * (c) Locked by global uihashtbl_mtx
91187961Sdas * (d) Locked by the ui_vmsize_mtx
9293032Simp */
9393032Simpstruct uidinfo {
9493032Simp	LIST_ENTRY(uidinfo) ui_hash;	/* (c) hash chain of uidinfos */
9593032Simp	struct mtx ui_vmsize_mtx;
9693032Simp	vm_ooffset_t ui_vmsize;		/* (d) swap reservation by uid */
9793032Simp	long	ui_sbsize;		/* (b) socket buffer space consumed */
98187961Sdas	long	ui_proccnt;		/* (b) number of processes */
99103728Swollman	long	ui_ptscnt;		/* (b) number of pseudo-terminals */
100103766Sbde	uid_t	ui_uid;			/* (a) uid */
101103728Swollman	u_int	ui_ref;			/* (b) reference count */
10293032Simp};
10393032Simp
10493032Simp#define	UIDINFO_VMSIZE_LOCK(ui)		mtx_lock(&((ui)->ui_vmsize_mtx))
10593032Simp#define	UIDINFO_VMSIZE_UNLOCK(ui)	mtx_unlock(&((ui)->ui_vmsize_mtx))
10693032Simp
107103766Sbdestruct proc;
108112163Sdasstruct rusage_ext;
109103766Sbdestruct thread;
110112163Sdas
111112163Sdasvoid	 addupc_intr(struct thread *td, uintfptr_t pc, u_int ticks);
1121539Srgrimesvoid	 addupc_task(struct thread *td, uintfptr_t pc, u_int ticks);
113103012Stjrvoid	 calccru(struct proc *p, struct timeval *up, struct timeval *sp);
11493032Simpvoid	 calcru(struct proc *p, struct timeval *up, struct timeval *sp);
11593032Simpint	 chgproccnt(struct uidinfo *uip, int diff, rlim_t maxval);
116103012Stjrint	 chgsbsize(struct uidinfo *uip, u_int *hiwat, u_int to,
1171539Srgrimes	    rlim_t maxval);
118103728Swollmanint	 chgptscnt(struct uidinfo *uip, int diff, rlim_t maxval);
119103728Swollmanint	 fuswintr(void *base);
120103728Swollmanstruct plimit
121103728Swollman	*lim_alloc(void);
122103728Swollmanvoid	 lim_copy(struct plimit *dst, struct plimit *src);
123103728Swollmanrlim_t	 lim_cur(struct proc *p, int which);
124103728Swollmanvoid	 lim_fork(struct proc *p1, struct proc *p2);
125103728Swollmanvoid	 lim_free(struct plimit *limp);
126103728Swollmanstruct plimit
127103728Swollman	*lim_hold(struct plimit *limp);
128103728Swollmanrlim_t	 lim_max(struct proc *p, int which);
129103728Swollmanvoid	 lim_rlimit(struct proc *p, int which, struct rlimit *rlp);
130103728Swollmanvoid	 ruadd(struct rusage *ru, struct rusage_ext *rux, struct rusage *ru2,
131103728Swollman	    struct rusage_ext *rux2);
132103728Swollmanvoid	 rucollect(struct rusage *ru, struct rusage *ru2);
133103728Swollmanvoid	 rufetch(struct proc *p, struct rusage *ru);
134103728Swollmanvoid	 rufetchcalc(struct proc *p, struct rusage *ru, struct timeval *up,
13569201Sphk	    struct timeval *sp);
136103728Swollmanvoid	 ruxagg(struct proc *p, struct thread *td);
137103728Swollmanint	 suswintr(void *base, int word);
138103728Swollmanstruct uidinfo
139103728Swollman	*uifind(uid_t uid);
140103728Swollmanvoid	 uifree(struct uidinfo *uip);
141103728Swollmanvoid	 uihashinit(void);
142103728Swollmanvoid	 uihold(struct uidinfo *uip);
143103728Swollman
144103728Swollman#endif /* _KERNEL */
145103766Sbde#endif /* !_SYS_RESOURCEVAR_H_ */
146103766Sbde