xref: /titanic_44/usr/src/uts/common/sys/callo.h (revision e3e7cd2990d896de7fff76bc91b8cf570470d6ba)
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 2008 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 	callout_id_t	c_xid;		/* extended callout ID; see below */
54 	clock_t		c_runtime;	/* absolute run time */
55 	int64_t		c_runhrtime;	/* run time ticks since epoch */
56 	void		(*c_func)(void *); /* function to call */
57 	void		*c_arg;		/* argument to function */
58 	kthread_id_t	c_executor;	/* thread executing callout */
59 	kcondvar_t	c_done;		/* signal callout completion */
60 } callout_t;
61 
62 /*
63  * The extended callout ID consists of the callout ID (as returned by
64  * timeout()) plus a bit indicating whether the callout is executing.
65  *
66  * The callout ID uniquely identifies a callout.  It contains a table ID,
67  * indicating which callout table the callout belongs to, a bit indicating
68  * whether this is a short-term or long-term callout, and a running counter.
69  * The highest bit of the running counter is always set; this ensures that
70  * the callout ID is always non-zero, thus eliminating the need for an
71  * explicit wrap-around test during ID generation.
72  *
73  * The long-term bit exists to address the problem of callout ID collision.
74  * This is an issue because the system typically generates a large number of
75  * timeout() requests, which means that callout IDs eventually get recycled.
76  * Most timeouts are very short-lived, so that ID recycling isn't a problem;
77  * but there are a handful of timeouts which are sufficiently long-lived to
78  * see their own IDs reused.  We use the long-term bit to partition the
79  * ID namespace into pieces; the short-term space gets all the heavy traffic
80  * and can wrap frequently (i.e., on the order of a day) with no ill effects;
81  * the long-term space gets very little traffic and thus never wraps.
82  */
83 #define	CALLOUT_EXECUTING	(1UL << (8 * sizeof (long) - 1))
84 #define	CALLOUT_LONGTERM	(1UL << (8 * sizeof (long) - 2))
85 #define	CALLOUT_COUNTER_HIGH	(1UL << (8 * sizeof (long) - 3))
86 #define	CALLOUT_FANOUT_BITS	3
87 #define	CALLOUT_TYPE_BITS	1
88 #define	CALLOUT_NTYPES		(1 << CALLOUT_TYPE_BITS)
89 #define	CALLOUT_FANOUT		(1 << CALLOUT_FANOUT_BITS)
90 #define	CALLOUT_FANOUT_MASK	(CALLOUT_FANOUT - 1)
91 #define	CALLOUT_COUNTER_SHIFT	(CALLOUT_TYPE_BITS + CALLOUT_FANOUT_BITS)
92 #define	CALLOUT_COUNTER_LOW	(1 << CALLOUT_COUNTER_SHIFT)
93 #define	CALLOUT_TABLES		CALLOUT_COUNTER_LOW
94 #define	CALLOUT_TABLE_MASK	(CALLOUT_TABLES - 1)
95 #define	CALLOUT_TABLE(t, f)	\
96 	(((t) << CALLOUT_FANOUT_BITS) + ((f) & CALLOUT_FANOUT_MASK))
97 
98 /*
99  * We assume that during any period of CALLOUT_LONGTERM_TICKS ticks, at most
100  * (CALLOUT_COUNTER_HIGH / CALLOUT_COUNTER_LOW) callouts will be generated.
101  */
102 #define	CALLOUT_LONGTERM_TICKS	0x4000
103 #define	CALLOUT_BUCKETS		512		/* MUST be a power of 2 */
104 #define	CALLOUT_BUCKET_MASK	(CALLOUT_BUCKETS - 1)
105 #define	CALLOUT_HASH(x)		((x) & CALLOUT_BUCKET_MASK)
106 #define	CALLOUT_IDHASH(x)	CALLOUT_HASH((x) >> CALLOUT_COUNTER_SHIFT)
107 #define	CALLOUT_LBHASH(x)	CALLOUT_HASH(x)
108 
109 #define	CALLOUT_THREADS		2		/* keep it simple for now */
110 
111 #define	CALLOUT_REALTIME	0		/* realtime callout type */
112 #define	CALLOUT_NORMAL		1		/* normal callout type */
113 
114 /*
115  * All of the state information associated with a callout table.
116  * The fields are ordered with cache performance in mind.
117  */
118 typedef struct callout_table {
119 	kmutex_t	ct_lock;	/* protects all callout state */
120 	callout_t	*ct_freelist;	/* free callout structures */
121 	clock_t		ct_curtime;	/* current time; tracks lbolt */
122 	clock_t		ct_runtime;	/* the callouts we're running now */
123 	int64_t		ct_curhrtime;	/* current time ticks since epoch */
124 	taskq_t		*ct_taskq;	/* taskq to execute normal callouts */
125 	callout_id_t	ct_short_id;	/* most recently issued short-term ID */
126 	callout_id_t	ct_long_id;	/* most recently issued long-term ID */
127 	callout_t 	*ct_idhash[CALLOUT_BUCKETS];	/* ID hash chains */
128 	callout_t 	*ct_lbhash[CALLOUT_BUCKETS];	/* lbolt hash chains */
129 } callout_table_t;
130 
131 #ifdef	_KERNEL
132 extern	void		callout_init(void);
133 extern	void		callout_schedule(void);
134 #endif
135 
136 #ifdef	__cplusplus
137 }
138 #endif
139 
140 #endif	/* _SYS_CALLO_H */
141