1139825Simp/*-
21541Srgrimes * Copyright (c) 1991, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 4. Neither the name of the University nor the names of its contributors
141541Srgrimes *    may be used to endorse or promote products derived from this software
151541Srgrimes *    without specific prior written permission.
161541Srgrimes *
171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes *
2914501Shsu *	@(#)resourcevar.h	8.4 (Berkeley) 1/9/95
3050477Speter * $FreeBSD$
311541Srgrimes */
321541Srgrimes
331541Srgrimes#ifndef	_SYS_RESOURCEVAR_H_
341541Srgrimes#define	_SYS_RESOURCEVAR_H_
351541Srgrimes
3634924Sbde#include <sys/resource.h>
3765495Struckman#include <sys/queue.h>
3876166Smarkm#ifdef _KERNEL
3976166Smarkm#include <sys/_lock.h>
4076166Smarkm#include <sys/_mutex.h>
4176166Smarkm#endif
4234924Sbde
431541Srgrimes/*
441541Srgrimes * Kernel per-process accounting / statistics
451541Srgrimes * (not necessarily resident except when running).
46131437Sjhb *
47131437Sjhb * Locking key:
48131437Sjhb *      b - created at fork, never changes
49131437Sjhb *      c - locked by proc mtx
50170305Sjeff *      j - locked by proc slock
51131437Sjhb *      k - only accessed by curthread
521541Srgrimes */
531541Srgrimesstruct pstats {
54170174Sjeff#define	pstat_startzero	p_cru
55131437Sjhb	struct	rusage p_cru;		/* Stats for reaped children. */
56131437Sjhb	struct	itimerval p_timer[3];	/* (j) Virtual-time timers. */
571541Srgrimes#define	pstat_endzero	pstat_startcopy
581541Srgrimes
59121636Sjhb#define	pstat_startcopy	p_prof
60131437Sjhb	struct uprof {			/* Profile arguments. */
61131437Sjhb		caddr_t	pr_base;	/* (c + j) Buffer base. */
62131437Sjhb		u_long	pr_size;	/* (c + j) Buffer size. */
63131437Sjhb		u_long	pr_off;		/* (c + j) PC offset. */
64131437Sjhb		u_long	pr_scale;	/* (c + j) PC scaling. */
651541Srgrimes	} p_prof;
661541Srgrimes#define	pstat_endcopy	p_start
67131437Sjhb	struct	timeval p_start;	/* (b) Starting time. */
681541Srgrimes};
691541Srgrimes
70125525Sjhb#ifdef _KERNEL
71125525Sjhb
721541Srgrimes/*
731541Srgrimes * Kernel shareable process resource limits.  Because this structure
741541Srgrimes * is moderately large but changes infrequently, it is normally
75111163Stjr * shared copy-on-write after forks.
761541Srgrimes */
771541Srgrimesstruct plimit {
781541Srgrimes	struct	rlimit pl_rlimit[RLIM_NLIMITS];
79125454Sjhb	int	pl_refcnt;		/* number of references */
801541Srgrimes};
811541Srgrimes
82220137Straszstruct racct;
83220137Strasz
84210226Strasz/*-
85210224Strasz * Per uid resource consumption.  This structure is used to track
86210224Strasz * the total resource consumption (process count, socket buffer size,
87210224Strasz * etc) for the uid and impose limits.
88133125Srwatson *
89133125Srwatson * Locking guide:
90133125Srwatson * (a) Constant from inception
91177277Spjd * (b) Lockless, updated using atomics
92133125Srwatson * (c) Locked by global uihashtbl_mtx
93194766Skib * (d) Locked by the ui_vmsize_mtx
9465495Struckman */
9565495Struckmanstruct uidinfo {
96133125Srwatson	LIST_ENTRY(uidinfo) ui_hash;	/* (c) hash chain of uidinfos */
97194766Skib	struct mtx ui_vmsize_mtx;
98194766Skib	vm_ooffset_t ui_vmsize;		/* (d) swap reservation by uid */
99177277Spjd	long	ui_sbsize;		/* (b) socket buffer space consumed */
100133125Srwatson	long	ui_proccnt;		/* (b) number of processes */
101181905Sed	long	ui_ptscnt;		/* (b) number of pseudo-terminals */
102133125Srwatson	uid_t	ui_uid;			/* (a) uid */
103133125Srwatson	u_int	ui_ref;			/* (b) reference count */
104220137Strasz	struct racct *ui_racct;		/* (a) resource accounting */
10565495Struckman};
10665495Struckman
107194766Skib#define	UIDINFO_VMSIZE_LOCK(ui)		mtx_lock(&((ui)->ui_vmsize_mtx))
108194766Skib#define	UIDINFO_VMSIZE_UNLOCK(ui)	mtx_unlock(&((ui)->ui_vmsize_mtx))
109194766Skib
110125523Sjhbstruct proc;
111136152Sjhbstruct rusage_ext;
11283366Sjulianstruct thread;
11334924Sbde
114153489Sjhbvoid	 addupc_intr(struct thread *td, uintfptr_t pc, u_int ticks);
115153489Sjhbvoid	 addupc_task(struct thread *td, uintfptr_t pc, u_int ticks);
116136152Sjhbvoid	 calccru(struct proc *p, struct timeval *up, struct timeval *sp);
117136152Sjhbvoid	 calcru(struct proc *p, struct timeval *up, struct timeval *sp);
118177277Spjdint	 chgproccnt(struct uidinfo *uip, int diff, rlim_t maxval);
119100591Sjdpint	 chgsbsize(struct uidinfo *uip, u_int *hiwat, u_int to,
120132319Salfred	    rlim_t maxval);
121181905Sedint	 chgptscnt(struct uidinfo *uip, int diff, rlim_t maxval);
12292719Salfredint	 fuswintr(void *base);
123230470Strocinyint	 kern_proc_setrlimit(struct thread *td, struct proc *p, u_int which,
124230470Strociny	    struct rlimit *limp);
125125495Sjhbstruct plimit
126125495Sjhb	*lim_alloc(void);
127125495Sjhbvoid	 lim_copy(struct plimit *dst, struct plimit *src);
128125454Sjhbrlim_t	 lim_cur(struct proc *p, int which);
129170174Sjeffvoid	 lim_fork(struct proc *p1, struct proc *p2);
130125454Sjhbvoid	 lim_free(struct plimit *limp);
1311541Srgrimesstruct plimit
132125454Sjhb	*lim_hold(struct plimit *limp);
133125495Sjhbrlim_t	 lim_max(struct proc *p, int which);
134125495Sjhbvoid	 lim_rlimit(struct proc *p, int which, struct rlimit *rlp);
135136152Sjhbvoid	 ruadd(struct rusage *ru, struct rusage_ext *rux, struct rusage *ru2,
136136152Sjhb	    struct rusage_ext *rux2);
137170472Sattiliovoid	 rucollect(struct rusage *ru, struct rusage *ru2);
138170174Sjeffvoid	 rufetch(struct proc *p, struct rusage *ru);
139170472Sattiliovoid	 rufetchcalc(struct proc *p, struct rusage *ru, struct timeval *up,
140170472Sattilio	    struct timeval *sp);
141224188Sjhbvoid	 rufetchtd(struct thread *td, struct rusage *ru);
142208488Skibvoid	 ruxagg(struct proc *p, struct thread *td);
14392719Salfredint	 suswintr(void *base, int word);
14465495Struckmanstruct uidinfo
14592719Salfred	*uifind(uid_t uid);
146125495Sjhbvoid	 uifree(struct uidinfo *uip);
147125495Sjhbvoid	 uihashinit(void);
14892719Salfredvoid	 uihold(struct uidinfo *uip);
149220137Straszvoid	 ui_racct_foreach(void (*callback)(struct racct *racct,
150220137Strasz	    void *arg2, void *arg3), void *arg2, void *arg3);
1514470Sbde
152125495Sjhb#endif /* _KERNEL */
153125495Sjhb#endif /* !_SYS_RESOURCEVAR_H_ */
154