xref: /openbsd-src/sys/kern/sched_bsd.c (revision 7c0ec4b8992567abb1e1536622dc789a9a39d9f1)
1 /*	$OpenBSD: sched_bsd.c,v 1.94 2024/07/08 13:17:12 claudio Exp $	*/
2 /*	$NetBSD: kern_synch.c,v 1.37 1996/04/22 01:38:37 christos Exp $	*/
3 
4 /*-
5  * Copyright (c) 1982, 1986, 1990, 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * (c) UNIX System Laboratories, Inc.
8  * All or some portions of this file are derived from material licensed
9  * to the University of California by American Telephone and Telegraph
10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11  * the permission of UNIX System Laboratories, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)kern_synch.c	8.6 (Berkeley) 1/21/94
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/clockintr.h>
43 #include <sys/proc.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/resourcevar.h>
47 #include <uvm/uvm_extern.h>
48 #include <sys/sched.h>
49 #include <sys/timeout.h>
50 #include <sys/smr.h>
51 #include <sys/tracepoint.h>
52 
53 #ifdef KTRACE
54 #include <sys/ktrace.h>
55 #endif
56 
57 uint64_t roundrobin_period;	/* [I] roundrobin period (ns) */
58 int	lbolt;			/* once a second sleep address */
59 
60 struct mutex sched_lock;
61 
62 void			update_loadavg(void *);
63 void			schedcpu(void *);
64 uint32_t		decay_aftersleep(uint32_t, uint32_t);
65 
66 extern struct cpuset sched_idle_cpus;
67 
68 /*
69  * constants for averages over 1, 5, and 15 minutes when sampling at
70  * 5 second intervals.
71  */
72 static const fixpt_t cexp[3] = {
73 	0.9200444146293232 * FSCALE,	/* exp(-1/12) */
74 	0.9834714538216174 * FSCALE,	/* exp(-1/60) */
75 	0.9944598480048967 * FSCALE,	/* exp(-1/180) */
76 };
77 
78 struct loadavg averunnable;
79 
80 /*
81  * Force switch among equal priority processes every 100ms.
82  */
83 void
84 roundrobin(struct clockrequest *cr, void *cf, void *arg)
85 {
86 	uint64_t count;
87 	struct cpu_info *ci = curcpu();
88 	struct schedstate_percpu *spc = &ci->ci_schedstate;
89 
90 	count = clockrequest_advance(cr, roundrobin_period);
91 
92 	if (ci->ci_curproc != NULL) {
93 		if (spc->spc_schedflags & SPCF_SEENRR || count >= 2) {
94 			/*
95 			 * The process has already been through a roundrobin
96 			 * without switching and may be hogging the CPU.
97 			 * Indicate that the process should yield.
98 			 */
99 			atomic_setbits_int(&spc->spc_schedflags,
100 			    SPCF_SEENRR | SPCF_SHOULDYIELD);
101 		} else {
102 			atomic_setbits_int(&spc->spc_schedflags,
103 			    SPCF_SEENRR);
104 		}
105 	}
106 
107 	if (spc->spc_nrun || spc->spc_schedflags & SPCF_SHOULDYIELD)
108 		need_resched(ci);
109 }
110 
111 
112 
113 /*
114  * update_loadav: compute a tenex style load average of a quantity on
115  * 1, 5, and 15 minute intervals.
116  */
117 void
118 update_loadavg(void *unused)
119 {
120 	static struct timeout to = TIMEOUT_INITIALIZER(update_loadavg, NULL);
121 	CPU_INFO_ITERATOR cii;
122 	struct cpu_info *ci;
123 	u_int i, nrun = 0;
124 
125 	CPU_INFO_FOREACH(cii, ci) {
126 		if (!cpuset_isset(&sched_idle_cpus, ci))
127 			nrun++;
128 		nrun += ci->ci_schedstate.spc_nrun;
129 	}
130 
131 	for (i = 0; i < 3; i++) {
132 		averunnable.ldavg[i] = (cexp[i] * averunnable.ldavg[i] +
133 		    nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
134 	}
135 
136 	timeout_add_sec(&to, 5);
137 }
138 
139 /*
140  * Constants for digital decay and forget:
141  *	90% of (p_estcpu) usage in 5 * loadav time
142  *	95% of (p_pctcpu) usage in 60 seconds (load insensitive)
143  *          Note that, as ps(1) mentions, this can let percentages
144  *          total over 100% (I've seen 137.9% for 3 processes).
145  *
146  * Note that hardclock updates p_estcpu and p_cpticks independently.
147  *
148  * We wish to decay away 90% of p_estcpu in (5 * loadavg) seconds.
149  * That is, the system wants to compute a value of decay such
150  * that the following for loop:
151  * 	for (i = 0; i < (5 * loadavg); i++)
152  * 		p_estcpu *= decay;
153  * will compute
154  * 	p_estcpu *= 0.1;
155  * for all values of loadavg:
156  *
157  * Mathematically this loop can be expressed by saying:
158  * 	decay ** (5 * loadavg) ~= .1
159  *
160  * The system computes decay as:
161  * 	decay = (2 * loadavg) / (2 * loadavg + 1)
162  *
163  * We wish to prove that the system's computation of decay
164  * will always fulfill the equation:
165  * 	decay ** (5 * loadavg) ~= .1
166  *
167  * If we compute b as:
168  * 	b = 2 * loadavg
169  * then
170  * 	decay = b / (b + 1)
171  *
172  * We now need to prove two things:
173  *	1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1)
174  *	2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg)
175  *
176  * Facts:
177  *         For x close to zero, exp(x) =~ 1 + x, since
178  *              exp(x) = 0! + x**1/1! + x**2/2! + ... .
179  *              therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b.
180  *         For x close to zero, ln(1+x) =~ x, since
181  *              ln(1+x) = x - x**2/2 + x**3/3 - ...     -1 < x < 1
182  *              therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1).
183  *         ln(.1) =~ -2.30
184  *
185  * Proof of (1):
186  *    Solve (factor)**(power) =~ .1 given power (5*loadav):
187  *	solving for factor,
188  *      ln(factor) =~ (-2.30/5*loadav), or
189  *      factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) =
190  *          exp(-1/b) =~ (b-1)/b =~ b/(b+1).                    QED
191  *
192  * Proof of (2):
193  *    Solve (factor)**(power) =~ .1 given factor == (b/(b+1)):
194  *	solving for power,
195  *      power*ln(b/(b+1)) =~ -2.30, or
196  *      power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav.  QED
197  *
198  * Actual power values for the implemented algorithm are as follows:
199  *      loadav: 1       2       3       4
200  *      power:  5.68    10.32   14.94   19.55
201  */
202 
203 /* calculations for digital decay to forget 90% of usage in 5*loadav sec */
204 #define	loadfactor(loadav)	(2 * (loadav))
205 #define	decay_cpu(loadfac, cpu)	(((loadfac) * (cpu)) / ((loadfac) + FSCALE))
206 
207 /* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */
208 fixpt_t	ccpu = 0.95122942450071400909 * FSCALE;		/* exp(-1/20) */
209 
210 /*
211  * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the
212  * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below
213  * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT).
214  *
215  * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used:
216  *	1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits).
217  *
218  * If you don't want to bother with the faster/more-accurate formula, you
219  * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate
220  * (more general) method of calculating the %age of CPU used by a process.
221  */
222 #define	CCPU_SHIFT	11
223 
224 /*
225  * Recompute process priorities, every second.
226  */
227 void
228 schedcpu(void *unused)
229 {
230 	static struct timeout to = TIMEOUT_INITIALIZER(schedcpu, NULL);
231 	fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
232 	struct proc *p;
233 	unsigned int newcpu;
234 
235 	LIST_FOREACH(p, &allproc, p_list) {
236 		/*
237 		 * Idle threads are never placed on the runqueue,
238 		 * therefore computing their priority is pointless.
239 		 */
240 		if (p->p_cpu != NULL &&
241 		    p->p_cpu->ci_schedstate.spc_idleproc == p)
242 			continue;
243 		/*
244 		 * Increment sleep time (if sleeping). We ignore overflow.
245 		 */
246 		if (p->p_stat == SSLEEP || p->p_stat == SSTOP)
247 			p->p_slptime++;
248 		p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT;
249 		/*
250 		 * If the process has slept the entire second,
251 		 * stop recalculating its priority until it wakes up.
252 		 */
253 		if (p->p_slptime > 1)
254 			continue;
255 		SCHED_LOCK();
256 		/*
257 		 * p_pctcpu is only for diagnostic tools such as ps.
258 		 */
259 #if	(FSHIFT >= CCPU_SHIFT)
260 		p->p_pctcpu += (stathz == 100)?
261 			((fixpt_t) p->p_cpticks) << (FSHIFT - CCPU_SHIFT):
262                 	100 * (((fixpt_t) p->p_cpticks)
263 				<< (FSHIFT - CCPU_SHIFT)) / stathz;
264 #else
265 		p->p_pctcpu += ((FSCALE - ccpu) *
266 			(p->p_cpticks * FSCALE / stathz)) >> FSHIFT;
267 #endif
268 		p->p_cpticks = 0;
269 		newcpu = (u_int) decay_cpu(loadfac, p->p_estcpu);
270 		setpriority(p, newcpu, p->p_p->ps_nice);
271 
272 		if (p->p_stat == SRUN &&
273 		    (p->p_runpri / SCHED_PPQ) != (p->p_usrpri / SCHED_PPQ)) {
274 			remrunqueue(p);
275 			setrunqueue(p->p_cpu, p, p->p_usrpri);
276 		}
277 		SCHED_UNLOCK();
278 	}
279 	wakeup(&lbolt);
280 	timeout_add_sec(&to, 1);
281 }
282 
283 /*
284  * Recalculate the priority of a process after it has slept for a while.
285  * For all load averages >= 1 and max p_estcpu of 255, sleeping for at
286  * least six times the loadfactor will decay p_estcpu to zero.
287  */
288 uint32_t
289 decay_aftersleep(uint32_t estcpu, uint32_t slptime)
290 {
291 	fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
292 	uint32_t newcpu;
293 
294 	if (slptime > 5 * loadfac)
295 		newcpu = 0;
296 	else {
297 		newcpu = estcpu;
298 		slptime--;	/* the first time was done in schedcpu */
299 		while (newcpu && --slptime)
300 			newcpu = decay_cpu(loadfac, newcpu);
301 
302 	}
303 
304 	return (newcpu);
305 }
306 
307 /*
308  * General yield call.  Puts the current process back on its run queue and
309  * performs a voluntary context switch.
310  */
311 void
312 yield(void)
313 {
314 	struct proc *p = curproc;
315 
316 	SCHED_LOCK();
317 	setrunqueue(p->p_cpu, p, p->p_usrpri);
318 	p->p_ru.ru_nvcsw++;
319 	mi_switch();
320 	SCHED_UNLOCK();
321 }
322 
323 /*
324  * General preemption call.  Puts the current process back on its run queue
325  * and performs an involuntary context switch.  If a process is supplied,
326  * we switch to that process.  Otherwise, we use the normal process selection
327  * criteria.
328  */
329 void
330 preempt(void)
331 {
332 	struct proc *p = curproc;
333 
334 	SCHED_LOCK();
335 	setrunqueue(p->p_cpu, p, p->p_usrpri);
336 	p->p_ru.ru_nivcsw++;
337 	mi_switch();
338 	SCHED_UNLOCK();
339 }
340 
341 void
342 mi_switch(void)
343 {
344 	struct schedstate_percpu *spc = &curcpu()->ci_schedstate;
345 	struct proc *p = curproc;
346 	struct proc *nextproc;
347 	struct timespec ts;
348 	int oldipl;
349 #ifdef MULTIPROCESSOR
350 	int hold_count;
351 #endif
352 
353 	KASSERT(p->p_stat != SONPROC);
354 
355 	SCHED_ASSERT_LOCKED();
356 
357 #ifdef MULTIPROCESSOR
358 	/*
359 	 * Release the kernel_lock, as we are about to yield the CPU.
360 	 */
361 	if (_kernel_lock_held())
362 		hold_count = __mp_release_all(&kernel_lock);
363 	else
364 		hold_count = 0;
365 #endif
366 
367 	/*
368 	 * Compute the amount of time during which the current
369 	 * process was running, and add that to its total so far.
370 	 */
371 	nanouptime(&ts);
372 	if (timespeccmp(&ts, &spc->spc_runtime, <)) {
373 #if 0
374 		printf("uptime is not monotonic! "
375 		    "ts=%lld.%09lu, runtime=%lld.%09lu\n",
376 		    (long long)tv.tv_sec, tv.tv_nsec,
377 		    (long long)spc->spc_runtime.tv_sec,
378 		    spc->spc_runtime.tv_nsec);
379 #endif
380 		timespecclear(&ts);
381 	} else {
382 		timespecsub(&ts, &spc->spc_runtime, &ts);
383 	}
384 	tu_enter(&p->p_tu);
385 	timespecadd(&p->p_tu.tu_runtime, &ts, &p->p_tu.tu_runtime);
386 	tu_leave(&p->p_tu);
387 
388 	/* Stop any optional clock interrupts. */
389 	if (ISSET(spc->spc_schedflags, SPCF_ITIMER)) {
390 		atomic_clearbits_int(&spc->spc_schedflags, SPCF_ITIMER);
391 		clockintr_cancel(&spc->spc_itimer);
392 	}
393 	if (ISSET(spc->spc_schedflags, SPCF_PROFCLOCK)) {
394 		atomic_clearbits_int(&spc->spc_schedflags, SPCF_PROFCLOCK);
395 		clockintr_cancel(&spc->spc_profclock);
396 	}
397 
398 	/*
399 	 * Process is about to yield the CPU; clear the appropriate
400 	 * scheduling flags.
401 	 */
402 	atomic_clearbits_int(&spc->spc_schedflags, SPCF_SWITCHCLEAR);
403 
404 	nextproc = sched_chooseproc();
405 
406 	/* preserve old IPL level so we can switch back to that */
407 	oldipl = MUTEX_OLDIPL(&sched_lock);
408 
409 	if (p != nextproc) {
410 		uvmexp.swtch++;
411 		TRACEPOINT(sched, off__cpu, nextproc->p_tid + THREAD_PID_OFFSET,
412 		    nextproc->p_p->ps_pid);
413 		cpu_switchto(p, nextproc);
414 		TRACEPOINT(sched, on__cpu, NULL);
415 	} else {
416 		TRACEPOINT(sched, remain__cpu, NULL);
417 		p->p_stat = SONPROC;
418 	}
419 
420 	clear_resched(curcpu());
421 
422 	SCHED_ASSERT_LOCKED();
423 
424 	/* Restore proc's IPL. */
425 	MUTEX_OLDIPL(&sched_lock) = oldipl;
426 	SCHED_UNLOCK();
427 
428 	SCHED_ASSERT_UNLOCKED();
429 
430 	assertwaitok();
431 	smr_idle();
432 
433 	/*
434 	 * We're running again; record our new start time.  We might
435 	 * be running on a new CPU now, so refetch the schedstate_percpu
436 	 * pointer.
437 	 */
438 	KASSERT(p->p_cpu == curcpu());
439 	spc = &p->p_cpu->ci_schedstate;
440 
441 	/* Start any optional clock interrupts needed by the thread. */
442 	if (ISSET(p->p_p->ps_flags, PS_ITIMER)) {
443 		atomic_setbits_int(&spc->spc_schedflags, SPCF_ITIMER);
444 		clockintr_advance(&spc->spc_itimer, hardclock_period);
445 	}
446 	if (ISSET(p->p_p->ps_flags, PS_PROFIL)) {
447 		atomic_setbits_int(&spc->spc_schedflags, SPCF_PROFCLOCK);
448 		clockintr_advance(&spc->spc_profclock, profclock_period);
449 	}
450 
451 	nanouptime(&spc->spc_runtime);
452 
453 #ifdef MULTIPROCESSOR
454 	/*
455 	 * Reacquire the kernel_lock now.  We do this after we've
456 	 * released the scheduler lock to avoid deadlock, and before
457 	 * we reacquire the interlock and the scheduler lock.
458 	 */
459 	if (hold_count)
460 		__mp_acquire_count(&kernel_lock, hold_count);
461 #endif
462 	SCHED_LOCK();
463 }
464 
465 /*
466  * Change process state to be runnable,
467  * placing it on the run queue.
468  */
469 void
470 setrunnable(struct proc *p)
471 {
472 	struct process *pr = p->p_p;
473 	u_char prio;
474 
475 	SCHED_ASSERT_LOCKED();
476 
477 	switch (p->p_stat) {
478 	case 0:
479 	case SRUN:
480 	case SONPROC:
481 	case SDEAD:
482 	case SIDL:
483 	default:
484 		panic("setrunnable");
485 	case SSTOP:
486 		/*
487 		 * If we're being traced (possibly because someone attached us
488 		 * while we were stopped), check for a signal from the debugger.
489 		 */
490 		if ((pr->ps_flags & PS_TRACED) != 0 && pr->ps_xsig != 0)
491 			atomic_setbits_int(&p->p_siglist, sigmask(pr->ps_xsig));
492 		prio = p->p_usrpri;
493 		setrunqueue(NULL, p, prio);
494 		break;
495 	case SSLEEP:
496 		prio = p->p_slppri;
497 
498 		/* if not yet asleep, don't add to runqueue */
499 		if (ISSET(p->p_flag, P_WSLEEP))
500 			return;
501 		setrunqueue(NULL, p, prio);
502 		TRACEPOINT(sched, wakeup, p->p_tid + THREAD_PID_OFFSET,
503 		    p->p_p->ps_pid, CPU_INFO_UNIT(p->p_cpu));
504 		break;
505 	}
506 	if (p->p_slptime > 1) {
507 		uint32_t newcpu;
508 
509 		newcpu = decay_aftersleep(p->p_estcpu, p->p_slptime);
510 		setpriority(p, newcpu, pr->ps_nice);
511 	}
512 	p->p_slptime = 0;
513 }
514 
515 /*
516  * Compute the priority of a process.
517  */
518 void
519 setpriority(struct proc *p, uint32_t newcpu, uint8_t nice)
520 {
521 	unsigned int newprio;
522 
523 	newprio = min((PUSER + newcpu + NICE_WEIGHT * (nice - NZERO)), MAXPRI);
524 
525 	SCHED_ASSERT_LOCKED();
526 	p->p_estcpu = newcpu;
527 	p->p_usrpri = newprio;
528 }
529 
530 /*
531  * We adjust the priority of the current process.  The priority of a process
532  * gets worse as it accumulates CPU time.  The cpu usage estimator (p_estcpu)
533  * is increased here.  The formula for computing priorities (in kern_synch.c)
534  * will compute a different value each time p_estcpu increases. This can
535  * cause a switch, but unless the priority crosses a PPQ boundary the actual
536  * queue will not change.  The cpu usage estimator ramps up quite quickly
537  * when the process is running (linearly), and decays away exponentially, at
538  * a rate which is proportionally slower when the system is busy.  The basic
539  * principle is that the system will 90% forget that the process used a lot
540  * of CPU time in 5 * loadav seconds.  This causes the system to favor
541  * processes which haven't run much recently, and to round-robin among other
542  * processes.
543  */
544 void
545 schedclock(struct proc *p)
546 {
547 	struct cpu_info *ci = curcpu();
548 	struct schedstate_percpu *spc = &ci->ci_schedstate;
549 	uint32_t newcpu;
550 
551 	if (p == spc->spc_idleproc || spc->spc_spinning)
552 		return;
553 
554 	SCHED_LOCK();
555 	newcpu = ESTCPULIM(p->p_estcpu + 1);
556 	setpriority(p, newcpu, p->p_p->ps_nice);
557 	SCHED_UNLOCK();
558 }
559 
560 void (*cpu_setperf)(int);
561 
562 #define PERFPOL_MANUAL 0
563 #define PERFPOL_AUTO 1
564 #define PERFPOL_HIGH 2
565 int perflevel = 100;
566 int perfpolicy = PERFPOL_AUTO;
567 
568 #ifndef SMALL_KERNEL
569 /*
570  * The code below handles CPU throttling.
571  */
572 #include <sys/sysctl.h>
573 
574 void setperf_auto(void *);
575 struct timeout setperf_to = TIMEOUT_INITIALIZER(setperf_auto, NULL);
576 extern int hw_power;
577 
578 void
579 setperf_auto(void *v)
580 {
581 	static uint64_t *idleticks, *totalticks;
582 	static int downbeats;
583 	int i, j = 0;
584 	int speedup = 0;
585 	CPU_INFO_ITERATOR cii;
586 	struct cpu_info *ci;
587 	uint64_t idle, total, allidle = 0, alltotal = 0;
588 
589 	if (perfpolicy != PERFPOL_AUTO)
590 		return;
591 
592 	if (cpu_setperf == NULL)
593 		return;
594 
595 	if (hw_power) {
596 		speedup = 1;
597 		goto faster;
598 	}
599 
600 	if (!idleticks)
601 		if (!(idleticks = mallocarray(ncpusfound, sizeof(*idleticks),
602 		    M_DEVBUF, M_NOWAIT | M_ZERO)))
603 			return;
604 	if (!totalticks)
605 		if (!(totalticks = mallocarray(ncpusfound, sizeof(*totalticks),
606 		    M_DEVBUF, M_NOWAIT | M_ZERO))) {
607 			free(idleticks, M_DEVBUF,
608 			    sizeof(*idleticks) * ncpusfound);
609 			return;
610 		}
611 	CPU_INFO_FOREACH(cii, ci) {
612 		if (!cpu_is_online(ci))
613 			continue;
614 		total = 0;
615 		for (i = 0; i < CPUSTATES; i++) {
616 			total += ci->ci_schedstate.spc_cp_time[i];
617 		}
618 		total -= totalticks[j];
619 		idle = ci->ci_schedstate.spc_cp_time[CP_IDLE] - idleticks[j];
620 		if (idle < total / 3)
621 			speedup = 1;
622 		alltotal += total;
623 		allidle += idle;
624 		idleticks[j] += idle;
625 		totalticks[j] += total;
626 		j++;
627 	}
628 	if (allidle < alltotal / 2)
629 		speedup = 1;
630 	if (speedup && downbeats < 5)
631 		downbeats++;
632 
633 	if (speedup && perflevel != 100) {
634 faster:
635 		perflevel = 100;
636 		cpu_setperf(perflevel);
637 	} else if (!speedup && perflevel != 0 && --downbeats <= 0) {
638 		perflevel = 0;
639 		cpu_setperf(perflevel);
640 	}
641 
642 	timeout_add_msec(&setperf_to, 100);
643 }
644 
645 int
646 sysctl_hwsetperf(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
647 {
648 	int err;
649 
650 	if (!cpu_setperf)
651 		return EOPNOTSUPP;
652 
653 	if (perfpolicy != PERFPOL_MANUAL)
654 		return sysctl_rdint(oldp, oldlenp, newp, perflevel);
655 
656 	err = sysctl_int_bounded(oldp, oldlenp, newp, newlen,
657 	    &perflevel, 0, 100);
658 	if (err)
659 		return err;
660 
661 	if (newp != NULL)
662 		cpu_setperf(perflevel);
663 
664 	return 0;
665 }
666 
667 int
668 sysctl_hwperfpolicy(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
669 {
670 	char policy[32];
671 	int err;
672 
673 	if (!cpu_setperf)
674 		return EOPNOTSUPP;
675 
676 	switch (perfpolicy) {
677 	case PERFPOL_MANUAL:
678 		strlcpy(policy, "manual", sizeof(policy));
679 		break;
680 	case PERFPOL_AUTO:
681 		strlcpy(policy, "auto", sizeof(policy));
682 		break;
683 	case PERFPOL_HIGH:
684 		strlcpy(policy, "high", sizeof(policy));
685 		break;
686 	default:
687 		strlcpy(policy, "unknown", sizeof(policy));
688 		break;
689 	}
690 
691 	if (newp == NULL)
692 		return sysctl_rdstring(oldp, oldlenp, newp, policy);
693 
694 	err = sysctl_string(oldp, oldlenp, newp, newlen, policy, sizeof(policy));
695 	if (err)
696 		return err;
697 	if (strcmp(policy, "manual") == 0)
698 		perfpolicy = PERFPOL_MANUAL;
699 	else if (strcmp(policy, "auto") == 0)
700 		perfpolicy = PERFPOL_AUTO;
701 	else if (strcmp(policy, "high") == 0)
702 		perfpolicy = PERFPOL_HIGH;
703 	else
704 		return EINVAL;
705 
706 	if (perfpolicy == PERFPOL_AUTO) {
707 		timeout_add_msec(&setperf_to, 200);
708 	} else if (perfpolicy == PERFPOL_HIGH) {
709 		perflevel = 100;
710 		cpu_setperf(perflevel);
711 	}
712 	return 0;
713 }
714 #endif
715 
716 /*
717  * Start the scheduler's periodic timeouts.
718  */
719 void
720 scheduler_start(void)
721 {
722 	schedcpu(NULL);
723 	update_loadavg(NULL);
724 
725 #ifndef SMALL_KERNEL
726 	if (perfpolicy == PERFPOL_AUTO)
727 		timeout_add_msec(&setperf_to, 200);
728 #endif
729 }
730 
731