ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo2l.c
Revision: 3.4
Committed: Mon Nov 3 18:33:13 1997 UTC (26 years, 5 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.3: +1 -1 lines
Log Message:
added more routines for display support

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