xref: /dflybsd-src/test/sysperf/exec1.c (revision 8cdef6cbcc73174e567af3341631ec8fa492796a)
16157d9fcSMatthew Dillon /*
26157d9fcSMatthew Dillon  * exec1.c
36157d9fcSMatthew Dillon  *
443b3c06fSMatthew Dillon  * $DragonFly: src/test/sysperf/exec1.c,v 1.2 2004/04/14 17:59:45 dillon Exp $
56157d9fcSMatthew Dillon  */
66157d9fcSMatthew Dillon 
76157d9fcSMatthew Dillon #include "blib.h"
86157d9fcSMatthew Dillon #include <sys/resource.h>
96157d9fcSMatthew Dillon #include <sys/wait.h>
106157d9fcSMatthew Dillon #include <sys/time.h>
11*8cdef6cbSMatthew Dillon #include <machine/atomic.h>
126157d9fcSMatthew Dillon 
136157d9fcSMatthew Dillon char *Av0;
146157d9fcSMatthew Dillon 
156157d9fcSMatthew Dillon static
166157d9fcSMatthew Dillon void
execltest(void)176157d9fcSMatthew Dillon execltest(void)
186157d9fcSMatthew Dillon {
196157d9fcSMatthew Dillon     pid_t pid;
206157d9fcSMatthew Dillon     char *elm;
216157d9fcSMatthew Dillon 
226157d9fcSMatthew Dillon     if ((elm = strrchr(Av0, '/')) == NULL)
236157d9fcSMatthew Dillon 	elm = Av0;
246157d9fcSMatthew Dillon     else
256157d9fcSMatthew Dillon 	++elm;
266157d9fcSMatthew Dillon 
276157d9fcSMatthew Dillon     if ((pid = vfork()) == 0) {
286157d9fcSMatthew Dillon 	execl(Av0, elm, "dummy", NULL);
296157d9fcSMatthew Dillon 	_exit(1);
306157d9fcSMatthew Dillon     } else if (pid < 0) {
316157d9fcSMatthew Dillon 	perror("vfork");
326157d9fcSMatthew Dillon 	exit(1);
336157d9fcSMatthew Dillon     } else {
346157d9fcSMatthew Dillon 	int status;
356157d9fcSMatthew Dillon 
366157d9fcSMatthew Dillon 	while (waitpid(pid, &status, 0) != pid)
376157d9fcSMatthew Dillon 	    ;
386157d9fcSMatthew Dillon 	if (WEXITSTATUS(status)) {
396157d9fcSMatthew Dillon 	    fprintf(stderr, "execl in child failed\n");
406157d9fcSMatthew Dillon 	    exit(1);
416157d9fcSMatthew Dillon 	}
426157d9fcSMatthew Dillon     }
436157d9fcSMatthew Dillon }
446157d9fcSMatthew Dillon 
456157d9fcSMatthew Dillon int
main(int ac,char ** av)466157d9fcSMatthew Dillon main(int ac, char **av)
476157d9fcSMatthew Dillon {
486157d9fcSMatthew Dillon     int i;
496157d9fcSMatthew Dillon     int count;
50*8cdef6cbSMatthew Dillon     int status;
51*8cdef6cbSMatthew Dillon     int ncpus;
52*8cdef6cbSMatthew Dillon     int n;
53*8cdef6cbSMatthew Dillon     long *countr;
54*8cdef6cbSMatthew Dillon 
55*8cdef6cbSMatthew Dillon     countr = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0);
566157d9fcSMatthew Dillon 
576157d9fcSMatthew Dillon     Av0 = av[0];
58*8cdef6cbSMatthew Dillon     if (ac == 2 && strcmp(av[1], "dummy") == 0)
596157d9fcSMatthew Dillon 	exit(0);
60*8cdef6cbSMatthew Dillon     ncpus = 1;
61*8cdef6cbSMatthew Dillon     if (ac > 1)
62*8cdef6cbSMatthew Dillon 	ncpus = strtol(av[1], NULL, 0);
636157d9fcSMatthew Dillon 
646157d9fcSMatthew Dillon     count = 0;
656157d9fcSMatthew Dillon     start_timing();
666157d9fcSMatthew Dillon     while (stop_timing(0, NULL) == 0) {
676157d9fcSMatthew Dillon 	for (i = 0; i < 100; ++i)
686157d9fcSMatthew Dillon 	    execltest();
696157d9fcSMatthew Dillon 	count += 100;
706157d9fcSMatthew Dillon     }
7143b3c06fSMatthew Dillon     count *= 5;		/* 5 second run */
726157d9fcSMatthew Dillon     start_timing();
73*8cdef6cbSMatthew Dillon     for (n = 0; n < ncpus; ++n) {
74*8cdef6cbSMatthew Dillon 	if (fork() == 0) {
75*8cdef6cbSMatthew Dillon 	    count = 0;
76*8cdef6cbSMatthew Dillon 	    while (get_timing() < 5000000) {
776157d9fcSMatthew Dillon 		execltest();
78*8cdef6cbSMatthew Dillon 		++count;
79*8cdef6cbSMatthew Dillon 		stop_timing(0, NULL);
80*8cdef6cbSMatthew Dillon 	    }
81*8cdef6cbSMatthew Dillon 	    atomic_add_long(countr, count);
82*8cdef6cbSMatthew Dillon 	    _exit(0);
83*8cdef6cbSMatthew Dillon 	}
84*8cdef6cbSMatthew Dillon     }
85*8cdef6cbSMatthew Dillon     while (wait3(&status, 0, NULL) >= 0 || errno == EINTR)
86*8cdef6cbSMatthew Dillon 	;
876157d9fcSMatthew Dillon #ifdef ISSTATIC
88*8cdef6cbSMatthew Dillon     stop_timing(*countr, "execl static program:");
896157d9fcSMatthew Dillon #else
90*8cdef6cbSMatthew Dillon     stop_timing(*countr, "execl dynamic program:");
916157d9fcSMatthew Dillon #endif
926157d9fcSMatthew Dillon     return(0);
936157d9fcSMatthew Dillon }
946157d9fcSMatthew Dillon 
95