subr_param.c revision 189649
1146773Ssam/*-
2146773Ssam * Copyright (c) 1980, 1986, 1989, 1993
3146773Ssam *	The Regents of the University of California.  All rights reserved.
4146773Ssam * (c) UNIX System Laboratories, Inc.
5146773Ssam * All or some portions of this file are derived from material licensed
6146773Ssam * to the University of California by American Telephone and Telegraph
7146773Ssam * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8146773Ssam * the permission of UNIX System Laboratories, Inc.
9146773Ssam *
10146773Ssam * Redistribution and use in source and binary forms, with or without
11146773Ssam * modification, are permitted provided that the following conditions
12146773Ssam * are met:
13146773Ssam * 1. Redistributions of source code must retain the above copyright
14285275Spkelsey *    notice, this list of conditions and the following disclaimer.
15285275Spkelsey * 2. Redistributions in binary form must reproduce the above copyright
16146773Ssam *    notice, this list of conditions and the following disclaimer in the
17146773Ssam *    documentation and/or other materials provided with the distribution.
18313537Sglebius * 4. Neither the name of the University nor the names of its contributors
19313537Sglebius *    may be used to endorse or promote products derived from this software
20146773Ssam *    without specific prior written permission.
21146773Ssam *
22146773Ssam * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23146773Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24313537Sglebius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25146773Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26313537Sglebius * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27146773Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28146773Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29285275Spkelsey * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30285275Spkelsey * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31285275Spkelsey * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32285275Spkelsey * SUCH DAMAGE.
33285275Spkelsey *
34285275Spkelsey *	@(#)param.c	8.3 (Berkeley) 8/20/94
35285275Spkelsey */
36285275Spkelsey
37285275Spkelsey#include <sys/cdefs.h>
38285275Spkelsey__FBSDID("$FreeBSD: head/sys/kern/subr_param.c 189649 2009-03-10 21:28:43Z jhb $");
39285275Spkelsey
40285275Spkelsey#include "opt_param.h"
41285275Spkelsey#include "opt_maxusers.h"
42285275Spkelsey
43285275Spkelsey#include <sys/param.h>
44285275Spkelsey#include <sys/systm.h>
45285275Spkelsey#include <sys/kernel.h>
46285275Spkelsey#include <sys/sysctl.h>
47285275Spkelsey
48285275Spkelsey#include <vm/vm_param.h>
49285275Spkelsey
50146773Ssam/*
51285275Spkelsey * System parameter formulae.
52146773Ssam */
53285275Spkelsey
54146773Ssam#ifndef HZ
55#  if defined(__amd64__) || defined(__i386__) || defined(__ia64__) || defined(__sparc64__)
56#    define	HZ 1000
57#  else
58#    define	HZ 100
59#  endif
60#  ifndef HZ_VM
61#    define	HZ_VM 10
62#  endif
63#else
64#  ifndef HZ_VM
65#    define	HZ_VM HZ
66#  endif
67#endif
68#define	NPROC (20 + 16 * maxusers)
69#ifndef NBUF
70#define NBUF 0
71#endif
72#ifndef MAXFILES
73#define	MAXFILES (maxproc * 2)
74#endif
75
76/* Values of enum VM_GUEST members are used as indices in
77 * vm_guest_sysctl_names */
78enum VM_GUEST { VM_GUEST_NO = 0, VM_GUEST_VM, VM_GUEST_XEN };
79
80static int sysctl_kern_vm_guest(SYSCTL_HANDLER_ARGS);
81
82int	hz;
83int	tick;
84int	maxusers;			/* base tunable */
85int	maxproc;			/* maximum # of processes */
86int	maxprocperuid;			/* max # of procs per user */
87int	maxfiles;			/* sys. wide open files limit */
88int	maxfilesperproc;		/* per-proc open files limit */
89int	ncallout;			/* maximum # of timer events */
90int	nbuf;
91int	nswbuf;
92long	maxswzone;			/* max swmeta KVA storage */
93long	maxbcache;			/* max buffer cache KVA storage */
94long	maxpipekva;			/* Limit on pipe KVA */
95int 	vm_guest;			/* Running as virtual machine guest? */
96u_long	maxtsiz;			/* max text size */
97u_long	dfldsiz;			/* initial data size limit */
98u_long	maxdsiz;			/* max data size */
99u_long	dflssiz;			/* initial stack size limit */
100u_long	maxssiz;			/* max stack size */
101u_long	sgrowsiz;			/* amount to grow stack */
102
103SYSCTL_INT(_kern, OID_AUTO, hz, CTLFLAG_RDTUN, &hz, 0, "ticks/second");
104SYSCTL_INT(_kern, OID_AUTO, maxswzone, CTLFLAG_RDTUN, &maxswzone, 0,
105    "max swmeta KVA storage");
106SYSCTL_INT(_kern, OID_AUTO, maxbcache, CTLFLAG_RDTUN, &maxbcache, 0,
107    "max buffer cache KVA storage");
108SYSCTL_ULONG(_kern, OID_AUTO, maxtsiz, CTLFLAG_RDTUN, &maxtsiz, 0,
109    "max text size");
110SYSCTL_ULONG(_kern, OID_AUTO, dfldsiz, CTLFLAG_RDTUN, &dfldsiz, 0,
111    "initial data size limit");
112SYSCTL_ULONG(_kern, OID_AUTO, maxdsiz, CTLFLAG_RDTUN, &maxdsiz, 0,
113    "max data size");
114SYSCTL_ULONG(_kern, OID_AUTO, dflssiz, CTLFLAG_RDTUN, &dflssiz, 0,
115    "initial stack size limit");
116SYSCTL_ULONG(_kern, OID_AUTO, maxssiz, CTLFLAG_RDTUN, &maxssiz, 0,
117    "max stack size");
118SYSCTL_ULONG(_kern, OID_AUTO, sgrowsiz, CTLFLAG_RDTUN, &sgrowsiz, 0,
119    "amount to grow stack");
120SYSCTL_PROC(_kern, OID_AUTO, vm_guest, CTLFLAG_RD | CTLTYPE_STRING,
121    NULL, 0, sysctl_kern_vm_guest, "A",
122    "Virtual machine detected? (none|generic|xen)");
123
124/*
125 * These have to be allocated somewhere; allocating
126 * them here forces loader errors if this file is omitted
127 * (if they've been externed everywhere else; hah!).
128 */
129struct	buf *swbuf;
130
131static const char *const vm_guest_sysctl_names[] = {
132	"none",
133	"generic",
134	"xen",
135	NULL
136};
137
138#ifndef XEN
139static const char *const vm_bnames[] = {
140	"QEMU",				/* QEMU */
141	"Plex86",			/* Plex86 */
142	"Bochs",			/* Bochs */
143	NULL
144};
145
146static const char *const vm_pnames[] = {
147	"VMware Virtual Platform",	/* VMWare VM */
148	"Virtual Machine",		/* Microsoft VirtualPC */
149	"VirtualBox",			/* Sun xVM VirtualBox */
150	"Parallels Virtual Platform",	/* Parallels VM */
151	NULL
152};
153
154
155/*
156 * Detect known Virtual Machine hosts by inspecting the emulated BIOS.
157 */
158static enum VM_GUEST
159detect_virtual(void)
160{
161	char *sysenv;
162	int i;
163
164	sysenv = getenv("smbios.bios.vendor");
165	if (sysenv != NULL) {
166		for (i = 0; vm_bnames[i] != NULL; i++)
167			if (strcmp(sysenv, vm_bnames[i]) == 0) {
168				freeenv(sysenv);
169				return (VM_GUEST_VM);
170			}
171		freeenv(sysenv);
172	}
173	sysenv = getenv("smbios.system.product");
174	if (sysenv != NULL) {
175		for (i = 0; vm_pnames[i] != NULL; i++)
176			if (strcmp(sysenv, vm_pnames[i]) == 0) {
177				freeenv(sysenv);
178				return (VM_GUEST_VM);
179			}
180		freeenv(sysenv);
181	}
182	return (VM_GUEST_NO);
183}
184#endif
185
186/*
187 * Boot time overrides that are not scaled against main memory
188 */
189void
190init_param1(void)
191{
192#ifndef XEN
193	vm_guest = detect_virtual();
194#else
195	vm_guest = VM_GUEST_XEN;
196#endif
197	hz = -1;
198	TUNABLE_INT_FETCH("kern.hz", &hz);
199	if (hz == -1)
200		hz = vm_guest > VM_GUEST_NO ? HZ_VM : HZ;
201	tick = 1000000 / hz;
202
203#ifdef VM_SWZONE_SIZE_MAX
204	maxswzone = VM_SWZONE_SIZE_MAX;
205#endif
206	TUNABLE_LONG_FETCH("kern.maxswzone", &maxswzone);
207#ifdef VM_BCACHE_SIZE_MAX
208	maxbcache = VM_BCACHE_SIZE_MAX;
209#endif
210	TUNABLE_LONG_FETCH("kern.maxbcache", &maxbcache);
211
212	maxtsiz = MAXTSIZ;
213	TUNABLE_ULONG_FETCH("kern.maxtsiz", &maxtsiz);
214	dfldsiz = DFLDSIZ;
215	TUNABLE_ULONG_FETCH("kern.dfldsiz", &dfldsiz);
216	maxdsiz = MAXDSIZ;
217	TUNABLE_ULONG_FETCH("kern.maxdsiz", &maxdsiz);
218	dflssiz = DFLSSIZ;
219	TUNABLE_ULONG_FETCH("kern.dflssiz", &dflssiz);
220	maxssiz = MAXSSIZ;
221	TUNABLE_ULONG_FETCH("kern.maxssiz", &maxssiz);
222	sgrowsiz = SGROWSIZ;
223	TUNABLE_ULONG_FETCH("kern.sgrowsiz", &sgrowsiz);
224}
225
226/*
227 * Boot time overrides that are scaled against main memory
228 */
229void
230init_param2(long physpages)
231{
232
233	/* Base parameters */
234	maxusers = MAXUSERS;
235	TUNABLE_INT_FETCH("kern.maxusers", &maxusers);
236	if (maxusers == 0) {
237		maxusers = physpages / (2 * 1024 * 1024 / PAGE_SIZE);
238		if (maxusers < 32)
239			maxusers = 32;
240		if (maxusers > 384)
241			maxusers = 384;
242	}
243
244	/*
245	 * The following can be overridden after boot via sysctl.  Note:
246	 * unless overriden, these macros are ultimately based on maxusers.
247	 */
248	maxproc = NPROC;
249	TUNABLE_INT_FETCH("kern.maxproc", &maxproc);
250	/*
251	 * Limit maxproc so that kmap entries cannot be exhausted by
252	 * processes.
253	 */
254	if (maxproc > (physpages / 12))
255		maxproc = physpages / 12;
256	maxfiles = MAXFILES;
257	TUNABLE_INT_FETCH("kern.maxfiles", &maxfiles);
258	maxprocperuid = (maxproc * 9) / 10;
259	maxfilesperproc = (maxfiles * 9) / 10;
260
261	/*
262	 * Cannot be changed after boot.
263	 */
264	nbuf = NBUF;
265	TUNABLE_INT_FETCH("kern.nbuf", &nbuf);
266
267	ncallout = 16 + maxproc + maxfiles;
268	TUNABLE_INT_FETCH("kern.ncallout", &ncallout);
269}
270
271/*
272 * Boot time overrides that are scaled against the kmem map
273 */
274void
275init_param3(long kmempages)
276{
277
278	/*
279	 * The default for maxpipekva is max(5% of the kmem map, 512KB).
280	 * See sys_pipe.c for more details.
281	 */
282	maxpipekva = (kmempages / 20) * PAGE_SIZE;
283	if (maxpipekva < 512 * 1024)
284		maxpipekva = 512 * 1024;
285	TUNABLE_LONG_FETCH("kern.ipc.maxpipekva", &maxpipekva);
286}
287
288/*
289 * Sysctl stringiying handler for kern.vm_guest.
290 */
291static int
292sysctl_kern_vm_guest(SYSCTL_HANDLER_ARGS)
293{
294	return (SYSCTL_OUT(req, vm_guest_sysctl_names[vm_guest],
295	    strlen(vm_guest_sysctl_names[vm_guest])));
296}
297