xref: /dragonfly/sys/sys/resourcevar.h (revision ee09cc60d22ee912e69f15d20ddebfe520b3ea68)
1 /*
2  * Copyright (c) 1991, 1993
3  *        The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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
27  * SUCH DAMAGE.
28  *
29  *        @(#)resourcevar.h   8.4 (Berkeley) 1/9/95
30  * $FreeBSD: src/sys/sys/resourcevar.h,v 1.16.2.1 2000/09/07 19:13:55 truckman Exp $
31  * $DragonFly: src/sys/sys/resourcevar.h,v 1.16 2008/05/08 01:26:01 dillon Exp $
32  */
33 
34 #ifndef   _SYS_RESOURCEVAR_H_
35 #define   _SYS_RESOURCEVAR_H_
36 
37 #ifndef _SYS_TYPES_H_
38 #include <sys/types.h>
39 #endif
40 #ifndef _SYS_RESOURCE_H_
41 #include <sys/resource.h>
42 #endif
43 #ifndef _SYS_QUEUE_H_
44 #include <sys/queue.h>
45 #endif
46 #ifndef _SYS_VARSYM_H_
47 #include <sys/varsym.h>
48 #endif
49 #ifndef _SYS_TIME_H_
50 #include <sys/time.h>
51 #endif
52 #ifndef _SYS_SPINLOCK_H_
53 #include <sys/spinlock.h>
54 #endif
55 
56 struct uprof {                          /* profile arguments */
57           caddr_t   pr_base;  /* buffer base */
58           u_long    pr_size;  /* buffer size */
59           u_long    pr_off;             /* pc offset */
60           u_long    pr_scale; /* pc scaling */
61           u_long    pr_addr;  /* temp storage for addr until AST */
62           u_long    pr_ticks; /* temp storage for ticks until AST */
63 };
64 
65 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
66 
67 /*
68  * For krateprintf()
69  */
70 struct krate {
71           int freq;
72           int ticks;
73           int count;
74 };
75 
76 /*
77  * Kernel shareable process resource limits.  Because this structure
78  * is moderately large but changes infrequently, it is normally
79  * shared copy-on-write after forks.
80  *
81  * Threaded programs cache p_limit in the thread structure, allowing
82  * lockless read access.
83  *
84  * p_refcnt can change often, don't force cache mastership changes for
85  * the rest of the (usually read only) data structure.  Place p_refcnt
86  * in its own cache line.  The rest of the structure is stable as long
87  * as the caller has a ref.
88  */
89 struct plimit {
90           struct    rlimit pl_rlimit[RLIM_NLIMITS];
91           rlim_t    p_cpulimit;                   /* current cpu limit in usec */
92           struct {
93                     struct spinlock p_spin;       /* protect modifications */
94                     uint32_t p_refcnt;  /* refs & exclusivity */
95           } __cachealign;
96 } __cachealign;
97 
98 #define PLIMITF_EXCLUSIVE     0x80000000U
99 #define PLIMITF_MASK                    0x7FFFFFFFU
100 
101 #define PLIMIT_TESTCPU_OK     0
102 #define PLIMIT_TESTCPU_XCPU   1
103 #define PLIMIT_TESTCPU_KILL   2
104 
105 /*
106  * Per-cpu tracking structure attached to uidinfo.  These counts are only
107  * synchronized with the uidinfo rollup fields at +/-32.  Resource limits
108  * only check against the ui_posixlocks and ui_openfiles so some slop
109  * is possible (checking against the pcpu structures would be cause cache
110  * line ping-ponging)
111  */
112 struct uidcount {
113           int       pu_posixlocks;
114           int       pu_openfiles;
115 } __cachealign;
116 
117 #define PUP_LIMIT   32        /* +/-32 rollup */
118 
119 /*
120  * Per uid resource consumption
121  *
122  * There typically aren't too many uidinfo's, so shove the ref count
123  * out of the way with a __cachealign.
124  */
125 struct uidinfo {
126           /*
127            * Protects access to ui_sbsize, ui_proccnt, ui_posixlocks
128            */
129           LIST_ENTRY(uidinfo) ui_hash;
130           rlim_t    ui_sbsize;                    /* socket buffer space consumed */
131           long      ui_proccnt;                   /* number of processes */
132           uid_t     ui_uid;                       /* uid */
133           int       ui_posixlocks;                /* (rollup) number of POSIX locks */
134           int       ui_openfiles;                 /* (rollup) number of open files */
135           struct varsymset ui_varsymset;          /* variant symlinks */
136           struct uidcount *ui_pcpu;
137           struct {
138                     struct spinlock ui_lock;/* not currently used (FUTURE) */
139                     int       ui_ref;             /* reference count */
140           } __cachealign;
141 };
142 
143 #endif
144 
145 #ifdef _KERNEL
146 
147 struct proc;
148 struct lwp;
149 
150 void      addupc_intr (struct proc *p, u_long pc, u_int ticks);
151 void      addupc_task (struct proc *p, u_long pc, u_int ticks);
152 void      calcru (struct lwp *lp, struct timeval *up, struct timeval *sp);
153 void      calcru_proc (struct proc *p, struct rusage *ru);
154 int       chgproccnt (struct uidinfo *uip, int diff, int max);
155 int       chgsbsize (struct uidinfo *uip, u_long *hiwat, u_long to, rlim_t max);
156 void      ruadd (struct rusage *ru, struct rusage *ru2);
157 struct uidinfo *uifind (uid_t uid);
158 struct uidinfo *uicreate(uid_t uid);
159 void      uihold (struct uidinfo *uip);
160 void      uidrop (struct uidinfo *uip);
161 void      uireplace (struct uidinfo **puip, struct uidinfo *nuip);
162 void      uihashinit (void);
163 
164 void plimit_init0(struct plimit *);
165 struct plimit *plimit_fork(struct proc *);
166 u_int64_t plimit_getadjvalue(int i);
167 void plimit_lwp_fork(struct proc *);
168 int plimit_testcpulimit(struct proc *, u_int64_t);
169 void plimit_modify(struct proc *, int, struct rlimit *);
170 void plimit_free(struct plimit *);
171 
172 #endif
173 
174 #endif    /* !_SYS_RESOURCEVAR_H_ */
175