1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: rholo2l.c,v 3.23 2025/01/22 18:28:32 greg Exp $"; |
3 |
#endif |
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 "rtprocess.h" |
17 |
|
18 |
#ifndef MAXPROC |
19 |
#define MAXPROC 64 |
20 |
#endif |
21 |
|
22 |
int nprocs = 0; /* running process count */ |
23 |
|
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; /* 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(void) /* start rtrace process */ |
42 |
{ |
43 |
static char buf1[8]; |
44 |
int rmaxpack = 0; |
45 |
int psiz, n; |
46 |
/* get number of processes */ |
47 |
if (ncprocs <= 0 || nprocs > 0) |
48 |
return(0); |
49 |
if (ncprocs > MAXPROC) { |
50 |
sprintf(errmsg, |
51 |
"number of rtrace processes reduced from %d to %d", |
52 |
ncprocs, MAXPROC); |
53 |
error(WARNING, errmsg); |
54 |
ncprocs = MAXPROC; |
55 |
} |
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++] = "-pRGB"; |
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 |
} |
77 |
maxqlen = 0; |
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)) + 1; |
83 |
if (!maxqlen) { |
84 |
maxqlen = n; |
85 |
sleep(2); |
86 |
} else if (n != maxqlen) |
87 |
error(INTERNAL, "varying pipe buffer size!"); |
88 |
rmaxpack += n; |
89 |
} |
90 |
rtbuf = (float *)malloc(RPACKSIZ*6*sizeof(float)*maxqlen); |
91 |
if (rtbuf == NULL) |
92 |
error(SYSTEM, "malloc failure in start_rtrace"); |
93 |
return(rmaxpack); |
94 |
} |
95 |
|
96 |
|
97 |
static int |
98 |
bestout(void) /* get best process to process packet */ |
99 |
{ |
100 |
int cnt; |
101 |
int pn, i; |
102 |
|
103 |
pn = 0; /* find shortest queue */ |
104 |
for (i = 1; i < nprocs; i++) |
105 |
if (pqlen[i] < pqlen[pn]) |
106 |
pn = i; |
107 |
/* sanity check */ |
108 |
if (pqlen[pn] == maxqlen) |
109 |
return(-1); |
110 |
cnt = 0; /* count number of ties */ |
111 |
for (i = pn; i < nprocs; i++) |
112 |
if (pqlen[i] == pqlen[pn]) |
113 |
cnt++; |
114 |
/* break ties fairly */ |
115 |
if ((cnt = irandom(cnt))) |
116 |
for (i = pn; i < nprocs; i++) |
117 |
if (pqlen[i] == pqlen[pn] && !cnt--) |
118 |
return(i); |
119 |
return(pn); |
120 |
} |
121 |
|
122 |
|
123 |
static int |
124 |
slots_avail(void) /* count packet slots available */ |
125 |
{ |
126 |
int nslots = 0; |
127 |
int i; |
128 |
|
129 |
for (i = nprocs; i--; ) |
130 |
nslots += maxqlen - pqlen[i]; |
131 |
return(nslots); |
132 |
} |
133 |
|
134 |
|
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 */ |
142 |
if ((pn = bestout()) < 0) |
143 |
error(INTERNAL, "rtrace input queues are full!"); |
144 |
/* write out the packet */ |
145 |
packrays(rtbuf, p); |
146 |
if ((n = p->nr) < RPACKSIZ) /* add flush block? */ |
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 |
PACKET *rpl = pqueue[pn]; |
155 |
while (rpl->next != NULL) |
156 |
rpl = rpl->next; |
157 |
rpl->next = p; |
158 |
} |
159 |
} |
160 |
|
161 |
|
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 |
PACKET *p; |
171 |
int n, nr; |
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].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, |
185 |
poll ? &tpoll : (struct timeval *)NULL); |
186 |
if (n < 0) { |
187 |
if (errno == EINTR) /* interrupted select call */ |
188 |
return(NULL); |
189 |
error(SYSTEM, "select call failure in get_packets"); |
190 |
} |
191 |
if (n == 0) /* is nothing ready? */ |
192 |
return(NULL); |
193 |
/* make read call(s) */ |
194 |
for (pn = 0; pn < nprocs; pn++) { |
195 |
if (!FD_ISSET(rtpd[pn].r, &readset) && |
196 |
!FD_ISSET(rtpd[pn].r, &errset)) |
197 |
continue; |
198 |
reread: |
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)) |
203 |
goto reread; |
204 |
error(SYSTEM, "read error in get_packets"); |
205 |
} |
206 |
if (n == 0) |
207 |
goto eoferr; |
208 |
bp = rtbuf; /* finish processing */ |
209 |
for (p = pqueue[pn]; n && p != NULL; p = p->next) { |
210 |
if ((nr = p->nr) < RPACKSIZ) |
211 |
nr++; /* add flush block */ |
212 |
n -= 4*sizeof(float)*nr; |
213 |
if (n < 0) { /* get remainder */ |
214 |
n += readbuf(rtpd[pn].r, |
215 |
(char *)(bp+4*nr)+n, -n); |
216 |
if (n) |
217 |
goto eoferr; |
218 |
} |
219 |
donerays(p, bp); |
220 |
bp += 4*nr; |
221 |
pqlen[pn]--; |
222 |
} |
223 |
if (n) /* read past end? */ |
224 |
error(INTERNAL, "packet sync error in get_packets"); |
225 |
/* take from queue */ |
226 |
if (pldone == NULL) |
227 |
pldone = plend = pqueue[pn]; |
228 |
else |
229 |
plend->next = pqueue[pn]; |
230 |
while (plend->next != p) |
231 |
plend = plend->next; |
232 |
plend->next = NULL; |
233 |
pqueue[pn] = p; |
234 |
} |
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( /* queue a packet list, return finished */ |
244 |
PACKET *pl |
245 |
) |
246 |
{ |
247 |
PACKET *p; |
248 |
/* consistency check */ |
249 |
if (nprocs < 1) |
250 |
error(CONSISTENCY, "do_packets called with no active process"); |
251 |
/* queue each new packet */ |
252 |
while (pl != NULL) { |
253 |
p = pl; pl = p->next; p->next = NULL; |
254 |
queue_packet(p); |
255 |
} |
256 |
return(get_packets(slots_avail())); /* return processed packets */ |
257 |
} |
258 |
|
259 |
|
260 |
PACKET * |
261 |
flush_queue(void) /* empty all rtrace queues */ |
262 |
{ |
263 |
PACKET *rpdone = NULL; |
264 |
PACKET *rpl = NULL; |
265 |
float *bp; |
266 |
PACKET *p; |
267 |
int i, n, nr; |
268 |
|
269 |
for (i = 0; i < nprocs; i++) |
270 |
if (pqlen[i]) { |
271 |
if (rpdone == NULL) { /* tack on queue */ |
272 |
rpdone = rpl = pqueue[i]; |
273 |
if ((nr = rpl->nr) < RPACKSIZ) nr++; |
274 |
} else { |
275 |
rpl->next = pqueue[i]; |
276 |
nr = 0; |
277 |
} |
278 |
while (rpl->next != NULL) { |
279 |
nr += (rpl = rpl->next)->nr; |
280 |
if (rpl->nr < RPACKSIZ) |
281 |
nr++; /* add flush block */ |
282 |
} |
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 */ |
287 |
for (p = pqueue[i]; p != NULL; p = p->next) { |
288 |
if ((nr = p->nr) < RPACKSIZ) |
289 |
nr++; /* add flush block */ |
290 |
n -= 4*sizeof(float)*nr; |
291 |
if (n >= 0) |
292 |
donerays(p, bp); |
293 |
else |
294 |
p->nr = 0; /* short data error */ |
295 |
bp += 4*nr; |
296 |
} |
297 |
pqueue[i] = NULL; /* zero this queue */ |
298 |
pqlen[i] = 0; |
299 |
} |
300 |
return(rpdone); /* return all packets completed */ |
301 |
} |
302 |
|
303 |
|
304 |
static void |
305 |
killpersist(void) /* kill persistent process */ |
306 |
{ |
307 |
FILE *fp; |
308 |
int pid; |
309 |
|
310 |
if ((fp = fopen(pfile, "r")) == NULL) |
311 |
return; |
312 |
if (fscanf(fp, "%*s %d", &pid) != 1 || kill(pid, SIGALRM) < 0) |
313 |
unlink(pfile); |
314 |
fclose(fp); |
315 |
} |
316 |
|
317 |
|
318 |
int |
319 |
end_rtrace(void) /* close rtrace process(es) */ |
320 |
{ |
321 |
int status = 0, rv; |
322 |
|
323 |
if (nprocs > 1) |
324 |
killpersist(); |
325 |
status = close_processes(rtpd, nprocs); |
326 |
nprocs = 0; |
327 |
free((void *)rtbuf); |
328 |
rtbuf = NULL; |
329 |
maxqlen = 0; |
330 |
return(status); |
331 |
} |