xref: /onnv-gate/usr/src/uts/common/sys/callo.h (revision 6422:d51a10d738ba)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6422Sqiao  * Common Development and Distribution License (the "License").
6*6422Sqiao  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
220Sstevel@tonic-gate /*	  All Rights Reserved  	*/
230Sstevel@tonic-gate 
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
26*6422Sqiao  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
27*6422Sqiao  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #ifndef _SYS_CALLO_H
310Sstevel@tonic-gate #define	_SYS_CALLO_H
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include <sys/t_lock.h>
360Sstevel@tonic-gate #include <sys/taskq.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #ifdef	__cplusplus
390Sstevel@tonic-gate extern "C" {
400Sstevel@tonic-gate #endif
410Sstevel@tonic-gate 
420Sstevel@tonic-gate typedef long	callout_id_t;		/* internal form of timeout_id_t */
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * The callout mechanism provides general-purpose event scheduling:
460Sstevel@tonic-gate  * an arbitrary function is called in a specified amount of time.
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate typedef struct callout {
490Sstevel@tonic-gate 	struct callout	*c_idnext;	/* next in ID hash, or on freelist */
500Sstevel@tonic-gate 	struct callout	*c_idprev;	/* prev in ID hash */
510Sstevel@tonic-gate 	struct callout	*c_lbnext;	/* next in lbolt hash */
520Sstevel@tonic-gate 	struct callout	*c_lbprev;	/* prev in lbolt hash */
530Sstevel@tonic-gate 	callout_id_t	c_xid;		/* extended callout ID; see below */
540Sstevel@tonic-gate 	clock_t		c_runtime;	/* absolute run time */
55*6422Sqiao 	int64_t		c_runhrtime;	/* run time ticks since epoch */
560Sstevel@tonic-gate 	void		(*c_func)(void *); /* function to call */
570Sstevel@tonic-gate 	void		*c_arg;		/* argument to function */
580Sstevel@tonic-gate 	kthread_id_t	c_executor;	/* thread executing callout */
590Sstevel@tonic-gate 	kcondvar_t	c_done;		/* signal callout completion */
600Sstevel@tonic-gate } callout_t;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate  * The extended callout ID consists of the callout ID (as returned by
640Sstevel@tonic-gate  * timeout()) plus a bit indicating whether the callout is executing.
650Sstevel@tonic-gate  *
660Sstevel@tonic-gate  * The callout ID uniquely identifies a callout.  It contains a table ID,
670Sstevel@tonic-gate  * indicating which callout table the callout belongs to, a bit indicating
680Sstevel@tonic-gate  * whether this is a short-term or long-term callout, and a running counter.
690Sstevel@tonic-gate  * The highest bit of the running counter is always set; this ensures that
700Sstevel@tonic-gate  * the callout ID is always non-zero, thus eliminating the need for an
710Sstevel@tonic-gate  * explicit wrap-around test during ID generation.
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  * The long-term bit exists to address the problem of callout ID collision.
740Sstevel@tonic-gate  * This is an issue because the system typically generates a large number of
750Sstevel@tonic-gate  * timeout() requests, which means that callout IDs eventually get recycled.
760Sstevel@tonic-gate  * Most timeouts are very short-lived, so that ID recycling isn't a problem;
770Sstevel@tonic-gate  * but there are a handful of timeouts which are sufficiently long-lived to
780Sstevel@tonic-gate  * see their own IDs reused.  We use the long-term bit to partition the
790Sstevel@tonic-gate  * ID namespace into pieces; the short-term space gets all the heavy traffic
800Sstevel@tonic-gate  * and can wrap frequently (i.e., on the order of a day) with no ill effects;
810Sstevel@tonic-gate  * the long-term space gets very little traffic and thus never wraps.
820Sstevel@tonic-gate  */
830Sstevel@tonic-gate #define	CALLOUT_EXECUTING	(1UL << (8 * sizeof (long) - 1))
840Sstevel@tonic-gate #define	CALLOUT_LONGTERM	(1UL << (8 * sizeof (long) - 2))
850Sstevel@tonic-gate #define	CALLOUT_COUNTER_HIGH	(1UL << (8 * sizeof (long) - 3))
860Sstevel@tonic-gate #define	CALLOUT_FANOUT_BITS	3
870Sstevel@tonic-gate #define	CALLOUT_TYPE_BITS	1
880Sstevel@tonic-gate #define	CALLOUT_NTYPES		(1 << CALLOUT_TYPE_BITS)
890Sstevel@tonic-gate #define	CALLOUT_FANOUT		(1 << CALLOUT_FANOUT_BITS)
900Sstevel@tonic-gate #define	CALLOUT_FANOUT_MASK	(CALLOUT_FANOUT - 1)
910Sstevel@tonic-gate #define	CALLOUT_COUNTER_SHIFT	(CALLOUT_TYPE_BITS + CALLOUT_FANOUT_BITS)
920Sstevel@tonic-gate #define	CALLOUT_COUNTER_LOW	(1 << CALLOUT_COUNTER_SHIFT)
930Sstevel@tonic-gate #define	CALLOUT_TABLES		CALLOUT_COUNTER_LOW
940Sstevel@tonic-gate #define	CALLOUT_TABLE_MASK	(CALLOUT_TABLES - 1)
950Sstevel@tonic-gate #define	CALLOUT_TABLE(t, f)	\
960Sstevel@tonic-gate 	(((t) << CALLOUT_FANOUT_BITS) + ((f) & CALLOUT_FANOUT_MASK))
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate  * We assume that during any period of CALLOUT_LONGTERM_TICKS ticks, at most
1000Sstevel@tonic-gate  * (CALLOUT_COUNTER_HIGH / CALLOUT_COUNTER_LOW) callouts will be generated.
1010Sstevel@tonic-gate  */
1020Sstevel@tonic-gate #define	CALLOUT_LONGTERM_TICKS	0x4000
1030Sstevel@tonic-gate #define	CALLOUT_BUCKETS		512		/* MUST be a power of 2 */
1040Sstevel@tonic-gate #define	CALLOUT_BUCKET_MASK	(CALLOUT_BUCKETS - 1)
1050Sstevel@tonic-gate #define	CALLOUT_HASH(x)		((x) & CALLOUT_BUCKET_MASK)
1060Sstevel@tonic-gate #define	CALLOUT_IDHASH(x)	CALLOUT_HASH((x) >> CALLOUT_COUNTER_SHIFT)
1070Sstevel@tonic-gate #define	CALLOUT_LBHASH(x)	CALLOUT_HASH(x)
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate #define	CALLOUT_THREADS		2		/* keep it simple for now */
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate #define	CALLOUT_REALTIME	0		/* realtime callout type */
1120Sstevel@tonic-gate #define	CALLOUT_NORMAL		1		/* normal callout type */
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate /*
1150Sstevel@tonic-gate  * All of the state information associated with a callout table.
1160Sstevel@tonic-gate  * The fields are ordered with cache performance in mind.
1170Sstevel@tonic-gate  */
1180Sstevel@tonic-gate typedef struct callout_table {
1190Sstevel@tonic-gate 	kmutex_t	ct_lock;	/* protects all callout state */
1200Sstevel@tonic-gate 	callout_t	*ct_freelist;	/* free callout structures */
1210Sstevel@tonic-gate 	clock_t		ct_curtime;	/* current time; tracks lbolt */
1220Sstevel@tonic-gate 	clock_t		ct_runtime;	/* the callouts we're running now */
123*6422Sqiao 	int64_t		ct_curhrtime;	/* current time ticks since epoch */
1240Sstevel@tonic-gate 	taskq_t		*ct_taskq;	/* taskq to execute normal callouts */
1250Sstevel@tonic-gate 	callout_id_t	ct_short_id;	/* most recently issued short-term ID */
1260Sstevel@tonic-gate 	callout_id_t	ct_long_id;	/* most recently issued long-term ID */
1270Sstevel@tonic-gate 	callout_t 	*ct_idhash[CALLOUT_BUCKETS];	/* ID hash chains */
1280Sstevel@tonic-gate 	callout_t 	*ct_lbhash[CALLOUT_BUCKETS];	/* lbolt hash chains */
1290Sstevel@tonic-gate } callout_table_t;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate #ifdef	_KERNEL
1320Sstevel@tonic-gate extern	void		callout_init(void);
1330Sstevel@tonic-gate extern	void		callout_schedule(void);
1340Sstevel@tonic-gate #endif
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate #ifdef	__cplusplus
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate #endif
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate #endif	/* _SYS_CALLO_H */
141