1 /* $NetBSD: rf_revent.c,v 1.24 2006/11/16 01:33:23 christos Exp $ */
3 * Copyright (c) 1995 Carnegie-Mellon University.
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.
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.
18 * Carnegie Mellon requests users of this software to return to
20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
29 * revent.c -- reconstruction event handling code
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: rf_revent.c,v 1.24 2006/11/16 01:33:23 christos Exp $");
35 #include <sys/errno.h>
38 #include "rf_revent.h"
39 #include "rf_etimer.h"
40 #include "rf_general.h"
42 #include "rf_shutdown.h"
44 #define RF_MAX_FREE_REVENT 128
45 #define RF_MIN_FREE_REVENT 32
46 #define RF_EVENTQ_WAIT 5000
49 #include <sys/kernel.h>
51 static void rf_ShutdownReconEvent(void *);
53 static RF_ReconEvent_t
*
54 GetReconEventDesc(RF_RowCol_t col
, void *arg
, RF_Revent_t type
);
56 static void rf_ShutdownReconEvent(void *ignored
)
58 pool_destroy(&rf_pools
.revent
);
62 rf_ConfigureReconEvent(RF_ShutdownList_t
**listp
)
65 rf_pool_init(&rf_pools
.revent
, sizeof(RF_ReconEvent_t
),
66 "rf_revent_pl", RF_MIN_FREE_REVENT
, RF_MAX_FREE_REVENT
);
67 rf_ShutdownCreate(listp
, rf_ShutdownReconEvent
, NULL
);
72 /* returns the next reconstruction event, blocking the calling thread
73 * until one becomes available. will now return null if it is blocked
74 * or will return an event if it is not */
77 rf_GetNextReconEvent(RF_RaidReconDesc_t
*reconDesc
)
79 RF_Raid_t
*raidPtr
= reconDesc
->raidPtr
;
80 RF_ReconCtrl_t
*rctrl
= raidPtr
->reconControl
;
81 RF_ReconEvent_t
*event
;
84 RF_LOCK_MUTEX(rctrl
->eq_mutex
);
85 /* q null and count==0 must be equivalent conditions */
86 RF_ASSERT((rctrl
->eventQueue
== NULL
) == (rctrl
->eq_count
== 0));
88 /* mpsleep timeout value: secs = timo_val/hz. 'ticks' here is
89 defined as cycle-counter ticks, not softclock ticks */
91 #define MAX_RECON_EXEC_USECS (100 * 1000) /* 100 ms */
92 #define RECON_DELAY_MS 25
93 #define RECON_TIMO ((RECON_DELAY_MS * hz) / 1000)
95 /* we are not pre-emptible in the kernel, but we don't want to run
96 * forever. If we run w/o blocking for more than MAX_RECON_EXEC_TICKS
97 * ticks of the cycle counter, delay for RECON_DELAY before
98 * continuing. this may murder us with context switches, so we may
99 * need to increase both the MAX...TICKS and the RECON_DELAY_MS. */
100 if (reconDesc
->reconExecTimerRunning
) {
103 RF_ETIMER_STOP(reconDesc
->recon_exec_timer
);
104 RF_ETIMER_EVAL(reconDesc
->recon_exec_timer
);
105 reconDesc
->reconExecTicks
+=
106 RF_ETIMER_VAL_US(reconDesc
->recon_exec_timer
);
107 if (reconDesc
->reconExecTicks
> reconDesc
->maxReconExecTicks
)
108 reconDesc
->maxReconExecTicks
=
109 reconDesc
->reconExecTicks
;
110 if (reconDesc
->reconExecTicks
>= MAX_RECON_EXEC_USECS
) {
111 /* we've been running too long. delay for
113 #if RF_RECON_STATS > 0
114 reconDesc
->numReconExecDelays
++;
115 #endif /* RF_RECON_STATS > 0 */
117 status
= ltsleep(&reconDesc
->reconExecTicks
, PRIBIO
,
118 "recon delay", RECON_TIMO
,
120 RF_ASSERT(status
== EWOULDBLOCK
);
121 reconDesc
->reconExecTicks
= 0;
126 while (!rctrl
->eventQueue
) {
127 #if RF_RECON_STATS > 0
128 reconDesc
->numReconEventWaits
++;
129 #endif /* RF_RECON_STATS > 0 */
131 ltsleep(&(rctrl
)->eventQueue
, PRIBIO
, "raidframe eventq",
132 RF_EVENTQ_WAIT
, &((rctrl
)->eq_mutex
));
136 if ((stall_count
> 10) &&
137 rctrl
->headSepCBList
) {
138 /* There is work to do on the callback list, and
139 we've waited long enough... */
140 rf_WakeupHeadSepCBWaiters(raidPtr
);
143 reconDesc
->reconExecTicks
= 0; /* we've just waited */
146 reconDesc
->reconExecTimerRunning
= 1;
147 if (RF_ETIMER_VAL_US(reconDesc
->recon_exec_timer
)!=0) {
148 /* it moved!! reset the timer. */
149 RF_ETIMER_START(reconDesc
->recon_exec_timer
);
151 event
= rctrl
->eventQueue
;
152 rctrl
->eventQueue
= event
->next
;
156 /* q null and count==0 must be equivalent conditions */
157 RF_ASSERT((rctrl
->eventQueue
== NULL
) == (rctrl
->eq_count
== 0));
158 RF_UNLOCK_MUTEX(rctrl
->eq_mutex
);
161 /* enqueues a reconstruction event on the indicated queue */
163 rf_CauseReconEvent(RF_Raid_t
*raidPtr
, RF_RowCol_t col
, void *arg
,
166 RF_ReconCtrl_t
*rctrl
= raidPtr
->reconControl
;
167 RF_ReconEvent_t
*event
= GetReconEventDesc(col
, arg
, type
);
169 if (type
== RF_REVENT_BUFCLEAR
) {
170 RF_ASSERT(col
!= rctrl
->fcol
);
172 RF_ASSERT(col
>= 0 && col
<= raidPtr
->numCol
);
173 RF_LOCK_MUTEX(rctrl
->eq_mutex
);
174 /* q null and count==0 must be equivalent conditions */
175 RF_ASSERT((rctrl
->eventQueue
== NULL
) == (rctrl
->eq_count
== 0));
176 event
->next
= rctrl
->eventQueue
;
177 rctrl
->eventQueue
= event
;
179 RF_UNLOCK_MUTEX(rctrl
->eq_mutex
);
181 wakeup(&(rctrl
)->eventQueue
);
183 /* allocates and initializes a recon event descriptor */
184 static RF_ReconEvent_t
*
185 GetReconEventDesc(RF_RowCol_t col
, void *arg
, RF_Revent_t type
)
189 t
= pool_get(&rf_pools
.revent
, PR_WAITOK
);
198 rf_DrainReconEventQueue() -- used in the event of a reconstruction
199 problem, this function simply drains all pending events from the
200 reconstruct event queue.
204 rf_DrainReconEventQueue(RF_RaidReconDesc_t
*reconDesc
)
206 RF_ReconCtrl_t
*rctrl
= reconDesc
->raidPtr
->reconControl
;
207 RF_ReconEvent_t
*event
;
209 RF_LOCK_MUTEX(rctrl
->eq_mutex
);
210 while (rctrl
->eventQueue
!=NULL
) {
212 event
= rctrl
->eventQueue
;
213 rctrl
->eventQueue
= event
->next
;
217 rf_FreeReconEventDesc(event
);
219 RF_UNLOCK_MUTEX(rctrl
->eq_mutex
);
223 rf_FreeReconEventDesc(RF_ReconEvent_t
*event
)
225 pool_put(&rf_pools
.revent
, event
);