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) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Library level Event queue management routines
41 #define DEBUG_SUBSYSTEM S_LNET
42 #include <linux/lnet/lib-lnet.h>
45 * Create an event queue that has room for \a count number of events.
47 * The event queue is circular and older events will be overwritten by new
48 * ones if they are not removed in time by the user using the functions
49 * LNetEQGet(), LNetEQWait(), or LNetEQPoll(). It is up to the user to
50 * determine the appropriate size of the event queue to prevent this loss
51 * of events. Note that when EQ handler is specified in \a callback, no
52 * event loss can happen, since the handler is run for each event deposited
55 * \param count The number of events to be stored in the event queue. It
56 * will be rounded up to the next power of two.
57 * \param callback A handler function that runs when an event is deposited
58 * into the EQ. The constant value LNET_EQ_HANDLER_NONE can be used to
59 * indicate that no event handler is desired.
60 * \param handle On successful return, this location will hold a handle for
61 * the newly created EQ.
63 * \retval 0 On success.
64 * \retval -EINVAL If an parameter is not valid.
65 * \retval -ENOMEM If memory for the EQ can't be allocated.
67 * \see lnet_eq_handler_t for the discussion on EQ handler semantics.
70 LNetEQAlloc(unsigned int count, lnet_eq_handler_t callback,
71 lnet_handle_eq_t *handle)
75 LASSERT (the_lnet.ln_init);
76 LASSERT (the_lnet.ln_refcount > 0);
78 /* We need count to be a power of 2 so that when eq_{enq,deq}_seq
79 * overflow, they don't skip entries, so the queue has the same
80 * apparent capacity at all times */
82 count = cfs_power2_roundup(count);
84 if (callback != LNET_EQ_HANDLER_NONE && count != 0) {
85 CWARN("EQ callback is guaranteed to get every event, "
86 "do you still want to set eqcount %d for polling "
87 "event which will have locking overhead? "
88 "Please contact with developer to confirm\n", count);
91 /* count can be 0 if only need callback, we can eliminate
92 * overhead of enqueue event */
93 if (count == 0 && callback == LNET_EQ_HANDLER_NONE)
101 LIBCFS_ALLOC(eq->eq_events, count * sizeof(lnet_event_t));
102 if (eq->eq_events == NULL)
104 /* NB allocator has set all event sequence numbers to 0,
105 * so all them should be earlier than eq_deq_seq */
111 eq->eq_callback = callback;
113 eq->eq_refs = cfs_percpt_alloc(lnet_cpt_table(),
114 sizeof(*eq->eq_refs[0]));
115 if (eq->eq_refs == NULL)
118 /* MUST hold both exclusive lnet_res_lock */
119 lnet_res_lock(LNET_LOCK_EX);
120 /* NB: hold lnet_eq_wait_lock for EQ link/unlink, so we can do
121 * both EQ lookup and poll event with only lnet_eq_wait_lock */
124 lnet_res_lh_initialize(&the_lnet.ln_eq_container, &eq->eq_lh);
125 list_add(&eq->eq_list, &the_lnet.ln_eq_container.rec_active);
127 lnet_eq_wait_unlock();
128 lnet_res_unlock(LNET_LOCK_EX);
130 lnet_eq2handle(handle, eq);
134 if (eq->eq_events != NULL)
135 LIBCFS_FREE(eq->eq_events, count * sizeof(lnet_event_t));
137 if (eq->eq_refs != NULL)
138 cfs_percpt_free(eq->eq_refs);
143 EXPORT_SYMBOL(LNetEQAlloc);
146 * Release the resources associated with an event queue if it's idle;
147 * otherwise do nothing and it's up to the user to try again.
149 * \param eqh A handle for the event queue to be released.
151 * \retval 0 If the EQ is not in use and freed.
152 * \retval -ENOENT If \a eqh does not point to a valid EQ.
153 * \retval -EBUSY If the EQ is still in use by some MDs.
156 LNetEQFree(lnet_handle_eq_t eqh)
159 lnet_event_t *events = NULL;
166 LASSERT(the_lnet.ln_init);
167 LASSERT(the_lnet.ln_refcount > 0);
169 lnet_res_lock(LNET_LOCK_EX);
170 /* NB: hold lnet_eq_wait_lock for EQ link/unlink, so we can do
171 * both EQ lookup and poll event with only lnet_eq_wait_lock */
174 eq = lnet_handle2eq(&eqh);
180 cfs_percpt_for_each(ref, i, eq->eq_refs) {
185 CDEBUG(D_NET, "Event equeue (%d: %d) busy on destroy.\n",
191 /* stash for free after lock dropped */
192 events = eq->eq_events;
196 lnet_res_lh_invalidate(&eq->eq_lh);
197 list_del(&eq->eq_list);
198 lnet_eq_free_locked(eq);
200 lnet_eq_wait_unlock();
201 lnet_res_unlock(LNET_LOCK_EX);
204 LIBCFS_FREE(events, size * sizeof(lnet_event_t));
206 cfs_percpt_free(refs);
210 EXPORT_SYMBOL(LNetEQFree);
213 lnet_eq_enqueue_event(lnet_eq_t *eq, lnet_event_t *ev)
215 /* MUST called with resource lock hold but w/o lnet_eq_wait_lock */
218 if (eq->eq_size == 0) {
219 LASSERT(eq->eq_callback != LNET_EQ_HANDLER_NONE);
225 ev->sequence = eq->eq_enq_seq++;
227 LASSERT(eq->eq_size == LOWEST_BIT_SET(eq->eq_size));
228 index = ev->sequence & (eq->eq_size - 1);
230 eq->eq_events[index] = *ev;
232 if (eq->eq_callback != LNET_EQ_HANDLER_NONE)
235 /* Wake anyone waiting in LNetEQPoll() */
236 if (waitqueue_active(&the_lnet.ln_eq_waitq))
237 wake_up_all(&the_lnet.ln_eq_waitq);
238 lnet_eq_wait_unlock();
242 lnet_eq_dequeue_event(lnet_eq_t *eq, lnet_event_t *ev)
244 int new_index = eq->eq_deq_seq & (eq->eq_size - 1);
245 lnet_event_t *new_event = &eq->eq_events[new_index];
249 /* must called with lnet_eq_wait_lock hold */
250 if (LNET_SEQ_GT(eq->eq_deq_seq, new_event->sequence))
253 /* We've got a new event... */
256 CDEBUG(D_INFO, "event: %p, sequence: %lu, eq->size: %u\n",
257 new_event, eq->eq_deq_seq, eq->eq_size);
259 /* ...but did it overwrite an event we've not seen yet? */
260 if (eq->eq_deq_seq == new_event->sequence) {
263 /* don't complain with CERROR: some EQs are sized small
264 * anyway; if it's important, the caller should complain */
265 CDEBUG(D_NET, "Event Queue Overflow: eq seq %lu ev seq %lu\n",
266 eq->eq_deq_seq, new_event->sequence);
270 eq->eq_deq_seq = new_event->sequence + 1;
275 * A nonblocking function that can be used to get the next event in an EQ.
276 * If an event handler is associated with the EQ, the handler will run before
277 * this function returns successfully. The event is removed from the queue.
279 * \param eventq A handle for the event queue.
280 * \param event On successful return (1 or -EOVERFLOW), this location will
281 * hold the next event in the EQ.
283 * \retval 0 No pending event in the EQ.
284 * \retval 1 Indicates success.
285 * \retval -ENOENT If \a eventq does not point to a valid EQ.
286 * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that
287 * at least one event between this event and the last event obtained from the
288 * EQ has been dropped due to limited space in the EQ.
291 LNetEQGet (lnet_handle_eq_t eventq, lnet_event_t *event)
295 return LNetEQPoll(&eventq, 1, 0,
298 EXPORT_SYMBOL(LNetEQGet);
301 * Block the calling process until there is an event in the EQ.
302 * If an event handler is associated with the EQ, the handler will run before
303 * this function returns successfully. This function returns the next event
304 * in the EQ and removes it from the EQ.
306 * \param eventq A handle for the event queue.
307 * \param event On successful return (1 or -EOVERFLOW), this location will
308 * hold the next event in the EQ.
310 * \retval 1 Indicates success.
311 * \retval -ENOENT If \a eventq does not point to a valid EQ.
312 * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that
313 * at least one event between this event and the last event obtained from the
314 * EQ has been dropped due to limited space in the EQ.
317 LNetEQWait (lnet_handle_eq_t eventq, lnet_event_t *event)
321 return LNetEQPoll(&eventq, 1, LNET_TIME_FOREVER,
324 EXPORT_SYMBOL(LNetEQWait);
328 lnet_eq_wait_locked(int *timeout_ms)
330 int tms = *timeout_ms;
336 return -1; /* don't want to wait and no new event */
338 init_waitqueue_entry_current(&wl);
339 set_current_state(TASK_INTERRUPTIBLE);
340 add_wait_queue(&the_lnet.ln_eq_waitq, &wl);
342 lnet_eq_wait_unlock();
345 waitq_wait(&wl, TASK_INTERRUPTIBLE);
350 now = cfs_time_current();
351 waitq_timedwait(&wl, TASK_INTERRUPTIBLE,
352 cfs_time_seconds(tms) / 1000);
353 cfs_duration_usec(cfs_time_sub(cfs_time_current(), now), &tv);
354 tms -= (int)(tv.tv_sec * 1000 + tv.tv_usec / 1000);
355 if (tms < 0) /* no more wait but may have new event */
359 wait = tms != 0; /* might need to call here again */
363 remove_wait_queue(&the_lnet.ln_eq_waitq, &wl);
371 * Block the calling process until there's an event from a set of EQs or
374 * If an event handler is associated with the EQ, the handler will run before
375 * this function returns successfully, in which case the corresponding event
378 * LNetEQPoll() provides a timeout to allow applications to poll, block for a
379 * fixed period, or block indefinitely.
381 * \param eventqs,neq An array of EQ handles, and size of the array.
382 * \param timeout_ms Time in milliseconds to wait for an event to occur on
383 * one of the EQs. The constant LNET_TIME_FOREVER can be used to indicate an
385 * \param event,which On successful return (1 or -EOVERFLOW), \a event will
386 * hold the next event in the EQs, and \a which will contain the index of the
387 * EQ from which the event was taken.
389 * \retval 0 No pending event in the EQs after timeout.
390 * \retval 1 Indicates success.
391 * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that
392 * at least one event between this event and the last event obtained from the
393 * EQ indicated by \a which has been dropped due to limited space in the EQ.
394 * \retval -ENOENT If there's an invalid handle in \a eventqs.
397 LNetEQPoll(lnet_handle_eq_t *eventqs, int neq, int timeout_ms,
398 lnet_event_t *event, int *which)
405 LASSERT (the_lnet.ln_init);
406 LASSERT (the_lnet.ln_refcount > 0);
414 for (i = 0; i < neq; i++) {
415 lnet_eq_t *eq = lnet_handle2eq(&eventqs[i]);
418 lnet_eq_wait_unlock();
422 rc = lnet_eq_dequeue_event(eq, event);
424 lnet_eq_wait_unlock();
434 * return value of lnet_eq_wait_locked:
435 * -1 : did nothing and it's sure no new event
436 * 1 : sleep inside and wait until new event
437 * 0 : don't want to wait anymore, but might have new event
438 * so need to call dequeue again
440 wait = lnet_eq_wait_locked(&timeout_ms);
441 if (wait < 0) /* no new event */
445 lnet_eq_wait_unlock();