xref: /openbsd/sys/kern/sched_bsd.c (revision 097a140d)
1 /*	$OpenBSD: sched_bsd.c,v 1.65 2020/12/10 04:26:50 gnezdo 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 	SCHED_LOCK(s);
305 	setrunqueue(p->p_cpu, p, p->p_usrpri);
306 	p->p_ru.ru_nvcsw++;
307 	mi_switch();
308 	SCHED_UNLOCK(s);
309 }
310 
311 /*
312  * General preemption call.  Puts the current process back on its run queue
313  * and performs an involuntary context switch.  If a process is supplied,
314  * we switch to that process.  Otherwise, we use the normal process selection
315  * criteria.
316  */
317 void
318 preempt(void)
319 {
320 	struct proc *p = curproc;
321 	int s;
322 
323 	SCHED_LOCK(s);
324 	setrunqueue(p->p_cpu, p, p->p_usrpri);
325 	p->p_ru.ru_nivcsw++;
326 	mi_switch();
327 	SCHED_UNLOCK(s);
328 }
329 
330 void
331 mi_switch(void)
332 {
333 	struct schedstate_percpu *spc = &curcpu()->ci_schedstate;
334 	struct proc *p = curproc;
335 	struct proc *nextproc;
336 	struct process *pr = p->p_p;
337 	struct timespec ts;
338 #ifdef MULTIPROCESSOR
339 	int hold_count;
340 	int sched_count;
341 #endif
342 
343 	assertwaitok();
344 	KASSERT(p->p_stat != SONPROC);
345 
346 	SCHED_ASSERT_LOCKED();
347 
348 #ifdef MULTIPROCESSOR
349 	/*
350 	 * Release the kernel_lock, as we are about to yield the CPU.
351 	 */
352 	sched_count = __mp_release_all_but_one(&sched_lock);
353 	if (_kernel_lock_held())
354 		hold_count = __mp_release_all(&kernel_lock);
355 	else
356 		hold_count = 0;
357 #endif
358 
359 	/*
360 	 * Compute the amount of time during which the current
361 	 * process was running, and add that to its total so far.
362 	 */
363 	nanouptime(&ts);
364 	if (timespeccmp(&ts, &spc->spc_runtime, <)) {
365 #if 0
366 		printf("uptime is not monotonic! "
367 		    "ts=%lld.%09lu, runtime=%lld.%09lu\n",
368 		    (long long)tv.tv_sec, tv.tv_nsec,
369 		    (long long)spc->spc_runtime.tv_sec,
370 		    spc->spc_runtime.tv_nsec);
371 #endif
372 	} else {
373 		timespecsub(&ts, &spc->spc_runtime, &ts);
374 		timespecadd(&p->p_rtime, &ts, &p->p_rtime);
375 	}
376 
377 	/* add the time counts for this thread to the process's total */
378 	tuagg_unlocked(pr, p);
379 
380 	/*
381 	 * Process is about to yield the CPU; clear the appropriate
382 	 * scheduling flags.
383 	 */
384 	atomic_clearbits_int(&spc->spc_schedflags, SPCF_SWITCHCLEAR);
385 
386 	nextproc = sched_chooseproc();
387 
388 	if (p != nextproc) {
389 		uvmexp.swtch++;
390 		TRACEPOINT(sched, off__cpu, nextproc->p_tid,
391 		    nextproc->p_p->ps_pid);
392 		cpu_switchto(p, nextproc);
393 		TRACEPOINT(sched, on__cpu, NULL);
394 	} else {
395 		TRACEPOINT(sched, remain__cpu, NULL);
396 		p->p_stat = SONPROC;
397 	}
398 
399 	clear_resched(curcpu());
400 
401 	SCHED_ASSERT_LOCKED();
402 
403 	/*
404 	 * To preserve lock ordering, we need to release the sched lock
405 	 * and grab it after we grab the big lock.
406 	 * In the future, when the sched lock isn't recursive, we'll
407 	 * just release it here.
408 	 */
409 #ifdef MULTIPROCESSOR
410 	__mp_unlock(&sched_lock);
411 #endif
412 
413 	SCHED_ASSERT_UNLOCKED();
414 
415 	smr_idle();
416 
417 	/*
418 	 * We're running again; record our new start time.  We might
419 	 * be running on a new CPU now, so don't use the cache'd
420 	 * schedstate_percpu pointer.
421 	 */
422 	KASSERT(p->p_cpu == curcpu());
423 
424 	nanouptime(&p->p_cpu->ci_schedstate.spc_runtime);
425 
426 #ifdef MULTIPROCESSOR
427 	/*
428 	 * Reacquire the kernel_lock now.  We do this after we've
429 	 * released the scheduler lock to avoid deadlock, and before
430 	 * we reacquire the interlock and the scheduler lock.
431 	 */
432 	if (hold_count)
433 		__mp_acquire_count(&kernel_lock, hold_count);
434 	__mp_acquire_count(&sched_lock, sched_count + 1);
435 #endif
436 }
437 
438 /*
439  * Change process state to be runnable,
440  * placing it on the run queue.
441  */
442 void
443 setrunnable(struct proc *p)
444 {
445 	struct process *pr = p->p_p;
446 	u_char prio;
447 
448 	SCHED_ASSERT_LOCKED();
449 
450 	switch (p->p_stat) {
451 	case 0:
452 	case SRUN:
453 	case SONPROC:
454 	case SDEAD:
455 	case SIDL:
456 	default:
457 		panic("setrunnable");
458 	case SSTOP:
459 		/*
460 		 * If we're being traced (possibly because someone attached us
461 		 * while we were stopped), check for a signal from the debugger.
462 		 */
463 		if ((pr->ps_flags & PS_TRACED) != 0 && pr->ps_xsig != 0)
464 			atomic_setbits_int(&p->p_siglist, sigmask(pr->ps_xsig));
465 		prio = p->p_usrpri;
466 		unsleep(p);
467 		break;
468 	case SSLEEP:
469 		prio = p->p_slppri;
470 		unsleep(p);		/* e.g. when sending signals */
471 		break;
472 	}
473 	setrunqueue(NULL, p, prio);
474 	if (p->p_slptime > 1) {
475 		uint32_t newcpu;
476 
477 		newcpu = decay_aftersleep(p->p_estcpu, p->p_slptime);
478 		setpriority(p, newcpu, pr->ps_nice);
479 	}
480 	p->p_slptime = 0;
481 }
482 
483 /*
484  * Compute the priority of a process.
485  */
486 void
487 setpriority(struct proc *p, uint32_t newcpu, uint8_t nice)
488 {
489 	unsigned int newprio;
490 
491 	newprio = min((PUSER + newcpu + NICE_WEIGHT * (nice - NZERO)), MAXPRI);
492 
493 	SCHED_ASSERT_LOCKED();
494 	p->p_estcpu = newcpu;
495 	p->p_usrpri = newprio;
496 }
497 
498 /*
499  * We adjust the priority of the current process.  The priority of a process
500  * gets worse as it accumulates CPU time.  The cpu usage estimator (p_estcpu)
501  * is increased here.  The formula for computing priorities (in kern_synch.c)
502  * will compute a different value each time p_estcpu increases. This can
503  * cause a switch, but unless the priority crosses a PPQ boundary the actual
504  * queue will not change.  The cpu usage estimator ramps up quite quickly
505  * when the process is running (linearly), and decays away exponentially, at
506  * a rate which is proportionally slower when the system is busy.  The basic
507  * principle is that the system will 90% forget that the process used a lot
508  * of CPU time in 5 * loadav seconds.  This causes the system to favor
509  * processes which haven't run much recently, and to round-robin among other
510  * processes.
511  */
512 void
513 schedclock(struct proc *p)
514 {
515 	struct cpu_info *ci = curcpu();
516 	struct schedstate_percpu *spc = &ci->ci_schedstate;
517 	uint32_t newcpu;
518 	int s;
519 
520 	if (p == spc->spc_idleproc || spc->spc_spinning)
521 		return;
522 
523 	SCHED_LOCK(s);
524 	newcpu = ESTCPULIM(p->p_estcpu + 1);
525 	setpriority(p, newcpu, p->p_p->ps_nice);
526 	SCHED_UNLOCK(s);
527 }
528 
529 void (*cpu_setperf)(int);
530 
531 #define PERFPOL_MANUAL 0
532 #define PERFPOL_AUTO 1
533 #define PERFPOL_HIGH 2
534 int perflevel = 100;
535 int perfpolicy = PERFPOL_MANUAL;
536 
537 #ifndef SMALL_KERNEL
538 /*
539  * The code below handles CPU throttling.
540  */
541 #include <sys/sysctl.h>
542 
543 void setperf_auto(void *);
544 struct timeout setperf_to = TIMEOUT_INITIALIZER(setperf_auto, NULL);
545 
546 void
547 setperf_auto(void *v)
548 {
549 	static uint64_t *idleticks, *totalticks;
550 	static int downbeats;
551 
552 	int i, j;
553 	int speedup;
554 	CPU_INFO_ITERATOR cii;
555 	struct cpu_info *ci;
556 	uint64_t idle, total, allidle, alltotal;
557 
558 	if (perfpolicy != PERFPOL_AUTO)
559 		return;
560 
561 	if (!idleticks)
562 		if (!(idleticks = mallocarray(ncpusfound, sizeof(*idleticks),
563 		    M_DEVBUF, M_NOWAIT | M_ZERO)))
564 			return;
565 	if (!totalticks)
566 		if (!(totalticks = mallocarray(ncpusfound, sizeof(*totalticks),
567 		    M_DEVBUF, M_NOWAIT | M_ZERO))) {
568 			free(idleticks, M_DEVBUF,
569 			    sizeof(*idleticks) * ncpusfound);
570 			return;
571 		}
572 
573 	alltotal = allidle = 0;
574 	j = 0;
575 	speedup = 0;
576 	CPU_INFO_FOREACH(cii, ci) {
577 		if (!cpu_is_online(ci))
578 			continue;
579 		total = 0;
580 		for (i = 0; i < CPUSTATES; i++) {
581 			total += ci->ci_schedstate.spc_cp_time[i];
582 		}
583 		total -= totalticks[j];
584 		idle = ci->ci_schedstate.spc_cp_time[CP_IDLE] - idleticks[j];
585 		if (idle < total / 3)
586 			speedup = 1;
587 		alltotal += total;
588 		allidle += idle;
589 		idleticks[j] += idle;
590 		totalticks[j] += total;
591 		j++;
592 	}
593 	if (allidle < alltotal / 2)
594 		speedup = 1;
595 	if (speedup)
596 		downbeats = 5;
597 
598 	if (speedup && perflevel != 100) {
599 		perflevel = 100;
600 		cpu_setperf(perflevel);
601 	} else if (!speedup && perflevel != 0 && --downbeats <= 0) {
602 		perflevel = 0;
603 		cpu_setperf(perflevel);
604 	}
605 
606 	timeout_add_msec(&setperf_to, 100);
607 }
608 
609 int
610 sysctl_hwsetperf(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
611 {
612 	int err;
613 
614 	if (!cpu_setperf)
615 		return EOPNOTSUPP;
616 
617 	if (perfpolicy != PERFPOL_MANUAL)
618 		return sysctl_rdint(oldp, oldlenp, newp, perflevel);
619 
620 	err = sysctl_int_bounded(oldp, oldlenp, newp, newlen,
621 	    &perflevel, 0, 100);
622 	if (err)
623 		return err;
624 	cpu_setperf(perflevel);
625 	return 0;
626 }
627 
628 int
629 sysctl_hwperfpolicy(void *oldp, size_t *oldlenp, void *newp, size_t newlen)
630 {
631 	char policy[32];
632 	int err;
633 
634 	if (!cpu_setperf)
635 		return EOPNOTSUPP;
636 
637 	switch (perfpolicy) {
638 	case PERFPOL_MANUAL:
639 		strlcpy(policy, "manual", sizeof(policy));
640 		break;
641 	case PERFPOL_AUTO:
642 		strlcpy(policy, "auto", sizeof(policy));
643 		break;
644 	case PERFPOL_HIGH:
645 		strlcpy(policy, "high", sizeof(policy));
646 		break;
647 	default:
648 		strlcpy(policy, "unknown", sizeof(policy));
649 		break;
650 	}
651 
652 	if (newp == NULL)
653 		return sysctl_rdstring(oldp, oldlenp, newp, policy);
654 
655 	err = sysctl_string(oldp, oldlenp, newp, newlen, policy, sizeof(policy));
656 	if (err)
657 		return err;
658 	if (strcmp(policy, "manual") == 0)
659 		perfpolicy = PERFPOL_MANUAL;
660 	else if (strcmp(policy, "auto") == 0)
661 		perfpolicy = PERFPOL_AUTO;
662 	else if (strcmp(policy, "high") == 0)
663 		perfpolicy = PERFPOL_HIGH;
664 	else
665 		return EINVAL;
666 
667 	if (perfpolicy == PERFPOL_AUTO) {
668 		timeout_add_msec(&setperf_to, 200);
669 	} else if (perfpolicy == PERFPOL_HIGH) {
670 		perflevel = 100;
671 		cpu_setperf(perflevel);
672 	}
673 	return 0;
674 }
675 #endif
676