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