| 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 |  |  | 
| 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 <signal.h> | 
| 16 | < | #include <sys/time.h> | 
| 16 | > | #include "rtprocess.h" | 
| 17 |  |  | 
| 18 |  | #ifndef MAXPROC | 
| 19 |  | #define MAXPROC         64 | 
| 23 |  |  | 
| 24 |  | static char     pfile[] = TEMPLATE;             /* persist file name */ | 
| 25 |  |  | 
| 26 | < | static int      rtpd[MAXPROC][3];               /* process descriptors */ | 
| 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 */ | 
| 28 | > | static int      maxqlen;                        /* maximum packets per queue */ | 
| 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; | 
| 59 |  | rtargv[rtargc++] = "-I-"; | 
| 60 |  | rtargv[rtargc++] = "-h-"; | 
| 61 |  | rtargv[rtargc++] = "-ld-"; | 
| 62 | + | rtargv[rtargc++] = "-pRGB"; | 
| 63 |  | sprintf(buf1, "%d", RPACKSIZ); | 
| 64 |  | rtargv[rtargc++] = "-x"; rtargv[rtargc++] = buf1; | 
| 65 |  | rtargv[rtargc++] = "-y"; rtargv[rtargc++] = "0"; | 
| 76 |  | } | 
| 77 |  | maxqlen = 0; | 
| 78 |  | for (nprocs = 0; nprocs < ncprocs; nprocs++) {  /* spawn children */ | 
| 79 | < | psiz = open_process(rtpd[nprocs], rtargv); | 
| 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)); | 
| 83 | < | if (maxqlen == 0) { | 
| 84 | < | if (!(maxqlen = n)) | 
| 78 | < | error(INTERNAL, | 
| 79 | < | "bad pipe buffer size assumption"); | 
| 82 | > | n = psiz/(RPACKSIZ*6*sizeof(float)) + 1; | 
| 83 | > | if (!maxqlen) { | 
| 84 | > | maxqlen = n; | 
| 85 |  | sleep(2); | 
| 86 |  | } else if (n != maxqlen) | 
| 87 |  | error(INTERNAL, "varying pipe buffer size!"); | 
| 95 |  |  | 
| 96 |  |  | 
| 97 |  | static int | 
| 98 | < | bestout()                       /* get best process to process packet */ | 
| 98 | > | bestout(void)                   /* get best process to process packet */ | 
| 99 |  | { | 
| 100 |  | int     cnt; | 
| 101 | < | register int    pn, i; | 
| 101 | > | int     pn, i; | 
| 102 |  |  | 
| 103 |  | pn = 0;                 /* find shortest queue */ | 
| 104 |  | for (i = 1; i < nprocs; i++) | 
| 112 |  | if (pqlen[i] == pqlen[pn]) | 
| 113 |  | cnt++; | 
| 114 |  | /* break ties fairly */ | 
| 115 | < | if ((cnt = random() % cnt)) | 
| 115 | > | if ((cnt = irandom(cnt))) | 
| 116 |  | for (i = pn; i < nprocs; i++) | 
| 117 |  | if (pqlen[i] == pqlen[pn] && !cnt--) | 
| 118 |  | return(i); | 
| 120 |  | } | 
| 121 |  |  | 
| 122 |  |  | 
| 123 | < | int | 
| 124 | < | slots_avail()                   /* count packet slots available */ | 
| 123 | > | static int | 
| 124 | > | slots_avail(void)                       /* count packet slots available */ | 
| 125 |  | { | 
| 126 | < | register int    nslots = 0; | 
| 127 | < | register int    i; | 
| 126 | > | int     nslots = 0; | 
| 127 | > | int     i; | 
| 128 |  |  | 
| 129 |  | for (i = nprocs; i--; ) | 
| 130 |  | nslots += maxqlen - pqlen[i]; | 
| 132 |  | } | 
| 133 |  |  | 
| 134 |  |  | 
| 135 | < | queue_packet(p)                 /* queue up a beam packet */ | 
| 136 | < | register PACKET *p; | 
| 135 | > | static void | 
| 136 | > | queue_packet(                   /* queue up a beam packet */ | 
| 137 | > | PACKET  *p | 
| 138 | > | ) | 
| 139 |  | { | 
| 140 |  | int     pn, n; | 
| 141 |  | /* determine process to write to */ | 
| 144 |  | /* write out the packet */ | 
| 145 |  | packrays(rtbuf, p); | 
| 146 |  | if ((n = p->nr) < RPACKSIZ)     /* add flush block? */ | 
| 147 | < | bzero((char *)(rtbuf+6*n++), 6*sizeof(float)); | 
| 148 | < | if (writebuf(rtpd[pn][1], (char *)rtbuf, 6*sizeof(float)*n) < 0) | 
| 147 | > | memset((char *)(rtbuf+6*n++), '\0', 6*sizeof(float)); | 
| 148 | > | if (writebuf(rtpd[pn].w, rtbuf, 6*sizeof(float)*n) < 0) | 
| 149 |  | error(SYSTEM, "write error in queue_packet"); | 
| 150 |  | p->next = NULL; | 
| 151 |  | if (!pqlen[pn]++)       /* add it to the end of the queue */ | 
| 152 |  | pqueue[pn] = p; | 
| 153 |  | else { | 
| 154 | < | register PACKET *rpl = pqueue[pn]; | 
| 154 | > | PACKET  *rpl = pqueue[pn]; | 
| 155 |  | while (rpl->next != NULL) | 
| 156 |  | rpl = rpl->next; | 
| 157 |  | rpl->next = p; | 
| 159 |  | } | 
| 160 |  |  | 
| 161 |  |  | 
| 162 | < | PACKET * | 
| 163 | < | get_packets(poll)               /* read packets from rtrace processes */ | 
| 164 | < | int     poll; | 
| 162 | > | static PACKET * | 
| 163 | > | get_packets(            /* read packets from rtrace processes */ | 
| 164 | > | int     poll | 
| 165 | > | ) | 
| 166 |  | { | 
| 167 |  | static struct timeval   tpoll;  /* zero timeval struct */ | 
| 168 |  | fd_set  readset, errset; | 
| 169 |  | PACKET  *pldone = NULL, *plend; | 
| 170 | < | register PACKET *p; | 
| 170 | > | PACKET  *p; | 
| 171 |  | int     n, nr; | 
| 172 | < | register int    pn; | 
| 172 | > | int     pn; | 
| 173 |  | float   *bp; | 
| 174 |  | /* prepare select call */ | 
| 175 |  | FD_ZERO(&readset); FD_ZERO(&errset); n = 0; | 
| 176 |  | for (pn = nprocs; pn--; ) { | 
| 177 |  | if (pqlen[pn]) | 
| 178 | < | FD_SET(rtpd[pn][0], &readset); | 
| 179 | < | FD_SET(rtpd[pn][0], &errset); | 
| 180 | < | if (rtpd[pn][0] >= n) | 
| 181 | < | n = rtpd[pn][0] + 1; | 
| 178 | > | FD_SET(rtpd[pn].r, &readset); | 
| 179 | > | FD_SET(rtpd[pn].r, &errset); | 
| 180 | > | if (rtpd[pn].r >= n) | 
| 181 | > | n = rtpd[pn].r + 1; | 
| 182 |  | } | 
| 183 |  | /* make the call */ | 
| 184 |  | n = select(n, &readset, (fd_set *)NULL, &errset, | 
| 192 |  | return(NULL); | 
| 193 |  | /* make read call(s) */ | 
| 194 |  | for (pn = 0; pn < nprocs; pn++) { | 
| 195 | < | if (!FD_ISSET(rtpd[pn][0], &readset) && | 
| 196 | < | !FD_ISSET(rtpd[pn][0], &errset)) | 
| 195 | > | if (!FD_ISSET(rtpd[pn].r, &readset) && | 
| 196 | > | !FD_ISSET(rtpd[pn].r, &errset)) | 
| 197 |  | continue; | 
| 198 |  | reread: | 
| 199 | < | n = read(rtpd[pn][0], (char *)rtbuf, | 
| 199 | > | n = read(rtpd[pn].r, (char *)rtbuf, | 
| 200 |  | 4*sizeof(float)*RPACKSIZ*pqlen[pn]); | 
| 201 |  | if (n < 0) { | 
| 202 | < | if (errno == EINTR | errno == EAGAIN) | 
| 202 | > | if ((errno == EINTR) | (errno == EAGAIN)) | 
| 203 |  | goto reread; | 
| 204 |  | error(SYSTEM, "read error in get_packets"); | 
| 205 |  | } | 
| 211 |  | nr++;                   /* add flush block */ | 
| 212 |  | n -= 4*sizeof(float)*nr; | 
| 213 |  | if (n < 0) {                    /* get remainder */ | 
| 214 | < | n += readbuf(rtpd[pn][0], | 
| 214 | > | n += readbuf(rtpd[pn].r, | 
| 215 |  | (char *)(bp+4*nr)+n, -n); | 
| 216 |  | if (n) | 
| 217 |  | goto eoferr; | 
| 235 |  | return(pldone);                         /* return finished packets */ | 
| 236 |  | eoferr: | 
| 237 |  | error(USER, "rtrace process died"); | 
| 238 | + | return NULL; /* pro forma return */ | 
| 239 |  | } | 
| 240 |  |  | 
| 241 |  |  | 
| 242 |  | PACKET * | 
| 243 | < | do_packets(pl)                  /* queue a packet list, return finished */ | 
| 244 | < | register PACKET *pl; | 
| 243 | > | do_packets(                     /* queue a packet list, return finished */ | 
| 244 | > | PACKET  *pl | 
| 245 | > | ) | 
| 246 |  | { | 
| 247 | < | register PACKET *p; | 
| 247 | > | PACKET  *p; | 
| 248 |  | /* consistency check */ | 
| 249 |  | if (nprocs < 1) | 
| 250 |  | error(CONSISTENCY, "do_packets called with no active process"); | 
| 258 |  |  | 
| 259 |  |  | 
| 260 |  | PACKET * | 
| 261 | < | flush_queue()                   /* empty all rtrace queues */ | 
| 261 | > | flush_queue(void)                       /* empty all rtrace queues */ | 
| 262 |  | { | 
| 263 |  | PACKET  *rpdone = NULL; | 
| 264 | < | register PACKET *rpl; | 
| 264 | > | PACKET  *rpl = NULL; | 
| 265 |  | float   *bp; | 
| 266 | < | register PACKET *p; | 
| 266 | > | PACKET  *p; | 
| 267 |  | int     i, n, nr; | 
| 268 |  |  | 
| 269 |  | for (i = 0; i < nprocs; i++) | 
| 280 |  | if (rpl->nr < RPACKSIZ) | 
| 281 |  | nr++;           /* add flush block */ | 
| 282 |  | } | 
| 283 | < | n = readbuf(rtpd[i][0], (char *)rtbuf, | 
| 274 | < | 4*sizeof(float)*nr); | 
| 283 | > | n = readbuf(rtpd[i].r, rtbuf, 4*sizeof(float)*nr); | 
| 284 |  | if (n < 0) | 
| 285 |  | error(SYSTEM, "read failure in flush_queue"); | 
| 286 |  | bp = rtbuf;                     /* process packets */ | 
| 301 |  | } | 
| 302 |  |  | 
| 303 |  |  | 
| 304 | < | static | 
| 305 | < | killpersist()                   /* kill persistent process */ | 
| 304 | > | static void | 
| 305 | > | killpersist(void)                       /* kill persistent process */ | 
| 306 |  | { | 
| 307 |  | FILE    *fp; | 
| 308 |  | int     pid; | 
| 316 |  |  | 
| 317 |  |  | 
| 318 |  | int | 
| 319 | < | end_rtrace()                    /* close rtrace process(es) */ | 
| 319 | > | end_rtrace(void)                        /* close rtrace process(es) */ | 
| 320 |  | { | 
| 321 |  | int     status = 0, rv; | 
| 322 |  |  | 
| 323 |  | if (nprocs > 1) | 
| 324 |  | killpersist(); | 
| 325 | < | while (nprocs > 0) { | 
| 326 | < | rv = close_process(rtpd[--nprocs]); | 
| 327 | < | if (rv > 0) | 
| 319 | < | status = rv; | 
| 320 | < | } | 
| 321 | < | free((char *)rtbuf); | 
| 325 | > | status = close_processes(rtpd, nprocs); | 
| 326 | > | nprocs = 0; | 
| 327 | > | free((void *)rtbuf); | 
| 328 |  | rtbuf = NULL; | 
| 329 |  | maxqlen = 0; | 
| 330 |  | return(status); |