ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rpiece.c
Revision: 2.28
Committed: Tue Oct 19 16:02:39 1993 UTC (30 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.27: +5 -2 lines
Log Message:
fixed bug in opening non-existent sync file
added lock failure report

File Contents

# Content
1 /* Copyright (c) 1993 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * Generate sections of a picture.
9 */
10
11 #include "standard.h"
12
13 #ifndef F_SETLKW
14
15 main(argc, argv)
16 int argc;
17 char *argv[];
18 {
19 fprintf(stderr, "%s: no NFS lock manager on this machine\n", argv[0]);
20 exit(1);
21 }
22
23 #else
24
25 #include <signal.h>
26 #include "color.h"
27 #include "view.h"
28 #include "resolu.h"
29
30 #ifndef NFS
31 #define NFS 1
32 #endif
33 /* set the following to 0 to forgo forking */
34 #ifndef MAXFORK
35 #if NFS
36 #define MAXFORK 3 /* allotment of duped processes */
37 #else
38 #define MAXFORK 0
39 #endif
40 #endif
41 /* protection from SYSV signals(!) */
42 #if defined(sgi)
43 #define guard_io() sighold(SIGALRM)
44 #define unguard() sigrelse(SIGALRM)
45 #endif
46 #ifndef guard_io
47 #define guard_io() 0
48 #define unguard() 0
49 #endif
50 /* rpict command */
51 char *rpargv[128] = {"rpict", "-S", "1"};
52 int rpargc = 3;
53 int rpd[3];
54 FILE *torp, *fromrp;
55 COLR *pbuf;
56 /* our view parameters */
57 VIEW ourview = STDVIEW;
58 double pixaspect = 1.0;
59 int hres = 1024, vres = 1024, hmult = 4, vmult = 4;
60 /* output file */
61 char *outfile = NULL;
62 int outfd;
63 long scanorig;
64 FILE *syncfp = NULL; /* synchronization file pointer */
65 int synclst = F_UNLCK; /* synchronization file lock status */
66 int nforked = 0;
67
68 #define sflock(t) if ((t)!=synclst) dolock(fileno(syncfp),synclst=t)
69
70 char *progname;
71 int verbose = 0;
72 int rvrlim = -1;
73
74 extern long lseek(), ftell();
75
76 int gotalrm = 0;
77 int onalrm() { gotalrm++; }
78
79
80 main(argc, argv)
81 int argc;
82 char *argv[];
83 {
84 register int i, rval;
85
86 progname = argv[0];
87 for (i = 1; i < argc; i++) {
88 /* expand arguments */
89 while (rval = expandarg(&argc, &argv, i))
90 if (rval < 0) {
91 fprintf(stderr, "%s: cannot expand '%s'",
92 argv[0], argv[i]);
93 exit(1);
94 }
95 if (argv[i][0] == '-')
96 switch (argv[i][1]) {
97 case 'v':
98 switch (argv[i][2]) {
99 case '\0': /* verbose option */
100 verbose = !verbose;
101 continue;
102 case 'f': /* view file */
103 if (viewfile(argv[++i], &ourview, NULL) <= 0) {
104 fprintf(stderr,
105 "%s: not a view file\n", argv[i]);
106 exit(1);
107 }
108 continue;
109 default: /* view option? */
110 rval = getviewopt(&ourview, argc-i, argv+i);
111 if (rval >= 0) {
112 i += rval;
113 continue;
114 }
115 break;
116 }
117 break;
118 case 'p': /* pixel aspect ratio? */
119 if (argv[i][2] != 'a' || argv[i][3])
120 break;
121 pixaspect = atof(argv[i+1]);
122 continue;
123 case 'x': /* overall x resolution */
124 if (argv[i][2])
125 break;
126 hres = atoi(argv[++i]);
127 continue;
128 case 'y': /* overall y resolution */
129 if (argv[i][2])
130 break;
131 vres = atoi(argv[++i]);
132 continue;
133 case 'X': /* horizontal multiplier */
134 if (argv[i][2])
135 break;
136 hmult = atoi(argv[++i]);
137 continue;
138 case 'Y': /* vertical multiplier */
139 if (argv[i][2])
140 break;
141 vmult = atoi(argv[++i]);
142 continue;
143 case 'R': /* recover */
144 if (argv[i][2])
145 break;
146 rvrlim = 0;
147 /* fall through */
148 case 'F': /* syncronization file */
149 if (argv[i][2])
150 break;
151 if ((syncfp =
152 fdopen(open(argv[++i],O_RDWR|O_CREAT,0666),"r+")) == NULL) {
153 fprintf(stderr, "%s: cannot open\n",
154 argv[i]);
155 exit(1);
156 }
157 continue;
158 case 'z': /* z-file ist verbotten */
159 fprintf(stderr, "%s: -z option not allowed\n",
160 argv[0]);
161 exit(1);
162 case 'o': /* output file */
163 if (argv[i][2])
164 break;
165 outfile = argv[++i];
166 continue;
167 } else if (i >= argc-1)
168 break;
169 rpargv[rpargc++] = argv[i];
170 }
171 if (i >= argc) {
172 fprintf(stderr, "%s: missing octree argument\n", argv[0]);
173 exit(1);
174 }
175 if (outfile == NULL) {
176 fprintf(stderr, "%s: missing output file\n", argv[0]);
177 exit(1);
178 }
179 init(argc, argv);
180 rpiece();
181 exit(cleanup(0));
182 }
183
184
185 dolock(fd, ltyp) /* lock or unlock a file */
186 int fd;
187 int ltyp;
188 {
189 static struct flock fls; /* static so initialized to zeroes */
190 extern char *sys_errlist[];
191
192 fls.l_type = ltyp;
193 if (fcntl(fd, F_SETLKW, &fls) < 0) {
194 fprintf(stderr, "%s: cannot lock/unlock file: %s\n",
195 progname, sys_errlist[errno]);
196 exit(1);
197 }
198 }
199
200
201 init(ac, av) /* set up output file and start rpict */
202 int ac;
203 char **av;
204 {
205 static char hrbuf[16], vrbuf[16];
206 extern char VersionID[];
207 char *err;
208 FILE *fp;
209 int hr, vr;
210 /* set up view */
211 if ((err = setview(&ourview)) != NULL) {
212 fprintf(stderr, "%s: %s\n", progname, err);
213 exit(1);
214 }
215 if (syncfp != NULL) {
216 sflock(F_RDLCK);
217 fscanf(syncfp, "%d %d", &hmult, &vmult);
218 sflock(F_UNLCK);
219 }
220 /* compute piece size */
221 hres /= hmult;
222 vres /= vmult;
223 normaspect(viewaspect(&ourview)*hmult/vmult, &pixaspect, &hres, &vres);
224 sprintf(hrbuf, "%d", hres);
225 rpargv[rpargc++] = "-x"; rpargv[rpargc++] = hrbuf;
226 sprintf(vrbuf, "%d", vres);
227 rpargv[rpargc++] = "-y"; rpargv[rpargc++] = vrbuf;
228 rpargv[rpargc++] = "-pa"; rpargv[rpargc++] = "0";
229 rpargv[rpargc++] = av[ac-1];
230 rpargv[rpargc] = NULL;
231 /* open output file */
232 if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) {
233 dolock(outfd, F_WRLCK);
234 if ((fp = fdopen(dup(outfd), "w")) == NULL)
235 goto filerr;
236 printargs(ac, av, fp); /* write header */
237 fprintf(fp, "SOFTWARE= %s\n", VersionID);
238 fputs(VIEWSTR, fp);
239 fprintview(&ourview, fp);
240 putc('\n', fp);
241 if (pixaspect < .99 || pixaspect > 1.01)
242 fputaspect(pixaspect, fp);
243 fputformat(COLRFMT, fp);
244 putc('\n', fp);
245 fprtresolu(hres*hmult, vres*vmult, fp);
246 } else if ((outfd = open(outfile, O_RDWR)) >= 0) {
247 dolock(outfd, F_RDLCK);
248 if ((fp = fdopen(dup(outfd), "r+")) == NULL)
249 goto filerr;
250 getheader(fp, NULL, NULL); /* skip header */
251 if (!fscnresolu(&hr, &vr, fp) || /* check resolution */
252 hr != hres*hmult || vr != vres*vmult) {
253 fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n",
254 progname, outfile);
255 exit(1);
256 }
257 } else {
258 fprintf(stderr, "%s: cannot open file \"%s\"\n",
259 progname, outfile);
260 exit(1);
261 }
262 scanorig = ftell(fp); /* record position of first scanline */
263 if (fclose(fp) == -1) /* done with stream i/o */
264 goto filerr;
265 dolock(outfd, F_UNLCK);
266 /* start rpict process */
267 if (open_process(rpd, rpargv) <= 0) {
268 fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
269 exit(1);
270 }
271 if ((fromrp = fdopen(rpd[0], "r")) == NULL ||
272 (torp = fdopen(rpd[1], "w")) == NULL) {
273 fprintf(stderr, "%s: cannot open stream to %s\n",
274 progname, rpargv[0]);
275 exit(1);
276 }
277 if ((pbuf = (COLR *)bmalloc(hres*vres*sizeof(COLR))) == NULL) {
278 fprintf(stderr, "%s: out of memory\n", progname);
279 exit(1);
280 }
281 signal(SIGALRM, onalrm);
282 return;
283 filerr:
284 fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile);
285 exit(1);
286 }
287
288
289 int
290 nextpiece(xp, yp) /* get next piece assignment */
291 int *xp, *yp;
292 {
293 if (gotalrm) /* someone wants us to quit */
294 return(0);
295 if (syncfp != NULL) { /* use sync file */
296 /*
297 * So we don't necessarily have to lock and unlock the file
298 * multiple times (very slow), we establish an exclusive
299 * lock at the beginning on our synchronization file and
300 * maintain it in the subroutine rvrpiece().
301 */
302 sflock(F_WRLCK);
303 fseek(syncfp, 0L, 0); /* read position */
304 if (fscanf(syncfp, "%*d %*d %d %d", xp, yp) < 2) {
305 *xp = hmult-1;
306 *yp = vmult;
307 }
308 if (rvrlim == 0) /* initialize recovery limit */
309 rvrlim = *xp*vmult + *yp;
310 if (rvrpiece(xp, yp)) { /* do stragglers first */
311 sflock(F_UNLCK);
312 return(1);
313 }
314 if (--(*yp) < 0) { /* decrement position */
315 *yp = vmult-1;
316 if (--(*xp) < 0) { /* all done */
317 sflock(F_UNLCK);
318 return(0);
319 }
320 }
321 fseek(syncfp, 0L, 0); /* write new position */
322 fprintf(syncfp, "%4d %4d\n%4d %4d\n\n", hmult, vmult, *xp, *yp);
323 fflush(syncfp);
324 sflock(F_UNLCK); /* release sync file */
325 return(1);
326 }
327 return(scanf("%d %d", xp, yp) == 2); /* use stdin */
328 }
329
330
331 int
332 rvrpiece(xp, yp) /* check for recoverable pieces */
333 register int *xp, *yp;
334 {
335 static char *pdone = NULL; /* which pieces are done */
336 static long readpos = -1; /* how far we've read */
337 register int i;
338 /*
339 * This routine is called by nextpiece() with an
340 * exclusive lock on syncfp and the file pointer at the
341 * appropriate position to read in the finished pieces.
342 */
343 if (rvrlim < 0)
344 return(0); /* only check if asked */
345 if (pdone == NULL) /* first call */
346 pdone = calloc(hmult*vmult, sizeof(char));
347 if (readpos != -1) /* mark what's been done */
348 fseek(syncfp, readpos, 0);
349 while (fscanf(syncfp, "%d %d", xp, yp) == 2)
350 pdone[*xp*vmult+*yp] = 1;
351 if (!feof(syncfp)) {
352 fprintf(stderr, "%s: format error in sync file\n", progname);
353 exit(1);
354 }
355 readpos = ftell(syncfp);
356 i = hmult*vmult; /* find an unaccounted for piece */
357 while (i-- > rvrlim)
358 if (!pdone[i]) {
359 *xp = i / vmult;
360 *yp = i % vmult;
361 pdone[i] = 1; /* consider it done */
362 return(1);
363 }
364 rvrlim = -1; /* nothing left to recover */
365 free(pdone);
366 pdone = NULL;
367 return(0);
368 }
369
370
371 int
372 cleanup(rstat) /* close rpict process and clean up */
373 int rstat;
374 {
375 int status;
376
377 bfree((char *)pbuf, hres*vres*sizeof(COLR));
378 fclose(torp);
379 fclose(fromrp);
380 while (wait(&status) != -1)
381 if (rstat == 0)
382 rstat = status>>8 & 0xff;
383 return(rstat);
384 }
385
386
387 rpiece() /* render picture piece by piece */
388 {
389 VIEW pview;
390 int xorg, yorg;
391 /* compute view parameters */
392 copystruct(&pview, &ourview);
393 switch (ourview.type) {
394 case VT_PER:
395 pview.horiz = 2.*180./PI*atan(
396 tan(PI/180./2.*ourview.horiz)/hmult );
397 pview.vert = 2.*180./PI*atan(
398 tan(PI/180./2.*ourview.vert)/vmult );
399 break;
400 case VT_PAR:
401 case VT_ANG:
402 pview.horiz = ourview.horiz / hmult;
403 pview.vert = ourview.vert / vmult;
404 break;
405 case VT_HEM:
406 pview.horiz = 2.*180./PI*asin(
407 sin(PI/180./2.*ourview.horiz)/hmult );
408 pview.vert = 2.*180./PI*asin(
409 sin(PI/180./2.*ourview.vert)/vmult );
410 break;
411 default:
412 fprintf(stderr, "%s: unknown view type '-vt%c'\n",
413 progname, ourview.type);
414 exit(cleanup(1));
415 }
416 /* render each piece */
417 while (nextpiece(&xorg, &yorg)) {
418 pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1);
419 pview.voff = ourview.voff + yorg - 0.5*(vmult-1);
420 fputs(VIEWSTR, torp);
421 fprintview(&pview, torp);
422 putc('\n', torp);
423 fflush(torp); /* assigns piece to rpict */
424 putpiece(xorg, yorg); /* place piece in output */
425 }
426 }
427
428
429 int
430 putpiece(xpos, ypos) /* get next piece from rpict */
431 int xpos, ypos;
432 {
433 struct flock fls;
434 int pid, status;
435 int hr, vr;
436 register int y;
437 /* check bounds */
438 if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) {
439 fprintf(stderr, "%s: requested piece (%d,%d) out of range\n",
440 progname, xpos, ypos);
441 exit(cleanup(1));
442 }
443 /* check header from rpict */
444 guard_io();
445 getheader(fromrp, NULL, NULL);
446 if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) {
447 fprintf(stderr, "%s: resolution mismatch from %s\n",
448 progname, rpargv[0]);
449 exit(cleanup(1));
450 }
451 if (verbose) { /* notify caller */
452 printf("%d %d begun\n", xpos, ypos);
453 fflush(stdout);
454 }
455 unguard();
456 /* load new piece into buffer */
457 for (y = 0; y < vr; y++) {
458 guard_io();
459 if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) {
460 fprintf(stderr, "%s: read error from %s\n",
461 progname, rpargv[0]);
462 exit(cleanup(1));
463 }
464 unguard();
465 }
466 #if MAXFORK
467 /* fork so we don't slow rpict down */
468 if ((pid = fork()) > 0) {
469 if (++nforked >= MAXFORK) {
470 wait(&status); /* reap a child */
471 if (status)
472 exit(cleanup(status>>8 & 0xff));
473 nforked--;
474 }
475 return(pid);
476 }
477 #else
478 pid = -1; /* no forking */
479 #endif
480 fls.l_start = scanorig +
481 ((long)(vmult-1-ypos)*vres*hmult+xpos)*hres*sizeof(COLR);
482 #if NFS
483 fls.l_len = ((long)(vres-1)*hmult+1)*hres*sizeof(COLR);
484 /* lock file section so NFS doesn't mess up */
485 fls.l_whence = 0;
486 fls.l_type = F_WRLCK;
487 if (fcntl(outfd, F_SETLKW, &fls) < 0)
488 filerr("lock");
489 #endif
490 /* write new piece to file */
491 if (lseek(outfd, fls.l_start, 0) == -1)
492 filerr("seek");
493 if (hmult == 1) {
494 if (writebuf(outfd, (char *)pbuf,
495 vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR))
496 filerr("write");
497 } else
498 for (y = 0; y < vr; y++) {
499 if (writebuf(outfd, (char *)(pbuf+y*hr),
500 hr*sizeof(COLR)) != hr*sizeof(COLR))
501 filerr("write");
502 if (y < vr-1 && lseek(outfd,
503 (long)(hmult-1)*hr*sizeof(COLR),
504 1) == -1)
505 filerr("seek");
506 }
507 #if NFS
508 fls.l_type = F_UNLCK; /* release lock */
509 if (fcntl(outfd, F_SETLKW, &fls) < 0)
510 filerr("lock");
511 #endif
512 if (syncfp != NULL) { /* record what's been done */
513 sflock(F_WRLCK);
514 fseek(syncfp, 0L, 2); /* append index */
515 fprintf(syncfp, "%4d %4d\n", xpos, ypos);
516 fflush(syncfp);
517 /*** Unlock not necessary, since
518 sflock(F_UNLCK); _exit() or nextpiece() is next ***/
519 }
520 if (verbose) { /* notify caller */
521 printf("%d %d done\n", xpos, ypos);
522 fflush(stdout);
523 }
524 if (pid == -1) /* didn't fork or fork failed */
525 return(0);
526 _exit(0); /* else exit child process (releasing locks) */
527 }
528
529
530 filerr(t) /* report file error and exit */
531 char *t;
532 {
533 extern char *sys_errlist[];
534
535 fprintf(stderr, "%s: %s error on file \"%s\": %s\n",
536 progname, t, outfile, sys_errlist[errno]);
537 _exit(1);
538 }
539
540 #endif