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.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
36 * Library level Event queue management routines
39 #define DEBUG_SUBSYSTEM S_LNET
40 #include <lnet/lib-lnet.h>
43 * Create an event queue that has room for \a count number of events.
45 * The event queue is circular and older events will be overwritten by new
46 * ones if they are not removed in time by the user using the functions
47 * LNetEQGet(), LNetEQWait(), or LNetEQPoll(). It is up to the user to
48 * determine the appropriate size of the event queue to prevent this loss
49 * of events. Note that when EQ handler is specified in \a callback, no
50 * event loss can happen, since the handler is run for each event deposited
53 * \param count The number of events to be stored in the event queue. It
54 * will be rounded up to the next power of two.
55 * \param callback A handler function that runs when an event is deposited
56 * into the EQ. The constant value LNET_EQ_HANDLER_NONE can be used to
57 * indicate that no event handler is desired.
58 * \param handle On successful return, this location will hold a handle for
59 * the newly created EQ.
61 * \retval 0 On success.
62 * \retval -EINVAL If an parameter is not valid.
63 * \retval -ENOMEM If memory for the EQ can't be allocated.
65 * \see lnet_eq_handler_t for the discussion on EQ handler semantics.
68 LNetEQAlloc(unsigned int count, lnet_eq_handler_t callback,
69 lnet_handle_eq_t *handle)
73 LASSERT (the_lnet.ln_init);
74 LASSERT (the_lnet.ln_refcount > 0);
76 /* We need count to be a power of 2 so that when eq_{enq,deq}_seq
77 * overflow, they don't skip entries, so the queue has the same
78 * apparent capacity at all times */
80 count = cfs_power2_roundup(count);
82 if (callback != LNET_EQ_HANDLER_NONE && count != 0) {
83 CWARN("EQ callback is guaranteed to get every event, "
84 "do you still want to set eqcount %d for polling "
85 "event which will have locking overhead? "
86 "Please contact with developer to confirm\n", count);
89 /* count can be 0 if only need callback, we can eliminate
90 * overhead of enqueue event */
91 if (count == 0 && callback == LNET_EQ_HANDLER_NONE)
99 LIBCFS_ALLOC(eq->eq_events, count * sizeof(lnet_event_t));
100 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 */
112 eq->eq_callback = callback;
116 lnet_res_lh_initialize(&the_lnet.ln_eq_container, &eq->eq_lh);
117 cfs_list_add(&eq->eq_list, &the_lnet.ln_eq_container.rec_active);
121 lnet_eq2handle(handle, eq);
126 * Release the resources associated with an event queue if it's idle;
127 * otherwise do nothing and it's up to the user to try again.
129 * \param eqh A handle for the event queue to be released.
131 * \retval 0 If the EQ is not in use and freed.
132 * \retval -ENOENT If \a eqh does not point to a valid EQ.
133 * \retval -EBUSY If the EQ is still in use by some MDs.
136 LNetEQFree(lnet_handle_eq_t eqh)
140 lnet_event_t *events;
142 LASSERT (the_lnet.ln_init);
143 LASSERT (the_lnet.ln_refcount > 0);
147 eq = lnet_handle2eq(&eqh);
153 if (eq->eq_refcount != 0) {
154 CDEBUG(D_NET, "Event queue (%d) busy on destroy.\n",
160 /* stash for free after lock dropped */
161 events = eq->eq_events;
164 lnet_res_lh_invalidate(&eq->eq_lh);
165 cfs_list_del (&eq->eq_list);
166 lnet_eq_free_locked(eq);
171 LIBCFS_FREE(events, size * sizeof(lnet_event_t));
177 lnet_eq_enqueue_event(lnet_eq_t *eq, lnet_event_t *ev)
179 /* MUST called with resource lock hold */
182 if (eq->eq_size == 0) {
183 LASSERT(eq->eq_callback != LNET_EQ_HANDLER_NONE);
188 ev->sequence = eq->eq_enq_seq++;
190 LASSERT(eq->eq_size == LOWEST_BIT_SET(eq->eq_size));
191 index = ev->sequence & (eq->eq_size - 1);
193 eq->eq_events[index] = *ev;
195 if (eq->eq_callback != LNET_EQ_HANDLER_NONE)
199 /* Wake anyone waiting in LNetEQPoll() */
200 if (cfs_waitq_active(&the_lnet.ln_eq_waitq))
201 cfs_waitq_broadcast(&the_lnet.ln_eq_waitq);
203 # ifndef HAVE_LIBPTHREAD
204 /* LNetEQPoll() calls into _the_ LND to wait for action */
206 /* Wake anyone waiting in LNetEQPoll() */
207 pthread_cond_broadcast(&the_lnet.ln_eq_cond);
213 lnet_eq_dequeue_event(lnet_eq_t *eq, lnet_event_t *ev)
215 int new_index = eq->eq_deq_seq & (eq->eq_size - 1);
216 lnet_event_t *new_event = &eq->eq_events[new_index];
220 if (LNET_SEQ_GT (eq->eq_deq_seq, new_event->sequence)) {
224 /* We've got a new event... */
227 CDEBUG(D_INFO, "event: %p, sequence: %lu, eq->size: %u\n",
228 new_event, eq->eq_deq_seq, eq->eq_size);
230 /* ...but did it overwrite an event we've not seen yet? */
231 if (eq->eq_deq_seq == new_event->sequence) {
234 /* don't complain with CERROR: some EQs are sized small
235 * anyway; if it's important, the caller should complain */
236 CDEBUG(D_NET, "Event Queue Overflow: eq seq %lu ev seq %lu\n",
237 eq->eq_deq_seq, new_event->sequence);
241 eq->eq_deq_seq = new_event->sequence + 1;
246 * A nonblocking function that can be used to get the next event in an EQ.
247 * If an event handler is associated with the EQ, the handler will run before
248 * this function returns successfully. The event is removed from the queue.
250 * \param eventq A handle for the event queue.
251 * \param event On successful return (1 or -EOVERFLOW), this location will
252 * hold the next event in the EQ.
254 * \retval 0 No pending event in the EQ.
255 * \retval 1 Indicates success.
256 * \retval -ENOENT If \a eventq does not point to a valid EQ.
257 * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that
258 * at least one event between this event and the last event obtained from the
259 * EQ has been dropped due to limited space in the EQ.
262 LNetEQGet (lnet_handle_eq_t eventq, lnet_event_t *event)
266 return LNetEQPoll(&eventq, 1, 0,
271 * Block the calling process until there is an event in the EQ.
272 * If an event handler is associated with the EQ, the handler will run before
273 * this function returns successfully. This function returns the next event
274 * in the EQ and removes it from the EQ.
276 * \param eventq A handle for the event queue.
277 * \param event On successful return (1 or -EOVERFLOW), this location will
278 * hold the next event in the EQ.
280 * \retval 1 Indicates success.
281 * \retval -ENOENT If \a eventq does not point to a valid EQ.
282 * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that
283 * at least one event between this event and the last event obtained from the
284 * EQ has been dropped due to limited space in the EQ.
287 LNetEQWait (lnet_handle_eq_t eventq, lnet_event_t *event)
291 return LNetEQPoll(&eventq, 1, LNET_TIME_FOREVER,
298 lnet_eq_wait_locked(int *timeout_ms)
300 int tms = *timeout_ms;
306 return -1; /* don't want to wait and no new event */
308 cfs_waitlink_init(&wl);
309 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
310 cfs_waitq_add(&the_lnet.ln_eq_waitq, &wl);
315 cfs_waitq_wait(&wl, CFS_TASK_INTERRUPTIBLE);
320 now = cfs_time_current();
321 cfs_waitq_timedwait(&wl, CFS_TASK_INTERRUPTIBLE,
322 cfs_time_seconds(tms) / 1000);
323 cfs_duration_usec(cfs_time_sub(cfs_time_current(), now), &tv);
324 tms -= (int)(tv.tv_sec * 1000 + tv.tv_usec / 1000);
325 if (tms < 0) /* no more wait but may have new event */
329 wait = tms != 0; /* might need to call here again */
333 cfs_waitq_del(&the_lnet.ln_eq_waitq, &wl);
338 #else /* !__KERNEL__ */
340 # ifdef HAVE_LIBPTHREAD
342 lnet_eq_cond_wait(struct timespec *ts)
345 pthread_cond_wait(&the_lnet.ln_eq_cond, &the_lnet.ln_lock);
347 pthread_cond_timedwait(&the_lnet.ln_eq_cond,
348 &the_lnet.ln_lock, ts);
354 lnet_eq_wait_locked(int *timeout_ms)
356 lnet_ni_t *eq_waitni = NULL;
357 int tms = *timeout_ms;
362 if (the_lnet.ln_eq_waitni != NULL) {
363 /* I have a single NI that I have to call into, to get
364 * events queued, or to block. */
365 eq_waitni = the_lnet.ln_eq_waitni;
366 lnet_ni_addref_locked(eq_waitni);
370 if (tms <= 0) { /* even for tms == 0 */
371 (eq_waitni->ni_lnd->lnd_wait)(eq_waitni, tms);
374 gettimeofday(&then, NULL);
376 (eq_waitni->ni_lnd->lnd_wait)(eq_waitni, tms);
378 gettimeofday(&now, NULL);
379 tms -= (now.tv_sec - then.tv_sec) * 1000 +
380 (now.tv_usec - then.tv_usec) / 1000;
386 lnet_ni_decref_locked(eq_waitni);
387 } else { /* w/o eq_waitni */
388 # ifndef HAVE_LIBPTHREAD
389 /* If I'm single-threaded, LNET fails at startup if it can't
390 * set the_lnet.ln_eqwaitni correctly. */
392 # else /* HAVE_LIBPTHREAD */
395 if (tms == 0) /* don't want to wait and new event */
399 lnet_eq_cond_wait(NULL);
403 gettimeofday(&then, NULL);
405 ts.tv_sec = then.tv_sec + tms / 1000;
406 ts.tv_nsec = then.tv_usec * 1000 +
407 (tms % 1000) * 1000000;
408 if (ts.tv_nsec >= 1000000000) {
410 ts.tv_nsec -= 1000000000;
413 lnet_eq_cond_wait(&ts);
415 gettimeofday(&now, NULL);
416 tms -= (now.tv_sec - then.tv_sec) * 1000 +
417 (now.tv_usec - then.tv_usec) / 1000;
421 # endif /* HAVE_LIBPTHREAD */
430 #endif /* __KERNEL__ */
434 * Block the calling process until there's an event from a set of EQs or
437 * If an event handler is associated with the EQ, the handler will run before
438 * this function returns successfully, in which case the corresponding event
441 * LNetEQPoll() provides a timeout to allow applications to poll, block for a
442 * fixed period, or block indefinitely.
444 * \param eventqs,neq An array of EQ handles, and size of the array.
445 * \param timeout_ms Time in milliseconds to wait for an event to occur on
446 * one of the EQs. The constant LNET_TIME_FOREVER can be used to indicate an
448 * \param event,which On successful return (1 or -EOVERFLOW), \a event will
449 * hold the next event in the EQs, and \a which will contain the index of the
450 * EQ from which the event was taken.
452 * \retval 0 No pending event in the EQs after timeout.
453 * \retval 1 Indicates success.
454 * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that
455 * at least one event between this event and the last event obtained from the
456 * EQ indicated by \a which has been dropped due to limited space in the EQ.
457 * \retval -ENOENT If there's an invalid handle in \a eventqs.
460 LNetEQPoll (lnet_handle_eq_t *eventqs, int neq, int timeout_ms,
461 lnet_event_t *event, int *which)
468 LASSERT (the_lnet.ln_init);
469 LASSERT (the_lnet.ln_refcount > 0);
480 /* Recursion breaker */
481 if (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING &&
482 !LNetHandleIsEqual(eventqs[0], the_lnet.ln_rc_eqh))
483 lnet_router_checker();
487 for (i = 0; i < neq; i++) {
488 lnet_eq_t *eq = lnet_handle2eq(&eventqs[i]);
495 rc = lnet_eq_dequeue_event(eq, event);
507 * return value of lnet_eq_wait_locked:
508 * -1 : did nothing and it's sure no new event
509 * 1 : sleep inside and wait until new event
510 * 0 : don't want to wait anymore, but might have new event
511 * so need to call dequeue again
513 wait = lnet_eq_wait_locked(&timeout_ms);
514 if (wait < 0) /* no new event */