rf_fifo.c revision 1.1 1 /* $NetBSD: rf_fifo.c,v 1.1 1998/11/13 04:20:29 oster Exp $ */
2 /*
3 * Copyright (c) 1995 Carnegie-Mellon University.
4 * All rights reserved.
5 *
6 * Author: Mark Holland
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 /***************************************************
30 *
31 * rf_fifo.c -- prioritized fifo queue code.
32 * There are only two priority levels: hi and lo.
33 *
34 * Aug 4, 1994, adapted from raidSim version (MCH)
35 *
36 ***************************************************/
37
38 /*
39 * :
40 * Log: rf_fifo.c,v
41 * Revision 1.20 1996/06/18 20:53:11 jimz
42 * fix up disk queueing (remove configure routine,
43 * add shutdown list arg to create routines)
44 *
45 * Revision 1.19 1996/06/14 00:08:21 jimz
46 * make happier in all environments
47 *
48 * Revision 1.18 1996/06/13 20:41:24 jimz
49 * add random queueing
50 *
51 * Revision 1.17 1996/06/09 02:36:46 jimz
52 * lots of little crufty cleanup- fixup whitespace
53 * issues, comment #ifdefs, improve typing in some
54 * places (esp size-related)
55 *
56 * Revision 1.16 1996/06/07 22:26:27 jimz
57 * type-ify which_ru (RF_ReconUnitNum_t)
58 *
59 * Revision 1.15 1996/06/07 21:33:04 jimz
60 * begin using consistent types for sector numbers,
61 * stripe numbers, row+col numbers, recon unit numbers
62 *
63 * Revision 1.14 1996/06/06 01:15:02 jimz
64 * added debugging
65 *
66 * Revision 1.13 1996/05/30 23:22:16 jimz
67 * bugfixes of serialization, timing problems
68 * more cleanup
69 *
70 * Revision 1.12 1996/05/30 11:29:41 jimz
71 * Numerous bug fixes. Stripe lock release code disagreed with the taking code
72 * about when stripes should be locked (I made it consistent: no parity, no lock)
73 * There was a lot of extra serialization of I/Os which I've removed- a lot of
74 * it was to calculate values for the cache code, which is no longer with us.
75 * More types, function, macro cleanup. Added code to properly quiesce the array
76 * on shutdown. Made a lot of stuff array-specific which was (bogusly) general
77 * before. Fixed memory allocation, freeing bugs.
78 *
79 * Revision 1.11 1996/05/27 18:56:37 jimz
80 * more code cleanup
81 * better typing
82 * compiles in all 3 environments
83 *
84 * Revision 1.10 1996/05/23 21:46:35 jimz
85 * checkpoint in code cleanup (release prep)
86 * lots of types, function names have been fixed
87 *
88 * Revision 1.9 1995/12/12 18:10:06 jimz
89 * MIN -> RF_MIN, MAX -> RF_MAX, ASSERT -> RF_ASSERT
90 * fix 80-column brain damage in comments
91 *
92 * Revision 1.8 1995/12/01 18:22:15 root
93 * added copyright info
94 *
95 * Revision 1.7 1995/11/07 15:32:16 wvcii
96 * added function FifoPeek()
97 *
98 */
99
100 #include "rf_types.h"
101 #include "rf_alloclist.h"
102 #include "rf_stripelocks.h"
103 #include "rf_layout.h"
104 #include "rf_diskqueue.h"
105 #include "rf_fifo.h"
106 #include "rf_debugMem.h"
107 #include "rf_general.h"
108 #include "rf_threadid.h"
109 #include "rf_options.h"
110
111 #if !defined(KERNEL) && RF_INCLUDE_QUEUE_RANDOM > 0
112 #include "rf_randmacros.h"
113 RF_DECLARE_STATIC_RANDOM
114 #endif /* !KERNEL && RF_INCLUDE_QUEUE_RANDOM > 0 */
115
116 /* just malloc a header, zero it (via calloc), and return it */
117 /*ARGSUSED*/
118 void *rf_FifoCreate(sectPerDisk, clList, listp)
119 RF_SectorCount_t sectPerDisk;
120 RF_AllocListElem_t *clList;
121 RF_ShutdownList_t **listp;
122 {
123 RF_FifoHeader_t *q;
124
125 #if !defined(KERNEL) && RF_INCLUDE_QUEUE_RANDOM > 0
126 RF_INIT_STATIC_RANDOM(1);
127 #endif /* !KERNEL && RF_INCLUDE_QUEUE_RANDOM > 0 */
128 RF_CallocAndAdd(q, 1, sizeof(RF_FifoHeader_t), (RF_FifoHeader_t *), clList);
129 q->hq_count = q->lq_count = 0;
130 #if !defined(KERNEL) && RF_INCLUDE_QUEUE_RANDOM > 0
131 q->rval = (long)RF_STATIC_RANDOM();
132 #endif /* !KERNEL && RF_INCLUDE_QUEUE_RANDOM > 0 */
133 return((void *)q);
134 }
135
136 void rf_FifoEnqueue(q_in, elem, priority)
137 void *q_in;
138 RF_DiskQueueData_t *elem;
139 int priority;
140 {
141 RF_FifoHeader_t *q = (RF_FifoHeader_t *)q_in;
142
143 RF_ASSERT(priority == RF_IO_NORMAL_PRIORITY || priority == RF_IO_LOW_PRIORITY);
144
145 elem->next = NULL;
146 if (priority == RF_IO_NORMAL_PRIORITY) {
147 if (!q->hq_tail) {
148 RF_ASSERT(q->hq_count == 0 && q->hq_head == NULL);
149 q->hq_head = q->hq_tail = elem;
150 } else {
151 RF_ASSERT(q->hq_count != 0 && q->hq_head != NULL);
152 q->hq_tail->next = elem;
153 q->hq_tail = elem;
154 }
155 q->hq_count++;
156 }
157 else {
158 RF_ASSERT(elem->next == NULL);
159 if (rf_fifoDebug) {
160 int tid;
161 rf_get_threadid(tid);
162 printf("[%d] fifo: ENQ lopri\n", tid);
163 }
164 if (!q->lq_tail) {
165 RF_ASSERT(q->lq_count == 0 && q->lq_head == NULL);
166 q->lq_head = q->lq_tail = elem;
167 } else {
168 RF_ASSERT(q->lq_count != 0 && q->lq_head != NULL);
169 q->lq_tail->next = elem;
170 q->lq_tail = elem;
171 }
172 q->lq_count++;
173 }
174 if ((q->hq_count + q->lq_count)!= elem->queue->queueLength) {
175 printf("Queue lengths differ!: %d %d %d\n",
176 q->hq_count, q->lq_count, (int)elem->queue->queueLength);
177 printf("%d %d %d %d\n",
178 (int)elem->queue->numOutstanding,
179 (int)elem->queue->maxOutstanding,
180 (int)elem->queue->row,
181 (int)elem->queue->col);
182 }
183 RF_ASSERT((q->hq_count + q->lq_count) == elem->queue->queueLength);
184 }
185
186 RF_DiskQueueData_t *rf_FifoDequeue(q_in)
187 void *q_in;
188 {
189 RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
190 RF_DiskQueueData_t *nd;
191
192 RF_ASSERT(q);
193 if (q->hq_head) {
194 RF_ASSERT(q->hq_count != 0 && q->hq_tail != NULL);
195 nd = q->hq_head; q->hq_head = q->hq_head->next;
196 if (!q->hq_head) q->hq_tail = NULL;
197 nd->next = NULL;
198 q->hq_count--;
199 } else if (q->lq_head) {
200 RF_ASSERT(q->lq_count != 0 && q->lq_tail != NULL);
201 nd = q->lq_head; q->lq_head = q->lq_head->next;
202 if (!q->lq_head) q->lq_tail = NULL;
203 nd->next = NULL;
204 q->lq_count--;
205 if (rf_fifoDebug) {
206 int tid;
207 rf_get_threadid(tid);
208 printf("[%d] fifo: DEQ lopri %lx\n", tid, (long)nd);
209 }
210 } else {
211 RF_ASSERT(q->hq_count == 0 && q->lq_count == 0 && q->hq_tail == NULL && q->lq_tail == NULL);
212 nd = NULL;
213 }
214 return(nd);
215 }
216
217 /* This never gets used!! No loss (I hope) if we don't include it... GO */
218 #if !defined(__NetBSD__) && !defined(_KERNEL)
219
220 static RF_DiskQueueData_t *n_in_q(headp, tailp, countp, n, deq)
221 RF_DiskQueueData_t **headp;
222 RF_DiskQueueData_t **tailp;
223 int *countp;
224 int n;
225 int deq;
226 {
227 RF_DiskQueueData_t *r, *s;
228 int i;
229
230 for(s=NULL,i=n,r=*headp;r;s=r,r=r->next) {
231 if (i == 0)
232 break;
233 i--;
234 }
235 RF_ASSERT(r != NULL);
236 if (deq == 0)
237 return(r);
238 if (s) {
239 s->next = r->next;
240 }
241 else {
242 *headp = r->next;
243 }
244 if (*tailp == r)
245 *tailp = s;
246 (*countp)--;
247 return(r);
248 }
249 #endif
250
251 #if !defined(KERNEL) && RF_INCLUDE_QUEUE_RANDOM > 0
252 RF_DiskQueueData_t *rf_RandomPeek(q_in)
253 void *q_in;
254 {
255 RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
256 RF_DiskQueueData_t *req;
257 int n;
258
259 if (q->hq_head) {
260 n = q->rval % q->hq_count;
261 req = n_in_q(&q->hq_head, &q->hq_tail, &q->hq_count, n, 0);
262 }
263 else {
264 RF_ASSERT(q->hq_count == 0);
265 if (q->lq_head == NULL) {
266 RF_ASSERT(q->lq_count == 0);
267 return(NULL);
268 }
269 n = q->rval % q->lq_count;
270 req = n_in_q(&q->lq_head, &q->lq_tail, &q->lq_count, n, 0);
271 }
272 RF_ASSERT((q->hq_count + q->lq_count) == req->queue->queueLength);
273 RF_ASSERT(req != NULL);
274 return(req);
275 }
276
277 RF_DiskQueueData_t *rf_RandomDequeue(q_in)
278 void *q_in;
279 {
280 RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
281 RF_DiskQueueData_t *req;
282 int n;
283
284 if (q->hq_head) {
285 n = q->rval % q->hq_count;
286 q->rval = (long)RF_STATIC_RANDOM();
287 req = n_in_q(&q->hq_head, &q->hq_tail, &q->hq_count, n, 1);
288 }
289 else {
290 RF_ASSERT(q->hq_count == 0);
291 if (q->lq_head == NULL) {
292 RF_ASSERT(q->lq_count == 0);
293 return(NULL);
294 }
295 n = q->rval % q->lq_count;
296 q->rval = (long)RF_STATIC_RANDOM();
297 req = n_in_q(&q->lq_head, &q->lq_tail, &q->lq_count, n, 1);
298 }
299 RF_ASSERT((q->hq_count + q->lq_count) == (req->queue->queueLength-1));
300 return(req);
301 }
302 #endif /* !KERNEL && RF_INCLUDE_QUEUE_RANDOM > 0 */
303
304 /* Return ptr to item at head of queue. Used to examine request
305 * info without actually dequeueing the request.
306 */
307 RF_DiskQueueData_t *rf_FifoPeek(void *q_in)
308 {
309 RF_DiskQueueData_t *headElement = NULL;
310 RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
311
312 RF_ASSERT(q);
313 if (q->hq_head)
314 headElement = q->hq_head;
315 else if (q->lq_head)
316 headElement = q->lq_head;
317 return(headElement);
318 }
319
320 /* We sometimes need to promote a low priority access to a regular priority access.
321 * Currently, this is only used when the user wants to write a stripe which is currently
322 * under reconstruction.
323 * This routine will promote all accesses tagged with the indicated parityStripeID from
324 * the low priority queue to the end of the normal priority queue.
325 * We assume the queue is locked upon entry.
326 */
327 int rf_FifoPromote(q_in, parityStripeID, which_ru)
328 void *q_in;
329 RF_StripeNum_t parityStripeID;
330 RF_ReconUnitNum_t which_ru;
331 {
332 RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
333 RF_DiskQueueData_t *lp = q->lq_head, *pt = NULL; /* lp = lo-pri queue pointer, pt = trailer */
334 int retval = 0;
335
336 while (lp) {
337
338 /* search for the indicated parity stripe in the low-pri queue */
339 if (lp->parityStripeID == parityStripeID && lp->which_ru == which_ru) {
340 /*printf("FifoPromote: promoting access for psid %ld\n",parityStripeID);*/
341 if (pt) pt->next = lp->next; /* delete an entry other than the first */
342 else q->lq_head = lp->next; /* delete the head entry */
343
344 if (!q->lq_head) q->lq_tail = NULL; /* we deleted the only entry */
345 else if (lp == q->lq_tail) q->lq_tail = pt; /* we deleted the tail entry */
346
347 lp->next = NULL;
348 q->lq_count--;
349
350 if (q->hq_tail) {q->hq_tail->next = lp; q->hq_tail = lp;} /* append to hi-priority queue */
351 else {q->hq_head = q->hq_tail = lp;}
352 q->hq_count++;
353
354 /*UpdateShortestSeekFinishTimeForced(lp->requestPtr, lp->diskState);*/ /* deal with this later, if ever */
355
356 lp = (pt) ? pt->next : q->lq_head; /* reset low-pri pointer and continue */
357 retval++;
358
359 } else {pt = lp; lp = lp->next;}
360 }
361
362 /* sanity check. delete this if you ever put more than one entry in the low-pri queue */
363 RF_ASSERT(retval == 0 || retval == 1);
364 if (rf_fifoDebug) {
365 int tid;
366 rf_get_threadid(tid);
367 printf("[%d] fifo: promote %d\n", tid, retval);
368 }
369 return(retval);
370 }
371