subr_param.c revision 186286
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 186286 2008-12-18 15:34:38Z ivoras $");
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
76186252Sivorasenum VM_GUEST { VM_GUEST_NO, VM_GUEST_VM, VM_GUEST_XEN };
77186252Sivoras
78186286Sivorasstatic int sysctl_kern_vm_guest(SYSCTL_HANDLER_ARGS);
79186286Sivoras
8080418Speterint	hz;
8180418Speterint	tick;
8280418Speterint	maxusers;			/* base tunable */
8380418Speterint	maxproc;			/* maximum # of processes */
8480418Speterint	maxprocperuid;			/* max # of procs per user */
8580418Speterint	maxfiles;			/* sys. wide open files limit */
8680418Speterint	maxfilesperproc;		/* per-proc open files limit */
8780418Speterint	ncallout;			/* maximum # of timer events */
8880418Speterint	nbuf;
8980418Speterint	nswbuf;
90151342Skrisint	maxswzone;			/* max swmeta KVA storage */
91151342Skrisint	maxbcache;			/* max buffer cache KVA storage */
92117325Ssilbyint	maxpipekva;			/* Limit on pipe KVA */
93186286Sivorasint 	vm_guest;			/* Running as virtual machine guest? */
94137393Sdesu_long	maxtsiz;			/* max text size */
95137393Sdesu_long	dfldsiz;			/* initial data size limit */
96137393Sdesu_long	maxdsiz;			/* max data size */
97137393Sdesu_long	dflssiz;			/* initial stack size limit */
98137393Sdesu_long	maxssiz;			/* max stack size */
99137393Sdesu_long	sgrowsiz;			/* amount to grow stack */
1001541Srgrimes
101178872SpjdSYSCTL_INT(_kern, OID_AUTO, hz, CTLFLAG_RDTUN, &hz, 0, "ticks/second");
102178872SpjdSYSCTL_INT(_kern, OID_AUTO, maxswzone, CTLFLAG_RDTUN, &maxswzone, 0,
103178872Spjd    "max swmeta KVA storage");
104178872SpjdSYSCTL_INT(_kern, OID_AUTO, maxbcache, CTLFLAG_RDTUN, &maxbcache, 0,
105178872Spjd    "max buffer cache KVA storage");
106178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, maxtsiz, CTLFLAG_RDTUN, &maxtsiz, 0,
107178872Spjd    "max text size");
108178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, dfldsiz, CTLFLAG_RDTUN, &dfldsiz, 0,
109178872Spjd    "initial data size limit");
110178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, maxdsiz, CTLFLAG_RDTUN, &maxdsiz, 0,
111178872Spjd    "max data size");
112178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, dflssiz, CTLFLAG_RDTUN, &dflssiz, 0,
113178872Spjd    "initial stack size limit");
114178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, maxssiz, CTLFLAG_RDTUN, &maxssiz, 0,
115178872Spjd    "max stack size");
116178872SpjdSYSCTL_ULONG(_kern, OID_AUTO, sgrowsiz, CTLFLAG_RDTUN, &sgrowsiz, 0,
117178872Spjd    "amount to grow stack");
118186286SivorasSYSCTL_PROC(_kern, OID_AUTO, vm_guest, CTLFLAG_RD | CTLTYPE_STRING,
119186286Sivoras    NULL, 0, sysctl_kern_vm_guest, "A",
120186286Sivoras    "Virtual machine detected? (none|generic|xen)");
121172696Salfred
1221541Srgrimes/*
1231541Srgrimes * These have to be allocated somewhere; allocating
1241541Srgrimes * them here forces loader errors if this file is omitted
1251541Srgrimes * (if they've been externed everywhere else; hah!).
1261541Srgrimes */
1279759Sbdestruct	buf *swbuf;
12867046Sjasone
129185772Sjkimstatic const char *const vm_bnames[] = {
130185772Sjkim	"QEMU",				/* QEMU */
131185772Sjkim	"Plex86",			/* Plex86 */
132185772Sjkim	"Bochs",			/* Bochs */
133185772Sjkim	NULL
134185772Sjkim};
135185772Sjkim
136184326Ssobomaxstatic const char *const vm_pnames[] = {
137184323Ssobomax	"VMware Virtual Platform",	/* VMWare VM */
138184323Ssobomax	"Virtual Machine",		/* Microsoft VirtualPC */
139184323Ssobomax	"VirtualBox",			/* Sun xVM VirtualBox */
140184323Ssobomax	"Parallels Virtual Platform",	/* Parallels VM */
141184323Ssobomax	NULL
142184323Ssobomax};
143184323Ssobomax
144186286Sivorasstatic const char *const vm_guest_sysctl_names[] = {
145186286Sivoras	"none",
146186286Sivoras	"generic",
147186286Sivoras	"xen",
148186286Sivoras	NULL
149186286Sivoras};
150186286Sivoras
151186286Sivoras
152186286Sivoras/*
153186286Sivoras * Detect known Virtual Machine hosts by inspecting the emulated BIOS.
154186286Sivoras */
155186252Sivorasstatic enum VM_GUEST
156184323Ssobomaxdetect_virtual(void)
157184323Ssobomax{
158184323Ssobomax	char *sysenv;
159184323Ssobomax	int i;
160184323Ssobomax
161185772Sjkim	sysenv = getenv("smbios.bios.vendor");
162185772Sjkim	if (sysenv != NULL) {
163185772Sjkim		for (i = 0; vm_bnames[i] != NULL; i++)
164185772Sjkim			if (strcmp(sysenv, vm_bnames[i]) == 0) {
165185772Sjkim				freeenv(sysenv);
166186252Sivoras				return (VM_GUEST_VM);
167185772Sjkim			}
168185772Sjkim		freeenv(sysenv);
169185772Sjkim	}
170184323Ssobomax	sysenv = getenv("smbios.system.product");
171184323Ssobomax	if (sysenv != NULL) {
172185772Sjkim		for (i = 0; vm_pnames[i] != NULL; i++)
173185772Sjkim			if (strcmp(sysenv, vm_pnames[i]) == 0) {
174185772Sjkim				freeenv(sysenv);
175186252Sivoras				return (VM_GUEST_VM);
176185772Sjkim			}
177185772Sjkim		freeenv(sysenv);
178184323Ssobomax	}
179186252Sivoras	return (VM_GUEST_NO);
180184323Ssobomax}
181184323Ssobomax
18267046Sjasone/*
18387546Sdillon * Boot time overrides that are not scaled against main memory
18480418Speter */
18580418Spetervoid
18687546Sdilloninit_param1(void)
18780418Speter{
188186252Sivoras#ifndef XEN
189186252Sivoras	vm_guest = detect_virtual();
190186252Sivoras#else
191186252Sivoras	vm_guest = VM_GUEST_XEN;
192186252Sivoras#endif
193184323Ssobomax	hz = -1;
19480418Speter	TUNABLE_INT_FETCH("kern.hz", &hz);
195185772Sjkim	if (hz == -1)
196186252Sivoras		hz = vm_guest > VM_GUEST_NO ? HZ_VM : HZ;
19780418Speter	tick = 1000000 / hz;
19880418Speter
19981986Sdillon#ifdef VM_SWZONE_SIZE_MAX
20081933Sdillon	maxswzone = VM_SWZONE_SIZE_MAX;
20181986Sdillon#endif
202151342Skris	TUNABLE_INT_FETCH("kern.maxswzone", &maxswzone);
20381986Sdillon#ifdef VM_BCACHE_SIZE_MAX
20481933Sdillon	maxbcache = VM_BCACHE_SIZE_MAX;
20581986Sdillon#endif
206151342Skris	TUNABLE_INT_FETCH("kern.maxbcache", &maxbcache);
20784783Sps
20884783Sps	maxtsiz = MAXTSIZ;
209137393Sdes	TUNABLE_ULONG_FETCH("kern.maxtsiz", &maxtsiz);
21084783Sps	dfldsiz = DFLDSIZ;
211137393Sdes	TUNABLE_ULONG_FETCH("kern.dfldsiz", &dfldsiz);
21284783Sps	maxdsiz = MAXDSIZ;
213137393Sdes	TUNABLE_ULONG_FETCH("kern.maxdsiz", &maxdsiz);
21484783Sps	dflssiz = DFLSSIZ;
215137393Sdes	TUNABLE_ULONG_FETCH("kern.dflssiz", &dflssiz);
21684783Sps	maxssiz = MAXSSIZ;
217137393Sdes	TUNABLE_ULONG_FETCH("kern.maxssiz", &maxssiz);
21884783Sps	sgrowsiz = SGROWSIZ;
219137393Sdes	TUNABLE_ULONG_FETCH("kern.sgrowsiz", &sgrowsiz);
22080418Speter}
22187546Sdillon
22287546Sdillon/*
22387546Sdillon * Boot time overrides that are scaled against main memory
22487546Sdillon */
22587546Sdillonvoid
226102600Speterinit_param2(long physpages)
22787546Sdillon{
22887546Sdillon
22987546Sdillon	/* Base parameters */
23090274Sdillon	maxusers = MAXUSERS;
23190274Sdillon	TUNABLE_INT_FETCH("kern.maxusers", &maxusers);
23290274Sdillon	if (maxusers == 0) {
23389769Sdillon		maxusers = physpages / (2 * 1024 * 1024 / PAGE_SIZE);
23487546Sdillon		if (maxusers < 32)
23587546Sdillon			maxusers = 32;
23689769Sdillon		if (maxusers > 384)
23789769Sdillon			maxusers = 384;
23887546Sdillon	}
23987546Sdillon
24087546Sdillon	/*
24187546Sdillon	 * The following can be overridden after boot via sysctl.  Note:
24287546Sdillon	 * unless overriden, these macros are ultimately based on maxusers.
24387546Sdillon	 */
24487546Sdillon	maxproc = NPROC;
24587546Sdillon	TUNABLE_INT_FETCH("kern.maxproc", &maxproc);
24691780Ssilby	/*
24791780Ssilby	 * Limit maxproc so that kmap entries cannot be exhausted by
24891780Ssilby	 * processes.
24991780Ssilby	 */
25091780Ssilby	if (maxproc > (physpages / 12))
25191780Ssilby		maxproc = physpages / 12;
25287546Sdillon	maxfiles = MAXFILES;
25387546Sdillon	TUNABLE_INT_FETCH("kern.maxfiles", &maxfiles);
25487817Ssilby	maxprocperuid = (maxproc * 9) / 10;
25587817Ssilby	maxfilesperproc = (maxfiles * 9) / 10;
256117391Ssilby
257117391Ssilby	/*
258117391Ssilby	 * Cannot be changed after boot.
259117391Ssilby	 */
260117391Ssilby	nbuf = NBUF;
261117391Ssilby	TUNABLE_INT_FETCH("kern.nbuf", &nbuf);
26287546Sdillon
263117391Ssilby	ncallout = 16 + maxproc + maxfiles;
264117391Ssilby	TUNABLE_INT_FETCH("kern.ncallout", &ncallout);
265117391Ssilby}
266117391Ssilby
267117391Ssilby/*
268180262Salc * Boot time overrides that are scaled against the kmem map
269117391Ssilby */
270117391Ssilbyvoid
271117391Ssilbyinit_param3(long kmempages)
272117391Ssilby{
273127612Salc
27487546Sdillon	/*
275180262Salc	 * The default for maxpipekva is max(5% of the kmem map, 512KB).
276127612Salc	 * See sys_pipe.c for more details.
277117325Ssilby	 */
278118764Ssilby	maxpipekva = (kmempages / 20) * PAGE_SIZE;
279117325Ssilby	if (maxpipekva < 512 * 1024)
280117325Ssilby		maxpipekva = 512 * 1024;
281118764Ssilby	TUNABLE_INT_FETCH("kern.ipc.maxpipekva", &maxpipekva);
28287546Sdillon}
283186286Sivoras
284186286Sivoras/*
285186286Sivoras * Sysctl stringiying handler for kern.vm_guest.
286186286Sivoras */
287186286Sivorasstatic int
288186286Sivorassysctl_kern_vm_guest(SYSCTL_HANDLER_ARGS)
289186286Sivoras{
290186286Sivoras	return (SYSCTL_OUT(req, vm_guest_sysctl_names[vm_guest],
291186286Sivoras	    strlen(vm_guest_sysctl_names[vm_guest])));
292186286Sivoras}
293