xref: /openbsd/sys/sys/resourcevar.h (revision 3095c52a)
1 /*	$OpenBSD: resourcevar.h,v 1.35 2024/10/24 23:24:58 jsg Exp $	*/
2 /*	$NetBSD: resourcevar.h,v 1.12 1995/11/22 23:01:53 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)resourcevar.h	8.3 (Berkeley) 2/22/94
33  */
34 
35 #ifndef	_SYS_RESOURCEVAR_H_
36 #define	_SYS_RESOURCEVAR_H_
37 
38 #include <sys/refcnt.h>
39 
40 /*
41  * Kernel shareable process resource limits.  Because this structure
42  * is moderately large but changes infrequently, it is shared
43  * copy-on-write after forks.
44  */
45 struct plimit {
46 	struct	rlimit pl_rlimit[RLIM_NLIMITS];
47 	struct	refcnt pl_refcnt;
48 };
49 
50 /* add user profiling from AST */
51 #define	ADDUPROF(p)							\
52 do {									\
53 	atomic_clearbits_int(&(p)->p_flag, P_OWEUPC);			\
54 	addupc_task((p), (p)->p_prof_addr, (p)->p_prof_ticks);		\
55 	(p)->p_prof_ticks = 0;						\
56 } while (0)
57 
58 #ifdef _KERNEL
59 
60 #include <lib/libkern/libkern.h>	/* for KASSERT() */
61 
62 extern uint64_t profclock_period;
63 
64 void	 addupc_intr(struct proc *, u_long, u_long);
65 void	 addupc_task(struct proc *, u_long, u_int);
66 struct clockrequest;
67 void	 profclock(struct clockrequest *, void *, void *);
68 void	 tuagg_add_process(struct process *, struct proc *);
69 void	 tuagg_add_runtime(void);
70 struct tusage;
71 void	 tuagg_get_proc(struct tusage *, struct proc *);
72 void	 tuagg_get_process(struct tusage *, struct process *);
73 void	 calctsru(struct tusage *, struct timespec *, struct timespec *,
74 	    struct timespec *);
75 void	 calcru(struct tusage *, struct timeval *, struct timeval *,
76 	    struct timeval *);
77 void	 lim_startup(struct plimit *);
78 void	 lim_free(struct plimit *);
79 void	 lim_fork(struct process *, struct process *);
80 struct plimit *lim_read_enter(void);
81 
82 /*
83  * Finish read access to resource limits.
84  */
85 static inline void
lim_read_leave(struct plimit * limit)86 lim_read_leave(struct plimit *limit)
87 {
88 	/* nothing */
89 }
90 
91 /*
92  * Get the value of the resource limit in current process.
93  */
94 static inline rlim_t
lim_cur(int which)95 lim_cur(int which)
96 {
97 	struct plimit *limit;
98 	rlim_t val;
99 
100 	KASSERT(which >= 0 && which < RLIM_NLIMITS);
101 
102 	limit = lim_read_enter();
103 	val = limit->pl_rlimit[which].rlim_cur;
104 	lim_read_leave(limit);
105 	return (val);
106 }
107 
108 rlim_t	 lim_cur_proc(struct proc *, int);
109 
110 void	 ruadd(struct rusage *, const struct rusage *);
111 void	 rucheck(void *);
112 
113 #endif
114 #endif	/* !_SYS_RESOURCEVAR_H_ */
115