rf_fifo.c revision 1.3.8.1 1 /* $NetBSD: rf_fifo.c,v 1.3.8.1 2000/11/20 11:42:54 bouyer 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
150 /* Return ptr to item at head of queue. Used to examine request
151 * info without actually dequeueing the request.
152 */
153 RF_DiskQueueData_t *
154 rf_FifoPeek(void *q_in)
155 {
156 RF_DiskQueueData_t *headElement = NULL;
157 RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
158
159 RF_ASSERT(q);
160 if (q->hq_head)
161 headElement = q->hq_head;
162 else
163 if (q->lq_head)
164 headElement = q->lq_head;
165 return (headElement);
166 }
167 /* We sometimes need to promote a low priority access to a regular priority access.
168 * Currently, this is only used when the user wants to write a stripe which is currently
169 * under reconstruction.
170 * This routine will promote all accesses tagged with the indicated parityStripeID from
171 * the low priority queue to the end of the normal priority queue.
172 * We assume the queue is locked upon entry.
173 */
174 int
175 rf_FifoPromote(q_in, parityStripeID, which_ru)
176 void *q_in;
177 RF_StripeNum_t parityStripeID;
178 RF_ReconUnitNum_t which_ru;
179 {
180 RF_FifoHeader_t *q = (RF_FifoHeader_t *) q_in;
181 RF_DiskQueueData_t *lp = q->lq_head, *pt = NULL; /* lp = lo-pri queue
182 * pointer, pt = trailer */
183 int retval = 0;
184
185 while (lp) {
186
187 /* search for the indicated parity stripe in the low-pri queue */
188 if (lp->parityStripeID == parityStripeID && lp->which_ru == which_ru) {
189 /* printf("FifoPromote: promoting access for psid
190 * %ld\n",parityStripeID); */
191 if (pt)
192 pt->next = lp->next; /* delete an entry other
193 * than the first */
194 else
195 q->lq_head = lp->next; /* delete the head entry */
196
197 if (!q->lq_head)
198 q->lq_tail = NULL; /* we deleted the only
199 * entry */
200 else
201 if (lp == q->lq_tail)
202 q->lq_tail = pt; /* we deleted the tail
203 * entry */
204
205 lp->next = NULL;
206 q->lq_count--;
207
208 if (q->hq_tail) {
209 q->hq_tail->next = lp;
210 q->hq_tail = lp;
211 }
212 /* append to hi-priority queue */
213 else {
214 q->hq_head = q->hq_tail = lp;
215 }
216 q->hq_count++;
217
218 /* UpdateShortestSeekFinishTimeForced(lp->requestPtr,
219 * lp->diskState); *//* deal with this later, if ever */
220
221 lp = (pt) ? pt->next : q->lq_head; /* reset low-pri pointer
222 * and continue */
223 retval++;
224
225 } else {
226 pt = lp;
227 lp = lp->next;
228 }
229 }
230
231 /* sanity check. delete this if you ever put more than one entry in
232 * the low-pri queue */
233 RF_ASSERT(retval == 0 || retval == 1);
234 return (retval);
235 }
236