Whamcloud - gitweb
LU-1711 mount: obd_mount to start osd
[fs/lustre-release.git] / lnet / lnet / lib-eq.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lnet/lnet/lib-eq.c
35  *
36  * Library level Event queue management routines
37  */
38
39 #define DEBUG_SUBSYSTEM S_LNET
40 #include <lnet/lib-lnet.h>
41
42 /**
43  * Create an event queue that has room for \a count number of events.
44  *
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
51  * into the EQ.
52  *
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.
60  *
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.
64  *
65  * \see lnet_eq_handler_t for the discussion on EQ handler semantics.
66  */
67 int
68 LNetEQAlloc(unsigned int count, lnet_eq_handler_t callback,
69             lnet_handle_eq_t *handle)
70 {
71         lnet_eq_t     *eq;
72
73         LASSERT (the_lnet.ln_init);
74         LASSERT (the_lnet.ln_refcount > 0);
75
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 */
79
80         count = cfs_power2_roundup(count);
81
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);
87         }
88
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)
92                 return -EINVAL;
93
94         eq = lnet_eq_alloc();
95         if (eq == NULL)
96                 return -ENOMEM;
97
98         if (count != 0) {
99                 LIBCFS_ALLOC(eq->eq_events, count * sizeof(lnet_event_t));
100                 if (eq->eq_events == NULL)
101                         goto failed;
102                 /* NB allocator has set all event sequence numbers to 0,
103                  * so all them should be earlier than eq_deq_seq */
104         }
105
106         eq->eq_deq_seq = 1;
107         eq->eq_enq_seq = 1;
108         eq->eq_size = count;
109         eq->eq_callback = callback;
110
111         eq->eq_refs = cfs_percpt_alloc(lnet_cpt_table(),
112                                        sizeof(*eq->eq_refs[0]));
113         if (eq->eq_refs == NULL)
114                 goto failed;
115
116         /* MUST hold both exclusive lnet_res_lock */
117         lnet_res_lock(LNET_LOCK_EX);
118         /* NB: hold lnet_eq_wait_lock for EQ link/unlink, so we can do
119          * both EQ lookup and poll event with only lnet_eq_wait_lock */
120         lnet_eq_wait_lock();
121
122         lnet_res_lh_initialize(&the_lnet.ln_eq_container, &eq->eq_lh);
123         cfs_list_add(&eq->eq_list, &the_lnet.ln_eq_container.rec_active);
124
125         lnet_eq_wait_unlock();
126         lnet_res_unlock(LNET_LOCK_EX);
127
128         lnet_eq2handle(handle, eq);
129         return 0;
130
131 failed:
132         if (eq->eq_events != NULL)
133                 LIBCFS_FREE(eq->eq_events, count * sizeof(lnet_event_t));
134
135         if (eq->eq_refs != NULL)
136                 cfs_percpt_free(eq->eq_refs);
137
138         lnet_eq_free(eq);
139         return -ENOMEM;
140 }
141 EXPORT_SYMBOL(LNetEQAlloc);
142
143 /**
144  * Release the resources associated with an event queue if it's idle;
145  * otherwise do nothing and it's up to the user to try again.
146  *
147  * \param eqh A handle for the event queue to be released.
148  *
149  * \retval 0 If the EQ is not in use and freed.
150  * \retval -ENOENT If \a eqh does not point to a valid EQ.
151  * \retval -EBUSY  If the EQ is still in use by some MDs.
152  */
153 int
154 LNetEQFree(lnet_handle_eq_t eqh)
155 {
156         struct lnet_eq  *eq;
157         lnet_event_t    *events = NULL;
158         int             **refs = NULL;
159         int             *ref;
160         int             rc = 0;
161         int             size = 0;
162         int             i;
163
164         LASSERT(the_lnet.ln_init);
165         LASSERT(the_lnet.ln_refcount > 0);
166
167         lnet_res_lock(LNET_LOCK_EX);
168         /* NB: hold lnet_eq_wait_lock for EQ link/unlink, so we can do
169          * both EQ lookup and poll event with only lnet_eq_wait_lock */
170         lnet_eq_wait_lock();
171
172         eq = lnet_handle2eq(&eqh);
173         if (eq == NULL) {
174                 rc = -ENOENT;
175                 goto out;
176         }
177
178         cfs_percpt_for_each(ref, i, eq->eq_refs) {
179                 LASSERT(*ref >= 0);
180                 if (*ref == 0)
181                         continue;
182
183                 CDEBUG(D_NET, "Event equeue (%d: %d) busy on destroy.\n",
184                        i, *ref);
185                 rc = -EBUSY;
186                 goto out;
187         }
188
189         /* stash for free after lock dropped */
190         events  = eq->eq_events;
191         size    = eq->eq_size;
192         refs    = eq->eq_refs;
193
194         lnet_res_lh_invalidate(&eq->eq_lh);
195         cfs_list_del(&eq->eq_list);
196         lnet_eq_free_locked(eq);
197  out:
198         lnet_eq_wait_unlock();
199         lnet_res_unlock(LNET_LOCK_EX);
200
201         if (events != NULL)
202                 LIBCFS_FREE(events, size * sizeof(lnet_event_t));
203         if (refs != NULL)
204                 cfs_percpt_free(refs);
205
206         return rc;
207 }
208 EXPORT_SYMBOL(LNetEQFree);
209
210 void
211 lnet_eq_enqueue_event(lnet_eq_t *eq, lnet_event_t *ev)
212 {
213         /* MUST called with resource lock hold but w/o lnet_eq_wait_lock */
214         int index;
215
216         if (eq->eq_size == 0) {
217                 LASSERT(eq->eq_callback != LNET_EQ_HANDLER_NONE);
218                 eq->eq_callback(ev);
219                 return;
220         }
221
222         lnet_eq_wait_lock();
223         ev->sequence = eq->eq_enq_seq++;
224
225         LASSERT(eq->eq_size == LOWEST_BIT_SET(eq->eq_size));
226         index = ev->sequence & (eq->eq_size - 1);
227
228         eq->eq_events[index] = *ev;
229
230         if (eq->eq_callback != LNET_EQ_HANDLER_NONE)
231                 eq->eq_callback(ev);
232
233 #ifdef __KERNEL__
234         /* Wake anyone waiting in LNetEQPoll() */
235         if (cfs_waitq_active(&the_lnet.ln_eq_waitq))
236                 cfs_waitq_broadcast(&the_lnet.ln_eq_waitq);
237 #else
238 # ifndef HAVE_LIBPTHREAD
239         /* LNetEQPoll() calls into _the_ LND to wait for action */
240 # else
241         /* Wake anyone waiting in LNetEQPoll() */
242         pthread_cond_broadcast(&the_lnet.ln_eq_cond);
243 # endif
244 #endif
245         lnet_eq_wait_unlock();
246 }
247
248 int
249 lnet_eq_dequeue_event(lnet_eq_t *eq, lnet_event_t *ev)
250 {
251         int             new_index = eq->eq_deq_seq & (eq->eq_size - 1);
252         lnet_event_t    *new_event = &eq->eq_events[new_index];
253         int             rc;
254         ENTRY;
255
256         /* must called with lnet_eq_wait_lock hold */
257         if (LNET_SEQ_GT(eq->eq_deq_seq, new_event->sequence))
258                 RETURN(0);
259
260         /* We've got a new event... */
261         *ev = *new_event;
262
263         CDEBUG(D_INFO, "event: %p, sequence: %lu, eq->size: %u\n",
264                new_event, eq->eq_deq_seq, eq->eq_size);
265
266         /* ...but did it overwrite an event we've not seen yet? */
267         if (eq->eq_deq_seq == new_event->sequence) {
268                 rc = 1;
269         } else {
270                 /* don't complain with CERROR: some EQs are sized small
271                  * anyway; if it's important, the caller should complain */
272                 CDEBUG(D_NET, "Event Queue Overflow: eq seq %lu ev seq %lu\n",
273                        eq->eq_deq_seq, new_event->sequence);
274                 rc = -EOVERFLOW;
275         }
276
277         eq->eq_deq_seq = new_event->sequence + 1;
278         RETURN(rc);
279 }
280
281 /**
282  * A nonblocking function that can be used to get the next event in an EQ.
283  * If an event handler is associated with the EQ, the handler will run before
284  * this function returns successfully. The event is removed from the queue.
285  *
286  * \param eventq A handle for the event queue.
287  * \param event On successful return (1 or -EOVERFLOW), this location will
288  * hold the next event in the EQ.
289  *
290  * \retval 0          No pending event in the EQ.
291  * \retval 1          Indicates success.
292  * \retval -ENOENT    If \a eventq does not point to a valid EQ.
293  * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that
294  * at least one event between this event and the last event obtained from the
295  * EQ has been dropped due to limited space in the EQ.
296  */
297 int
298 LNetEQGet (lnet_handle_eq_t eventq, lnet_event_t *event)
299 {
300         int which;
301
302         return LNetEQPoll(&eventq, 1, 0,
303                          event, &which);
304 }
305 EXPORT_SYMBOL(LNetEQGet);
306
307 /**
308  * Block the calling process until there is an event in the EQ.
309  * If an event handler is associated with the EQ, the handler will run before
310  * this function returns successfully. This function returns the next event
311  * in the EQ and removes it from the EQ.
312  *
313  * \param eventq A handle for the event queue.
314  * \param event On successful return (1 or -EOVERFLOW), this location will
315  * hold the next event in the EQ.
316  *
317  * \retval 1          Indicates success.
318  * \retval -ENOENT    If \a eventq does not point to a valid EQ.
319  * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that
320  * at least one event between this event and the last event obtained from the
321  * EQ has been dropped due to limited space in the EQ.
322  */
323 int
324 LNetEQWait (lnet_handle_eq_t eventq, lnet_event_t *event)
325 {
326         int which;
327
328         return LNetEQPoll(&eventq, 1, LNET_TIME_FOREVER,
329                          event, &which);
330 }
331 EXPORT_SYMBOL(LNetEQWait);
332
333 #ifdef __KERNEL__
334
335 static int
336 lnet_eq_wait_locked(int *timeout_ms)
337 {
338         int             tms = *timeout_ms;
339         int             wait;
340         cfs_waitlink_t  wl;
341         cfs_time_t      now;
342
343         if (tms == 0)
344                 return -1; /* don't want to wait and no new event */
345
346         cfs_waitlink_init(&wl);
347         cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
348         cfs_waitq_add(&the_lnet.ln_eq_waitq, &wl);
349
350         lnet_eq_wait_unlock();
351
352         if (tms < 0) {
353                 cfs_waitq_wait(&wl, CFS_TASK_INTERRUPTIBLE);
354
355         } else {
356                 struct timeval tv;
357
358                 now = cfs_time_current();
359                 cfs_waitq_timedwait(&wl, CFS_TASK_INTERRUPTIBLE,
360                                     cfs_time_seconds(tms) / 1000);
361                 cfs_duration_usec(cfs_time_sub(cfs_time_current(), now), &tv);
362                 tms -= (int)(tv.tv_sec * 1000 + tv.tv_usec / 1000);
363                 if (tms < 0) /* no more wait but may have new event */
364                         tms = 0;
365         }
366
367         wait = tms != 0; /* might need to call here again */
368         *timeout_ms = tms;
369
370         lnet_eq_wait_lock();
371         cfs_waitq_del(&the_lnet.ln_eq_waitq, &wl);
372
373         return wait;
374 }
375
376 #else /* !__KERNEL__ */
377
378 # ifdef HAVE_LIBPTHREAD
379 static void
380 lnet_eq_cond_wait(struct timespec *ts)
381 {
382         if (ts == NULL) {
383                 pthread_cond_wait(&the_lnet.ln_eq_cond,
384                                   &the_lnet.ln_eq_wait_lock);
385         } else {
386                 pthread_cond_timedwait(&the_lnet.ln_eq_cond,
387                                        &the_lnet.ln_eq_wait_lock, ts);
388         }
389 }
390 # endif
391
392 static int
393 lnet_eq_wait_locked(int *timeout_ms)
394 {
395         lnet_ni_t       *eq_waitni = NULL;
396         int             tms = *timeout_ms;
397         int             wait;
398         struct timeval  then;
399         struct timeval  now;
400
401         if (the_lnet.ln_eq_waitni != NULL) {
402                 /* I have a single NI that I have to call into, to get
403                  * events queued, or to block. */
404                 lnet_eq_wait_unlock();
405
406                 lnet_net_lock(0);
407                 eq_waitni = the_lnet.ln_eq_waitni;
408                 if (unlikely(eq_waitni == NULL)) {
409                         lnet_net_unlock(0);
410
411                         lnet_eq_wait_lock();
412                         return -1;
413                 }
414
415                 lnet_ni_addref_locked(eq_waitni, 0);
416                 lnet_net_unlock(0);
417
418                 if (tms <= 0) { /* even for tms == 0 */
419                         (eq_waitni->ni_lnd->lnd_wait)(eq_waitni, tms);
420
421                 } else {
422                         gettimeofday(&then, NULL);
423
424                         (eq_waitni->ni_lnd->lnd_wait)(eq_waitni, tms);
425
426                         gettimeofday(&now, NULL);
427                         tms -= (now.tv_sec - then.tv_sec) * 1000 +
428                                (now.tv_usec - then.tv_usec) / 1000;
429                         if (tms < 0)
430                                 tms = 0;
431                 }
432
433                 lnet_ni_decref(eq_waitni);
434                 lnet_eq_wait_lock();
435         } else { /* w/o eq_waitni */
436 # ifndef HAVE_LIBPTHREAD
437                 /* If I'm single-threaded, LNET fails at startup if it can't
438                  * set the_lnet.ln_eqwaitni correctly.  */
439                 LBUG();
440 # else /* HAVE_LIBPTHREAD */
441                 struct timespec  ts;
442
443                 if (tms == 0) /* don't want to wait and new event */
444                         return -1;
445
446                 if (tms < 0) {
447                         lnet_eq_cond_wait(NULL);
448
449                 } else {
450
451                         gettimeofday(&then, NULL);
452
453                         ts.tv_sec = then.tv_sec + tms / 1000;
454                         ts.tv_nsec = then.tv_usec * 1000 +
455                                      (tms % 1000) * 1000000;
456                         if (ts.tv_nsec >= 1000000000) {
457                                 ts.tv_sec++;
458                                 ts.tv_nsec -= 1000000000;
459                         }
460
461                         lnet_eq_cond_wait(&ts);
462
463                         gettimeofday(&now, NULL);
464                         tms -= (now.tv_sec - then.tv_sec) * 1000 +
465                                (now.tv_usec - then.tv_usec) / 1000;
466                         if (tms < 0)
467                                 tms = 0;
468                 }
469 # endif /* HAVE_LIBPTHREAD */
470         }
471
472         wait = tms != 0;
473         *timeout_ms = tms;
474
475         return wait;
476 }
477
478 #endif /* __KERNEL__ */
479
480
481 /**
482  * Block the calling process until there's an event from a set of EQs or
483  * timeout happens.
484  *
485  * If an event handler is associated with the EQ, the handler will run before
486  * this function returns successfully, in which case the corresponding event
487  * is consumed.
488  *
489  * LNetEQPoll() provides a timeout to allow applications to poll, block for a
490  * fixed period, or block indefinitely.
491  *
492  * \param eventqs,neq An array of EQ handles, and size of the array.
493  * \param timeout_ms Time in milliseconds to wait for an event to occur on
494  * one of the EQs. The constant LNET_TIME_FOREVER can be used to indicate an
495  * infinite timeout.
496  * \param event,which On successful return (1 or -EOVERFLOW), \a event will
497  * hold the next event in the EQs, and \a which will contain the index of the
498  * EQ from which the event was taken.
499  *
500  * \retval 0          No pending event in the EQs after timeout.
501  * \retval 1          Indicates success.
502  * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that
503  * at least one event between this event and the last event obtained from the
504  * EQ indicated by \a which has been dropped due to limited space in the EQ.
505  * \retval -ENOENT    If there's an invalid handle in \a eventqs.
506  */
507 int
508 LNetEQPoll(lnet_handle_eq_t *eventqs, int neq, int timeout_ms,
509            lnet_event_t *event, int *which)
510 {
511         int     wait = 1;
512         int     rc;
513         int     i;
514         ENTRY;
515
516         LASSERT (the_lnet.ln_init);
517         LASSERT (the_lnet.ln_refcount > 0);
518
519         if (neq < 1)
520                 RETURN(-ENOENT);
521
522         lnet_eq_wait_lock();
523
524         for (;;) {
525 #ifndef __KERNEL__
526                 lnet_eq_wait_unlock();
527
528                 /* Recursion breaker */
529                 if (the_lnet.ln_rc_state == LNET_RC_STATE_RUNNING &&
530                     !LNetHandleIsEqual(eventqs[0], the_lnet.ln_rc_eqh))
531                         lnet_router_checker();
532
533                 lnet_eq_wait_lock();
534 #endif
535                 for (i = 0; i < neq; i++) {
536                         lnet_eq_t *eq = lnet_handle2eq(&eventqs[i]);
537
538                         if (eq == NULL) {
539                                 lnet_eq_wait_unlock();
540                                 RETURN(-ENOENT);
541                         }
542
543                         rc = lnet_eq_dequeue_event(eq, event);
544                         if (rc != 0) {
545                                 lnet_eq_wait_unlock();
546                                 *which = i;
547                                 RETURN(rc);
548                         }
549                 }
550
551                 if (wait == 0)
552                         break;
553
554                 /*
555                  * return value of lnet_eq_wait_locked:
556                  * -1 : did nothing and it's sure no new event
557                  *  1 : sleep inside and wait until new event
558                  *  0 : don't want to wait anymore, but might have new event
559                  *      so need to call dequeue again
560                  */
561                 wait = lnet_eq_wait_locked(&timeout_ms);
562                 if (wait < 0) /* no new event */
563                         break;
564         }
565
566         lnet_eq_wait_unlock();
567         RETURN(0);
568 }