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