Home | History | Annotate | Line # | Download | only in raidframe
rf_revent.c revision 1.2
      1 /*	$NetBSD: rf_revent.c,v 1.2 1999/01/26 02:34:02 oster Exp $	*/
      2 /*
      3  * Copyright (c) 1995 Carnegie-Mellon University.
      4  * All rights reserved.
      5  *
      6  * Author:
      7  *
      8  * Permission to use, copy, modify and distribute this software and
      9  * its documentation is hereby granted, provided that both the copyright
     10  * notice and this permission notice appear in all copies of the
     11  * software, derivative works or modified versions, and any portions
     12  * thereof, and that both notices appear in supporting documentation.
     13  *
     14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     16  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  *
     18  * Carnegie Mellon requests users of this software to return to
     19  *
     20  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21  *  School of Computer Science
     22  *  Carnegie Mellon University
     23  *  Pittsburgh PA 15213-3890
     24  *
     25  * any improvements or extensions that they make and grant Carnegie the
     26  * rights to redistribute these changes.
     27  */
     28 /*
     29  * revent.c -- reconstruction event handling code
     30  */
     31 
     32 #include <sys/errno.h>
     33 
     34 #include "rf_raid.h"
     35 #include "rf_revent.h"
     36 #include "rf_etimer.h"
     37 #include "rf_general.h"
     38 #include "rf_freelist.h"
     39 #include "rf_desc.h"
     40 #include "rf_shutdown.h"
     41 
     42 static RF_FreeList_t *rf_revent_freelist;
     43 #define RF_MAX_FREE_REVENT 128
     44 #define RF_REVENT_INC        8
     45 #define RF_REVENT_INITIAL    8
     46 
     47 
     48 
     49 #include <sys/proc.h>
     50 
     51 extern int hz;
     52 
     53 #define DO_WAIT(_rc)   tsleep(&(_rc)->eventQueue, PRIBIO | PCATCH, "raidframe eventq", 0)
     54 
     55 #define DO_SIGNAL(_rc)     wakeup(&(_rc)->eventQueue)
     56 
     57 
     58 static void rf_ShutdownReconEvent(void *);
     59 
     60 static RF_ReconEvent_t *GetReconEventDesc(RF_RowCol_t row, RF_RowCol_t col,
     61 	void *arg, RF_Revent_t type);
     62 RF_ReconEvent_t *rf_GetNextReconEvent(RF_RaidReconDesc_t   *,
     63 				      RF_RowCol_t, void (*continueFunc)(void *),
     64 				      void *);
     65 
     66 static void rf_ShutdownReconEvent(ignored)
     67   void  *ignored;
     68 {
     69 	RF_FREELIST_DESTROY(rf_revent_freelist,next,(RF_ReconEvent_t *));
     70 }
     71 
     72 int rf_ConfigureReconEvent(listp)
     73   RF_ShutdownList_t  **listp;
     74 {
     75   int rc;
     76 
     77   RF_FREELIST_CREATE(rf_revent_freelist, RF_MAX_FREE_REVENT,
     78     RF_REVENT_INC, sizeof(RF_ReconEvent_t));
     79   if (rf_revent_freelist == NULL)
     80     return(ENOMEM);
     81   rc = rf_ShutdownCreate(listp, rf_ShutdownReconEvent, NULL);
     82   if (rc) {
     83     RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n", __FILE__,
     84       __LINE__, rc);
     85     rf_ShutdownReconEvent(NULL);
     86     return(rc);
     87   }
     88   RF_FREELIST_PRIME(rf_revent_freelist, RF_REVENT_INITIAL,next,
     89     (RF_ReconEvent_t *));
     90   return(0);
     91 }
     92 
     93 /* returns the next reconstruction event, blocking the calling thread until
     94  * one becomes available
     95  */
     96 
     97 /* will now return null if it is blocked or will return an event if it is not */
     98 
     99 RF_ReconEvent_t *rf_GetNextReconEvent(reconDesc, row, continueFunc, continueArg)
    100   RF_RaidReconDesc_t   *reconDesc;
    101   RF_RowCol_t           row;
    102   void                (*continueFunc)(void *);
    103   void                 *continueArg;
    104 {
    105   RF_Raid_t *raidPtr = reconDesc->raidPtr;
    106   RF_ReconCtrl_t *rctrl = raidPtr->reconControl[row];
    107   RF_ReconEvent_t *event;
    108 
    109   RF_ASSERT( row >= 0 && row <= raidPtr->numRow );
    110   RF_LOCK_MUTEX(rctrl->eq_mutex);
    111   RF_ASSERT( (rctrl->eventQueue==NULL) == (rctrl->eq_count == 0));  /* q null and count==0 must be equivalent conditions */
    112 
    113 
    114   rctrl->continueFunc=continueFunc;
    115   rctrl->continueArg=continueArg;
    116 
    117 
    118 /* mpsleep timeout value: secs = timo_val/hz.  'ticks' here is defined as cycle-counter ticks, not softclock ticks */
    119 #define MAX_RECON_EXEC_TICKS 15000000  /* 150 Mhz => this many ticks in 100 ms */
    120 #define RECON_DELAY_MS 25
    121 #define RECON_TIMO     ((RECON_DELAY_MS * hz) / 1000)
    122 
    123   /* we are not pre-emptible in the kernel, but we don't want to run forever.  If we run w/o blocking
    124    * for more than MAX_RECON_EXEC_TICKS ticks of the cycle counter, delay for RECON_DELAY before continuing.
    125    * this may murder us with context switches, so we may need to increase both the MAX...TICKS and the RECON_DELAY_MS.
    126    */
    127   if (reconDesc->reconExecTimerRunning) {
    128     int status;
    129 
    130     RF_ETIMER_STOP(reconDesc->recon_exec_timer);
    131     RF_ETIMER_EVAL(reconDesc->recon_exec_timer);
    132     reconDesc->reconExecTicks += RF_ETIMER_VAL_TICKS(reconDesc->recon_exec_timer);
    133     if (reconDesc->reconExecTicks > reconDesc->maxReconExecTicks)
    134       reconDesc->maxReconExecTicks = reconDesc->reconExecTicks;
    135     if (reconDesc->reconExecTicks >= MAX_RECON_EXEC_TICKS) {
    136       /* we've been running too long.  delay for RECON_DELAY_MS */
    137 #if RF_RECON_STATS > 0
    138       reconDesc->numReconExecDelays++;
    139 #endif /* RF_RECON_STATS > 0 */
    140       status = tsleep(&reconDesc->reconExecTicks, PRIBIO | PCATCH, "recon delay", RECON_TIMO );
    141       RF_ASSERT(status == EWOULDBLOCK);
    142       reconDesc->reconExecTicks = 0;
    143     }
    144   }
    145 
    146 
    147   while (!rctrl->eventQueue) {
    148 #if RF_RECON_STATS > 0
    149     reconDesc->numReconEventWaits++;
    150 #endif /* RF_RECON_STATS > 0 */
    151     DO_WAIT(rctrl);
    152     reconDesc->reconExecTicks = 0; /* we've just waited */
    153   }
    154 
    155   reconDesc->reconExecTimerRunning = 1;
    156   RF_ETIMER_START(reconDesc->recon_exec_timer);
    157 
    158   event = rctrl->eventQueue;
    159   rctrl->eventQueue = event->next;
    160   event->next = NULL;
    161   rctrl->eq_count--;
    162   RF_ASSERT( (rctrl->eventQueue==NULL) == (rctrl->eq_count == 0));  /* q null and count==0 must be equivalent conditions */
    163   RF_UNLOCK_MUTEX(rctrl->eq_mutex);
    164   return(event);
    165 }
    166 
    167 /* enqueues a reconstruction event on the indicated queue */
    168 void rf_CauseReconEvent(raidPtr, row, col, arg, type)
    169   RF_Raid_t    *raidPtr;
    170   RF_RowCol_t   row;
    171   RF_RowCol_t   col;
    172   void         *arg;
    173   RF_Revent_t   type;
    174 {
    175   RF_ReconCtrl_t *rctrl = raidPtr->reconControl[row];
    176   RF_ReconEvent_t *event = GetReconEventDesc(row, col, arg, type);
    177 
    178   if (type == RF_REVENT_BUFCLEAR) {
    179     RF_ASSERT(col != rctrl->fcol);
    180   }
    181 
    182   RF_ASSERT( row >= 0 && row <= raidPtr->numRow && col >=0 && col <= raidPtr->numCol );
    183   RF_LOCK_MUTEX(rctrl->eq_mutex);
    184   RF_ASSERT( (rctrl->eventQueue==NULL) == (rctrl->eq_count == 0));  /* q null and count==0 must be equivalent conditions */
    185   event->next = rctrl->eventQueue;
    186   rctrl->eventQueue = event;
    187   rctrl->eq_count++;
    188   RF_UNLOCK_MUTEX(rctrl->eq_mutex);
    189 
    190   DO_SIGNAL(rctrl);
    191 }
    192 
    193 /* allocates and initializes a recon event descriptor */
    194 static RF_ReconEvent_t *GetReconEventDesc(row, col, arg, type)
    195   RF_RowCol_t   row;
    196   RF_RowCol_t   col;
    197   void         *arg;
    198   RF_Revent_t   type;
    199 {
    200 	RF_ReconEvent_t *t;
    201 
    202 	RF_FREELIST_GET(rf_revent_freelist,t,next,(RF_ReconEvent_t *));
    203 	if (t == NULL)
    204 		return(NULL);
    205 	t->col = col;
    206 	t->arg = arg;
    207 	t->type = type;
    208 	return(t);
    209 }
    210 
    211 void rf_FreeReconEventDesc(event)
    212   RF_ReconEvent_t  *event;
    213 {
    214 	RF_FREELIST_FREE(rf_revent_freelist,event,next);
    215 }
    216