rx stats atomic inclusion needs kmutexes for emulation
[openafs.git] / src / rx / rx_event.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #ifdef  KERNEL
12 #include "afs/param.h"
13 #else
14 #include <afs/param.h>
15 #endif
16
17 #ifdef AFS_SUN59_ENV
18 #include <sys/time_impl.h>
19 #endif
20
21
22 #ifdef KERNEL
23 #ifndef UKERNEL
24 #include "afs/afs_osi.h"
25 #else /* !UKERNEL */
26 #include "afs/sysincludes.h"
27 #include "afsincludes.h"
28 #endif /* !UKERNEL */
29 #include "rx/rx_clock.h"
30 #include "rx/rx_queue.h"
31 #include "rx/rx_event.h"
32 #include "rx/rx_kernel.h"
33 #include "rx_kmutex.h"
34 #ifdef RX_ENABLE_LOCKS
35 #include "rx/rx.h"
36 #endif /* RX_ENABLE_LOCKS */
37 #include "rx/rx_globals.h"
38 #if defined(AFS_SGI_ENV)
39 #include "sys/debug.h"
40 /* These are necessary to get curproc (used by GLOCK asserts) to work. */
41 #include "h/proc.h"
42 #if !defined(AFS_SGI64_ENV) && !defined(UKERNEL)
43 #include "h/user.h"
44 #endif
45 extern void *osi_Alloc();
46 #endif
47 #if defined(AFS_OBSD_ENV)
48 #include "h/proc.h"
49 #endif
50 #else /* KERNEL */
51 #include <stdio.h>
52 #include "rx_clock.h"
53 #include "rx_queue.h"
54 #include "rx_event.h"
55 #include "rx_user.h"
56 #ifdef AFS_PTHREAD_ENV
57 #include <rx/rx_pthread.h>
58 #else
59 #include "rx_lwp.h"
60 #endif
61 #ifdef RX_ENABLE_LOCKS
62 #include "rx.h"
63 #endif /* RX_ENABLE_LOCKS */
64 #include "rx_globals.h"
65 #ifdef AFS_NT40_ENV
66 #include <malloc.h>
67 #endif
68 #endif /* KERNEL */
69
70
71 /* All event processing is relative to the apparent current time given by clock_GetTime */
72
73 /* This should be static, but event_test wants to look at the free list... */
74 struct rx_queue rxevent_free;   /* It's somewhat bogus to use a doubly-linked queue for the free list */
75 struct rx_queue rxepoch_free;   /* It's somewhat bogus to use a doubly-linked queue for the free list */
76 static struct rx_queue rxepoch_queue;   /* list of waiting epochs */
77 static int rxevent_allocUnit = 10;      /* Allocation unit (number of event records to allocate at one time) */
78 static int rxepoch_allocUnit = 10;      /* Allocation unit (number of epoch records to allocate at one time) */
79 int rxevent_nFree;              /* Number of free event records */
80 int rxevent_nPosted;            /* Current number of posted events */
81 int rxepoch_nFree;              /* Number of free epoch records */
82 static void (*rxevent_ScheduledEarlierEvent) (void);    /* Proc to call when an event is scheduled that is earlier than all other events */
83 struct xfreelist {
84     void *mem;
85     int size;
86     struct xfreelist *next;
87 };
88 static struct xfreelist *xfreemallocs = 0, *xsp = 0;
89
90 struct clock rxevent_nextRaiseEvents;   /* Time of next call to raise events */
91 struct clock rxevent_lastEvent;        /* backwards time detection */
92 int rxevent_raiseScheduled;     /* true if raise events is scheduled */
93
94 #ifdef RX_ENABLE_LOCKS
95 #ifdef RX_LOCKS_DB
96 /* rxdb_fileID is used to identify the lock location, along with line#. */
97 static int rxdb_fileID = RXDB_FILE_RX_EVENT;
98 #endif /* RX_LOCKS_DB */
99 #define RX_ENABLE_LOCKS  1
100 afs_kmutex_t rxevent_lock;
101 #endif /* RX_ENABLE_LOCKS */
102
103 #ifdef AFS_PTHREAD_ENV
104 /*
105  * This mutex protects the following global variables:
106  * rxevent_initialized
107  */
108
109 #include <assert.h>
110 afs_kmutex_t rx_event_mutex;
111 #define LOCK_EV_INIT MUTEX_ENTER(&rx_event_mutex)
112 #define UNLOCK_EV_INIT MUTEX_EXIT(&rx_event_mutex)
113 #else
114 #define LOCK_EV_INIT
115 #define UNLOCK_EV_INIT
116 #endif /* AFS_PTHREAD_ENV */
117
118
119 int
120 rxevent_adjTimes(struct clock *adjTime)
121 {
122     /* backwards clock correction */
123     int nAdjusted = 0;
124     struct rxepoch *qep, *nqep;
125     struct rxevent *qev, *nqev;
126
127     for (queue_Scan(&rxepoch_queue, qep, nqep, rxepoch)) {
128         for (queue_Scan(&qep->events, qev, nqev, rxevent)) {
129             if (clock_Gt(&qev->eventTime, adjTime)) {
130                 clock_Sub(&qev->eventTime, adjTime);
131                 nAdjusted++;
132             }
133         }
134         if (qep->epochSec > adjTime->sec) {
135             qep->epochSec -= adjTime->sec;
136         }
137     }
138     return nAdjusted;
139 }
140
141 /* Pass in the number of events to allocate at a time */
142 int rxevent_initialized = 0;
143 void
144 rxevent_Init(int nEvents, void (*scheduler) (void))
145 {
146     LOCK_EV_INIT;
147     if (rxevent_initialized) {
148         UNLOCK_EV_INIT;
149         return;
150     }
151     MUTEX_INIT(&rxevent_lock, "rxevent_lock", MUTEX_DEFAULT, 0);
152     clock_Init();
153     if (nEvents)
154         rxevent_allocUnit = nEvents;
155     queue_Init(&rxevent_free);
156     queue_Init(&rxepoch_free);
157     queue_Init(&rxepoch_queue);
158     rxevent_nFree = rxevent_nPosted = 0;
159     rxepoch_nFree = 0;
160     rxevent_ScheduledEarlierEvent = scheduler;
161     rxevent_initialized = 1;
162     clock_Zero(&rxevent_nextRaiseEvents);
163     clock_Zero(&rxevent_lastEvent);
164     rxevent_raiseScheduled = 0;
165     UNLOCK_EV_INIT;
166 }
167
168 /* Create and initialize new epoch structure */
169 struct rxepoch *
170 rxepoch_Allocate(struct clock *when)
171 {
172     struct rxepoch *ep;
173     int i;
174
175     /* If we are short on free epoch entries, create a block of new oned
176      * and add them to the free queue */
177     if (queue_IsEmpty(&rxepoch_free)) {
178 #if    defined(AFS_AIX32_ENV) && defined(KERNEL)
179         ep = rxi_Alloc(sizeof(struct rxepoch));
180         queue_Append(&rxepoch_free, &ep[0]), rxepoch_nFree++;
181 #else
182 #if defined(KERNEL) && !defined(UKERNEL) && defined(AFS_FBSD80_ENV)
183         ep = (struct rxepoch *)
184             afs_osi_Alloc_NoSleep(sizeof(struct rxepoch) * rxepoch_allocUnit);
185         xsp = xfreemallocs;
186         xfreemallocs =
187             (struct xfreelist *)afs_osi_Alloc_NoSleep(sizeof(struct xfreelist));
188 #else
189         ep = (struct rxepoch *)
190             osi_Alloc(sizeof(struct rxepoch) * rxepoch_allocUnit);
191         xsp = xfreemallocs;
192         xfreemallocs =
193             (struct xfreelist *)osi_Alloc(sizeof(struct xfreelist));
194 #endif
195         xfreemallocs->mem = (void *)ep;
196         xfreemallocs->size = sizeof(struct rxepoch) * rxepoch_allocUnit;
197         xfreemallocs->next = xsp;
198         for (i = 0; i < rxepoch_allocUnit; i++)
199             queue_Append(&rxepoch_free, &ep[i]), rxepoch_nFree++;
200 #endif
201     }
202     ep = queue_First(&rxepoch_free, rxepoch);
203     queue_Remove(ep);
204     rxepoch_nFree--;
205     ep->epochSec = when->sec;
206     queue_Init(&ep->events);
207     return ep;
208 }
209
210 /* Add the indicated event (function, arg) at the specified clock time.  The
211  * "when" argument specifies when "func" should be called, in clock (clock.h)
212  * units. */
213
214 static struct rxevent *
215 _rxevent_Post(struct clock *when, struct clock *now,
216               void (*func) (struct rxevent *, void *, void *, int),
217               void *arg, void *arg1, int arg2, int newargs)
218 {
219     struct rxevent *ev, *evqe, *evqpr;
220     struct rxepoch *ep, *epqe, *epqpr;
221     int isEarliest = 0;
222
223     MUTEX_ENTER(&rxevent_lock);
224 #ifdef RXDEBUG
225     if (rx_Log_event) {
226         struct clock now1;
227         clock_GetTime(&now1);
228         fprintf(rx_Log_event, "%ld.%ld: rxevent_Post(%ld.%ld, "
229                               "%"AFS_PTR_FMT", %"AFS_PTR_FMT", "
230                               "%"AFS_PTR_FMT", %d)\n",
231                 afs_printable_int32_ld(now1.sec),
232                 afs_printable_int32_ld(now1.usec),
233                 afs_printable_int32_ld(when->sec),
234                 afs_printable_int32_ld(when->usec),
235                 func, arg,
236                 arg1, arg2);
237     }
238 #endif
239     /* If a time was provided, check for consistency */
240     if (now->sec) {
241         if (clock_Gt(&rxevent_lastEvent, now)) {
242             struct clock adjTime = rxevent_lastEvent;
243             clock_Sub(&adjTime, now);
244             rxevent_adjTimes(&adjTime);
245         }
246         rxevent_lastEvent = *now;
247     }
248     /* Get a pointer to the epoch for this event, if none is found then
249      * create a new epoch and insert it into the sorted list */
250     for (ep = NULL, queue_ScanBackwards(&rxepoch_queue, epqe, epqpr, rxepoch)) {
251         if (when->sec == epqe->epochSec) {
252             /* already have an structure for this epoch */
253             ep = epqe;
254             if (ep == queue_First(&rxepoch_queue, rxepoch))
255                 isEarliest = 1;
256             break;
257         } else if (when->sec > epqe->epochSec) {
258             /* Create a new epoch and insert after qe */
259             ep = rxepoch_Allocate(when);
260             queue_InsertAfter(epqe, ep);
261             break;
262         }
263     }
264     if (ep == NULL) {
265         /* Create a new epoch and place it at the head of the list */
266         ep = rxepoch_Allocate(when);
267         queue_Prepend(&rxepoch_queue, ep);
268         isEarliest = 1;
269     }
270
271     /* If we're short on free event entries, create a block of new ones and add
272      * them to the free queue */
273     if (queue_IsEmpty(&rxevent_free)) {
274         int i;
275 #if     defined(AFS_AIX32_ENV) && defined(KERNEL)
276         ev = rxi_Alloc(sizeof(struct rxevent));
277         queue_Append(&rxevent_free, &ev[0]), rxevent_nFree++;
278 #else
279
280 #if defined(KERNEL) && !defined(UKERNEL) && defined(AFS_FBSD80_ENV)
281         ev = (struct rxevent *)afs_osi_Alloc_NoSleep(sizeof(struct rxevent) *
282                                          rxevent_allocUnit);
283         xsp = xfreemallocs;
284         xfreemallocs =
285             (struct xfreelist *)afs_osi_Alloc_NoSleep(sizeof(struct xfreelist));
286 #else
287         ev = (struct rxevent *)osi_Alloc(sizeof(struct rxevent) *
288                                          rxevent_allocUnit);
289         xsp = xfreemallocs;
290         xfreemallocs =
291             (struct xfreelist *)osi_Alloc(sizeof(struct xfreelist));
292 #endif
293         xfreemallocs->mem = (void *)ev;
294         xfreemallocs->size = sizeof(struct rxevent) * rxevent_allocUnit;
295         xfreemallocs->next = xsp;
296         for (i = 0; i < rxevent_allocUnit; i++)
297             queue_Append(&rxevent_free, &ev[i]), rxevent_nFree++;
298 #endif
299     }
300
301     /* Grab and initialize a new rxevent structure */
302     ev = queue_First(&rxevent_free, rxevent);
303     queue_Remove(ev);
304     rxevent_nFree--;
305
306     /* Record user defined event state */
307     ev->eventTime = *when;
308     if (newargs) {
309         ev->func.newfunc = func;
310     } else {
311         ev->func.oldfunc = (void (*)(struct rxevent *, void *, void*))func;
312     }
313     ev->arg = arg;
314     ev->arg1 = arg1;
315     ev->arg2 = arg2;
316     ev->newargs = newargs;
317     rxevent_nPosted += 1;       /* Rather than ++, to shut high-C up
318                                  *  regarding never-set variables
319                                  */
320
321     /* Insert the event into the sorted list of events for this epoch */
322     for (queue_ScanBackwards(&ep->events, evqe, evqpr, rxevent)) {
323         if (when->usec >= evqe->eventTime.usec) {
324             /* Insert event after evqe */
325             queue_InsertAfter(evqe, ev);
326             MUTEX_EXIT(&rxevent_lock);
327             return ev;
328         }
329     }
330     /* Insert event at head of current epoch */
331     queue_Prepend(&ep->events, ev);
332     if (isEarliest && rxevent_ScheduledEarlierEvent
333         && (!rxevent_raiseScheduled
334             || clock_Lt(&ev->eventTime, &rxevent_nextRaiseEvents))) {
335         rxevent_raiseScheduled = 1;
336         clock_Zero(&rxevent_nextRaiseEvents);
337         MUTEX_EXIT(&rxevent_lock);
338         /* Notify our external scheduler */
339         (*rxevent_ScheduledEarlierEvent) ();
340         MUTEX_ENTER(&rxevent_lock);
341     }
342     MUTEX_EXIT(&rxevent_lock);
343     return ev;
344 }
345
346 struct rxevent *
347 rxevent_Post(struct clock *when,
348              void (*func) (struct rxevent *, void *, void *),
349              void *arg, void *arg1)
350 {
351     struct clock now;
352     clock_Zero(&now);
353     return _rxevent_Post(when, &now,
354                          (void (*)(struct rxevent *, void *, void *, int))func,
355                          arg, arg1, 0, 0);
356 }
357
358 struct rxevent *
359 rxevent_Post2(struct clock *when,
360               void (*func) (struct rxevent *, void *, void *, int),
361               void *arg, void *arg1, int arg2)
362 {
363     struct clock now;
364     clock_Zero(&now);
365     return _rxevent_Post(when, &now, func, arg, arg1, arg2, 1);
366 }
367
368 struct rxevent *
369 rxevent_PostNow(struct clock *when, struct clock *now,
370                 void (*func) (struct rxevent *, void *, void *),
371                 void *arg, void *arg1)
372 {
373     return _rxevent_Post(when, now,
374                          (void (*)(struct rxevent *, void *, void *, int))func,
375                          arg, arg1, 0, 0);
376 }
377
378 struct rxevent *
379 rxevent_PostNow2(struct clock *when, struct clock *now,
380                  void (*func) (struct rxevent *, void *, void *, int),
381                  void *arg, void *arg1, int arg2)
382 {
383     return _rxevent_Post(when, now, func, arg, arg1, arg2, 1);
384 }
385
386 /* Cancel an event by moving it from the event queue to the free list.
387  * Warning, the event must be on the event queue!  If not, this should core
388  * dump (reference through 0).  This routine should be called using the macro
389  * event_Cancel, which checks for a null event and also nulls the caller's
390  * event pointer after cancelling the event.
391  */
392 #ifdef RX_ENABLE_LOCKS
393 #ifdef RX_REFCOUNT_CHECK
394 int rxevent_Cancel_type = 0;
395 #endif
396 #endif
397
398 void
399 rxevent_Cancel_1(struct rxevent *ev, struct rx_call *call,
400                  int type)
401 {
402 #ifdef RXDEBUG
403     if (rx_Log_event) {
404         struct clock now;
405         clock_GetTime(&now);
406         fprintf(rx_Log_event, "%d.%d: rxevent_Cancel_1(%d.%d, %"
407                 AFS_PTR_FMT ", %p" AFS_PTR_FMT ")\n",
408                 (int)now.sec, (int)now.usec, (int)ev->eventTime.sec,
409                 (int)ev->eventTime.usec, ev->func.newfunc,
410                 ev->arg);
411     }
412 #endif
413     /* Append it to the free list (rather than prepending) to keep the free
414      * list hot so nothing pages out
415      */
416     MUTEX_ENTER(&rxevent_lock);
417     if (!ev) {
418         MUTEX_EXIT(&rxevent_lock);
419         return;
420     }
421 #ifdef RX_ENABLE_LOCKS
422     /* It's possible we're currently processing this event. */
423     if (queue_IsOnQueue(ev)) {
424         queue_MoveAppend(&rxevent_free, ev);
425         rxevent_nPosted--;
426         rxevent_nFree++;
427         if (call) {
428             call->refCount--;
429 #ifdef RX_REFCOUNT_CHECK
430             call->refCDebug[type]--;
431             if (call->refCDebug[type] < 0) {
432                 rxevent_Cancel_type = type;
433                 osi_Panic("rxevent_Cancel: call refCount < 0");
434             }
435 #endif /* RX_REFCOUNT_CHECK */
436         }
437     }
438 #else /* RX_ENABLE_LOCKS */
439     queue_MoveAppend(&rxevent_free, ev);
440     rxevent_nPosted--;
441     rxevent_nFree++;
442 #endif /* RX_ENABLE_LOCKS */
443     MUTEX_EXIT(&rxevent_lock);
444 }
445
446 /* Process all epochs that have expired relative to the current clock time
447  * (which is not re-evaluated unless clock_NewTime has been called).  The
448  * relative time to the next epoch is returned in the output parameter next
449  * and the function returns 1.  If there are is no next epoch, the function
450  * returns 0.
451  */
452 int
453 rxevent_RaiseEvents(struct clock *next)
454 {
455     struct rxepoch *ep;
456     struct rxevent *ev;
457     volatile struct clock now;
458     MUTEX_ENTER(&rxevent_lock);
459
460     /* Events are sorted by time, so only scan until an event is found that has
461      * not yet timed out */
462
463     clock_Zero(&now);
464     while (queue_IsNotEmpty(&rxepoch_queue)) {
465         ep = queue_First(&rxepoch_queue, rxepoch);
466         if (queue_IsEmpty(&ep->events)) {
467             queue_Remove(ep);
468             queue_Append(&rxepoch_free, ep);
469             rxepoch_nFree++;
470             continue;
471         }
472         do {
473         reraise:
474             ev = queue_First(&ep->events, rxevent);
475             if (clock_Lt(&now, &ev->eventTime)) {
476                 clock_GetTime(&now);
477                 if (clock_Gt(&rxevent_lastEvent, &now)) {
478                     struct clock adjTime = rxevent_lastEvent;
479                     int adjusted;
480                     clock_Sub(&adjTime, &now);
481                     adjusted = rxevent_adjTimes(&adjTime);
482                     rxevent_lastEvent = now;
483                     if (adjusted > 0)
484                         goto reraise;
485                 }
486                 if (clock_Lt(&now, &ev->eventTime)) {
487                     *next = rxevent_nextRaiseEvents = ev->eventTime;
488                     rxevent_raiseScheduled = 1;
489                     clock_Sub(next, &now);
490                     MUTEX_EXIT(&rxevent_lock);
491                     return 1;
492                 }
493             }
494             queue_Remove(ev);
495             rxevent_nPosted--;
496             MUTEX_EXIT(&rxevent_lock);
497             if (ev->newargs) {
498                 ev->func.newfunc(ev, ev->arg, ev->arg1, ev->arg2);
499             } else {
500                 ev->func.oldfunc(ev, ev->arg, ev->arg1);
501             }
502             MUTEX_ENTER(&rxevent_lock);
503             queue_Append(&rxevent_free, ev);
504             rxevent_nFree++;
505         } while (queue_IsNotEmpty(&ep->events));
506     }
507 #ifdef RXDEBUG
508     if (rx_Log_event)
509         fprintf(rx_Log_event, "rxevent_RaiseEvents(%d.%d)\n", (int)now.sec,
510                 (int)now.usec);
511 #endif
512     rxevent_raiseScheduled = 0;
513     MUTEX_EXIT(&rxevent_lock);
514     return 0;
515 }
516
517 void
518 shutdown_rxevent(void)
519 {
520     struct xfreelist *xp, *nxp;
521
522     LOCK_EV_INIT;
523     if (!rxevent_initialized) {
524         UNLOCK_EV_INIT;
525         return;
526     }
527     rxevent_initialized = 0;
528     UNLOCK_EV_INIT;
529     MUTEX_DESTROY(&rxevent_lock);
530 #if     defined(AFS_AIX32_ENV) && defined(KERNEL)
531     /* Everything is freed in afs_osinet.c */
532 #else
533     xp = xfreemallocs;
534     while (xp) {
535         nxp = xp->next;
536         osi_Free((char *)xp->mem, xp->size);
537         osi_Free((char *)xp, sizeof(struct xfreelist));
538         xp = nxp;
539     }
540     xfreemallocs = NULL;
541 #endif
542
543 }