xref: /openbsd/sys/kern/sched_bsd.c (revision 09467b48)
1 /*	$OpenBSD: sched_bsd.c,v 1.63 2020/05/30 14:42:59 solene 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/proc.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/signalvar.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 
58 int	lbolt;			/* once a second sleep address */
59 int	rrticks_init;		/* # of hardclock ticks per roundrobin() */
60 
61 #ifdef MULTIPROCESSOR
62 struct __mp_lock sched_lock;
63 #endif
64 
65 void			schedcpu(void *);
66 uint32_t		decay_aftersleep(uint32_t, uint32_t);
67 
68 void
69 scheduler_start(void)
70 {
71 	static struct timeout schedcpu_to;
72 
73 	/*
74 	 * We avoid polluting the global namespace by keeping the scheduler
75 	 * timeouts static in this function.
76 	 * We setup the timeout here and kick schedcpu once to make it do
77 	 * its job.
78 	 */
79 	timeout_set(&schedcpu_to, schedcpu, &schedcpu_to);
80 
81 	rrticks_init = hz / 10;
82 	schedcpu(&schedcpu_to);
83 }
84 
85 /*
86  * Force switch among equal priority processes every 100ms.
87  */
88 void
89 roundrobin(struct cpu_info *ci)
90 {
91 	struct schedstate_percpu *spc = &ci->ci_schedstate;
92 
93 	spc->spc_rrticks = rrticks_init;
94 
95 	if (ci->ci_curproc != NULL) {
96 		if (spc->spc_schedflags & SPCF_SEENRR) {
97 			/*
98 			 * The process has already been through a roundrobin
99 			 * without switching and may be hogging the CPU.
100 			 * Indicate that the process should yield.
101 			 */
102 			atomic_setbits_int(&spc->spc_schedflags,
103 			    SPCF_SHOULDYIELD);
104 		} else {
105 			atomic_setbits_int(&spc->spc_schedflags,
106 			    SPCF_SEENRR);
107 		}
108 	}
109 
110 	if (spc->spc_nrun)
111 		need_resched(ci);
112 }
113 
114 /*
115  * Constants for digital decay and forget:
116  *	90% of (p_estcpu) usage in 5 * loadav time
117  *	95% of (p_pctcpu) usage in 60 seconds (load insensitive)
118  *          Note that, as ps(1) mentions, this can let percentages
119  *          total over 100% (I've seen 137.9% for 3 processes).
120  *
121  * Note that hardclock updates p_estcpu and p_cpticks independently.
122  *
123  * We wish to decay away 90% of p_estcpu in (5 * loadavg) seconds.
124  * That is, the system wants to compute a value of decay such
125  * that the following for loop:
126  * 	for (i = 0; i < (5 * loadavg); i++)
127  * 		p_estcpu *= decay;
128  * will compute
129  * 	p_estcpu *= 0.1;
130  * for all values of loadavg:
131  *
132  * Mathematically this loop can be expressed by saying:
133  * 	decay ** (5 * loadavg) ~= .1
134  *
135  * The system computes decay as:
136  * 	decay = (2 * loadavg) / (2 * loadavg + 1)
137  *
138  * We wish to prove that the system's computation of decay
139  * will always fulfill the equation:
140  * 	decay ** (5 * loadavg) ~= .1
141  *
142  * If we compute b as:
143  * 	b = 2 * loadavg
144  * then
145  * 	decay = b / (b + 1)
146  *
147  * We now need to prove two things:
148  *	1) Given factor ** (5 * loadavg) ~= .1, prove factor == b/(b+1)
149  *	2) Given b/(b+1) ** power ~= .1, prove power == (5 * loadavg)
150  *
151  * Facts:
152  *         For x close to zero, exp(x) =~ 1 + x, since
153  *              exp(x) = 0! + x**1/1! + x**2/2! + ... .
154  *              therefore exp(-1/b) =~ 1 - (1/b) = (b-1)/b.
155  *         For x close to zero, ln(1+x) =~ x, since
156  *              ln(1+x) = x - x**2/2 + x**3/3 - ...     -1 < x < 1
157  *              therefore ln(b/(b+1)) = ln(1 - 1/(b+1)) =~ -1/(b+1).
158  *         ln(.1) =~ -2.30
159  *
160  * Proof of (1):
161  *    Solve (factor)**(power) =~ .1 given power (5*loadav):
162  *	solving for factor,
163  *      ln(factor) =~ (-2.30/5*loadav), or
164  *      factor =~ exp(-1/((5/2.30)*loadav)) =~ exp(-1/(2*loadav)) =
165  *          exp(-1/b) =~ (b-1)/b =~ b/(b+1).                    QED
166  *
167  * Proof of (2):
168  *    Solve (factor)**(power) =~ .1 given factor == (b/(b+1)):
169  *	solving for power,
170  *      power*ln(b/(b+1)) =~ -2.30, or
171  *      power =~ 2.3 * (b + 1) = 4.6*loadav + 2.3 =~ 5*loadav.  QED
172  *
173  * Actual power values for the implemented algorithm are as follows:
174  *      loadav: 1       2       3       4
175  *      power:  5.68    10.32   14.94   19.55
176  */
177 
178 /* calculations for digital decay to forget 90% of usage in 5*loadav sec */
179 #define	loadfactor(loadav)	(2 * (loadav))
180 #define	decay_cpu(loadfac, cpu)	(((loadfac) * (cpu)) / ((loadfac) + FSCALE))
181 
182 /* decay 95% of `p_pctcpu' in 60 seconds; see CCPU_SHIFT before changing */
183 fixpt_t	ccpu = 0.95122942450071400909 * FSCALE;		/* exp(-1/20) */
184 
185 /*
186  * If `ccpu' is not equal to `exp(-1/20)' and you still want to use the
187  * faster/more-accurate formula, you'll have to estimate CCPU_SHIFT below
188  * and possibly adjust FSHIFT in "param.h" so that (FSHIFT >= CCPU_SHIFT).
189  *
190  * To estimate CCPU_SHIFT for exp(-1/20), the following formula was used:
191  *	1 - exp(-1/20) ~= 0.0487 ~= 0.0488 == 1 (fixed pt, *11* bits).
192  *
193  * If you don't want to bother with the faster/more-accurate formula, you
194  * can set CCPU_SHIFT to (FSHIFT + 1) which will use a slower/less-accurate
195  * (more general) method of calculating the %age of CPU used by a process.
196  */
197 #define	CCPU_SHIFT	11
198 
199 /*
200  * Recompute process priorities, every second.
201  */
202 void
203 schedcpu(void *arg)
204 {
205 	struct timeout *to = (struct timeout *)arg;
206 	fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
207 	struct proc *p;
208 	int s;
209 	unsigned int newcpu;
210 	int phz;
211 
212 	/*
213 	 * If we have a statistics clock, use that to calculate CPU
214 	 * time, otherwise revert to using the profiling clock (which,
215 	 * in turn, defaults to hz if there is no separate profiling
216 	 * clock available)
217 	 */
218 	phz = stathz ? stathz : profhz;
219 	KASSERT(phz);
220 
221 	LIST_FOREACH(p, &allproc, p_list) {
222 		/*
223 		 * Idle threads are never placed on the runqueue,
224 		 * therefore computing their priority is pointless.
225 		 */
226 		if (p->p_cpu != NULL &&
227 		    p->p_cpu->ci_schedstate.spc_idleproc == p)
228 			continue;
229 		/*
230 		 * Increment sleep time (if sleeping). We ignore overflow.
231 		 */
232 		if (p->p_stat == SSLEEP || p->p_stat == SSTOP)
233 			p->p_slptime++;
234 		p->p_pctcpu = (p->p_pctcpu * ccpu) >> FSHIFT;
235 		/*
236 		 * If the process has slept the entire second,
237 		 * stop recalculating its priority until it wakes up.
238 		 */
239 		if (p->p_slptime > 1)
240 			continue;
241 		SCHED_LOCK(s);
242 		/*
243 		 * p_pctcpu is only for diagnostic tools such as ps.
244 		 */
245 #if	(FSHIFT >= CCPU_SHIFT)
246 		p->p_pctcpu += (phz == 100)?
247 			((fixpt_t) p->p_cpticks) << (FSHIFT - CCPU_SHIFT):
248                 	100 * (((fixpt_t) p->p_cpticks)
249 				<< (FSHIFT - CCPU_SHIFT)) / phz;
250 #else
251 		p->p_pctcpu += ((FSCALE - ccpu) *
252 			(p->p_cpticks * FSCALE / phz)) >> FSHIFT;
253 #endif
254 		p->p_cpticks = 0;
255 		newcpu = (u_int) decay_cpu(loadfac, p->p_estcpu);
256 		setpriority(p, newcpu, p->p_p->ps_nice);
257 
258 		if (p->p_stat == SRUN &&
259 		    (p->p_runpri / SCHED_PPQ) != (p->p_usrpri / SCHED_PPQ)) {
260 			remrunqueue(p);
261 			setrunqueue(p->p_cpu, p, p->p_usrpri);
262 		}
263 		SCHED_UNLOCK(s);
264 	}
265 	uvm_meter();
266 	wakeup(&lbolt);
267 	timeout_add_sec(to, 1);
268 }
269 
270 /*
271  * Recalculate the priority of a process after it has slept for a while.
272  * For all load averages >= 1 and max p_estcpu of 255, sleeping for at
273  * least six times the loadfactor will decay p_estcpu to zero.
274  */
275 uint32_t
276 decay_aftersleep(uint32_t estcpu, uint32_t slptime)
277 {
278 	fixpt_t loadfac = loadfactor(averunnable.ldavg[0]);
279 	uint32_t newcpu;
280 
281 	if (slptime > 5 * loadfac)
282 		newcpu = 0;
283 	else {
284 		newcpu = estcpu;
285 		slptime--;	/* the first time was done in schedcpu */
286 		while (newcpu && --slptime)
287 			newcpu = decay_cpu(loadfac, newcpu);
288 
289 	}
290 
291 	return (newcpu);
292 }
293 
294 /*
295  * General yield call.  Puts the current process back on its run queue and
296  * performs a voluntary context switch.
297  */
298 void
299 yield(void)
300 {
301 	struct proc *p = curproc;
302 	int s;
303 
304 	NET_ASSERT_UNLOCKED();
305 
306 	SCHED_LOCK(s);
307 	setrunqueue(p->p_cpu, p, p->p_usrpri);
308 	p->p_ru.ru_nvcsw++;
309 	mi_switch();
310 	SCHED_UNLOCK(s);
311 }
312 
313 /*
314  * General preemption call.  Puts the current process back on its run queue
315  * and performs an involuntary context switch.  If a process is supplied,
316  * we switch to that process.  Otherwise, we use the normal process selection
317  * criteria.
318  */
319 void
320 preempt(void)
321 {
322 	struct proc *p = curproc;
323 	int s;
324 
325 	SCHED_LOCK(s);
326 	setrunqueue(p->p_cpu, p, p->p_usrpri);
327 	p->p_ru.ru_nivcsw++;
328 	mi_switch();
329 	SCHED_UNLOCK(s);
330 }
331 
332 void
333 mi_switch(void)
334 {
335 	struct schedstate_percpu *spc = &curcpu()->ci_schedstate;
336 	struct proc *p = curproc;
337 	struct proc *nextproc;
338 	struct process *pr = p->p_p;
339 	struct timespec ts;
340 #ifdef MULTIPROCESSOR
341 	int hold_count;
342 	int sched_count;
343 #endif
344 
345 	assertwaitok();
346 	KASSERT(p->p_stat != SONPROC);
347 
348 	SCHED_ASSERT_LOCKED();
349 
350 #ifdef MULTIPROCESSOR
351 	/*
352 	 * Release the kernel_lock, as we are about to yield the CPU.
353 	 */
354 	sched_count = __mp_release_all_but_one(&sched_lock);
355 	if (_kernel_lock_held())
356 		hold_count = __mp_release_all(&kernel_lock);
357 	else
358 		hold_count = 0;
359 #endif
360 
361 	/*
362 	 * Compute the amount of time during which the current
363 	 * process was running, and add that to its total so far.
364 	 */
365 	nanouptime(&ts);
366 	if (timespeccmp(&ts, &spc->spc_runtime, <)) {
367 #if 0
368 		printf("uptime is not monotonic! "
369 		    "ts=%lld.%09lu, runtime=%lld.%09lu\n",
370 		    (long long)tv.tv_sec, tv.tv_nsec,
371 		    (long long)spc->spc_runtime.tv_sec,
372 		    spc->spc_runtime.tv_nsec);
373 #endif
374 	} else {
375 		timespecsub(&ts, &spc->spc_runtime, &ts);
376 		timespecadd(&p->p_rtime, &ts, &p->p_rtime);
377 	}
378 
379 	/* add the time counts for this thread to the process's total */
380 	tuagg_unlocked(pr, p);
381 
382 	/*
383 	 * Process is about to yield the CPU; clear the appropriate
384 	 * scheduling flags.
385 	 */
386 	atomic_clearbits_int(&spc->spc_schedflags, SPCF_SWITCHCLEAR);
387 
388 	nextproc = sched_chooseproc();
389 
390 	if (p != nextproc) {
391 		uvmexp.swtch++;
392 		TRACEPOINT(sched, off__cpu, nextproc->p_tid,
393 		    nextproc->p_p->ps_pid);
394 		cpu_switchto(p, nextproc);
395 		TRACEPOINT(sched, on__cpu, NULL);
396 	} else {
397 		TRACEPOINT(sched, remain__cpu, NULL);
398 		p->p_stat = SONPROC;
399 	}
400 
401 	clear_resched(curcpu());
402 
403 	SCHED_ASSERT_LOCKED();
404 
405 	/*
406 	 * To preserve lock ordering, we need to release the sched lock
407 	 * and grab it after we grab the big lock.
408 	 * In the future, when the sched lock isn't recursive, we'll
409 	 * just release it here.
410 	 */
411 #ifdef MULTIPROCESSOR
412 	__mp_unlock(&sched_lock);
413 #endif
414 
415 	SCHED_ASSERT_UNLOCKED();
416 
417 	smr_idle();
418 
419 	/*
420 	 * We're running again; record our new start time.  We might
421 	 * be running on a new CPU now, so don't use the cache'd
422 	 * schedstate_percpu pointer.
423 	 */
424 	KASSERT(p->p_cpu == curcpu());
425 
426 	nanouptime(&p->p_cpu->ci_schedstate.spc_runtime);
427 
428 #ifdef MULTIPROCESSOR
429 	/*
430 	 * Reacquire the kernel_lock now.  We do this after we've
431 	 * released the scheduler lock to avoid deadlock, and before
432 	 * we reacquire the interlock and the scheduler lock.
433 	 */
434 	if (hold_count)
435 		__mp_acquire_count(&kernel_lock, hold_count);
436 	__mp_acquire_count(&sched_lock, sched_count + 1);
437 #endif
438 }
439 
440 /*
441  * Change process state to be runnable,
442  * placing it on the run queue.
443  */
444 void
445 setrunnable(struct proc *p)
446 {
447 	struct process *pr = p->p_p;
448 	u_char prio;
449 
450 	SCHED_ASSERT_LOCKED();
451 
452 	switch (p->p_stat) {
453 	case 0:
454 	case SRUN:
455 	case SONPROC:
456 	case SDEAD:
457 	case SIDL:
458 	default:
459 		panic("setrunnable");
460 	case SSTOP:
461 		/*
462 		 * If we're being traced (possibly because someone attached us
463 		 * while we were stopped), check for a signal from the debugger.
464 		 */
465 		if ((pr->ps_flags & PS_TRACED) != 0 && pr->ps_xsig != 0)
466 			atomic_setbits_int(&p->p_siglist, sigmask(pr->ps_xsig));
467 		prio = p->p_usrpri;
468 		unsleep(p);
469 		break;
470 	case SSLEEP:
471 		prio = p->p_slppri;
472 		unsleep(p);		/* e.g. when sending signals */
473 		break;
474 	}
475 	setrunqueue(NULL, p, prio);
476 	if (p->p_slptime > 1) {
477 		uint32_t newcpu;
478 
479 		newcpu = decay_aftersleep(p->p_estcpu, p->p_slptime);
480 		setpriority(p, newcpu, pr->ps_nice);
481 	}
482 	p->p_slptime = 0;
483 }
484 
485 /*
486  * Compute the priority of a process.
487  */
488 void
489 setpriority(struct proc *p, uint32_t newcpu, uint8_t nice)
490 {
491 	unsigned int newprio;
492 
493 	newprio = min((PUSER + newcpu + NICE_WEIGHT * (nice - NZERO)), MAXPRI);
494 
495 	SCHED_ASSERT_LOCKED();
496 	p->p_estcpu = newcpu;
497 	p->p_usrpri = newprio;
498 }
499 
500 /*
501  * We adjust the priority of the current process.  The priority of a process
502  * gets worse as it accumulates CPU time.  The cpu usage estimator (p_estcpu)
503  * is increased here.  The formula for computing priorities (in kern_synch.c)
504  * will compute a different value each time p_estcpu increases. This can
505  * cause a switch, but unless the priority crosses a PPQ boundary the actual
506  * queue will not change.  The cpu usage estimator ramps up quite quickly
507  * when the process is running (linearly), and decays away exponentially, at
508  * a rate which is proportionally slower when the system is busy.  The basic
509  * principle is that the system will 90% forget that the process used a lot
510  * of CPU time in 5 * loadav seconds.  This causes the system to favor
511  * processes which haven't run much recently, and to round-robin among other
512  * processes.
513  */
514 void
515 schedclock(struct proc *p)
516 {
517 	struct cpu_info *ci = curcpu();
518 	struct schedstate_percpu *spc = &ci->ci_schedstate;
519 	uint32_t newcpu;
520 	int s;
521 
522 	if (p == spc->spc_idleproc || spc->spc_spinning)
523 		return;
524 
525 	SCHED_LOCK(s);
526 	newcpu = ESTCPULIM(p->p_estcpu + 1);
527 	setpriority(p, newcpu, p->p_p->ps_nice);
528 	SCHED_UNLOCK(s);
529 }
530 
531 void (*cpu_setperf)(int);
532 
533 #define PERFPOL_MANUAL 0
534 #define PERFPOL_AUTO 1
535 #define PERFPOL_HIGH 2
536 int perflevel = 100;
537 int perfpolicy = PERFPOL_MANUAL;
538 
539 #ifndef SMALL_KERNEL
540 /*
541  * The code below handles CPU throttling.
542  */
543 #include <sys/sysctl.h>
544 
545 void setperf_auto(void *);
546 struct timeout setperf_to = TIMEOUT_INITIALIZER(setperf_auto, NULL);
547 
548 void
549 setperf_auto(void *v)
550 {
551 	static uint64_t *idleticks, *totalticks;
552 	static int downbeats;
553 
554 	int i, j;
555 	int speedup;
556 	CPU_INFO_ITERATOR cii;
557 	struct cpu_info *ci;
558 	uint64_t idle, total, allidle, alltotal;
559 
560 	if (perfpolicy != PERFPOL_AUTO)
561 		return;
562 
563 	if (!idleticks)
564 		if (!(idleticks = mallocarray(ncpusfound, sizeof(*idleticks),
565 		    M_DEVBUF, M_NOWAIT | M_ZERO)))
566 			return;
567 	if (!totalticks)
568 		if (!(totalticks = mallocarray(ncpusfound, sizeof(*totalticks),
569 		    M_DEVBUF, M_NOWAIT | M_ZERO))) {
570 			free(idleticks, M_DEVBUF,
571 			    sizeof(*idleticks) * ncpusfound);
572 			return;
573 		}
574 
575 	alltotal = allidle = 0;
576 	j = 0;
577 	speedup = 0;
578 	CPU_INFO_FOREACH(cii, ci) {
579 		if (!cpu_is_online(ci))
580 			continue;
581 		total = 0;
582 		for (i = 0; i < CPUSTATES; i++) {
583 			total += ci->ci_schedstate.spc_cp_time[i];
584 		}
585 		total -= totalticks[j];
586 		idle = ci->ci_schedstate.spc_cp_time[CP_IDLE] - idleticks[j];
587 		if (idle < total / 3)
588 			speedup = 1;
589 		alltotal += total;
590 		allidle += idle;
591 		idleticks[j] += idle;
592 		totalticks[j] += total;
593 		j++;
594 	}
595 	if (allidle < alltotal / 2)
596 		speedup = 1;
597 	if (speedup)
598 		downbeats = 5;
599 
600 	if (speedup && perflevel != 100) {
601 		perflevel = 100;
602 		cpu_setperf(perflevel);
603 	} else if (!speedup && perflevel != 0 && --downbeats <= 0) {
604 		perflevel = 0;
605 		cpu_setperf(perflevel);
606 	}
607 
608 	timeout_add_msec(&setperf_to, 100);
609 }
610 
611 int
612 sysctl_hwsetperf(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
613 {
614 	int err, newperf;
615 
616 	if (!cpu_setperf)
617 		return EOPNOTSUPP;
618 
619 	if (perfpolicy != PERFPOL_MANUAL)
620 		return sysctl_rdint(oldp, oldlenp, newp, perflevel);
621 
622 	newperf = perflevel;
623 	err = sysctl_int(oldp, oldlenp, newp, newlen, &newperf);
624 	if (err)
625 		return err;
626 	if (newperf > 100)
627 		newperf = 100;
628 	if (newperf < 0)
629 		newperf = 0;
630 	perflevel = newperf;
631 	cpu_setperf(perflevel);
632 
633 	return 0;
634 }
635 
636 int
637 sysctl_hwperfpolicy(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
638 {
639 	char policy[32];
640 	int err;
641 
642 	if (!cpu_setperf)
643 		return EOPNOTSUPP;
644 
645 	switch (perfpolicy) {
646 	case PERFPOL_MANUAL:
647 		strlcpy(policy, "manual", sizeof(policy));
648 		break;
649 	case PERFPOL_AUTO:
650 		strlcpy(policy, "auto", sizeof(policy));
651 		break;
652 	case PERFPOL_HIGH:
653 		strlcpy(policy, "high", sizeof(policy));
654 		break;
655 	default:
656 		strlcpy(policy, "unknown", sizeof(policy));
657 		break;
658 	}
659 
660 	if (newp == NULL)
661 		return sysctl_rdstring(oldp, oldlenp, newp, policy);
662 
663 	err = sysctl_string(oldp, oldlenp, newp, newlen, policy, sizeof(policy));
664 	if (err)
665 		return err;
666 	if (strcmp(policy, "manual") == 0)
667 		perfpolicy = PERFPOL_MANUAL;
668 	else if (strcmp(policy, "auto") == 0)
669 		perfpolicy = PERFPOL_AUTO;
670 	else if (strcmp(policy, "high") == 0)
671 		perfpolicy = PERFPOL_HIGH;
672 	else
673 		return EINVAL;
674 
675 	if (perfpolicy == PERFPOL_AUTO) {
676 		timeout_add_msec(&setperf_to, 200);
677 	} else if (perfpolicy == PERFPOL_HIGH) {
678 		perflevel = 100;
679 		cpu_setperf(perflevel);
680 	}
681 	return 0;
682 }
683 #endif
684