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