subr_param.c revision 190331
1139804Simp/*-
21541Srgrimes * Copyright (c) 1980, 1986, 1989, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes * (c) UNIX System Laboratories, Inc.
51541Srgrimes * All or some portions of this file are derived from material licensed
61541Srgrimes * to the University of California by American Telephone and Telegraph
71541Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with
81541Srgrimes * the permission of UNIX System Laboratories, Inc.
91541Srgrimes *
101541Srgrimes * Redistribution and use in source and binary forms, with or without
111541Srgrimes * modification, are permitted provided that the following conditions
121541Srgrimes * are met:
131541Srgrimes * 1. Redistributions of source code must retain the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer.
151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161541Srgrimes *    notice, this list of conditions and the following disclaimer in the
171541Srgrimes *    documentation and/or other materials provided with the distribution.
181541Srgrimes * 4. Neither the name of the University nor the names of its contributors
191541Srgrimes *    may be used to endorse or promote products derived from this software
201541Srgrimes *    without specific prior written permission.
211541Srgrimes *
221541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321541Srgrimes * SUCH DAMAGE.
331541Srgrimes *
3414526Shsu *	@(#)param.c	8.3 (Berkeley) 8/20/94
351541Srgrimes */
361541Srgrimes
37116182Sobrien#include <sys/cdefs.h>
38116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/subr_param.c 190331 2009-03-23 20:18:06Z jhb $");
39116182Sobrien
4014328Speter#include "opt_param.h"
4180418Speter#include "opt_maxusers.h"
4213226Swollman
431541Srgrimes#include <sys/param.h>
4480418Speter#include <sys/systm.h>
4580418Speter#include <sys/kernel.h>
46172696Salfred#include <sys/sysctl.h>
471541Srgrimes
48137393Sdes#include <vm/vm_param.h>
4984783Sps
501541Srgrimes/*
511541Srgrimes * System parameter formulae.
521541Srgrimes */
531541Srgrimes
541541Srgrimes#ifndef HZ
55145154Smarius#  if defined(__amd64__) || defined(__i386__) || defined(__ia64__) || defined(__sparc64__)
56137307Sphk#    define	HZ 1000
57137307Sphk#  else
58137307Sphk#    define	HZ 100
59138214Sbms#  endif
60184323Ssobomax#  ifndef HZ_VM
61184323Ssobomax#    define	HZ_VM 10
62184323Ssobomax#  endif
63184323Ssobomax#else
64184323Ssobomax#  ifndef HZ_VM
65184323Ssobomax#    define	HZ_VM HZ
66184323Ssobomax#  endif
671541Srgrimes#endif
6880418Speter#define	NPROC (20 + 16 * maxusers)
6980418Speter#ifndef NBUF
7080418Speter#define NBUF 0
7180418Speter#endif
7245515Sdes#ifndef MAXFILES
7380418Speter#define	MAXFILES (maxproc * 2)
7445515Sdes#endif
758747Sdg
76186619Sivoras/* Values of enum VM_GUEST members are used as indices in
77186619Sivoras * vm_guest_sysctl_names */
78186619Sivorasenum VM_GUEST { VM_GUEST_NO = 0, VM_GUEST_VM, VM_GUEST_XEN };
79186252Sivoras
80186286Sivorasstatic int sysctl_kern_vm_guest(SYSCTL_HANDLER_ARGS);
81186286Sivoras
8280418Speterint	hz;
8380418Speterint	tick;
8480418Speterint	maxusers;			/* base tunable */
8580418Speterint	maxproc;			/* maximum # of processes */
8680418Speterint	maxprocperuid;			/* max # of procs per user */
8780418Speterint	maxfiles;			/* sys. wide open files limit */
8880418Speterint	maxfilesperproc;		/* per-proc open files limit */
8980418Speterint	ncallout;			/* maximum # of timer events */
9080418Speterint	nbuf;
9180418Speterint	nswbuf;
92189595Sjhblong	maxswzone;			/* max swmeta KVA storage */
93189595Sjhblong	maxbcache;			/* max buffer cache KVA storage */
94189649Sjhblong	maxpipekva;			/* Limit on pipe KVA */
95186286Sivorasint 	vm_guest;			/* Running as virtual machine guest? */
96137393Sdesu_long	maxtsiz;			/* max text size */
97137393Sdesu_long	dfldsiz;			/* initial data size limit */
98137393Sdesu_long	maxdsiz;			/* max data size */
99137393Sdesu_long	dflssiz;			/* initial stack size limit */
100137393Sdesu_long	maxssiz;			/* max stack size */
101137393Sdesu_long	sgrowsiz;			/* amount to grow stack */
1021541Srgrimes
103190331SjhbSYSCTL_INT(_kern, OID_AUTO, hz, CTLFLAG_RDTUN, &hz, 0,
104190331Sjhb    "Number of clock ticks per second");
105189744SjhbSYSCTL_INT(_kern, OID_AUTO, ncallout, CTLFLAG_RDTUN, &ncallout, 0,
106189744Sjhb    "Number of pre-allocated timer events");
107189744SjhbSYSCTL_INT(_kern, OID_AUTO, nbuf, CTLFLAG_RDTUN, &nbuf, 0,
108190331Sjhb    "Number of buffers in the buffer cache");
109189744SjhbSYSCTL_INT(_kern, OID_AUTO, nswbuf, CTLFLAG_RDTUN, &nswbuf, 0,
110189744Sjhb    "Number of swap buffers");
111189745SjhbSYSCTL_LONG(_kern, OID_AUTO, maxswzone, CTLFLAG_RDTUN, &maxswzone, 0,
112190331Sjhb    "Maximum memory for swap metadata");
113189745SjhbSYSCTL_LONG(_kern, OID_AUTO, maxbcache, CTLFLAG_RDTUN, &maxbcache, 0,
114190331Sjhb    "Maximum value of vfs.maxbufspace");
115178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, maxtsiz, CTLFLAG_RDTUN, &maxtsiz, 0,
116190331Sjhb    "Maximum text size");
117178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, dfldsiz, CTLFLAG_RDTUN, &dfldsiz, 0,
118190331Sjhb    "Initial data size limit");
119178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, maxdsiz, CTLFLAG_RDTUN, &maxdsiz, 0,
120190331Sjhb    "Maximum data size");
121178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, dflssiz, CTLFLAG_RDTUN, &dflssiz, 0,
122190331Sjhb    "Initial stack size limit");
123178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, maxssiz, CTLFLAG_RDTUN, &maxssiz, 0,
124190331Sjhb    "Maximum stack size");
125178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, sgrowsiz, CTLFLAG_RDTUN, &sgrowsiz, 0,
126190331Sjhb    "Amount to grow stack on a stack fault");
127186286SivorasSYSCTL_PROC(_kern, OID_AUTO, vm_guest, CTLFLAG_RD | CTLTYPE_STRING,
128186286Sivoras    NULL, 0, sysctl_kern_vm_guest, "A",
129186286Sivoras    "Virtual machine detected? (none|generic|xen)");
130172696Salfred
1311541Srgrimes/*
1321541Srgrimes * These have to be allocated somewhere; allocating
1331541Srgrimes * them here forces loader errors if this file is omitted
1341541Srgrimes * (if they've been externed everywhere else; hah!).
1351541Srgrimes */
1369759Sbdestruct	buf *swbuf;
13767046Sjasone
138186522Sbzstatic const char *const vm_guest_sysctl_names[] = {
139186522Sbz	"none",
140186522Sbz	"generic",
141186522Sbz	"xen",
142186522Sbz	NULL
143186522Sbz};
144186522Sbz
145186522Sbz#ifndef XEN
146185772Sjkimstatic const char *const vm_bnames[] = {
147185772Sjkim	"QEMU",				/* QEMU */
148185772Sjkim	"Plex86",			/* Plex86 */
149185772Sjkim	"Bochs",			/* Bochs */
150185772Sjkim	NULL
151185772Sjkim};
152185772Sjkim
153184326Ssobomaxstatic const char *const vm_pnames[] = {
154184323Ssobomax	"VMware Virtual Platform",	/* VMWare VM */
155184323Ssobomax	"Virtual Machine",		/* Microsoft VirtualPC */
156184323Ssobomax	"VirtualBox",			/* Sun xVM VirtualBox */
157184323Ssobomax	"Parallels Virtual Platform",	/* Parallels VM */
158184323Ssobomax	NULL
159184323Ssobomax};
160184323Ssobomax
161186286Sivoras
162186286Sivoras/*
163186286Sivoras * Detect known Virtual Machine hosts by inspecting the emulated BIOS.
164186286Sivoras */
165186252Sivorasstatic enum VM_GUEST
166184323Ssobomaxdetect_virtual(void)
167184323Ssobomax{
168184323Ssobomax	char *sysenv;
169184323Ssobomax	int i;
170184323Ssobomax
171185772Sjkim	sysenv = getenv("smbios.bios.vendor");
172185772Sjkim	if (sysenv != NULL) {
173185772Sjkim		for (i = 0; vm_bnames[i] != NULL; i++)
174185772Sjkim			if (strcmp(sysenv, vm_bnames[i]) == 0) {
175185772Sjkim				freeenv(sysenv);
176186252Sivoras				return (VM_GUEST_VM);
177185772Sjkim			}
178185772Sjkim		freeenv(sysenv);
179185772Sjkim	}
180184323Ssobomax	sysenv = getenv("smbios.system.product");
181184323Ssobomax	if (sysenv != NULL) {
182185772Sjkim		for (i = 0; vm_pnames[i] != NULL; i++)
183185772Sjkim			if (strcmp(sysenv, vm_pnames[i]) == 0) {
184185772Sjkim				freeenv(sysenv);
185186252Sivoras				return (VM_GUEST_VM);
186185772Sjkim			}
187185772Sjkim		freeenv(sysenv);
188184323Ssobomax	}
189186252Sivoras	return (VM_GUEST_NO);
190184323Ssobomax}
191186522Sbz#endif
192184323Ssobomax
19367046Sjasone/*
19487546Sdillon * Boot time overrides that are not scaled against main memory
19580418Speter */
19680418Spetervoid
19787546Sdilloninit_param1(void)
19880418Speter{
199186252Sivoras#ifndef XEN
200186252Sivoras	vm_guest = detect_virtual();
201186252Sivoras#else
202186252Sivoras	vm_guest = VM_GUEST_XEN;
203186252Sivoras#endif
204184323Ssobomax	hz = -1;
20580418Speter	TUNABLE_INT_FETCH("kern.hz", &hz);
206185772Sjkim	if (hz == -1)
207186252Sivoras		hz = vm_guest > VM_GUEST_NO ? HZ_VM : HZ;
20880418Speter	tick = 1000000 / hz;
20980418Speter
21081986Sdillon#ifdef VM_SWZONE_SIZE_MAX
21181933Sdillon	maxswzone = VM_SWZONE_SIZE_MAX;
21281986Sdillon#endif
213189595Sjhb	TUNABLE_LONG_FETCH("kern.maxswzone", &maxswzone);
21481986Sdillon#ifdef VM_BCACHE_SIZE_MAX
21581933Sdillon	maxbcache = VM_BCACHE_SIZE_MAX;
21681986Sdillon#endif
217189595Sjhb	TUNABLE_LONG_FETCH("kern.maxbcache", &maxbcache);
21884783Sps
21984783Sps	maxtsiz = MAXTSIZ;
220137393Sdes	TUNABLE_ULONG_FETCH("kern.maxtsiz", &maxtsiz);
22184783Sps	dfldsiz = DFLDSIZ;
222137393Sdes	TUNABLE_ULONG_FETCH("kern.dfldsiz", &dfldsiz);
22384783Sps	maxdsiz = MAXDSIZ;
224137393Sdes	TUNABLE_ULONG_FETCH("kern.maxdsiz", &maxdsiz);
22584783Sps	dflssiz = DFLSSIZ;
226137393Sdes	TUNABLE_ULONG_FETCH("kern.dflssiz", &dflssiz);
22784783Sps	maxssiz = MAXSSIZ;
228137393Sdes	TUNABLE_ULONG_FETCH("kern.maxssiz", &maxssiz);
22984783Sps	sgrowsiz = SGROWSIZ;
230137393Sdes	TUNABLE_ULONG_FETCH("kern.sgrowsiz", &sgrowsiz);
23180418Speter}
23287546Sdillon
23387546Sdillon/*
23487546Sdillon * Boot time overrides that are scaled against main memory
23587546Sdillon */
23687546Sdillonvoid
237102600Speterinit_param2(long physpages)
23887546Sdillon{
23987546Sdillon
24087546Sdillon	/* Base parameters */
24190274Sdillon	maxusers = MAXUSERS;
24290274Sdillon	TUNABLE_INT_FETCH("kern.maxusers", &maxusers);
24390274Sdillon	if (maxusers == 0) {
24489769Sdillon		maxusers = physpages / (2 * 1024 * 1024 / PAGE_SIZE);
24587546Sdillon		if (maxusers < 32)
24687546Sdillon			maxusers = 32;
24789769Sdillon		if (maxusers > 384)
24889769Sdillon			maxusers = 384;
24987546Sdillon	}
25087546Sdillon
25187546Sdillon	/*
25287546Sdillon	 * The following can be overridden after boot via sysctl.  Note:
25387546Sdillon	 * unless overriden, these macros are ultimately based on maxusers.
25487546Sdillon	 */
25587546Sdillon	maxproc = NPROC;
25687546Sdillon	TUNABLE_INT_FETCH("kern.maxproc", &maxproc);
25791780Ssilby	/*
25891780Ssilby	 * Limit maxproc so that kmap entries cannot be exhausted by
25991780Ssilby	 * processes.
26091780Ssilby	 */
26191780Ssilby	if (maxproc > (physpages / 12))
26291780Ssilby		maxproc = physpages / 12;
26387546Sdillon	maxfiles = MAXFILES;
26487546Sdillon	TUNABLE_INT_FETCH("kern.maxfiles", &maxfiles);
26587817Ssilby	maxprocperuid = (maxproc * 9) / 10;
26687817Ssilby	maxfilesperproc = (maxfiles * 9) / 10;
267117391Ssilby
268117391Ssilby	/*
269117391Ssilby	 * Cannot be changed after boot.
270117391Ssilby	 */
271117391Ssilby	nbuf = NBUF;
272117391Ssilby	TUNABLE_INT_FETCH("kern.nbuf", &nbuf);
27387546Sdillon
274117391Ssilby	ncallout = 16 + maxproc + maxfiles;
275117391Ssilby	TUNABLE_INT_FETCH("kern.ncallout", &ncallout);
276117391Ssilby}
277117391Ssilby
278117391Ssilby/*
279180262Salc * Boot time overrides that are scaled against the kmem map
280117391Ssilby */
281117391Ssilbyvoid
282117391Ssilbyinit_param3(long kmempages)
283117391Ssilby{
284127612Salc
28587546Sdillon	/*
286180262Salc	 * The default for maxpipekva is max(5% of the kmem map, 512KB).
287127612Salc	 * See sys_pipe.c for more details.
288117325Ssilby	 */
289118764Ssilby	maxpipekva = (kmempages / 20) * PAGE_SIZE;
290117325Ssilby	if (maxpipekva < 512 * 1024)
291117325Ssilby		maxpipekva = 512 * 1024;
292189649Sjhb	TUNABLE_LONG_FETCH("kern.ipc.maxpipekva", &maxpipekva);
29387546Sdillon}
294186286Sivoras
295186286Sivoras/*
296186286Sivoras * Sysctl stringiying handler for kern.vm_guest.
297186286Sivoras */
298186286Sivorasstatic int
299186286Sivorassysctl_kern_vm_guest(SYSCTL_HANDLER_ARGS)
300186286Sivoras{
301186286Sivoras	return (SYSCTL_OUT(req, vm_guest_sysctl_names[vm_guest],
302186286Sivoras	    strlen(vm_guest_sysctl_names[vm_guest])));
303186286Sivoras}
304