ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo2l.c
(Generate patch)

Comparing ray/src/hd/rholo2l.c (file contents):
Revision 3.2 by gregl, Fri Oct 31 11:44:09 1997 UTC vs.
Revision 3.20 by greg, Sat Dec 16 18:33:55 2023 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Routines for local rtrace execution
6   */
7  
11 #include "rholo.h"
12 #include "random.h"
8   #include <signal.h>
9   #include <sys/time.h>
10 + #include <string.h>
11  
12 + #include "rholo.h"
13 + #include "random.h"
14 + #include "paths.h"
15 + #include "selcall.h"
16 + #include "rtprocess.h"
17 +
18   #ifndef MAXPROC
19 < #define MAXPROC         16
19 > #define MAXPROC         64
20   #endif
21  
22 < static char     PFILE[] = "/usr/tmp/RHpersist"; /* persist file name */
22 > int     nprocs = 0;                             /* running process count */
23  
24 < static int      rtpd[MAXPROC][3];               /* process descriptors */
24 > static char     pfile[] = TEMPLATE;             /* persist file name */
25 >
26 > static SUBPROC  rtpd[MAXPROC];          /* process descriptors */
27   static float    *rtbuf = NULL;                  /* allocated i/o buffer */
28   static int      maxqlen = 0;                    /* maximum packets per queue */
25 static int      nprocs = 0;                     /* number of processes */
29  
30   static PACKET   *pqueue[MAXPROC];               /* packet queues */
31   static int      pqlen[MAXPROC];                 /* packet queue lengths */
32  
33 + static int bestout(void);
34 + static int slots_avail(void);
35 + static void queue_packet(PACKET *p);
36 + static PACKET * get_packets(int poll);
37 + static void killpersist(void);
38  
39 +
40   int
41 < start_rtrace()                  /* start rtrace process */
41 > start_rtrace(void)                      /* start rtrace process */
42   {
43          static char     buf1[8];
44          int     rmaxpack = 0;
45 <        int     psiz, npt, n;
45 >        int     psiz, n;
46                                          /* get number of processes */
47 <        if ((npt = ncprocs) <= 0)
47 >        if (ncprocs <= 0 || nprocs > 0)
48                  return(0);
49 <        if (npt > MAXPROC) {
49 >        if (ncprocs > MAXPROC) {
50                  sprintf(errmsg,
51                          "number of rtrace processes reduced from %d to %d",
52 <                                npt, MAXPROC);
52 >                                ncprocs, MAXPROC);
53                  error(WARNING, errmsg);
54 <                npt = MAXPROC;
54 >                ncprocs = MAXPROC;
55          }
56 <                                        /* add compulsory options */
57 <        rtargv[rtargc++] = "-i-";
58 <        rtargv[rtargc++] = "-I-";
59 <        rtargv[rtargc++] = "-h-";
60 <        rtargv[rtargc++] = "-ld-";
61 <        sprintf(buf1, "%d", RPACKSIZ);
62 <        rtargv[rtargc++] = "-x"; rtargv[rtargc++] = buf1;
63 <        rtargv[rtargc++] = "-y"; rtargv[rtargc++] = "0";
64 <        rtargv[rtargc++] = "-fff";
65 <        rtargv[rtargc++] = "-ovl";
66 <        rtargv[rtargc++] = nowarn ? "-w-" : "-w+";
67 <        if (npt > 1) {
68 <                rtargv[rtargc++] = "-PP"; rtargv[rtargc++] = PFILE;
56 >        if (rtargv[rtargc-1] != vval(OCTREE)) {
57 >                                                /* add compulsory options */
58 >                rtargv[rtargc++] = "-i-";
59 >                rtargv[rtargc++] = "-I-";
60 >                rtargv[rtargc++] = "-h-";
61 >                rtargv[rtargc++] = "-ld-";
62 >                rtargv[rtargc++] = "-co-";
63 >                sprintf(buf1, "%d", RPACKSIZ);
64 >                rtargv[rtargc++] = "-x"; rtargv[rtargc++] = buf1;
65 >                rtargv[rtargc++] = "-y"; rtargv[rtargc++] = "0";
66 >                rtargv[rtargc++] = "-fff";
67 >                rtargv[rtargc++] = vbool(VDIST) ? "-ovl" : "-ovL";
68 >                if (nowarn)
69 >                        rtargv[rtargc++] = "-w-";
70 >                if (ncprocs > 1) {
71 >                        mktemp(pfile);
72 >                        rtargv[rtargc++] = "-PP"; rtargv[rtargc++] = pfile;
73 >                }
74 >                rtargv[rtargc++] = vval(OCTREE);
75 >                rtargv[rtargc] = NULL;
76          }
61        rtargv[rtargc++] = vval(OCTREE);
62        rtargv[rtargc] = NULL;
77          maxqlen = 0;
78 <        for (nprocs = 0; nprocs < npt; nprocs++) {      /* spawn children */
79 <                psiz = open_process(rtpd[nprocs], rtargv);
78 >        for (nprocs = 0; nprocs < ncprocs; nprocs++) {  /* spawn children */
79 >                psiz = open_process(&rtpd[nprocs], rtargv);
80                  if (psiz <= 0)
81                          error(SYSTEM, "cannot start rtrace process");
82                  n = psiz/(RPACKSIZ*6*sizeof(float));
# Line 70 | Line 84 | start_rtrace()                 /* start rtrace process */
84                          if (!(maxqlen = n))
85                                  error(INTERNAL,
86                                          "bad pipe buffer size assumption");
87 +                        sleep(2);
88                  } else if (n != maxqlen)
89                          error(INTERNAL, "varying pipe buffer size!");
90                  rmaxpack += n;
# Line 82 | Line 97 | start_rtrace()                 /* start rtrace process */
97  
98  
99   static int
100 < bestout()                       /* get best process to process packet */
100 > bestout(void)                   /* get best process to process packet */
101   {
102          int     cnt;
103 <        register int    pn, i;
103 >        int     pn, i;
104  
105          pn = 0;                 /* find shortest queue */
106          for (i = 1; i < nprocs; i++)
# Line 99 | Line 114 | bestout()                      /* get best process to process packet */
114                  if (pqlen[i] == pqlen[pn])
115                          cnt++;
116                                  /* break ties fairly */
117 <        if ((cnt = random() % cnt))
117 >        if ((cnt = irandom(cnt)))
118                  for (i = pn; i < nprocs; i++)
119                          if (pqlen[i] == pqlen[pn] && !cnt--)
120                                  return(i);
# Line 107 | Line 122 | bestout()                      /* get best process to process packet */
122   }
123  
124  
125 < int
126 < slots_avail()                   /* count packet slots available */
125 > static int
126 > slots_avail(void)                       /* count packet slots available */
127   {
128 <        register int    nslots = 0;
129 <        register int    i;
128 >        int     nslots = 0;
129 >        int     i;
130  
131          for (i = nprocs; i--; )
132                  nslots += maxqlen - pqlen[i];
# Line 119 | Line 134 | slots_avail()                  /* count packet slots available */
134   }
135  
136  
137 < queue_packet(p)                 /* queue up a beam packet */
138 < register PACKET *p;
137 > static void
138 > queue_packet(                   /* queue up a beam packet */
139 >        PACKET  *p
140 > )
141   {
142          int     pn, n;
143                                  /* determine process to write to */
# Line 129 | Line 146 | register PACKET        *p;
146                                  /* write out the packet */
147          packrays(rtbuf, p);
148          if ((n = p->nr) < RPACKSIZ)     /* add flush block? */
149 <                bzero((char *)(rtbuf+6*n++), 6*sizeof(float));
150 <        if (writebuf(rtpd[pn][1], (char *)rtbuf, 6*sizeof(float)*n) < 0)
149 >                memset((char *)(rtbuf+6*n++), '\0', 6*sizeof(float));
150 >        if (writebuf(rtpd[pn].w, (char *)rtbuf, 6*sizeof(float)*n) < 0)
151                  error(SYSTEM, "write error in queue_packet");
152          p->next = NULL;
153          if (!pqlen[pn]++)       /* add it to the end of the queue */
154                  pqueue[pn] = p;
155          else {
156 <                register PACKET *rpl = pqueue[pn];
156 >                PACKET  *rpl = pqueue[pn];
157                  while (rpl->next != NULL)
158                          rpl = rpl->next;
159                  rpl->next = p;
# Line 144 | Line 161 | register PACKET        *p;
161   }
162  
163  
164 < PACKET *
165 < get_packets(poll)               /* read packets from rtrace processes */
166 < int     poll;
164 > static PACKET *
165 > get_packets(            /* read packets from rtrace processes */
166 >        int     poll
167 > )
168   {
169          static struct timeval   tpoll;  /* zero timeval struct */
170          fd_set  readset, errset;
171          PACKET  *pldone = NULL, *plend;
172 <        register PACKET *p;
172 >        PACKET  *p;
173          int     n, nr;
174 <        register int    pn;
174 >        int     pn;
175          float   *bp;
176                                          /* prepare select call */
177          FD_ZERO(&readset); FD_ZERO(&errset); n = 0;
178          for (pn = nprocs; pn--; ) {
179                  if (pqlen[pn])
180 <                        FD_SET(rtpd[pn][0], &readset);
181 <                FD_SET(rtpd[pn][0], &errset);
182 <                if (rtpd[pn][0] >= n)
183 <                        n = rtpd[pn][0] + 1;
180 >                        FD_SET(rtpd[pn].r, &readset);
181 >                FD_SET(rtpd[pn].r, &errset);
182 >                if (rtpd[pn].r >= n)
183 >                        n = rtpd[pn].r + 1;
184          }
185                                          /* make the call */
186          n = select(n, &readset, (fd_set *)NULL, &errset,
# Line 176 | Line 194 | int    poll;
194                  return(NULL);
195                                          /* make read call(s) */
196          for (pn = 0; pn < nprocs; pn++) {
197 <                if (!FD_ISSET(rtpd[pn][0], &readset) &&
198 <                                !FD_ISSET(rtpd[pn][0], &errset))
197 >                if (!FD_ISSET(rtpd[pn].r, &readset) &&
198 >                                !FD_ISSET(rtpd[pn].r, &errset))
199                          continue;
200          reread:
201 <                n = read(rtpd[pn][0], (char *)rtbuf,
201 >                n = read(rtpd[pn].r, (char *)rtbuf,
202                                  4*sizeof(float)*RPACKSIZ*pqlen[pn]);
203                  if (n < 0) {
204 <                        if (errno == EINTR | errno == EAGAIN)
204 >                        if ((errno == EINTR) | (errno == EAGAIN))
205                                  goto reread;
206                          error(SYSTEM, "read error in get_packets");
207                  }
# Line 195 | Line 213 | int    poll;
213                                  nr++;                   /* add flush block */
214                          n -= 4*sizeof(float)*nr;
215                          if (n < 0) {                    /* get remainder */
216 <                                n += readbuf(rtpd[pn][0],
216 >                                n += readbuf(rtpd[pn].r,
217                                                  (char *)(bp+4*nr)+n, -n);
218                                  if (n)
219                                          goto eoferr;
# Line 218 | Line 236 | int    poll;
236          }
237          return(pldone);                         /* return finished packets */
238   eoferr:
239 <        error(USER, "unexpected EOF from rtrace process");
239 >        error(USER, "rtrace process died");
240 >        return NULL; /* pro forma return */
241   }
242  
243  
244   PACKET *
245 < do_packets(pl)                  /* queue a packet list, return finished */
246 < register PACKET *pl;
245 > do_packets(                     /* queue a packet list, return finished */
246 >        PACKET  *pl
247 > )
248   {
249 <        register PACKET *p;
249 >        PACKET  *p;
250                                          /* consistency check */
251          if (nprocs < 1)
252                  error(CONSISTENCY, "do_packets called with no active process");
# Line 240 | Line 260 | register PACKET        *pl;
260  
261  
262   PACKET *
263 < flush_queue()                   /* empty all rtrace queues */
263 > flush_queue(void)                       /* empty all rtrace queues */
264   {
265          PACKET  *rpdone = NULL;
266 <        register PACKET *rpl;
266 >        PACKET  *rpl = NULL;
267          float   *bp;
268 <        register PACKET *p;
268 >        PACKET  *p;
269          int     i, n, nr;
270  
271          for (i = 0; i < nprocs; i++)
272                  if (pqlen[i]) {
273                          if (rpdone == NULL) {           /* tack on queue */
274                                  rpdone = rpl = pqueue[i];
275 <                                nr = rpl->nr;
275 >                                if ((nr = rpl->nr) < RPACKSIZ) nr++;
276                          } else {
277                                  rpl->next = pqueue[i];
278                                  nr = 0;
# Line 262 | Line 282 | flush_queue()                  /* empty all rtrace queues */
282                                  if (rpl->nr < RPACKSIZ)
283                                          nr++;           /* add flush block */
284                          }
285 <                        n = readbuf(rtpd[i][0], (char *)rtbuf,
285 >                        n = readbuf(rtpd[i].r, (char *)rtbuf,
286                                          4*sizeof(float)*nr);
287                          if (n < 0)
288                                  error(SYSTEM, "read failure in flush_queue");
# Line 284 | Line 304 | flush_queue()                  /* empty all rtrace queues */
304   }
305  
306  
307 < static
308 < killpersist()                   /* kill persistent process */
307 > static void
308 > killpersist(void)                       /* kill persistent process */
309   {
310          FILE    *fp;
311          int     pid;
312  
313 <        if ((fp = fopen(PFILE, "r")) == NULL)
313 >        if ((fp = fopen(pfile, "r")) == NULL)
314                  return;
315          if (fscanf(fp, "%*s %d", &pid) != 1 || kill(pid, SIGALRM) < 0)
316 <                unlink(PFILE);
316 >                unlink(pfile);
317          fclose(fp);
318   }
319  
320  
321   int
322 < end_rtrace()                    /* close rtrace process(es) */
322 > end_rtrace(void)                        /* close rtrace process(es) */
323   {
324          int     status = 0, rv;
325  
326          if (nprocs > 1)
327                  killpersist();
328 <        while (nprocs > 0) {
329 <                rv = close_process(rtpd[--nprocs]);
330 <                if (rv > 0)
311 <                        status = rv;
312 <        }
313 <        free((char *)rtbuf);
328 >        status = close_processes(rtpd, nprocs);
329 >        nprocs = 0;
330 >        free((void *)rtbuf);
331          rtbuf = NULL;
332          maxqlen = 0;
333          return(status);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines