rf_fifo.c revision 1.4 1 /* $NetBSD: rf_fifo.c,v 1.4 2000/01/08 23:45:05 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 #include "rf_types.h"
39 #include "rf_alloclist.h"
40 #include "rf_stripelocks.h"
41 #include "rf_layout.h"
42 #include "rf_diskqueue.h"
43 #include "rf_fifo.h"
44 #include "rf_debugMem.h"
45 #include "rf_general.h"
46 #include "rf_options.h"
47 #include "rf_raid.h"
48 #include "rf_types.h"
49
50 /* just malloc a header, zero it (via calloc), and return it */
51 /*ARGSUSED*/
52 void *
53 rf_FifoCreate(sectPerDisk, clList, listp)
54 RF_SectorCount_t sectPerDisk;
55 RF_AllocListElem_t *clList;
56 RF_ShutdownList_t **listp;
57 {
58 RF_FifoHeader_t *q;
59
60 RF_CallocAndAdd(q, 1, sizeof(RF_FifoHeader_t), (RF_FifoHeader_t *), clList);
61 q->hq_count = q->lq_count = 0;
62 return ((void *) q);
63 }
64
65 void
66 rf_FifoEnqueue(q_in, elem, priority)
67 void *q_in;
68 RF_DiskQueueData_t *elem;
69 int priority;
70 {
71 RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
72
73 RF_ASSERT(priority == RF_IO_NORMAL_PRIORITY || priority == RF_IO_LOW_PRIORITY);
74
75 elem->next = NULL;
76 if (priority == RF_IO_NORMAL_PRIORITY) {
77 if (!q->hq_tail) {
78 RF_ASSERT(q->hq_count == 0 && q->hq_head == NULL);
79 q->hq_head = q->hq_tail = elem;
80 } else {
81 RF_ASSERT(q->hq_count != 0 && q->hq_head != NULL);
82 q->hq_tail->next = elem;
83 q->hq_tail = elem;
84 }
85 q->hq_count++;
86 } else {
87 RF_ASSERT(elem->next == NULL);
88 if (rf_fifoDebug) {
89 printf("raid%d: fifo: ENQ lopri\n",
90 elem->raidPtr->raidid);
91 }
92 if (!q->lq_tail) {
93 RF_ASSERT(q->lq_count == 0 && q->lq_head == NULL);
94 q->lq_head = q->lq_tail = elem;
95 } else {
96 RF_ASSERT(q->lq_count != 0 && q->lq_head != NULL);
97 q->lq_tail->next = elem;
98 q->lq_tail = elem;
99 }
100 q->lq_count++;
101 }
102 if ((q->hq_count + q->lq_count) != elem->queue->queueLength) {
103 printf("Queue lengths differ!: %d %d %d\n",
104 q->hq_count, q->lq_count, (int) elem->queue->queueLength);
105 printf("%d %d %d %d\n",
106 (int) elem->queue->numOutstanding,
107 (int) elem->queue->maxOutstanding,
108 (int) elem->queue->row,
109 (int) elem->queue->col);
110 }
111 RF_ASSERT((q->hq_count + q->lq_count) == elem->queue->queueLength);
112 }
113
114 RF_DiskQueueData_t *
115 rf_FifoDequeue(q_in)
116 void *q_in;
117 {
118 RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
119 RF_DiskQueueData_t *nd;
120
121 RF_ASSERT(q);
122 if (q->hq_head) {
123 RF_ASSERT(q->hq_count != 0 && q->hq_tail != NULL);
124 nd = q->hq_head;
125 q->hq_head = q->hq_head->next;
126 if (!q->hq_head)
127 q->hq_tail = NULL;
128 nd->next = NULL;
129 q->hq_count--;
130 } else
131 if (q->lq_head) {
132 RF_ASSERT(q->lq_count != 0 && q->lq_tail != NULL);
133 nd = q->lq_head;
134 q->lq_head = q->lq_head->next;
135 if (!q->lq_head)
136 q->lq_tail = NULL;
137 nd->next = NULL;
138 q->lq_count--;
139 if (rf_fifoDebug) {
140 printf("raid%d: fifo: DEQ lopri %lx\n",
141 nd->raidPtr->raidid, (long) nd);
142 }
143 } else {
144 RF_ASSERT(q->hq_count == 0 && q->lq_count == 0 && q->hq_tail == NULL && q->lq_tail == NULL);
145 nd = NULL;
146 }
147 return (nd);
148 }
149 /* This never gets used!! No loss (I hope) if we don't include it... GO */
150 #if !defined(__NetBSD__) && !defined(_KERNEL)
151
152 static RF_DiskQueueData_t *
153 n_in_q(headp, tailp, countp, n, deq)
154 RF_DiskQueueData_t **headp;
155 RF_DiskQueueData_t **tailp;
156 int *countp;
157 int n;
158 int deq;
159 {
160 RF_DiskQueueData_t *r, *s;
161 int i;
162
163 for (s = NULL, i = n, r = *headp; r; s = r, r = r->next) {
164 if (i == 0)
165 break;
166 i--;
167 }
168 RF_ASSERT(r != NULL);
169 if (deq == 0)
170 return (r);
171 if (s) {
172 s->next = r->next;
173 } else {
174 *headp = r->next;
175 }
176 if (*tailp == r)
177 *tailp = s;
178 (*countp)--;
179 return (r);
180 }
181 #endif
182
183 #if !defined(KERNEL) && RF_INCLUDE_QUEUE_RANDOM > 0
184 RF_DiskQueueData_t *
185 rf_RandomPeek(q_in)
186 void *q_in;
187 {
188 RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
189 RF_DiskQueueData_t *req;
190 int n;
191
192 if (q->hq_head) {
193 n = q->rval % q->hq_count;
194 req = n_in_q(&q->hq_head, &q->hq_tail, &q->hq_count, n, 0);
195 } else {
196 RF_ASSERT(q->hq_count == 0);
197 if (q->lq_head == NULL) {
198 RF_ASSERT(q->lq_count == 0);
199 return (NULL);
200 }
201 n = q->rval % q->lq_count;
202 req = n_in_q(&q->lq_head, &q->lq_tail, &q->lq_count, n, 0);
203 }
204 RF_ASSERT((q->hq_count + q->lq_count) == req->queue->queueLength);
205 RF_ASSERT(req != NULL);
206 return (req);
207 }
208
209 RF_DiskQueueData_t *
210 rf_RandomDequeue(q_in)
211 void *q_in;
212 {
213 RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
214 RF_DiskQueueData_t *req;
215 int n;
216
217 if (q->hq_head) {
218 n = q->rval % q->hq_count;
219 q->rval = (long) RF_STATIC_RANDOM();
220 req = n_in_q(&q->hq_head, &q->hq_tail, &q->hq_count, n, 1);
221 } else {
222 RF_ASSERT(q->hq_count == 0);
223 if (q->lq_head == NULL) {
224 RF_ASSERT(q->lq_count == 0);
225 return (NULL);
226 }
227 n = q->rval % q->lq_count;
228 q->rval = (long) RF_STATIC_RANDOM();
229 req = n_in_q(&q->lq_head, &q->lq_tail, &q->lq_count, n, 1);
230 }
231 RF_ASSERT((q->hq_count + q->lq_count) == (req->queue->queueLength - 1));
232 return (req);
233 }
234 #endif /* !KERNEL && RF_INCLUDE_QUEUE_RANDOM > 0 */
235
236 /* Return ptr to item at head of queue. Used to examine request
237 * info without actually dequeueing the request.
238 */
239 RF_DiskQueueData_t *
240 rf_FifoPeek(void *q_in)
241 {
242 RF_DiskQueueData_t *headElement = NULL;
243 RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
244
245 RF_ASSERT(q);
246 if (q->hq_head)
247 headElement = q->hq_head;
248 else
249 if (q->lq_head)
250 headElement = q->lq_head;
251 return (headElement);
252 }
253 /* We sometimes need to promote a low priority access to a regular priority access.
254 * Currently, this is only used when the user wants to write a stripe which is currently
255 * under reconstruction.
256 * This routine will promote all accesses tagged with the indicated parityStripeID from
257 * the low priority queue to the end of the normal priority queue.
258 * We assume the queue is locked upon entry.
259 */
260 int
261 rf_FifoPromote(q_in, parityStripeID, which_ru)
262 void *q_in;
263 RF_StripeNum_t parityStripeID;
264 RF_ReconUnitNum_t which_ru;
265 {
266 RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
267 RF_DiskQueueData_t *lp = q->lq_head, *pt = NULL; /* lp = lo-pri queue
268 * pointer, pt = trailer */
269 int retval = 0;
270
271 while (lp) {
272
273 /* search for the indicated parity stripe in the low-pri queue */
274 if (lp->parityStripeID == parityStripeID && lp->which_ru == which_ru) {
275 /* printf("FifoPromote: promoting access for psid
276 * %ld\n",parityStripeID); */
277 if (pt)
278 pt->next = lp->next; /* delete an entry other
279 * than the first */
280 else
281 q->lq_head = lp->next; /* delete the head entry */
282
283 if (!q->lq_head)
284 q->lq_tail = NULL; /* we deleted the only
285 * entry */
286 else
287 if (lp == q->lq_tail)
288 q->lq_tail = pt; /* we deleted the tail
289 * entry */
290
291 lp->next = NULL;
292 q->lq_count--;
293
294 if (q->hq_tail) {
295 q->hq_tail->next = lp;
296 q->hq_tail = lp;
297 }
298 /* append to hi-priority queue */
299 else {
300 q->hq_head = q->hq_tail = lp;
301 }
302 q->hq_count++;
303
304 /* UpdateShortestSeekFinishTimeForced(lp->requestPtr,
305 * lp->diskState); *//* deal with this later, if ever */
306
307 lp = (pt) ? pt->next : q->lq_head; /* reset low-pri pointer
308 * and continue */
309 retval++;
310
311 } else {
312 pt = lp;
313 lp = lp->next;
314 }
315 }
316
317 /* sanity check. delete this if you ever put more than one entry in
318 * the low-pri queue */
319 RF_ASSERT(retval == 0 || retval == 1);
320 return (retval);
321 }
322