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