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