xref: /illumos-gate/usr/src/uts/common/sys/taskq_impl.h (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #ifndef	_SYS_TASKQ_IMPL_H
28*7c478bd9Sstevel@tonic-gate #define	_SYS_TASKQ_IMPL_H
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
37*7c478bd9Sstevel@tonic-gate extern "C" {
38*7c478bd9Sstevel@tonic-gate #endif
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate typedef struct taskq_bucket taskq_bucket_t;
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate typedef struct taskq_ent {
43*7c478bd9Sstevel@tonic-gate 	struct taskq_ent	*tqent_next;
44*7c478bd9Sstevel@tonic-gate 	struct taskq_ent	*tqent_prev;
45*7c478bd9Sstevel@tonic-gate 	task_func_t		*tqent_func;
46*7c478bd9Sstevel@tonic-gate 	void			*tqent_arg;
47*7c478bd9Sstevel@tonic-gate 	taskq_bucket_t		*tqent_bucket;
48*7c478bd9Sstevel@tonic-gate 	kthread_t		*tqent_thread;
49*7c478bd9Sstevel@tonic-gate 	kcondvar_t		tqent_cv;
50*7c478bd9Sstevel@tonic-gate } taskq_ent_t;
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate /*
53*7c478bd9Sstevel@tonic-gate  * Taskq Statistics fields are not protected by any locks.
54*7c478bd9Sstevel@tonic-gate  */
55*7c478bd9Sstevel@tonic-gate typedef struct tqstat {
56*7c478bd9Sstevel@tonic-gate 	uint_t		tqs_hits;
57*7c478bd9Sstevel@tonic-gate 	uint_t		tqs_misses;
58*7c478bd9Sstevel@tonic-gate 	uint_t		tqs_overflow;	/* no threads to allocate   */
59*7c478bd9Sstevel@tonic-gate 	uint_t		tqs_tcreates;	/* threads created 	*/
60*7c478bd9Sstevel@tonic-gate 	uint_t		tqs_tdeaths;	/* threads died		*/
61*7c478bd9Sstevel@tonic-gate 	uint_t		tqs_maxthreads;	/* max # of alive threads */
62*7c478bd9Sstevel@tonic-gate 	uint_t		tqs_nomem;	/* # of times there were no memory */
63*7c478bd9Sstevel@tonic-gate 	uint_t		tqs_disptcreates;
64*7c478bd9Sstevel@tonic-gate } tqstat_t;
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate /*
67*7c478bd9Sstevel@tonic-gate  * Per-CPU hash bucket manages taskq_bent_t structures using freelist.
68*7c478bd9Sstevel@tonic-gate  */
69*7c478bd9Sstevel@tonic-gate struct taskq_bucket {
70*7c478bd9Sstevel@tonic-gate 	kmutex_t	tqbucket_lock;
71*7c478bd9Sstevel@tonic-gate 	taskq_t		*tqbucket_taskq;	/* Enclosing taskq */
72*7c478bd9Sstevel@tonic-gate 	taskq_ent_t	tqbucket_freelist;
73*7c478bd9Sstevel@tonic-gate 	uint_t		tqbucket_nalloc;	/* # of allocated entries */
74*7c478bd9Sstevel@tonic-gate 	uint_t		tqbucket_nfree;		/* # of free entries */
75*7c478bd9Sstevel@tonic-gate 	kcondvar_t	tqbucket_cv;
76*7c478bd9Sstevel@tonic-gate 	ushort_t	tqbucket_flags;
77*7c478bd9Sstevel@tonic-gate 	hrtime_t	tqbucket_totaltime;
78*7c478bd9Sstevel@tonic-gate 	tqstat_t	tqbucket_stat;
79*7c478bd9Sstevel@tonic-gate };
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate /*
82*7c478bd9Sstevel@tonic-gate  * Bucket flags.
83*7c478bd9Sstevel@tonic-gate  */
84*7c478bd9Sstevel@tonic-gate #define	TQBUCKET_CLOSE		0x01
85*7c478bd9Sstevel@tonic-gate #define	TQBUCKET_SUSPEND	0x02
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate /*
88*7c478bd9Sstevel@tonic-gate  * taskq implementation flags: bit range 16-31
89*7c478bd9Sstevel@tonic-gate  */
90*7c478bd9Sstevel@tonic-gate #define	TASKQ_ACTIVE		0x00010000
91*7c478bd9Sstevel@tonic-gate #define	TASKQ_SUSPENDED		0x00020000
92*7c478bd9Sstevel@tonic-gate #define	TASKQ_NOINSTANCE	0x00040000
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate struct taskq {
95*7c478bd9Sstevel@tonic-gate 	char		tq_name[TASKQ_NAMELEN + 1];
96*7c478bd9Sstevel@tonic-gate 	kmutex_t	tq_lock;
97*7c478bd9Sstevel@tonic-gate 	krwlock_t	tq_threadlock;
98*7c478bd9Sstevel@tonic-gate 	kcondvar_t	tq_dispatch_cv;
99*7c478bd9Sstevel@tonic-gate 	kcondvar_t	tq_wait_cv;
100*7c478bd9Sstevel@tonic-gate 	uint_t		tq_flags;
101*7c478bd9Sstevel@tonic-gate 	int		tq_active;
102*7c478bd9Sstevel@tonic-gate 	int		tq_nthreads;
103*7c478bd9Sstevel@tonic-gate 	int		tq_nalloc;
104*7c478bd9Sstevel@tonic-gate 	int		tq_minalloc;
105*7c478bd9Sstevel@tonic-gate 	int		tq_maxalloc;
106*7c478bd9Sstevel@tonic-gate 	taskq_ent_t	*tq_freelist;
107*7c478bd9Sstevel@tonic-gate 	taskq_ent_t	tq_task;
108*7c478bd9Sstevel@tonic-gate 	int		tq_maxsize;
109*7c478bd9Sstevel@tonic-gate 	pri_t		tq_pri;		/* Scheduling priority	    */
110*7c478bd9Sstevel@tonic-gate 	taskq_bucket_t	*tq_buckets;	/* Per-cpu array of buckets */
111*7c478bd9Sstevel@tonic-gate 	int		tq_instance;
112*7c478bd9Sstevel@tonic-gate 	uint_t		tq_nbuckets;	/* # of buckets	(2^n)	    */
113*7c478bd9Sstevel@tonic-gate 	union {
114*7c478bd9Sstevel@tonic-gate 		kthread_t *_tq_thread;
115*7c478bd9Sstevel@tonic-gate 		kthread_t **_tq_threadlist;
116*7c478bd9Sstevel@tonic-gate 	}		tq_thr;
117*7c478bd9Sstevel@tonic-gate 	/*
118*7c478bd9Sstevel@tonic-gate 	 * Statistics.
119*7c478bd9Sstevel@tonic-gate 	 */
120*7c478bd9Sstevel@tonic-gate 	kstat_t		*tq_kstat;	/* Exported statistics */
121*7c478bd9Sstevel@tonic-gate 	hrtime_t	tq_totaltime;	/* Time spent processing tasks */
122*7c478bd9Sstevel@tonic-gate 	int		tq_tasks;	/* Total # of tasks posted */
123*7c478bd9Sstevel@tonic-gate 	int		tq_executed;	/* Total # of tasks executed */
124*7c478bd9Sstevel@tonic-gate 	int		tq_maxtasks;	/* Max number of tasks in the queue */
125*7c478bd9Sstevel@tonic-gate 	int		tq_tcreates;
126*7c478bd9Sstevel@tonic-gate 	int		tq_tdeaths;
127*7c478bd9Sstevel@tonic-gate };
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate #define	tq_thread tq_thr._tq_thread
130*7c478bd9Sstevel@tonic-gate #define	tq_threadlist tq_thr._tq_threadlist
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
133*7c478bd9Sstevel@tonic-gate }
134*7c478bd9Sstevel@tonic-gate #endif
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate #endif	/* _SYS_TASKQ_IMPL_H */
137