kernel - reformulate the maxusers auto-sizing calculation
[dragonfly.git] / sys / kern / subr_param.c
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  * $FreeBSD: src/sys/kern/subr_param.c,v 1.42.2.10 2002/03/09 21:05:47 silby Exp $
40  * $DragonFly: src/sys/kern/subr_param.c,v 1.7 2005/06/26 22:03:22 dillon Exp $
41  */
42
43 #include "opt_param.h"
44 #include "opt_maxusers.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <vm/pmap.h>
51 #include <machine/vmparam.h>
52
53 /*
54  * System parameter formulae.
55  */
56
57 #ifndef HZ
58 #define HZ 100
59 #endif
60 #define NPROC (20 + 16 * maxusers)
61 #ifndef NBUF
62 #define NBUF 0
63 #endif
64 #ifndef MAXFILES
65 #define MAXFILES (maxproc * 16)
66 #endif
67 #ifndef MAXPOSIXLOCKSPERUID
68 #define MAXPOSIXLOCKSPERUID (maxusers * 64) /* Should be a safe value */
69 #endif
70
71 int     hz;
72 int     stathz;
73 int     profhz;
74 int     ustick;                         /* tick interval in microseconds */
75 int     nstick;                         /* tick interval in nanoseconds */
76 int     maxusers;                       /* base tunable */
77 int     maxproc;                        /* maximum # of processes */
78 int     maxprocperuid;                  /* max # of procs per user */
79 int     maxfiles;                       /* system wide open files limit */
80 int     maxfilesrootres;                /* descriptors reserved for root use */
81 int     minfilesperproc;                /* per-proc min open files (safety) */
82 int     maxfilesperproc;                /* per-proc open files limit */
83 int     maxfilesperuser;                /* per-user open files limit */
84 int     maxposixlocksperuid;            /* max # POSIX locks per uid */
85 int     ncallout;                       /* maximum # of timer events */
86 int     mbuf_wait = 32;                 /* mbuf sleep time in ticks */
87 int     nbuf;
88 int     nswbuf;
89 long    maxswzone;                      /* max swmeta KVA storage */
90 long    maxbcache;                      /* max buffer cache KVA storage */
91 u_quad_t        maxtsiz;                        /* max text size */
92 u_quad_t        dfldsiz;                        /* initial data size limit */
93 u_quad_t        maxdsiz;                        /* max data size */
94 u_quad_t        dflssiz;                        /* initial stack size limit */
95 u_quad_t        maxssiz;                        /* max stack size */
96 u_quad_t        sgrowsiz;                       /* amount to grow stack */
97
98 /*
99  * These have to be allocated somewhere; allocating
100  * them here forces loader errors if this file is omitted
101  * (if they've been externed everywhere else; hah!).
102  */
103 struct  buf *swbuf;
104
105 /*
106  * Boot time overrides that are not scaled against main memory
107  */
108 void
109 init_param1(void)
110 {
111         hz = HZ;
112         TUNABLE_INT_FETCH("kern.hz", &hz);
113         stathz = hz * 128 / 100;
114         profhz = stathz;
115         ustick = 1000000 / hz;
116         nstick = 1000000000 / hz;
117         /* can adjust 30ms in 60s */
118         ntp_default_tick_delta = howmany(30000000, 60 * hz);
119
120 #ifdef VM_SWZONE_SIZE_MAX
121         maxswzone = VM_SWZONE_SIZE_MAX;
122 #endif
123         TUNABLE_LONG_FETCH("kern.maxswzone", &maxswzone);
124 #ifdef VM_BCACHE_SIZE_MAX
125         maxbcache = VM_BCACHE_SIZE_MAX;
126 #endif
127         TUNABLE_LONG_FETCH("kern.maxbcache", &maxbcache);
128         maxtsiz = MAXTSIZ;
129         TUNABLE_QUAD_FETCH("kern.maxtsiz", &maxtsiz);
130         dfldsiz = DFLDSIZ;
131         TUNABLE_QUAD_FETCH("kern.dfldsiz", &dfldsiz);
132         maxdsiz = MAXDSIZ;
133         TUNABLE_QUAD_FETCH("kern.maxdsiz", &maxdsiz);
134         dflssiz = DFLSSIZ;
135         TUNABLE_QUAD_FETCH("kern.dflssiz", &dflssiz);
136         maxssiz = MAXSSIZ;
137         TUNABLE_QUAD_FETCH("kern.maxssiz", &maxssiz);
138         sgrowsiz = SGROWSIZ;
139         TUNABLE_QUAD_FETCH("kern.sgrowsiz", &sgrowsiz);
140 }
141
142 /*
143  * Boot time overrides that are scaled against main memory
144  */
145 void
146 init_param2(int physpages)
147 {
148         size_t limsize;
149
150         /*
151          * Calculate manually becaus the VM page queues / system is not set up yet
152          */
153         limsize = (size_t)physpages * PAGE_SIZE;
154         if (limsize > KvaSize)
155                 limsize = KvaSize;
156         limsize /= 1024 * 1024;         /* smaller of KVM or physmem in MB */
157
158         /* Base parameters */
159         maxusers = MAXUSERS;
160         TUNABLE_INT_FETCH("kern.maxusers", &maxusers);
161         if (maxusers == 0) {
162                 maxusers = limsize / 8;         /* ~384 per 3G */
163                 if (maxusers < 32)
164                         maxusers = 32;
165                 /* no upper limit */
166         }
167
168         /*
169          * The following can be overridden after boot via sysctl.  Note:
170          * unless overriden, these macros are ultimately based on maxusers.
171          *
172          * Limit maxproc so that kmap entries cannot be exhausted by
173          * processes.
174          */
175         maxproc = NPROC;
176         TUNABLE_INT_FETCH("kern.maxproc", &maxproc);
177         if (maxproc < 32)
178                 maxproc = 32;
179         if (maxproc > limsize * 21)
180                 maxproc = limsize * 21;
181
182         /*
183          * Maximum number of open files
184          */
185         maxfiles = MAXFILES;
186         TUNABLE_INT_FETCH("kern.maxfiles", &maxfiles);
187         if (maxfiles < 128)
188                 maxfiles = 128;
189
190         /*
191          * Limit file descriptors so no single user can exhaust the
192          * system.
193          *
194          * WARNING: Do not set minfilesperproc too high or the user
195          *          can exhaust the system with a combination of fork()
196          *          and open().  Actual worst case is:
197          *
198          *          (minfilesperproc * maxprocperuid) + maxfilesperuser
199          */
200         maxprocperuid = maxproc / 4;
201         if (maxprocperuid < 128)
202                 maxprocperuid = maxproc / 2;
203         minfilesperproc = 8;
204         maxfilesperproc = maxfiles / 4;
205         maxfilesperuser = maxfilesperproc * 2;
206         maxfilesrootres = maxfiles / 20;
207
208         /*
209          * Severe hack to try to prevent pipe() descriptors from
210          * blowing away kernel memory.
211          */
212         if (KvaSize <= (vm_offset_t)(1536LL * 1024 * 1024) &&
213             maxfilesperuser > 20000) {
214                 maxfilesperuser = 20000;
215         }
216
217         maxposixlocksperuid = MAXPOSIXLOCKSPERUID;
218         TUNABLE_INT_FETCH("kern.maxposixlocksperuid", &maxposixlocksperuid);
219
220         /*
221          * Unless overriden, NBUF is typically 0 (auto-sized later).
222          */
223         nbuf = NBUF;
224         TUNABLE_INT_FETCH("kern.nbuf", &nbuf);
225
226         ncallout = 16 + maxproc + maxfiles;
227         TUNABLE_INT_FETCH("kern.ncallout", &ncallout);
228 }
229