xref: /illumos-gate/usr/src/uts/common/sys/disp.h (revision 685679f7e4cc349aa1260fad8dbfaf07089c7b19)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*685679f7Sakolb  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #ifndef _SYS_DISP_H
327c478bd9Sstevel@tonic-gate #define	_SYS_DISP_H
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.11	*/
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sys/priocntl.h>
377c478bd9Sstevel@tonic-gate #include <sys/thread.h>
387c478bd9Sstevel@tonic-gate #include <sys/class.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
417c478bd9Sstevel@tonic-gate extern "C" {
427c478bd9Sstevel@tonic-gate #endif
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * The following is the format of a dispatcher queue entry.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate typedef struct dispq {
487c478bd9Sstevel@tonic-gate 	kthread_t	*dq_first;	/* first thread on queue or NULL */
497c478bd9Sstevel@tonic-gate 	kthread_t	*dq_last;	/* last thread on queue or NULL */
507c478bd9Sstevel@tonic-gate 	int		dq_sruncnt;	/* number of loaded, runnable */
517c478bd9Sstevel@tonic-gate 					/*    threads on queue */
527c478bd9Sstevel@tonic-gate } dispq_t;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * Dispatch queue structure.
567c478bd9Sstevel@tonic-gate  */
577c478bd9Sstevel@tonic-gate typedef struct _disp {
587c478bd9Sstevel@tonic-gate 	disp_lock_t	disp_lock;	/* protects dispatching fields */
597c478bd9Sstevel@tonic-gate 	pri_t		disp_npri;	/* # of priority levels in queue */
607c478bd9Sstevel@tonic-gate 	dispq_t		*disp_q;		/* the dispatch queue */
617c478bd9Sstevel@tonic-gate 	dispq_t		*disp_q_limit;	/* ptr past end of dispatch queue */
627c478bd9Sstevel@tonic-gate 	ulong_t		*disp_qactmap;	/* bitmap of active dispatch queues */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	/*
657c478bd9Sstevel@tonic-gate 	 * Priorities:
667c478bd9Sstevel@tonic-gate 	 *	disp_maxrunpri is the maximum run priority of runnable threads
677c478bd9Sstevel@tonic-gate 	 * 	on this queue.  It is -1 if nothing is runnable.
687c478bd9Sstevel@tonic-gate 	 *
697c478bd9Sstevel@tonic-gate 	 *	disp_max_unbound_pri is the maximum run priority of threads on
707c478bd9Sstevel@tonic-gate 	 *	this dispatch queue but runnable by any CPU.  This may be left
717c478bd9Sstevel@tonic-gate 	 * 	artificially high, then corrected when some CPU tries to take
727c478bd9Sstevel@tonic-gate 	 *	an unbound thread.  It is -1 if nothing is runnable.
737c478bd9Sstevel@tonic-gate 	 */
747c478bd9Sstevel@tonic-gate 	pri_t		disp_maxrunpri;	/* maximum run priority */
757c478bd9Sstevel@tonic-gate 	pri_t		disp_max_unbound_pri;	/* max pri of unbound threads */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	volatile int	disp_nrunnable;	/* runnable threads in cpu dispq */
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	struct cpu	*disp_cpu;	/* cpu owning this queue or NULL */
80*685679f7Sakolb 	hrtime_t	disp_steal;	/* time when threads become stealable */
817c478bd9Sstevel@tonic-gate } disp_t;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #if defined(_KERNEL)
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #define	MAXCLSYSPRI	99
867c478bd9Sstevel@tonic-gate #define	MINCLSYSPRI	60
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * Global scheduling variables.
917c478bd9Sstevel@tonic-gate  *	- See sys/cpuvar.h for CPU-local variables.
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate extern int	nswapped;	/* number of swapped threads */
947c478bd9Sstevel@tonic-gate 				/* nswapped protected by swap_lock */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate extern	pri_t	minclsyspri;	/* minimum level of any system class */
977c478bd9Sstevel@tonic-gate extern	pri_t	maxclsyspri;	/* maximum level of any system class */
987c478bd9Sstevel@tonic-gate extern	pri_t	intr_pri;	/* interrupt thread priority base level */
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate  * Amount of time that may elapse before a thread is considered to have
1027c478bd9Sstevel@tonic-gate  * lost it's cache investment.
1037c478bd9Sstevel@tonic-gate  */
1047c478bd9Sstevel@tonic-gate #define	RECHOOSE_INTERVAL	3
1057c478bd9Sstevel@tonic-gate extern int	rechoose_interval;
1067c478bd9Sstevel@tonic-gate 
107*685679f7Sakolb /*
108*685679f7Sakolb  * Minimum amount of time that a thread can remain runnable before it can
109*685679f7Sakolb  * be stolen by another CPU (in nanoseconds).
110*685679f7Sakolb  */
111*685679f7Sakolb extern hrtime_t nosteal_nsec;
112*685679f7Sakolb 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * Kernel preemption occurs if a higher-priority thread is runnable with
1157c478bd9Sstevel@tonic-gate  * a priority at or above kpreemptpri.
1167c478bd9Sstevel@tonic-gate  *
1177c478bd9Sstevel@tonic-gate  * So that other processors can watch for such threads, a separate
1187c478bd9Sstevel@tonic-gate  * dispatch queue with unbound work above kpreemptpri is maintained.
1197c478bd9Sstevel@tonic-gate  * This is part of the CPU partition structure (cpupart_t).
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate extern	pri_t	kpreemptpri;	/* level above which preemption takes place */
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate extern void		disp_kp_alloc(disp_t *, pri_t);	/* allocate kp queue */
1247c478bd9Sstevel@tonic-gate extern void		disp_kp_free(disp_t *);		/* free kp queue */
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * Macro for use by scheduling classes to decide whether the thread is about
1287c478bd9Sstevel@tonic-gate  * to be scheduled or not.  This returns the maximum run priority.
1297c478bd9Sstevel@tonic-gate  */
1307c478bd9Sstevel@tonic-gate #define	DISP_MAXRUNPRI(t)	((t)->t_disp_queue->disp_maxrunpri)
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate  * Platform callbacks for various dispatcher operations
1347c478bd9Sstevel@tonic-gate  *
1357c478bd9Sstevel@tonic-gate  * idle_cpu() is invoked when a cpu goes idle, and has nothing to do.
1367c478bd9Sstevel@tonic-gate  * disp_enq_thread() is invoked when a thread is placed on a run queue.
1377c478bd9Sstevel@tonic-gate  */
1387c478bd9Sstevel@tonic-gate extern void	(*idle_cpu)();
1397c478bd9Sstevel@tonic-gate extern void	(*disp_enq_thread)(struct cpu *, int);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate extern int		dispdeq(kthread_t *);
1437c478bd9Sstevel@tonic-gate extern void		dispinit(void);
1447c478bd9Sstevel@tonic-gate extern void		disp_add(sclass_t *);
1457c478bd9Sstevel@tonic-gate extern int		intr_active(struct cpu *, int);
1467c478bd9Sstevel@tonic-gate extern int		servicing_interrupt(void);
1477c478bd9Sstevel@tonic-gate extern void		preempt(void);
1487c478bd9Sstevel@tonic-gate extern void		setbackdq(kthread_t *);
1497c478bd9Sstevel@tonic-gate extern void		setfrontdq(kthread_t *);
1507c478bd9Sstevel@tonic-gate extern void		swtch(void);
1517c478bd9Sstevel@tonic-gate extern void		swtch_to(kthread_t *);
1527c478bd9Sstevel@tonic-gate extern void		swtch_from_zombie(void)
1537c478bd9Sstevel@tonic-gate 				__NORETURN;
1547c478bd9Sstevel@tonic-gate extern void		dq_sruninc(kthread_t *);
1557c478bd9Sstevel@tonic-gate extern void		dq_srundec(kthread_t *);
1567c478bd9Sstevel@tonic-gate extern void		cpu_rechoose(kthread_t *);
1577c478bd9Sstevel@tonic-gate extern void		cpu_surrender(kthread_t *);
1587c478bd9Sstevel@tonic-gate extern void		kpreempt(int);
1597c478bd9Sstevel@tonic-gate extern struct cpu	*disp_lowpri_cpu(struct cpu *, struct lgrp_ld *, pri_t,
1607c478bd9Sstevel@tonic-gate 			    struct cpu *);
1617c478bd9Sstevel@tonic-gate extern int		disp_bound_threads(struct cpu *, int);
1627c478bd9Sstevel@tonic-gate extern int		disp_bound_anythreads(struct cpu *, int);
1637c478bd9Sstevel@tonic-gate extern int		disp_bound_partition(struct cpu *, int);
1647c478bd9Sstevel@tonic-gate extern void		disp_cpu_init(struct cpu *);
1657c478bd9Sstevel@tonic-gate extern void		disp_cpu_fini(struct cpu *);
1667c478bd9Sstevel@tonic-gate extern void		disp_cpu_inactive(struct cpu *);
1677c478bd9Sstevel@tonic-gate extern void		disp_adjust_unbound_pri(kthread_t *);
1687c478bd9Sstevel@tonic-gate extern void		resume(kthread_t *);
1697c478bd9Sstevel@tonic-gate extern void		resume_from_intr(kthread_t *);
1707c478bd9Sstevel@tonic-gate extern void		resume_from_zombie(kthread_t *)
1717c478bd9Sstevel@tonic-gate 				__NORETURN;
1727c478bd9Sstevel@tonic-gate extern void		disp_swapped_enq(kthread_t *);
1737c478bd9Sstevel@tonic-gate extern int		disp_anywork(void);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate #define	KPREEMPT_SYNC		(-1)
1767c478bd9Sstevel@tonic-gate #define	kpreempt_disable()				\
1777c478bd9Sstevel@tonic-gate 	{						\
1787c478bd9Sstevel@tonic-gate 		curthread->t_preempt++;			\
1797c478bd9Sstevel@tonic-gate 		ASSERT(curthread->t_preempt >= 1);	\
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate #define	kpreempt_enable()				\
1827c478bd9Sstevel@tonic-gate 	{						\
1837c478bd9Sstevel@tonic-gate 		ASSERT(curthread->t_preempt >= 1);	\
1847c478bd9Sstevel@tonic-gate 		if (--curthread->t_preempt == 0 &&	\
1857c478bd9Sstevel@tonic-gate 		    CPU->cpu_kprunrun)			\
1867c478bd9Sstevel@tonic-gate 			kpreempt(KPREEMPT_SYNC);	\
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate #endif	/* _KERNEL */
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate #endif
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate #endif	/* _SYS_DISP_H */
196