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