xref: /freebsd/sys/kern/subr_param.c (revision 0f8f86b71f022b803e99151c19db81b280f245dc)
1 /*
2  * Copyright (c) 1980, 1986, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)param.c	8.3 (Berkeley) 8/20/94
39  */
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include "opt_param.h"
45 #include "opt_maxusers.h"
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 
51 #include <machine/vmparam.h>
52 
53 /*
54  * System parameter formulae.
55  */
56 
57 #ifndef HZ
58 #ifdef __amd64__
59 #define	HZ 1024
60 #else
61 #define	HZ 100
62 #endif
63 #endif
64 #define	NPROC (20 + 16 * maxusers)
65 #ifndef NBUF
66 #define NBUF 0
67 #endif
68 #ifndef MAXFILES
69 #define	MAXFILES (maxproc * 2)
70 #endif
71 
72 int	hz;
73 int	tick;
74 int	maxusers;			/* base tunable */
75 int	maxproc;			/* maximum # of processes */
76 int	maxprocperuid;			/* max # of procs per user */
77 int	maxfiles;			/* sys. wide open files limit */
78 int	maxfilesperproc;		/* per-proc open files limit */
79 int	ncallout;			/* maximum # of timer events */
80 int	nbuf;
81 int	nswbuf;
82 int	maxswzone;			/* max swmeta KVA storage */
83 int	maxbcache;			/* max buffer cache KVA storage */
84 int	maxpipekva;			/* Limit on pipe KVA */
85 int	maxpipekvawired;		/* Limit on wired pipe KVA */
86 u_quad_t	maxtsiz;			/* max text size */
87 u_quad_t	dfldsiz;			/* initial data size limit */
88 u_quad_t	maxdsiz;			/* max data size */
89 u_quad_t	dflssiz;			/* initial stack size limit */
90 u_quad_t	maxssiz;			/* max stack size */
91 u_quad_t	sgrowsiz;			/* amount to grow stack */
92 
93 /*
94  * These have to be allocated somewhere; allocating
95  * them here forces loader errors if this file is omitted
96  * (if they've been externed everywhere else; hah!).
97  */
98 struct	buf *swbuf;
99 
100 /*
101  * Boot time overrides that are not scaled against main memory
102  */
103 void
104 init_param1(void)
105 {
106 
107 	hz = HZ;
108 	TUNABLE_INT_FETCH("kern.hz", &hz);
109 	tick = 1000000 / hz;
110 
111 #ifdef VM_SWZONE_SIZE_MAX
112 	maxswzone = VM_SWZONE_SIZE_MAX;
113 #endif
114 	TUNABLE_INT_FETCH("kern.maxswzone", &maxswzone);
115 #ifdef VM_BCACHE_SIZE_MAX
116 	maxbcache = VM_BCACHE_SIZE_MAX;
117 #endif
118 	TUNABLE_INT_FETCH("kern.maxbcache", &maxbcache);
119 
120 	maxtsiz = MAXTSIZ;
121 	TUNABLE_QUAD_FETCH("kern.maxtsiz", &maxtsiz);
122 	dfldsiz = DFLDSIZ;
123 	TUNABLE_QUAD_FETCH("kern.dfldsiz", &dfldsiz);
124 	maxdsiz = MAXDSIZ;
125 	TUNABLE_QUAD_FETCH("kern.maxdsiz", &maxdsiz);
126 	dflssiz = DFLSSIZ;
127 	TUNABLE_QUAD_FETCH("kern.dflssiz", &dflssiz);
128 	maxssiz = MAXSSIZ;
129 	TUNABLE_QUAD_FETCH("kern.maxssiz", &maxssiz);
130 	sgrowsiz = SGROWSIZ;
131 	TUNABLE_QUAD_FETCH("kern.sgrowsiz", &sgrowsiz);
132 }
133 
134 /*
135  * Boot time overrides that are scaled against main memory
136  */
137 void
138 init_param2(long physpages)
139 {
140 
141 	/* Base parameters */
142 	maxusers = MAXUSERS;
143 	TUNABLE_INT_FETCH("kern.maxusers", &maxusers);
144 	if (maxusers == 0) {
145 		maxusers = physpages / (2 * 1024 * 1024 / PAGE_SIZE);
146 		if (maxusers < 32)
147 			maxusers = 32;
148 		if (maxusers > 384)
149 			maxusers = 384;
150 	}
151 
152 	/*
153 	 * The following can be overridden after boot via sysctl.  Note:
154 	 * unless overriden, these macros are ultimately based on maxusers.
155 	 */
156 	maxproc = NPROC;
157 	TUNABLE_INT_FETCH("kern.maxproc", &maxproc);
158 	/*
159 	 * Limit maxproc so that kmap entries cannot be exhausted by
160 	 * processes.
161 	 */
162 	if (maxproc > (physpages / 12))
163 		maxproc = physpages / 12;
164 	maxfiles = MAXFILES;
165 	TUNABLE_INT_FETCH("kern.maxfiles", &maxfiles);
166 	maxprocperuid = (maxproc * 9) / 10;
167 	maxfilesperproc = (maxfiles * 9) / 10;
168 
169 	/*
170 	 * Cannot be changed after boot.
171 	 */
172 	nbuf = NBUF;
173 	TUNABLE_INT_FETCH("kern.nbuf", &nbuf);
174 
175 	ncallout = 16 + maxproc + maxfiles;
176 	TUNABLE_INT_FETCH("kern.ncallout", &ncallout);
177 }
178 
179 /*
180  * Boot time overrides that are scaled against the kernel map
181  */
182 void
183 init_param3(long kmempages)
184 {
185 	/*
186 	 * Limit pageable pipe memory usage to 5% of the kernel map
187 	 * (via pipe_map), and nonpageable pipe memory usage to 2.5%
188 	 * of the same.  Ensure that all have reasonable floors.
189 	 * (See sys_pipe.c for more info.)
190 	 */
191 	maxpipekva = (kmempages / 20) * PAGE_SIZE;
192 	maxpipekvawired = (kmempages / 40) * PAGE_SIZE;
193 
194 	if (maxpipekva < 512 * 1024)
195 		maxpipekva = 512 * 1024;
196 	if (maxpipekvawired < 512 * 1024)
197 		maxpipekvawired = 512 * 1024;
198 
199 	TUNABLE_INT_FETCH("kern.ipc.maxpipekva", &maxpipekva);
200 }
201