Whamcloud - gitweb
8e95154be2109d413530b884ae8e46de6019bc4d
[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  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lnet/lnet/lib-eq.c
37  *
38  * Library level Event queue management routines
39  */
40
41 #define DEBUG_SUBSYSTEM S_LNET
42 #include <lnet/lib-lnet.h>
43
44 /**
45  * Create an event queue that has room for \a count number of events.
46  *
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
53  * into the EQ.
54  *
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.
62  *
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.
66  *
67  * \see lnet_eq_handler_t for the discussion on EQ handler semantics.
68  */
69 int
70 LNetEQAlloc(unsigned int count, lnet_eq_handler_t callback,
71             lnet_handle_eq_t *handle)
72 {
73         lnet_eq_t     *eq;
74
75         LASSERT (the_lnet.ln_refcount > 0);
76
77         /* We need count to be a power of 2 so that when eq_{enq,deq}_seq
78          * overflow, they don't skip entries, so the queue has the same
79          * apparent capacity at all times */
80
81         if (count)
82                 count = roundup_pow_of_two(count);
83
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);
89         }
90
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)
94                 return -EINVAL;
95
96         eq = lnet_eq_alloc();
97         if (eq == NULL)
98                 return -ENOMEM;
99
100         if (count != 0) {
101                 LIBCFS_ALLOC(eq->eq_events, count * sizeof(lnet_event_t));
102                 if (eq->eq_events == NULL)
103                         goto failed;
104                 /* NB allocator has set all event sequence numbers to 0,
105                  * so all them should be earlier than eq_deq_seq */
106         }
107
108         eq->eq_deq_seq = 1;
109         eq->eq_enq_seq = 1;
110         eq->eq_size = count;
111         eq->eq_callback = callback;
112
113         eq->eq_refs = cfs_percpt_alloc(lnet_cpt_table(),
114                                        sizeof(*eq->eq_refs[0]));
115         if (eq->eq_refs == NULL)
116                 goto failed;
117
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 */
122         lnet_eq_wait_lock();
123
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);
126
127         lnet_eq_wait_unlock();
128         lnet_res_unlock(LNET_LOCK_EX);
129
130         lnet_eq2handle(handle, eq);
131         return 0;
132
133 failed:
134         if (eq->eq_events != NULL)
135                 LIBCFS_FREE(eq->eq_events, count * sizeof(lnet_event_t));
136
137         if (eq->eq_refs != NULL)
138                 cfs_percpt_free(eq->eq_refs);
139
140         lnet_eq_free(eq);
141         return -ENOMEM;
142 }
143 EXPORT_SYMBOL(LNetEQAlloc);
144
145 /**
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.
148  *
149  * \param eqh A handle for the event queue to be released.
150  *
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.
154  */
155 int
156 LNetEQFree(lnet_handle_eq_t eqh)
157 {
158         struct lnet_eq  *eq;
159         lnet_event_t    *events = NULL;
160         int             **refs = NULL;
161         int             *ref;
162         int             rc = 0;
163         int             size = 0;
164         int             i;
165
166         LASSERT(the_lnet.ln_refcount > 0);
167
168         lnet_res_lock(LNET_LOCK_EX);
169         /* NB: hold lnet_eq_wait_lock for EQ link/unlink, so we can do
170          * both EQ lookup and poll event with only lnet_eq_wait_lock */
171         lnet_eq_wait_lock();
172
173         eq = lnet_handle2eq(&eqh);
174         if (eq == NULL) {
175                 rc = -ENOENT;
176                 goto out;
177         }
178
179         cfs_percpt_for_each(ref, i, eq->eq_refs) {
180                 LASSERT(*ref >= 0);
181                 if (*ref == 0)
182                         continue;
183
184                 CDEBUG(D_NET, "Event equeue (%d: %d) busy on destroy.\n",
185                        i, *ref);
186                 rc = -EBUSY;
187                 goto out;
188         }
189
190         /* stash for free after lock dropped */
191         events  = eq->eq_events;
192         size    = eq->eq_size;
193         refs    = eq->eq_refs;
194
195         lnet_res_lh_invalidate(&eq->eq_lh);
196         list_del(&eq->eq_list);
197         lnet_eq_free(eq);
198  out:
199         lnet_eq_wait_unlock();
200         lnet_res_unlock(LNET_LOCK_EX);
201
202         if (events != NULL)
203                 LIBCFS_FREE(events, size * sizeof(lnet_event_t));
204         if (refs != NULL)
205                 cfs_percpt_free(refs);
206
207         return rc;
208 }
209 EXPORT_SYMBOL(LNetEQFree);
210
211 void
212 lnet_eq_enqueue_event(lnet_eq_t *eq, lnet_event_t *ev)
213 {
214         /* MUST called with resource lock hold but w/o lnet_eq_wait_lock */
215         int index;
216
217         if (eq->eq_size == 0) {
218                 LASSERT(eq->eq_callback != LNET_EQ_HANDLER_NONE);
219                 eq->eq_callback(ev);
220                 return;
221         }
222
223         lnet_eq_wait_lock();
224         ev->sequence = eq->eq_enq_seq++;
225
226         LASSERT(eq->eq_size == LOWEST_BIT_SET(eq->eq_size));
227         index = ev->sequence & (eq->eq_size - 1);
228
229         eq->eq_events[index] = *ev;
230
231         if (eq->eq_callback != LNET_EQ_HANDLER_NONE)
232                 eq->eq_callback(ev);
233
234         /* Wake anyone waiting in LNetEQPoll() */
235         if (waitqueue_active(&the_lnet.ln_eq_waitq))
236                 wake_up_all(&the_lnet.ln_eq_waitq);
237         lnet_eq_wait_unlock();
238 }
239
240 static int
241 lnet_eq_dequeue_event(lnet_eq_t *eq, lnet_event_t *ev)
242 {
243         int             new_index = eq->eq_deq_seq & (eq->eq_size - 1);
244         lnet_event_t    *new_event = &eq->eq_events[new_index];
245         int             rc;
246         ENTRY;
247
248         /* must called with lnet_eq_wait_lock hold */
249         if (LNET_SEQ_GT(eq->eq_deq_seq, new_event->sequence))
250                 RETURN(0);
251
252         /* We've got a new event... */
253         *ev = *new_event;
254
255         CDEBUG(D_INFO, "event: %p, sequence: %lu, eq->size: %u\n",
256                new_event, eq->eq_deq_seq, eq->eq_size);
257
258         /* ...but did it overwrite an event we've not seen yet? */
259         if (eq->eq_deq_seq == new_event->sequence) {
260                 rc = 1;
261         } else {
262                 /* don't complain with CERROR: some EQs are sized small
263                  * anyway; if it's important, the caller should complain */
264                 CDEBUG(D_NET, "Event Queue Overflow: eq seq %lu ev seq %lu\n",
265                        eq->eq_deq_seq, new_event->sequence);
266                 rc = -EOVERFLOW;
267         }
268
269         eq->eq_deq_seq = new_event->sequence + 1;
270         RETURN(rc);
271 }
272
273 /**
274  * A nonblocking function that can be used to get the next event in an EQ.
275  * If an event handler is associated with the EQ, the handler will run before
276  * this function returns successfully. The event is removed from the queue.
277  *
278  * \param eventq A handle for the event queue.
279  * \param event On successful return (1 or -EOVERFLOW), this location will
280  * hold the next event in the EQ.
281  *
282  * \retval 0          No pending event in the EQ.
283  * \retval 1          Indicates success.
284  * \retval -ENOENT    If \a eventq does not point to a valid EQ.
285  * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that
286  * at least one event between this event and the last event obtained from the
287  * EQ has been dropped due to limited space in the EQ.
288  */
289 int
290 LNetEQGet (lnet_handle_eq_t eventq, lnet_event_t *event)
291 {
292         int which;
293
294         return LNetEQPoll(&eventq, 1, 0,
295                          event, &which);
296 }
297 EXPORT_SYMBOL(LNetEQGet);
298
299 /**
300  * Block the calling process until there is an event in the EQ.
301  * If an event handler is associated with the EQ, the handler will run before
302  * this function returns successfully. This function returns the next event
303  * in the EQ and removes it from the EQ.
304  *
305  * \param eventq A handle for the event queue.
306  * \param event On successful return (1 or -EOVERFLOW), this location will
307  * hold the next event in the EQ.
308  *
309  * \retval 1          Indicates success.
310  * \retval -ENOENT    If \a eventq does not point to a valid EQ.
311  * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that
312  * at least one event between this event and the last event obtained from the
313  * EQ has been dropped due to limited space in the EQ.
314  */
315 int
316 LNetEQWait (lnet_handle_eq_t eventq, lnet_event_t *event)
317 {
318         int which;
319
320         return LNetEQPoll(&eventq, 1, LNET_TIME_FOREVER,
321                          event, &which);
322 }
323 EXPORT_SYMBOL(LNetEQWait);
324
325 static int
326 lnet_eq_wait_locked(int *timeout_ms)
327 __must_hold(&the_lnet.ln_eq_wait_lock)
328 {
329         int             tms = *timeout_ms;
330         int             wait;
331         wait_queue_t    wl;
332         cfs_time_t      now;
333
334         if (tms == 0)
335                 return -ENXIO; /* don't want to wait and no new event */
336
337         init_waitqueue_entry(&wl, current);
338         set_current_state(TASK_INTERRUPTIBLE);
339         add_wait_queue(&the_lnet.ln_eq_waitq, &wl);
340
341         lnet_eq_wait_unlock();
342
343         if (tms < 0) {
344                 schedule();
345         } else {
346                 struct timeval tv;
347
348                 now = cfs_time_current();
349                 schedule_timeout(cfs_time_seconds(tms) / 1000);
350                 cfs_duration_usec(cfs_time_sub(cfs_time_current(), now), &tv);
351                 tms -= (int)(tv.tv_sec * 1000 + tv.tv_usec / 1000);
352                 if (tms < 0) /* no more wait but may have new event */
353                         tms = 0;
354         }
355
356         wait = tms != 0; /* might need to call here again */
357         *timeout_ms = tms;
358
359         lnet_eq_wait_lock();
360         remove_wait_queue(&the_lnet.ln_eq_waitq, &wl);
361
362         return wait;
363 }
364
365 /**
366  * Block the calling process until there's an event from a set of EQs or
367  * timeout happens.
368  *
369  * If an event handler is associated with the EQ, the handler will run before
370  * this function returns successfully, in which case the corresponding event
371  * is consumed.
372  *
373  * LNetEQPoll() provides a timeout to allow applications to poll, block for a
374  * fixed period, or block indefinitely.
375  *
376  * \param eventqs,neq An array of EQ handles, and size of the array.
377  * \param timeout_ms Time in milliseconds to wait for an event to occur on
378  * one of the EQs. The constant LNET_TIME_FOREVER can be used to indicate an
379  * infinite timeout.
380  * \param event,which On successful return (1 or -EOVERFLOW), \a event will
381  * hold the next event in the EQs, and \a which will contain the index of the
382  * EQ from which the event was taken.
383  *
384  * \retval 0          No pending event in the EQs after timeout.
385  * \retval 1          Indicates success.
386  * \retval -EOVERFLOW Indicates success (i.e., an event is returned) and that
387  * at least one event between this event and the last event obtained from the
388  * EQ indicated by \a which has been dropped due to limited space in the EQ.
389  * \retval -ENOENT    If there's an invalid handle in \a eventqs.
390  */
391 int
392 LNetEQPoll(lnet_handle_eq_t *eventqs, int neq, int timeout_ms,
393            lnet_event_t *event, int *which)
394 {
395         int     wait = 1;
396         int     rc;
397         int     i;
398         ENTRY;
399
400         LASSERT (the_lnet.ln_refcount > 0);
401
402         if (neq < 1)
403                 RETURN(-ENOENT);
404
405         lnet_eq_wait_lock();
406
407         for (;;) {
408                 for (i = 0; i < neq; i++) {
409                         lnet_eq_t *eq = lnet_handle2eq(&eventqs[i]);
410
411                         if (eq == NULL) {
412                                 lnet_eq_wait_unlock();
413                                 RETURN(-ENOENT);
414                         }
415
416                         rc = lnet_eq_dequeue_event(eq, event);
417                         if (rc != 0) {
418                                 lnet_eq_wait_unlock();
419                                 *which = i;
420                                 RETURN(rc);
421                         }
422                 }
423
424                 if (wait == 0)
425                         break;
426
427                 /*
428                  * return value of lnet_eq_wait_locked:
429                  * -1 : did nothing and it's sure no new event
430                  *  1 : sleep inside and wait until new event
431                  *  0 : don't want to wait anymore, but might have new event
432                  *      so need to call dequeue again
433                  */
434                 wait = lnet_eq_wait_locked(&timeout_ms);
435                 if (wait < 0) /* no new event */
436                         break;
437         }
438
439         lnet_eq_wait_unlock();
440         RETURN(0);
441 }