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