ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo2l.c
Revision: 3.2
Committed: Fri Oct 31 11:44:09 1997 UTC (26 years, 5 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.1: +9 -6 lines
Log Message:
initial bug fixes

File Contents

# Content
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ SGI";
5 #endif
6
7 /*
8 * Routines for local rtrace execution
9 */
10
11 #include "rholo.h"
12 #include "random.h"
13 #include <signal.h>
14 #include <sys/time.h>
15
16 #ifndef MAXPROC
17 #define MAXPROC 16
18 #endif
19
20 static char PFILE[] = "/usr/tmp/RHpersist"; /* persist file name */
21
22 static int rtpd[MAXPROC][3]; /* process descriptors */
23 static float *rtbuf = NULL; /* allocated i/o buffer */
24 static int maxqlen = 0; /* maximum packets per queue */
25 static int nprocs = 0; /* number of processes */
26
27 static PACKET *pqueue[MAXPROC]; /* packet queues */
28 static int pqlen[MAXPROC]; /* packet queue lengths */
29
30
31 int
32 start_rtrace() /* start rtrace process */
33 {
34 static char buf1[8];
35 int rmaxpack = 0;
36 int psiz, npt, n;
37 /* get number of processes */
38 if ((npt = ncprocs) <= 0)
39 return(0);
40 if (npt > MAXPROC) {
41 sprintf(errmsg,
42 "number of rtrace processes reduced from %d to %d",
43 npt, MAXPROC);
44 error(WARNING, errmsg);
45 npt = MAXPROC;
46 }
47 /* add compulsory options */
48 rtargv[rtargc++] = "-i-";
49 rtargv[rtargc++] = "-I-";
50 rtargv[rtargc++] = "-h-";
51 rtargv[rtargc++] = "-ld-";
52 sprintf(buf1, "%d", RPACKSIZ);
53 rtargv[rtargc++] = "-x"; rtargv[rtargc++] = buf1;
54 rtargv[rtargc++] = "-y"; rtargv[rtargc++] = "0";
55 rtargv[rtargc++] = "-fff";
56 rtargv[rtargc++] = "-ovl";
57 rtargv[rtargc++] = nowarn ? "-w-" : "-w+";
58 if (npt > 1) {
59 rtargv[rtargc++] = "-PP"; rtargv[rtargc++] = PFILE;
60 }
61 rtargv[rtargc++] = vval(OCTREE);
62 rtargv[rtargc] = NULL;
63 maxqlen = 0;
64 for (nprocs = 0; nprocs < npt; nprocs++) { /* spawn children */
65 psiz = open_process(rtpd[nprocs], rtargv);
66 if (psiz <= 0)
67 error(SYSTEM, "cannot start rtrace process");
68 n = psiz/(RPACKSIZ*6*sizeof(float));
69 if (maxqlen == 0) {
70 if (!(maxqlen = n))
71 error(INTERNAL,
72 "bad pipe buffer size assumption");
73 } else if (n != maxqlen)
74 error(INTERNAL, "varying pipe buffer size!");
75 rmaxpack += n;
76 }
77 rtbuf = (float *)malloc(RPACKSIZ*6*sizeof(float)*maxqlen);
78 if (rtbuf == NULL)
79 error(SYSTEM, "malloc failure in start_rtrace");
80 return(rmaxpack);
81 }
82
83
84 static int
85 bestout() /* get best process to process packet */
86 {
87 int cnt;
88 register int pn, i;
89
90 pn = 0; /* find shortest queue */
91 for (i = 1; i < nprocs; i++)
92 if (pqlen[i] < pqlen[pn])
93 pn = i;
94 /* sanity check */
95 if (pqlen[pn] == maxqlen)
96 return(-1);
97 cnt = 0; /* count number of ties */
98 for (i = pn; i < nprocs; i++)
99 if (pqlen[i] == pqlen[pn])
100 cnt++;
101 /* break ties fairly */
102 if ((cnt = random() % cnt))
103 for (i = pn; i < nprocs; i++)
104 if (pqlen[i] == pqlen[pn] && !cnt--)
105 return(i);
106 return(pn);
107 }
108
109
110 int
111 slots_avail() /* count packet slots available */
112 {
113 register int nslots = 0;
114 register int i;
115
116 for (i = nprocs; i--; )
117 nslots += maxqlen - pqlen[i];
118 return(nslots);
119 }
120
121
122 queue_packet(p) /* queue up a beam packet */
123 register PACKET *p;
124 {
125 int pn, n;
126 /* determine process to write to */
127 if ((pn = bestout()) < 0)
128 error(INTERNAL, "rtrace input queues are full!");
129 /* write out the packet */
130 packrays(rtbuf, p);
131 if ((n = p->nr) < RPACKSIZ) /* add flush block? */
132 bzero((char *)(rtbuf+6*n++), 6*sizeof(float));
133 if (writebuf(rtpd[pn][1], (char *)rtbuf, 6*sizeof(float)*n) < 0)
134 error(SYSTEM, "write error in queue_packet");
135 p->next = NULL;
136 if (!pqlen[pn]++) /* add it to the end of the queue */
137 pqueue[pn] = p;
138 else {
139 register PACKET *rpl = pqueue[pn];
140 while (rpl->next != NULL)
141 rpl = rpl->next;
142 rpl->next = p;
143 }
144 }
145
146
147 PACKET *
148 get_packets(poll) /* read packets from rtrace processes */
149 int poll;
150 {
151 static struct timeval tpoll; /* zero timeval struct */
152 fd_set readset, errset;
153 PACKET *pldone = NULL, *plend;
154 register PACKET *p;
155 int n, nr;
156 register int pn;
157 float *bp;
158 /* prepare select call */
159 FD_ZERO(&readset); FD_ZERO(&errset); n = 0;
160 for (pn = nprocs; pn--; ) {
161 if (pqlen[pn])
162 FD_SET(rtpd[pn][0], &readset);
163 FD_SET(rtpd[pn][0], &errset);
164 if (rtpd[pn][0] >= n)
165 n = rtpd[pn][0] + 1;
166 }
167 /* make the call */
168 n = select(n, &readset, (fd_set *)NULL, &errset,
169 poll ? &tpoll : (struct timeval *)NULL);
170 if (n < 0) {
171 if (errno == EINTR) /* interrupted select call */
172 return(NULL);
173 error(SYSTEM, "select call failure in get_packets");
174 }
175 if (n == 0) /* is nothing ready? */
176 return(NULL);
177 /* make read call(s) */
178 for (pn = 0; pn < nprocs; pn++) {
179 if (!FD_ISSET(rtpd[pn][0], &readset) &&
180 !FD_ISSET(rtpd[pn][0], &errset))
181 continue;
182 reread:
183 n = read(rtpd[pn][0], (char *)rtbuf,
184 4*sizeof(float)*RPACKSIZ*pqlen[pn]);
185 if (n < 0) {
186 if (errno == EINTR | errno == EAGAIN)
187 goto reread;
188 error(SYSTEM, "read error in get_packets");
189 }
190 if (n == 0)
191 goto eoferr;
192 bp = rtbuf; /* finish processing */
193 for (p = pqueue[pn]; n && p != NULL; p = p->next) {
194 if ((nr = p->nr) < RPACKSIZ)
195 nr++; /* add flush block */
196 n -= 4*sizeof(float)*nr;
197 if (n < 0) { /* get remainder */
198 n += readbuf(rtpd[pn][0],
199 (char *)(bp+4*nr)+n, -n);
200 if (n)
201 goto eoferr;
202 }
203 donerays(p, bp);
204 bp += 4*nr;
205 pqlen[pn]--;
206 }
207 if (n) /* read past end? */
208 error(INTERNAL, "packet sync error in get_packets");
209 /* take from queue */
210 if (pldone == NULL)
211 pldone = plend = pqueue[pn];
212 else
213 plend->next = pqueue[pn];
214 while (plend->next != p)
215 plend = plend->next;
216 plend->next = NULL;
217 pqueue[pn] = p;
218 }
219 return(pldone); /* return finished packets */
220 eoferr:
221 error(USER, "unexpected EOF from rtrace process");
222 }
223
224
225 PACKET *
226 do_packets(pl) /* queue a packet list, return finished */
227 register PACKET *pl;
228 {
229 register PACKET *p;
230 /* consistency check */
231 if (nprocs < 1)
232 error(CONSISTENCY, "do_packets called with no active process");
233 /* queue each new packet */
234 while (pl != NULL) {
235 p = pl; pl = p->next; p->next = NULL;
236 queue_packet(p);
237 }
238 return(get_packets(slots_avail())); /* return processed packets */
239 }
240
241
242 PACKET *
243 flush_queue() /* empty all rtrace queues */
244 {
245 PACKET *rpdone = NULL;
246 register PACKET *rpl;
247 float *bp;
248 register PACKET *p;
249 int i, n, nr;
250
251 for (i = 0; i < nprocs; i++)
252 if (pqlen[i]) {
253 if (rpdone == NULL) { /* tack on queue */
254 rpdone = rpl = pqueue[i];
255 nr = rpl->nr;
256 } else {
257 rpl->next = pqueue[i];
258 nr = 0;
259 }
260 while (rpl->next != NULL) {
261 nr += (rpl = rpl->next)->nr;
262 if (rpl->nr < RPACKSIZ)
263 nr++; /* add flush block */
264 }
265 n = readbuf(rtpd[i][0], (char *)rtbuf,
266 4*sizeof(float)*nr);
267 if (n < 0)
268 error(SYSTEM, "read failure in flush_queue");
269 bp = rtbuf; /* process packets */
270 for (p = pqueue[i]; p != NULL; p = p->next) {
271 if ((nr = p->nr) < RPACKSIZ)
272 nr++; /* add flush block */
273 n -= 4*sizeof(float)*nr;
274 if (n >= 0)
275 donerays(p, bp);
276 else
277 p->nr = 0; /* short data error */
278 bp += 4*nr;
279 }
280 pqueue[i] = NULL; /* zero this queue */
281 pqlen[i] = 0;
282 }
283 return(rpdone); /* return all packets completed */
284 }
285
286
287 static
288 killpersist() /* kill persistent process */
289 {
290 FILE *fp;
291 int pid;
292
293 if ((fp = fopen(PFILE, "r")) == NULL)
294 return;
295 if (fscanf(fp, "%*s %d", &pid) != 1 || kill(pid, SIGALRM) < 0)
296 unlink(PFILE);
297 fclose(fp);
298 }
299
300
301 int
302 end_rtrace() /* close rtrace process(es) */
303 {
304 int status = 0, rv;
305
306 if (nprocs > 1)
307 killpersist();
308 while (nprocs > 0) {
309 rv = close_process(rtpd[--nprocs]);
310 if (rv > 0)
311 status = rv;
312 }
313 free((char *)rtbuf);
314 rtbuf = NULL;
315 maxqlen = 0;
316 return(status);
317 }