xref: /onnv-gate/usr/src/uts/common/sys/callo.h (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright (c) 1997-1998 by Sun Microsystems, Inc.
28*0Sstevel@tonic-gate  * All rights reserved.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #ifndef _SYS_CALLO_H
32*0Sstevel@tonic-gate #define	_SYS_CALLO_H
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #include <sys/t_lock.h>
37*0Sstevel@tonic-gate #include <sys/taskq.h>
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #ifdef	__cplusplus
40*0Sstevel@tonic-gate extern "C" {
41*0Sstevel@tonic-gate #endif
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate typedef long	callout_id_t;		/* internal form of timeout_id_t */
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate /*
46*0Sstevel@tonic-gate  * The callout mechanism provides general-purpose event scheduling:
47*0Sstevel@tonic-gate  * an arbitrary function is called in a specified amount of time.
48*0Sstevel@tonic-gate  */
49*0Sstevel@tonic-gate typedef struct callout {
50*0Sstevel@tonic-gate 	struct callout	*c_idnext;	/* next in ID hash, or on freelist */
51*0Sstevel@tonic-gate 	struct callout	*c_idprev;	/* prev in ID hash */
52*0Sstevel@tonic-gate 	struct callout	*c_lbnext;	/* next in lbolt hash */
53*0Sstevel@tonic-gate 	struct callout	*c_lbprev;	/* prev in lbolt hash */
54*0Sstevel@tonic-gate 	callout_id_t	c_xid;		/* extended callout ID; see below */
55*0Sstevel@tonic-gate 	clock_t		c_runtime;	/* absolute run time */
56*0Sstevel@tonic-gate 	void		(*c_func)(void *); /* function to call */
57*0Sstevel@tonic-gate 	void		*c_arg;		/* argument to function */
58*0Sstevel@tonic-gate 	kthread_id_t	c_executor;	/* thread executing callout */
59*0Sstevel@tonic-gate 	kcondvar_t	c_done;		/* signal callout completion */
60*0Sstevel@tonic-gate } callout_t;
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate /*
63*0Sstevel@tonic-gate  * The extended callout ID consists of the callout ID (as returned by
64*0Sstevel@tonic-gate  * timeout()) plus a bit indicating whether the callout is executing.
65*0Sstevel@tonic-gate  *
66*0Sstevel@tonic-gate  * The callout ID uniquely identifies a callout.  It contains a table ID,
67*0Sstevel@tonic-gate  * indicating which callout table the callout belongs to, a bit indicating
68*0Sstevel@tonic-gate  * whether this is a short-term or long-term callout, and a running counter.
69*0Sstevel@tonic-gate  * The highest bit of the running counter is always set; this ensures that
70*0Sstevel@tonic-gate  * the callout ID is always non-zero, thus eliminating the need for an
71*0Sstevel@tonic-gate  * explicit wrap-around test during ID generation.
72*0Sstevel@tonic-gate  *
73*0Sstevel@tonic-gate  * The long-term bit exists to address the problem of callout ID collision.
74*0Sstevel@tonic-gate  * This is an issue because the system typically generates a large number of
75*0Sstevel@tonic-gate  * timeout() requests, which means that callout IDs eventually get recycled.
76*0Sstevel@tonic-gate  * Most timeouts are very short-lived, so that ID recycling isn't a problem;
77*0Sstevel@tonic-gate  * but there are a handful of timeouts which are sufficiently long-lived to
78*0Sstevel@tonic-gate  * see their own IDs reused.  We use the long-term bit to partition the
79*0Sstevel@tonic-gate  * ID namespace into pieces; the short-term space gets all the heavy traffic
80*0Sstevel@tonic-gate  * and can wrap frequently (i.e., on the order of a day) with no ill effects;
81*0Sstevel@tonic-gate  * the long-term space gets very little traffic and thus never wraps.
82*0Sstevel@tonic-gate  */
83*0Sstevel@tonic-gate #define	CALLOUT_EXECUTING	(1UL << (8 * sizeof (long) - 1))
84*0Sstevel@tonic-gate #define	CALLOUT_LONGTERM	(1UL << (8 * sizeof (long) - 2))
85*0Sstevel@tonic-gate #define	CALLOUT_COUNTER_HIGH	(1UL << (8 * sizeof (long) - 3))
86*0Sstevel@tonic-gate #define	CALLOUT_FANOUT_BITS	3
87*0Sstevel@tonic-gate #define	CALLOUT_TYPE_BITS	1
88*0Sstevel@tonic-gate #define	CALLOUT_NTYPES		(1 << CALLOUT_TYPE_BITS)
89*0Sstevel@tonic-gate #define	CALLOUT_FANOUT		(1 << CALLOUT_FANOUT_BITS)
90*0Sstevel@tonic-gate #define	CALLOUT_FANOUT_MASK	(CALLOUT_FANOUT - 1)
91*0Sstevel@tonic-gate #define	CALLOUT_COUNTER_SHIFT	(CALLOUT_TYPE_BITS + CALLOUT_FANOUT_BITS)
92*0Sstevel@tonic-gate #define	CALLOUT_COUNTER_LOW	(1 << CALLOUT_COUNTER_SHIFT)
93*0Sstevel@tonic-gate #define	CALLOUT_TABLES		CALLOUT_COUNTER_LOW
94*0Sstevel@tonic-gate #define	CALLOUT_TABLE_MASK	(CALLOUT_TABLES - 1)
95*0Sstevel@tonic-gate #define	CALLOUT_TABLE(t, f)	\
96*0Sstevel@tonic-gate 	(((t) << CALLOUT_FANOUT_BITS) + ((f) & CALLOUT_FANOUT_MASK))
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate /*
99*0Sstevel@tonic-gate  * We assume that during any period of CALLOUT_LONGTERM_TICKS ticks, at most
100*0Sstevel@tonic-gate  * (CALLOUT_COUNTER_HIGH / CALLOUT_COUNTER_LOW) callouts will be generated.
101*0Sstevel@tonic-gate  */
102*0Sstevel@tonic-gate #define	CALLOUT_LONGTERM_TICKS	0x4000
103*0Sstevel@tonic-gate #define	CALLOUT_BUCKETS		512		/* MUST be a power of 2 */
104*0Sstevel@tonic-gate #define	CALLOUT_BUCKET_MASK	(CALLOUT_BUCKETS - 1)
105*0Sstevel@tonic-gate #define	CALLOUT_HASH(x)		((x) & CALLOUT_BUCKET_MASK)
106*0Sstevel@tonic-gate #define	CALLOUT_IDHASH(x)	CALLOUT_HASH((x) >> CALLOUT_COUNTER_SHIFT)
107*0Sstevel@tonic-gate #define	CALLOUT_LBHASH(x)	CALLOUT_HASH(x)
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate #define	CALLOUT_THREADS		2		/* keep it simple for now */
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate #define	CALLOUT_REALTIME	0		/* realtime callout type */
112*0Sstevel@tonic-gate #define	CALLOUT_NORMAL		1		/* normal callout type */
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate /*
115*0Sstevel@tonic-gate  * All of the state information associated with a callout table.
116*0Sstevel@tonic-gate  * The fields are ordered with cache performance in mind.
117*0Sstevel@tonic-gate  */
118*0Sstevel@tonic-gate typedef struct callout_table {
119*0Sstevel@tonic-gate 	kmutex_t	ct_lock;	/* protects all callout state */
120*0Sstevel@tonic-gate 	callout_t	*ct_freelist;	/* free callout structures */
121*0Sstevel@tonic-gate 	clock_t		ct_curtime;	/* current time; tracks lbolt */
122*0Sstevel@tonic-gate 	clock_t		ct_runtime;	/* the callouts we're running now */
123*0Sstevel@tonic-gate 	taskq_t		*ct_taskq;	/* taskq to execute normal callouts */
124*0Sstevel@tonic-gate 	callout_id_t	ct_short_id;	/* most recently issued short-term ID */
125*0Sstevel@tonic-gate 	callout_id_t	ct_long_id;	/* most recently issued long-term ID */
126*0Sstevel@tonic-gate 	callout_t 	*ct_idhash[CALLOUT_BUCKETS];	/* ID hash chains */
127*0Sstevel@tonic-gate 	callout_t 	*ct_lbhash[CALLOUT_BUCKETS];	/* lbolt hash chains */
128*0Sstevel@tonic-gate } callout_table_t;
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate #ifdef	_KERNEL
131*0Sstevel@tonic-gate extern	void		callout_init(void);
132*0Sstevel@tonic-gate extern	void		callout_schedule(void);
133*0Sstevel@tonic-gate #endif
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate #ifdef	__cplusplus
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate #endif
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate #endif	/* _SYS_CALLO_H */
140