xref: /dragonfly/test/sysperf/exec1.c (revision 799ba435)
1 /*
2  * exec1.c
3  *
4  * $DragonFly: src/test/sysperf/exec1.c,v 1.2 2004/04/14 17:59:45 dillon Exp $
5  */
6 
7 #include "blib.h"
8 #include <sys/resource.h>
9 #include <sys/wait.h>
10 #include <sys/time.h>
11 #include <machine/atomic.h>
12 
13 char *Av0;
14 
15 static
16 void
17 execltest(void)
18 {
19     pid_t pid;
20     char *elm;
21 
22     if ((elm = strrchr(Av0, '/')) == NULL)
23 	elm = Av0;
24     else
25 	++elm;
26 
27     if ((pid = vfork()) == 0) {
28 	execl(Av0, elm, "dummy", NULL);
29 	_exit(1);
30     } else if (pid < 0) {
31 	perror("vfork");
32 	exit(1);
33     } else {
34 	int status;
35 
36 	while (waitpid(pid, &status, 0) != pid)
37 	    ;
38 	if (WEXITSTATUS(status)) {
39 	    fprintf(stderr, "execl in child failed\n");
40 	    exit(1);
41 	}
42     }
43 }
44 
45 int
46 main(int ac, char **av)
47 {
48     int i;
49     int count;
50     int status;
51     int ncpus;
52     int n;
53     long *countr;
54 
55     countr = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0);
56 
57     Av0 = av[0];
58     if (ac == 2 && strcmp(av[1], "dummy") == 0)
59 	exit(0);
60     ncpus = 1;
61     if (ac > 1)
62 	ncpus = strtol(av[1], NULL, 0);
63 
64     count = 0;
65     start_timing();
66     while (stop_timing(0, NULL) == 0) {
67 	for (i = 0; i < 100; ++i)
68 	    execltest();
69 	count += 100;
70     }
71     count *= 5;		/* 5 second run */
72     start_timing();
73     for (n = 0; n < ncpus; ++n) {
74 	if (fork() == 0) {
75 	    count = 0;
76 	    while (get_timing() < 5000000) {
77 		execltest();
78 		++count;
79 		stop_timing(0, NULL);
80 	    }
81 	    atomic_add_long(countr, count);
82 	    _exit(0);
83 	}
84     }
85     while (wait3(&status, 0, NULL) >= 0 || errno == EINTR)
86 	;
87 #ifdef ISSTATIC
88     stop_timing(*countr, "execl static program:");
89 #else
90     stop_timing(*countr, "execl dynamic program:");
91 #endif
92     return(0);
93 }
94 
95