4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lnet/selftest/timer.c
38 * Author: Isaac Huang <isaac@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_LNET
47 * Timers are implemented as a sorted queue of expiry times. The queue
48 * is slotted, with each slot holding timers which expire in a
49 * 2**STTIMER_MINPOLL (8) second period. The timers in each slot are
50 * sorted by increasing expiry time. The number of slots is 2**7 (128),
51 * to cover a time period of 1024 seconds into the future before wrapping.
53 #define STTIMER_MINPOLL 3 /* log2 min poll interval (8 s) */
54 #define STTIMER_SLOTTIME (1 << STTIMER_MINPOLL)
55 #define STTIMER_SLOTTIMEMASK (~(STTIMER_SLOTTIME - 1))
56 #define STTIMER_NSLOTS (1 << 7)
57 #define STTIMER_SLOT(t) (&stt_data.stt_hash[(((t) >> STTIMER_MINPOLL) & \
58 (STTIMER_NSLOTS - 1))])
60 struct st_timer_data {
62 /* start time of the slot processed previously */
63 cfs_time_t stt_prev_slot;
64 cfs_list_t stt_hash[STTIMER_NSLOTS];
67 cfs_waitq_t stt_waitq;
73 stt_add_timer(stt_timer_t *timer)
77 spin_lock(&stt_data.stt_lock);
80 LASSERT (stt_data.stt_nthreads > 0);
82 LASSERT (!stt_data.stt_shuttingdown);
83 LASSERT (timer->stt_func != NULL);
84 LASSERT (cfs_list_empty(&timer->stt_list));
85 LASSERT (cfs_time_after(timer->stt_expires, cfs_time_current_sec()));
87 /* a simple insertion sort */
88 cfs_list_for_each_prev (pos, STTIMER_SLOT(timer->stt_expires)) {
89 stt_timer_t *old = cfs_list_entry(pos, stt_timer_t, stt_list);
91 if (cfs_time_aftereq(timer->stt_expires, old->stt_expires))
94 cfs_list_add(&timer->stt_list, pos);
96 spin_unlock(&stt_data.stt_lock);
100 * The function returns whether it has deactivated a pending timer or not.
101 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
102 * active timer returns 1.)
105 * When 0 is returned, it is possible that timer->stt_func _is_ running on
109 stt_del_timer (stt_timer_t *timer)
113 spin_lock(&stt_data.stt_lock);
116 LASSERT (stt_data.stt_nthreads > 0);
118 LASSERT (!stt_data.stt_shuttingdown);
120 if (!cfs_list_empty(&timer->stt_list)) {
122 cfs_list_del_init(&timer->stt_list);
125 spin_unlock(&stt_data.stt_lock);
129 /* called with stt_data.stt_lock held */
131 stt_expire_list (cfs_list_t *slot, cfs_time_t now)
136 while (!cfs_list_empty(slot)) {
137 timer = cfs_list_entry(slot->next, stt_timer_t, stt_list);
139 if (cfs_time_after(timer->stt_expires, now))
142 cfs_list_del_init(&timer->stt_list);
143 spin_unlock(&stt_data.stt_lock);
146 (*timer->stt_func) (timer->stt_data);
148 spin_lock(&stt_data.stt_lock);
155 stt_check_timers (cfs_time_t *last)
159 cfs_time_t this_slot;
161 now = cfs_time_current_sec();
162 this_slot = now & STTIMER_SLOTTIMEMASK;
164 spin_lock(&stt_data.stt_lock);
166 while (cfs_time_aftereq(this_slot, *last)) {
167 expired += stt_expire_list(STTIMER_SLOT(this_slot), now);
168 this_slot = cfs_time_sub(this_slot, STTIMER_SLOTTIME);
171 *last = now & STTIMER_SLOTTIMEMASK;
172 spin_unlock(&stt_data.stt_lock);
179 stt_timer_main (void *arg)
186 cfs_daemonize("st_timer");
189 while (!stt_data.stt_shuttingdown) {
190 stt_check_timers(&stt_data.stt_prev_slot);
192 cfs_waitq_wait_event_timeout(stt_data.stt_waitq,
193 stt_data.stt_shuttingdown,
194 cfs_time_seconds(STTIMER_SLOTTIME),
198 spin_lock(&stt_data.stt_lock);
199 stt_data.stt_nthreads--;
200 spin_unlock(&stt_data.stt_lock);
205 stt_start_timer_thread (void)
209 LASSERT (!stt_data.stt_shuttingdown);
211 pid = cfs_create_thread(stt_timer_main, NULL, 0);
215 spin_lock(&stt_data.stt_lock);
216 stt_data.stt_nthreads++;
217 spin_unlock(&stt_data.stt_lock);
221 #else /* !__KERNEL__ */
224 stt_check_events (void)
226 return stt_check_timers(&stt_data.stt_prev_slot);
230 stt_poll_interval (void)
232 return STTIMER_SLOTTIME;
243 stt_data.stt_shuttingdown = 0;
244 stt_data.stt_prev_slot = cfs_time_current_sec() & STTIMER_SLOTTIMEMASK;
246 spin_lock_init(&stt_data.stt_lock);
247 for (i = 0; i < STTIMER_NSLOTS; i++)
248 CFS_INIT_LIST_HEAD(&stt_data.stt_hash[i]);
251 stt_data.stt_nthreads = 0;
252 cfs_waitq_init(&stt_data.stt_waitq);
253 rc = stt_start_timer_thread();
255 CERROR ("Can't spawn timer thread: %d\n", rc);
266 spin_lock(&stt_data.stt_lock);
268 for (i = 0; i < STTIMER_NSLOTS; i++)
269 LASSERT (cfs_list_empty(&stt_data.stt_hash[i]));
271 stt_data.stt_shuttingdown = 1;
274 cfs_waitq_signal(&stt_data.stt_waitq);
275 lst_wait_until(stt_data.stt_nthreads == 0, stt_data.stt_lock,
276 "waiting for %d threads to terminate\n",
277 stt_data.stt_nthreads);
280 spin_unlock(&stt_data.stt_lock);