Home | History | Annotate | Line # | Download | only in raidframe
rf_revent.c revision 1.15
      1 /*	$NetBSD: rf_revent.c,v 1.15 2004/02/29 04:03:50 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/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: rf_revent.c,v 1.15 2004/02/29 04:03:50 oster Exp $");
     34 
     35 #include <sys/errno.h>
     36 
     37 #include "rf_raid.h"
     38 #include "rf_revent.h"
     39 #include "rf_etimer.h"
     40 #include "rf_general.h"
     41 #include "rf_desc.h"
     42 #include "rf_shutdown.h"
     43 
     44 static struct pool rf_revent_pool;
     45 #define RF_MAX_FREE_REVENT 128
     46 #define RF_REVENT_INC        8
     47 #define RF_REVENT_INITIAL    8
     48 
     49 
     50 
     51 #include <sys/proc.h>
     52 #include <sys/kernel.h>
     53 
     54 #define DO_WAIT(_rc)  \
     55 	ltsleep(&(_rc)->eventQueue, PRIBIO,  "raidframe eventq", \
     56 		0, &((_rc)->eq_mutex))
     57 
     58 #define DO_SIGNAL(_rc)     wakeup(&(_rc)->eventQueue)
     59 
     60 
     61 static void rf_ShutdownReconEvent(void *);
     62 
     63 static RF_ReconEvent_t *
     64 GetReconEventDesc(RF_RowCol_t col, void *arg, RF_Revent_t type);
     65 
     66 static void rf_ShutdownReconEvent(void *ignored)
     67 {
     68 	pool_destroy(&rf_revent_pool);
     69 }
     70 
     71 int
     72 rf_ConfigureReconEvent(RF_ShutdownList_t **listp)
     73 {
     74 
     75 	pool_init(&rf_revent_pool, sizeof(RF_ReconEvent_t),
     76 		  0, 0, 0, "rf_revent_pl", NULL);
     77 	pool_sethiwat(&rf_revent_pool, RF_MAX_FREE_REVENT);
     78 	pool_prime(&rf_revent_pool, RF_REVENT_INITIAL);
     79 
     80 	rf_ShutdownCreate(listp, rf_ShutdownReconEvent, NULL);
     81 
     82 	return (0);
     83 }
     84 
     85 /* returns the next reconstruction event, blocking the calling thread
     86  * until one becomes available.  will now return null if it is blocked
     87  * or will return an event if it is not */
     88 
     89 RF_ReconEvent_t *
     90 rf_GetNextReconEvent(RF_RaidReconDesc_t *reconDesc,
     91 		     void (*continueFunc)(void *), void *continueArg)
     92 {
     93 	RF_Raid_t *raidPtr = reconDesc->raidPtr;
     94 	RF_ReconCtrl_t *rctrl = raidPtr->reconControl;
     95 	RF_ReconEvent_t *event;
     96 
     97 	RF_LOCK_MUTEX(rctrl->eq_mutex);
     98 	/* q null and count==0 must be equivalent conditions */
     99 	RF_ASSERT((rctrl->eventQueue == NULL) == (rctrl->eq_count == 0));
    100 
    101 	rctrl->continueFunc = continueFunc;
    102 	rctrl->continueArg = continueArg;
    103 
    104 
    105 	/* mpsleep timeout value: secs = timo_val/hz.  'ticks' here is
    106 	   defined as cycle-counter ticks, not softclock ticks */
    107 
    108 #define MAX_RECON_EXEC_USECS (100 * 1000)  /* 100 ms */
    109 #define RECON_DELAY_MS 25
    110 #define RECON_TIMO     ((RECON_DELAY_MS * hz) / 1000)
    111 
    112 	/* we are not pre-emptible in the kernel, but we don't want to run
    113 	 * forever.  If we run w/o blocking for more than MAX_RECON_EXEC_TICKS
    114 	 * ticks of the cycle counter, delay for RECON_DELAY before
    115 	 * continuing. this may murder us with context switches, so we may
    116 	 * need to increase both the MAX...TICKS and the RECON_DELAY_MS. */
    117 	if (reconDesc->reconExecTimerRunning) {
    118 		int     status;
    119 
    120 		RF_ETIMER_STOP(reconDesc->recon_exec_timer);
    121 		RF_ETIMER_EVAL(reconDesc->recon_exec_timer);
    122 		reconDesc->reconExecTicks +=
    123 			RF_ETIMER_VAL_US(reconDesc->recon_exec_timer);
    124 		if (reconDesc->reconExecTicks > reconDesc->maxReconExecTicks)
    125 			reconDesc->maxReconExecTicks =
    126 				reconDesc->reconExecTicks;
    127 		if (reconDesc->reconExecTicks >= MAX_RECON_EXEC_USECS) {
    128 			/* we've been running too long.  delay for
    129 			 * RECON_DELAY_MS */
    130 #if RF_RECON_STATS > 0
    131 			reconDesc->numReconExecDelays++;
    132 #endif				/* RF_RECON_STATS > 0 */
    133 
    134 			status = ltsleep(&reconDesc->reconExecTicks, PRIBIO,
    135 					 "recon delay", RECON_TIMO,
    136 					 &rctrl->eq_mutex);
    137 			RF_ASSERT(status == EWOULDBLOCK);
    138 			reconDesc->reconExecTicks = 0;
    139 		}
    140 	}
    141 	while (!rctrl->eventQueue) {
    142 #if RF_RECON_STATS > 0
    143 		reconDesc->numReconEventWaits++;
    144 #endif				/* RF_RECON_STATS > 0 */
    145 		DO_WAIT(rctrl);
    146 		reconDesc->reconExecTicks = 0;	/* we've just waited */
    147 	}
    148 
    149 	reconDesc->reconExecTimerRunning = 1;
    150 	if (RF_ETIMER_VAL_US(reconDesc->recon_exec_timer)!=0) {
    151 		/* it moved!!  reset the timer. */
    152 		RF_ETIMER_START(reconDesc->recon_exec_timer);
    153 	}
    154 	event = rctrl->eventQueue;
    155 	rctrl->eventQueue = event->next;
    156 	event->next = NULL;
    157 	rctrl->eq_count--;
    158 
    159 	/* q null and count==0 must be equivalent conditions */
    160 	RF_ASSERT((rctrl->eventQueue == NULL) == (rctrl->eq_count == 0));
    161 	RF_UNLOCK_MUTEX(rctrl->eq_mutex);
    162 	return (event);
    163 }
    164 /* enqueues a reconstruction event on the indicated queue */
    165 void
    166 rf_CauseReconEvent(RF_Raid_t *raidPtr, RF_RowCol_t col, void *arg,
    167 		   RF_Revent_t type)
    168 {
    169 	RF_ReconCtrl_t *rctrl = raidPtr->reconControl;
    170 	RF_ReconEvent_t *event = GetReconEventDesc(col, arg, type);
    171 
    172 	if (type == RF_REVENT_BUFCLEAR) {
    173 		RF_ASSERT(col != rctrl->fcol);
    174 	}
    175 	RF_ASSERT(col >= 0 && col <= raidPtr->numCol);
    176 	RF_LOCK_MUTEX(rctrl->eq_mutex);
    177 	/* q null and count==0 must be equivalent conditions */
    178 	RF_ASSERT((rctrl->eventQueue == NULL) == (rctrl->eq_count == 0));
    179 	event->next = rctrl->eventQueue;
    180 	rctrl->eventQueue = event;
    181 	rctrl->eq_count++;
    182 	RF_UNLOCK_MUTEX(rctrl->eq_mutex);
    183 
    184 	DO_SIGNAL(rctrl);
    185 }
    186 /* allocates and initializes a recon event descriptor */
    187 static RF_ReconEvent_t *
    188 GetReconEventDesc(RF_RowCol_t col, void *arg, RF_Revent_t type)
    189 {
    190 	RF_ReconEvent_t *t;
    191 
    192 	t = pool_get(&rf_revent_pool, PR_WAITOK);
    193 	if (t == NULL)
    194 		return (NULL);
    195 	t->col = col;
    196 	t->arg = arg;
    197 	t->type = type;
    198 	t->next = NULL;
    199 	return (t);
    200 }
    201 
    202 void
    203 rf_FreeReconEventDesc(RF_ReconEvent_t *event)
    204 {
    205 	pool_put(&rf_revent_pool, event);
    206 }
    207