xref: /dragonfly/test/sysperf/pipe900k.c (revision b5daedd4)
1*b5daedd4SMatthew Dillon /*
2*b5daedd4SMatthew Dillon  * Chained pipe test with 900,000 processes and 900,000 pipes
3*b5daedd4SMatthew Dillon  *
4*b5daedd4SMatthew Dillon  * Requires system w/ 128GB+ of ram, kern.maxproc=4000000 set in
5*b5daedd4SMatthew Dillon  * /boot/loader.conf.  80 second stabilization time after last
6*b5daedd4SMatthew Dillon  * process is forked.
7*b5daedd4SMatthew Dillon  *
8*b5daedd4SMatthew Dillon  * Also test tear-down by ^C'ing the test.
9*b5daedd4SMatthew Dillon  */
10*b5daedd4SMatthew Dillon #include <sys/types.h>
11*b5daedd4SMatthew Dillon #include <sys/wait.h>
12*b5daedd4SMatthew Dillon #include <sys/mman.h>
13*b5daedd4SMatthew Dillon #include <sys/resource.h>
14*b5daedd4SMatthew Dillon #include <errno.h>
15*b5daedd4SMatthew Dillon #include <stdio.h>
16*b5daedd4SMatthew Dillon #include <stdlib.h>
17*b5daedd4SMatthew Dillon #include <unistd.h>
18*b5daedd4SMatthew Dillon #include <string.h>
19*b5daedd4SMatthew Dillon #include <signal.h>
20*b5daedd4SMatthew Dillon 
21*b5daedd4SMatthew Dillon #define COUNT	900000
22*b5daedd4SMatthew Dillon 
23*b5daedd4SMatthew Dillon int
main(int ac,char ** av)24*b5daedd4SMatthew Dillon main(int ac, char **av)
25*b5daedd4SMatthew Dillon {
26*b5daedd4SMatthew Dillon 	int status;
27*b5daedd4SMatthew Dillon 	int i;
28*b5daedd4SMatthew Dillon 	int j;
29*b5daedd4SMatthew Dillon 	int idno;
30*b5daedd4SMatthew Dillon 	int fdsbeg[2];
31*b5daedd4SMatthew Dillon 	int fdsend[2];
32*b5daedd4SMatthew Dillon 	char *path;
33*b5daedd4SMatthew Dillon 	char *id;
34*b5daedd4SMatthew Dillon 	char c;
35*b5daedd4SMatthew Dillon 
36*b5daedd4SMatthew Dillon 	if (ac == 1) {
37*b5daedd4SMatthew Dillon 		pipe(fdsbeg);
38*b5daedd4SMatthew Dillon 		for (i = 0; i < COUNT; i += 100) {
39*b5daedd4SMatthew Dillon #if 0
40*b5daedd4SMatthew Dillon 			asprintf(&path, "cp %s /tmp/x%06d", av[0], i);
41*b5daedd4SMatthew Dillon 			system(path);
42*b5daedd4SMatthew Dillon 			free(path);
43*b5daedd4SMatthew Dillon 			asprintf(&path, "/tmp/x%06d", i);
44*b5daedd4SMatthew Dillon #else
45*b5daedd4SMatthew Dillon 			asprintf(&path, "%s", av[0]);
46*b5daedd4SMatthew Dillon #endif
47*b5daedd4SMatthew Dillon 			asprintf(&id, "%d", i);
48*b5daedd4SMatthew Dillon 			pipe(fdsend);
49*b5daedd4SMatthew Dillon 			if (vfork() == 0) {
50*b5daedd4SMatthew Dillon 				close(fdsbeg[1]);
51*b5daedd4SMatthew Dillon 				dup2(fdsbeg[0], 0);
52*b5daedd4SMatthew Dillon 				dup2(fdsend[1], 1);
53*b5daedd4SMatthew Dillon 				close(fdsbeg[0]);
54*b5daedd4SMatthew Dillon 				close(fdsend[1]);
55*b5daedd4SMatthew Dillon 				execl(path, path, id, NULL);
56*b5daedd4SMatthew Dillon 				_exit(0);
57*b5daedd4SMatthew Dillon 			}
58*b5daedd4SMatthew Dillon 			close(fdsbeg[0]);
59*b5daedd4SMatthew Dillon 			close(fdsend[1]);
60*b5daedd4SMatthew Dillon 			fdsbeg[0] = fdsend[0];
61*b5daedd4SMatthew Dillon 			if (i % 1000 == 0) {
62*b5daedd4SMatthew Dillon 				printf("running %d\r", i);
63*b5daedd4SMatthew Dillon 				fflush(stdout);
64*b5daedd4SMatthew Dillon 			}
65*b5daedd4SMatthew Dillon 			free(path);
66*b5daedd4SMatthew Dillon 			free(id);
67*b5daedd4SMatthew Dillon 		}
68*b5daedd4SMatthew Dillon 	} else {
69*b5daedd4SMatthew Dillon 		idno = strtol(av[1], NULL, 0);
70*b5daedd4SMatthew Dillon 		setpriority(PRIO_PROCESS, 0, 5);
71*b5daedd4SMatthew Dillon 		for (j = 0; j < 100; ++j) {
72*b5daedd4SMatthew Dillon 			if (j != 99)
73*b5daedd4SMatthew Dillon 				pipe(fdsend);
74*b5daedd4SMatthew Dillon 			else
75*b5daedd4SMatthew Dillon 				fdsend[1] = 1;
76*b5daedd4SMatthew Dillon 			if (j == 99 || fork() == 0) {
77*b5daedd4SMatthew Dillon 				if (fdsend[1] != 1) {
78*b5daedd4SMatthew Dillon 					dup2(fdsend[1], 1);
79*b5daedd4SMatthew Dillon 					close(fdsend[1]);
80*b5daedd4SMatthew Dillon 				}
81*b5daedd4SMatthew Dillon 				if (j != 99)
82*b5daedd4SMatthew Dillon 					close(fdsend[0]);
83*b5daedd4SMatthew Dillon 				setpriority(PRIO_PROCESS, 0, 15);
84*b5daedd4SMatthew Dillon 
85*b5daedd4SMatthew Dillon 				while (1) {
86*b5daedd4SMatthew Dillon 					if (read(0, &c, 1) < 0)
87*b5daedd4SMatthew Dillon 						break;
88*b5daedd4SMatthew Dillon #if 0
89*b5daedd4SMatthew Dillon 					fprintf(stderr, "x%d\n", idno + j);
90*b5daedd4SMatthew Dillon 					fflush(stderr);
91*b5daedd4SMatthew Dillon #endif
92*b5daedd4SMatthew Dillon 					write(1, &c, 1);
93*b5daedd4SMatthew Dillon 				}
94*b5daedd4SMatthew Dillon 				_exit(0);
95*b5daedd4SMatthew Dillon 			}
96*b5daedd4SMatthew Dillon 			dup2(fdsend[0], 0);
97*b5daedd4SMatthew Dillon 			close(fdsend[1]);
98*b5daedd4SMatthew Dillon 			close(fdsend[0]);
99*b5daedd4SMatthew Dillon 		}
100*b5daedd4SMatthew Dillon 		while (wait3(NULL, 0, NULL) >= 0 || errno == EINTR)
101*b5daedd4SMatthew Dillon 			;
102*b5daedd4SMatthew Dillon 		_exit(0);
103*b5daedd4SMatthew Dillon 	}
104*b5daedd4SMatthew Dillon 	printf("running %d\n", i);
105*b5daedd4SMatthew Dillon 	for (;;) {
106*b5daedd4SMatthew Dillon 		write(fdsbeg[1], &c, 1);
107*b5daedd4SMatthew Dillon 		if (read(fdsend[0], &c, 1) < 0)
108*b5daedd4SMatthew Dillon 			break;
109*b5daedd4SMatthew Dillon 		write(2, "x", 1);
110*b5daedd4SMatthew Dillon 	}
111*b5daedd4SMatthew Dillon 	return 0;
112*b5daedd4SMatthew Dillon }
113