resourcevar.h revision 139825
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: head/sys/sys/resourcevar.h 139825 2005-01-07 02:29:27Z imp $
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
50131437Sjhb *      j - locked by sched_lock mtx
51131437Sjhb *      k - only accessed by curthread
521541Srgrimes */
531541Srgrimesstruct pstats {
541541Srgrimes#define	pstat_startzero	p_ru
55131437Sjhb	struct	rusage p_ru;		/* Stats for this process. */
56131437Sjhb	struct	rusage p_cru;		/* Stats for reaped children. */
57131437Sjhb	struct	itimerval p_timer[3];	/* (j) Virtual-time timers. */
581541Srgrimes#define	pstat_endzero	pstat_startcopy
591541Srgrimes
60121636Sjhb#define	pstat_startcopy	p_prof
61131437Sjhb	struct uprof {			/* Profile arguments. */
62131437Sjhb		caddr_t	pr_base;	/* (c + j) Buffer base. */
63131437Sjhb		u_long	pr_size;	/* (c + j) Buffer size. */
64131437Sjhb		u_long	pr_off;		/* (c + j) PC offset. */
65131437Sjhb		u_long	pr_scale;	/* (c + j) PC scaling. */
661541Srgrimes	} p_prof;
671541Srgrimes#define	pstat_endcopy	p_start
68131437Sjhb	struct	timeval p_start;	/* (b) Starting time. */
691541Srgrimes};
701541Srgrimes
71125525Sjhb#ifdef _KERNEL
72125525Sjhb
731541Srgrimes/*
741541Srgrimes * Kernel shareable process resource limits.  Because this structure
751541Srgrimes * is moderately large but changes infrequently, it is normally
76111163Stjr * shared copy-on-write after forks.
771541Srgrimes */
781541Srgrimesstruct plimit {
791541Srgrimes	struct	rlimit pl_rlimit[RLIM_NLIMITS];
80125454Sjhb	int	pl_refcnt;		/* number of references */
81125525Sjhb	struct	mtx *pl_mtx;
821541Srgrimes};
831541Srgrimes
84125525Sjhb#define	LIM_LOCK(lim)		mtx_lock((lim)->pl_mtx)
85125525Sjhb#define	LIM_UNLOCK(lim)		mtx_unlock((lim)->pl_mtx)
86125525Sjhb#define	LIM_LOCK_ASSERT(lim, f)	mtx_assert((lim)->pl_mtx, (f))
8769403Salfred
88133125Srwatson/*-
8965495Struckman * Per uid resource consumption
90133125Srwatson *
91133125Srwatson * Locking guide:
92133125Srwatson * (a) Constant from inception
93133125Srwatson * (b) Locked by ui_mtxp
94133125Srwatson * (c) Locked by global uihashtbl_mtx
9565495Struckman */
9665495Struckmanstruct uidinfo {
97133125Srwatson	LIST_ENTRY(uidinfo) ui_hash;	/* (c) hash chain of uidinfos */
98133125Srwatson	rlim_t	ui_sbsize;		/* (b) socket buffer space consumed */
99133125Srwatson	long	ui_proccnt;		/* (b) number of processes */
100133125Srwatson	uid_t	ui_uid;			/* (a) uid */
101133125Srwatson	u_int	ui_ref;			/* (b) reference count */
102125495Sjhb	struct mtx *ui_mtxp;		/* protect all counts/limits */
10365495Struckman};
10465495Struckman
10591342Salfred#define	UIDINFO_LOCK(ui)	mtx_lock((ui)->ui_mtxp)
10691342Salfred#define	UIDINFO_UNLOCK(ui)	mtx_unlock((ui)->ui_mtxp)
10789594Salfred
108125523Sjhbstruct proc;
109136152Sjhbstruct rusage_ext;
11083366Sjulianstruct thread;
11134924Sbde
112111032Sjulianvoid	 addupc_intr(struct thread *td, uintptr_t pc, u_int ticks);
113111032Sjulianvoid	 addupc_task(struct thread *td, uintptr_t pc, u_int ticks);
114136152Sjhbvoid	 calccru(struct proc *p, struct timeval *up, struct timeval *sp);
115136152Sjhbvoid	 calcru(struct proc *p, struct timeval *up, struct timeval *sp);
116132319Salfredint	 chgproccnt(struct uidinfo *uip, int diff, int maxval);
117100591Sjdpint	 chgsbsize(struct uidinfo *uip, u_int *hiwat, u_int to,
118132319Salfred	    rlim_t maxval);
11992719Salfredint	 fuswintr(void *base);
120125495Sjhbstruct plimit
121125495Sjhb	*lim_alloc(void);
122125495Sjhbvoid	 lim_copy(struct plimit *dst, struct plimit *src);
123125454Sjhbrlim_t	 lim_cur(struct proc *p, int which);
124125454Sjhbvoid	 lim_free(struct plimit *limp);
1251541Srgrimesstruct plimit
126125454Sjhb	*lim_hold(struct plimit *limp);
127125495Sjhbrlim_t	 lim_max(struct proc *p, int which);
128125495Sjhbvoid	 lim_rlimit(struct proc *p, int which, struct rlimit *rlp);
129136152Sjhbvoid	 ruadd(struct rusage *ru, struct rusage_ext *rux, struct rusage *ru2,
130136152Sjhb	    struct rusage_ext *rux2);
13192719Salfredint	 suswintr(void *base, int word);
13265495Struckmanstruct uidinfo
13392719Salfred	*uifind(uid_t uid);
134125495Sjhbvoid	 uifree(struct uidinfo *uip);
135125495Sjhbvoid	 uihashinit(void);
13692719Salfredvoid	 uihold(struct uidinfo *uip);
1374470Sbde
138125495Sjhb#endif /* _KERNEL */
139125495Sjhb#endif /* !_SYS_RESOURCEVAR_H_ */
140