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