xref: /illumos-gate/usr/src/cmd/fm/fmd/common/fmd_timerq.c (revision 533affcbc7fc4d0c8132976ea454aaa715fe2307)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <signal.h>
28 #include <strings.h>
29 #include <limits.h>
30 
31 #include <fmd_alloc.h>
32 #include <fmd_subr.h>
33 #include <fmd_thread.h>
34 #include <fmd_timerq.h>
35 
36 #include <fmd.h>
37 
38 /*
39  * Install a new timer to fire after at least 'delta' nanoseconds have elapsed.
40  * Timers are associated with persistent integer identifiers in some idspace.
41  * We allocate a new timer structure or re-use one from our freelist, and then
42  * place it on the queue's list in sorted order by expiration time.  If the new
43  * timer is now the earliest to expire, we awaken the fmd_timerq_exec() thread.
44  */
45 id_t
46 fmd_timerq_install(fmd_timerq_t *tmq, fmd_idspace_t *ids,
47     fmd_timer_f *func, void *arg, fmd_event_t *ep, hrtime_t delta)
48 {
49 	hrtime_t now = fmd_time_gethrtime();
50 	hrtime_t base = ep ? fmd_event_hrtime(ep) : now;
51 
52 	fmd_timer_t *tp, *up;
53 	hrtime_t hrt;
54 	id_t id;
55 
56 	(void) pthread_mutex_lock(&tmq->tmq_lock);
57 
58 	if ((tp = fmd_list_next(&tmq->tmq_free)) == NULL) {
59 		tp = fmd_zalloc(sizeof (fmd_timer_t), FMD_SLEEP);
60 		(void) pthread_cond_init(&tp->tmr_cv, NULL);
61 	} else
62 		fmd_list_delete(&tmq->tmq_free, tp);
63 
64 	if ((id = fmd_idspace_alloc(ids, tp)) == -1) {
65 		fmd_list_prepend(&tmq->tmq_free, tp);
66 		(void) pthread_mutex_unlock(&tmq->tmq_lock);
67 		return (id);
68 	}
69 
70 	if (delta < 0)
71 		delta = 0; /* ensure delta is at least 0ns from now */
72 
73 	if (base + delta < base)
74 		hrt = INT64_MAX; /* if wrap-around, set timer for apocalypse */
75 	else
76 		hrt = base + delta;
77 
78 	tp->tmr_hrt = hrt;
79 	tp->tmr_ids = ids;
80 	tp->tmr_id = id;
81 	tp->tmr_func = func;
82 	tp->tmr_arg = arg;
83 
84 	/*
85 	 * For now we use a simple insertion sort for tmq_list.  If we have
86 	 * scaling problems here due to heavy use of our timer subsystem,
87 	 * then tmq_list can and should be replaced with a O(logN) heap.
88 	 */
89 	for (up = fmd_list_next(&tmq->tmq_list); up; up = fmd_list_next(up)) {
90 		if (tp->tmr_hrt < up->tmr_hrt)
91 			break;
92 	}
93 
94 	if (up != NULL)
95 		fmd_list_insert_before(&tmq->tmq_list, up, tp);
96 	else
97 		fmd_list_insert_after(&tmq->tmq_list, up, tp);
98 
99 	if (up != NULL && fmd_list_next(&tmq->tmq_list) == tp)
100 		fmd_time_waitcancel(tmq->tmq_thread->thr_tid);
101 	else if (up == NULL && fmd_list_next(&tmq->tmq_list) == tp)
102 		(void) pthread_cond_signal(&tmq->tmq_cv);
103 
104 	(void) pthread_mutex_unlock(&tmq->tmq_lock);
105 
106 	TRACE((FMD_DBG_TMR, "timer %s:%ld insert +%lldns",
107 	    ids->ids_name, id, delta));
108 
109 	return (id);
110 }
111 
112 /*
113  * Remove the specified timer.  If the 'id' is invalid, we'll panic inside of
114  * fmd_idspace_free().  If the timer is still set, we move it to the freelist
115  * and update the timer thread as needed.  If the timer 'id' is valid but
116  * tmr_id is not equal to id, then the timer callback is running: we wait for
117  * tmr_id to change to zero (indicating tmr_func is done) before returning.
118  */
119 void *
120 fmd_timerq_remove(fmd_timerq_t *tmq, fmd_idspace_t *ids, id_t id)
121 {
122 	hrtime_t delta = 0;
123 	void *arg = NULL;
124 	fmd_timer_t *tp;
125 
126 	(void) pthread_mutex_lock(&tmq->tmq_lock);
127 	tp = fmd_idspace_free(ids, id);
128 	ASSERT(tp == NULL || tp->tmr_ids == ids);
129 
130 	if (tp == NULL) {
131 		(void) pthread_mutex_unlock(&tmq->tmq_lock);
132 		return (NULL); /* timer is no longer active */
133 	}
134 
135 	if (tp->tmr_id == id) {
136 		fmd_list_delete(&tmq->tmq_list, tp);
137 		delta = tp->tmr_hrt - fmd_time_gethrtime();
138 		arg = tp->tmr_arg;
139 		tp->tmr_id = 0;
140 		fmd_list_append(&tmq->tmq_free, tp);
141 
142 		/*
143 		 * If tmq_list is now empty, we must awaken the exec thread so
144 		 * it will sleep on tmq_cv waiting for the list to change.  We
145 		 * could also awaken the exec thread if we removed the head of
146 		 * tmq_list, but an early wakeup is harmless so we do nothing.
147 		 */
148 		if (fmd_list_next(&tmq->tmq_list) == NULL)
149 			fmd_time_waitcancel(tmq->tmq_thread->thr_tid);
150 	} else {
151 		/*
152 		 * Wait until tmr_id is zero, indicating that tmr_func is done.
153 		 * This relies on expired fmd_timer_t's being returned to our
154 		 * free list rather than having the data structure deallocated.
155 		 */
156 		while (tp->tmr_id != 0)
157 			(void) pthread_cond_wait(&tp->tmr_cv, &tmq->tmq_lock);
158 	}
159 
160 	(void) pthread_mutex_unlock(&tmq->tmq_lock);
161 
162 	TRACE((FMD_DBG_TMR, "timer %s:%ld remove -%lldns",
163 	    ids->ids_name, id, delta > 0 ? delta : 0LL));
164 
165 	return (arg);
166 }
167 
168 /*
169  * fmd_timerq_exec() is the main loop of the thread that runs the timer queue.
170  * We sleep on tmq_cv waiting for timers to show up on tmq_list.  When the list
171  * is non-empty, we execute the callback function for each expired timer.  If
172  * timers remain that are not yet expired, we nanosleep() until the next expiry
173  * time.  We awaken whenever nanosleep() expires or we are interrupted by a
174  * SIGALRM from fmd_timerq_install indicating that we need to rescan our list.
175  */
176 static void
177 fmd_timerq_exec(fmd_timerq_t *tmq)
178 {
179 	fmd_timer_t *tp;
180 	sigset_t set;
181 	hrtime_t now;
182 
183 	/*
184 	 * fmd_thread_create() initializes threads with all signals blocked.
185 	 * We must unblock SIGALRM (whose disposition has been to set to call
186 	 * an empty function by fmd_timerq_init()) in order to permit directed
187 	 * signals to interrupt our nanosleep() and make it return EINTR.
188 	 * This SIGALRM mechanism is used by the native clock (see fmd_time.c).
189 	 */
190 	(void) sigemptyset(&set);
191 	(void) sigaddset(&set, SIGALRM);
192 	(void) pthread_sigmask(SIG_UNBLOCK, &set, NULL);
193 	(void) pthread_mutex_lock(&tmq->tmq_lock);
194 
195 	for (;;) {
196 		while (!tmq->tmq_abort && fmd_list_next(&tmq->tmq_list) == NULL)
197 			(void) pthread_cond_wait(&tmq->tmq_cv, &tmq->tmq_lock);
198 
199 		if (tmq->tmq_abort) {
200 			(void) pthread_mutex_unlock(&tmq->tmq_lock);
201 			return; /* abort timerq thread */
202 		}
203 
204 		for (now = fmd_time_gethrtime(); (tp = fmd_list_next(
205 		    &tmq->tmq_list)) != NULL; now = fmd_time_gethrtime()) {
206 
207 			if (now == INT64_MAX || tp->tmr_hrt > now)
208 				break; /* no more timers left to expire */
209 
210 			tp->tmr_id = -tp->tmr_id;
211 			fmd_list_delete(&tmq->tmq_list, tp);
212 			(void) pthread_mutex_unlock(&tmq->tmq_lock);
213 
214 			TRACE((FMD_DBG_TMR, "tmr %s:%ld exec start (hrt=%llx)",
215 			    tp->tmr_ids->ids_name, -tp->tmr_id, tp->tmr_hrt));
216 
217 			tp->tmr_func(tp->tmr_arg, -tp->tmr_id, tp->tmr_hrt);
218 
219 			TRACE((FMD_DBG_TMR, "tmr %s:%ld exec end",
220 			    tp->tmr_ids->ids_name, -tp->tmr_id));
221 
222 			(void) pthread_mutex_lock(&tmq->tmq_lock);
223 			(void) fmd_idspace_free(tp->tmr_ids, -tp->tmr_id);
224 			fmd_list_append(&tmq->tmq_free, tp);
225 			tp->tmr_id = 0; /* for fmd_timer_remove() */
226 
227 			(void) pthread_cond_broadcast(&tp->tmr_cv);
228 		}
229 
230 		if (tp != NULL) {
231 			(void) pthread_mutex_unlock(&tmq->tmq_lock);
232 			fmd_time_waithrtime(tp->tmr_hrt - now);
233 			(void) pthread_mutex_lock(&tmq->tmq_lock);
234 		}
235 	}
236 }
237 
238 static void
239 fmd_timerq_alrm(int sig)
240 {
241 	TRACE((FMD_DBG_TMR, "timer thread received alarm sig#%d", sig));
242 }
243 
244 fmd_timerq_t *
245 fmd_timerq_create(void)
246 {
247 	fmd_timerq_t *tmq = fmd_zalloc(sizeof (fmd_timerq_t), FMD_SLEEP);
248 	struct sigaction act;
249 
250 	(void) pthread_mutex_init(&tmq->tmq_lock, NULL);
251 	(void) pthread_cond_init(&tmq->tmq_cv, NULL);
252 
253 	act.sa_handler = fmd_timerq_alrm;
254 	act.sa_flags = 0;
255 	(void) sigemptyset(&act.sa_mask);
256 	(void) sigaction(SIGALRM, &act, NULL);
257 
258 	if ((tmq->tmq_thread = fmd_thread_create(fmd.d_rmod,
259 	    (fmd_thread_f *)fmd_timerq_exec, tmq)) == NULL)
260 		fmd_panic("failed to create timer thread");
261 
262 	return (tmq);
263 }
264 
265 void
266 fmd_timerq_destroy(fmd_timerq_t *tmq)
267 {
268 	struct sigaction act;
269 	fmd_timer_t *tmr;
270 
271 	(void) pthread_mutex_lock(&tmq->tmq_lock);
272 	tmq->tmq_abort++;
273 
274 	if (fmd_list_next(&tmq->tmq_list) != NULL)
275 		fmd_time_waitcancel(tmq->tmq_thread->thr_tid);
276 	else
277 		(void) pthread_cond_signal(&tmq->tmq_cv);
278 
279 	(void) pthread_mutex_unlock(&tmq->tmq_lock);
280 	fmd_thread_destroy(tmq->tmq_thread, FMD_THREAD_JOIN);
281 	(void) pthread_mutex_lock(&tmq->tmq_lock);
282 
283 	while ((tmr = fmd_list_next(&tmq->tmq_list)) != NULL) {
284 		fmd_list_delete(&tmq->tmq_list, tmr);
285 		(void) fmd_idspace_free(tmr->tmr_ids, tmr->tmr_id);
286 		fmd_free(tmr, sizeof (fmd_timer_t));
287 	}
288 
289 	while ((tmr = fmd_list_next(&tmq->tmq_free)) != NULL) {
290 		fmd_list_delete(&tmq->tmq_free, tmr);
291 		ASSERT(tmr->tmr_id == 0);
292 		fmd_free(tmr, sizeof (fmd_timer_t));
293 	}
294 
295 	act.sa_handler = SIG_DFL;
296 	act.sa_flags = 0;
297 	(void) sigemptyset(&act.sa_mask);
298 	(void) sigaction(SIGALRM, &act, NULL);
299 
300 	fmd_free(tmq, sizeof (fmd_timerq_t));
301 }
302