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, 2012, Intel Corporation.
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
46 * Timers are implemented as a sorted queue of expiry times. The queue
47 * is slotted, with each slot holding timers which expire in a
48 * 2**STTIMER_MINPOLL (8) second period. The timers in each slot are
49 * sorted by increasing expiry time. The number of slots is 2**7 (128),
50 * to cover a time period of 1024 seconds into the future before wrapping.
52 #define STTIMER_MINPOLL 3 /* log2 min poll interval (8 s) */
53 #define STTIMER_SLOTTIME (1 << STTIMER_MINPOLL)
54 #define STTIMER_SLOTTIMEMASK (~(STTIMER_SLOTTIME - 1))
55 #define STTIMER_NSLOTS (1 << 7)
56 #define STTIMER_SLOT(t) (&stt_data.stt_hash[(((t) >> STTIMER_MINPOLL) & \
57 (STTIMER_NSLOTS - 1))])
59 static struct st_timer_data {
61 unsigned long stt_prev_slot; /* start time of the slot processed
63 struct list_head stt_hash[STTIMER_NSLOTS];
65 wait_queue_head_t stt_waitq;
70 stt_add_timer(struct stt_timer *timer)
72 struct list_head *pos;
74 spin_lock(&stt_data.stt_lock);
76 LASSERT(stt_data.stt_nthreads > 0);
77 LASSERT(!stt_data.stt_shuttingdown);
78 LASSERT(timer->stt_func);
79 LASSERT(list_empty(&timer->stt_list));
80 LASSERT(timer->stt_expires > ktime_get_real_seconds());
82 /* a simple insertion sort */
83 list_for_each_prev(pos, STTIMER_SLOT(timer->stt_expires)) {
84 struct stt_timer *old = list_entry(pos, struct stt_timer,
87 if (timer->stt_expires >= old->stt_expires)
90 list_add(&timer->stt_list, pos);
92 spin_unlock(&stt_data.stt_lock);
96 * The function returns whether it has deactivated a pending timer or not.
97 * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
98 * active timer returns 1.)
101 * When 0 is returned, it is possible that timer->stt_func _is_ running on
105 stt_del_timer(struct stt_timer *timer)
109 spin_lock(&stt_data.stt_lock);
111 LASSERT(stt_data.stt_nthreads > 0);
112 LASSERT(!stt_data.stt_shuttingdown);
114 if (!list_empty(&timer->stt_list)) {
116 list_del_init(&timer->stt_list);
119 spin_unlock(&stt_data.stt_lock);
123 /* called with stt_data.stt_lock held */
125 stt_expire_list(struct list_head *slot, time64_t now)
128 struct stt_timer *timer;
130 while (!list_empty(slot)) {
131 timer = list_entry(slot->next, struct stt_timer, stt_list);
133 if (timer->stt_expires > now)
136 list_del_init(&timer->stt_list);
137 spin_unlock(&stt_data.stt_lock);
140 (*timer->stt_func) (timer->stt_data);
142 spin_lock(&stt_data.stt_lock);
149 stt_check_timers(unsigned long *last)
153 unsigned long this_slot;
155 now = ktime_get_real_seconds();
156 this_slot = now & STTIMER_SLOTTIMEMASK;
158 spin_lock(&stt_data.stt_lock);
160 while (cfs_time_aftereq(this_slot, *last)) {
161 expired += stt_expire_list(STTIMER_SLOT(this_slot), now);
162 this_slot = cfs_time_sub(this_slot, STTIMER_SLOTTIME);
165 *last = now & STTIMER_SLOTTIMEMASK;
166 spin_unlock(&stt_data.stt_lock);
171 stt_timer_main(void *arg)
177 while (!stt_data.stt_shuttingdown) {
178 stt_check_timers(&stt_data.stt_prev_slot);
180 rc = wait_event_timeout(stt_data.stt_waitq,
181 stt_data.stt_shuttingdown,
182 cfs_time_seconds(STTIMER_SLOTTIME));
185 spin_lock(&stt_data.stt_lock);
186 stt_data.stt_nthreads--;
187 spin_unlock(&stt_data.stt_lock);
192 stt_start_timer_thread(void)
194 struct task_struct *task;
196 LASSERT(!stt_data.stt_shuttingdown);
198 task = kthread_run(stt_timer_main, NULL, "st_timer");
200 return PTR_ERR(task);
202 spin_lock(&stt_data.stt_lock);
203 stt_data.stt_nthreads++;
204 spin_unlock(&stt_data.stt_lock);
214 stt_data.stt_shuttingdown = 0;
215 stt_data.stt_prev_slot = ktime_get_real_seconds() & STTIMER_SLOTTIMEMASK;
217 spin_lock_init(&stt_data.stt_lock);
218 for (i = 0; i < STTIMER_NSLOTS; i++)
219 INIT_LIST_HEAD(&stt_data.stt_hash[i]);
221 stt_data.stt_nthreads = 0;
222 init_waitqueue_head(&stt_data.stt_waitq);
223 rc = stt_start_timer_thread();
225 CERROR("Can't spawn timer thread: %d\n", rc);
235 spin_lock(&stt_data.stt_lock);
237 for (i = 0; i < STTIMER_NSLOTS; i++)
238 LASSERT(list_empty(&stt_data.stt_hash[i]));
240 stt_data.stt_shuttingdown = 1;
242 wake_up(&stt_data.stt_waitq);
243 lst_wait_until(!stt_data.stt_nthreads, stt_data.stt_lock,
244 "waiting for %d threads to terminate\n",
245 stt_data.stt_nthreads);
247 spin_unlock(&stt_data.stt_lock);