xref: /dflybsd-src/test/sysperf/pipe2.c (revision 21ace8749732bfdb4cb7eedb58e66024da65ff94)
154e0874dSMatthew Dillon /*
254e0874dSMatthew Dillon  * pipe2.c
354e0874dSMatthew Dillon  */
4636b1f09SMatthew Dillon #include <sys/types.h>
5636b1f09SMatthew Dillon #include <sys/wait.h>
654e0874dSMatthew Dillon #include <sys/resource.h>
7636b1f09SMatthew Dillon #include "blib.h"
854e0874dSMatthew Dillon 
963929605SMatthew Dillon #define PAGE_SIZE	4096
1063929605SMatthew Dillon #define PAGE_MASK	(PAGE_SIZE - 1)
1154e0874dSMatthew Dillon 
1254e0874dSMatthew Dillon int
main(int ac,char ** av)1354e0874dSMatthew Dillon main(int ac, char **av)
1454e0874dSMatthew Dillon {
1554e0874dSMatthew Dillon     long long count = 0;
1654e0874dSMatthew Dillon     long long max;
1754e0874dSMatthew Dillon     char c;
1854e0874dSMatthew Dillon     int j;
1963929605SMatthew Dillon     int loops;
2054e0874dSMatthew Dillon     int bytes;
216f1cd508SMatthew Dillon     int ppri = 999;
2254e0874dSMatthew Dillon     int fds[2];
2354e0874dSMatthew Dillon     char *buf;
2454e0874dSMatthew Dillon     char *ptr;
2563929605SMatthew Dillon     char *msg = "datarate";
2654e0874dSMatthew Dillon 
2754e0874dSMatthew Dillon     if (ac == 1) {
2863929605SMatthew Dillon 	fprintf(stderr, "%s blocksize[k,m] [pipe_writer_pri] [msg]\n", av[0]);
2954e0874dSMatthew Dillon 	exit(1);
3054e0874dSMatthew Dillon     }
3154e0874dSMatthew Dillon     bytes = strtol(av[1], &ptr, 0);
3254e0874dSMatthew Dillon     if (*ptr == 'k' || *ptr == 'K') {
3354e0874dSMatthew Dillon 	bytes *= 1024;
3454e0874dSMatthew Dillon     } else if (*ptr == 'm' || *ptr == 'M') {
3554e0874dSMatthew Dillon 	bytes *= 1024 * 1024;
3654e0874dSMatthew Dillon     } else if (*ptr) {
3754e0874dSMatthew Dillon 	fprintf(stderr, "Illegal numerical suffix: %s\n", ptr);
3854e0874dSMatthew Dillon 	exit(1);
3954e0874dSMatthew Dillon     }
40505204a8SMatthew Dillon     if (bytes <= 0) {
41505204a8SMatthew Dillon 	fprintf(stderr, "I can't handle %d sized buffers\n", bytes);
4254e0874dSMatthew Dillon 	exit(1);
4354e0874dSMatthew Dillon     }
446f1cd508SMatthew Dillon     if (ac >= 3)
456f1cd508SMatthew Dillon 	ppri = strtol(av[2], NULL, 0);
4663929605SMatthew Dillon     if (ac >= 4)
4763929605SMatthew Dillon 	msg = av[3];
4854e0874dSMatthew Dillon 
49*21ace874SMatthew Dillon     buf = mmap(NULL, bytes * 2 + PAGE_SIZE,
50*21ace874SMatthew Dillon 	       PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0);
5163929605SMatthew Dillon     if (buf == MAP_FAILED) {
5263929605SMatthew Dillon 	perror("mmap/buffer");
5354e0874dSMatthew Dillon 	exit(1);
5454e0874dSMatthew Dillon     }
5554e0874dSMatthew Dillon 
5663929605SMatthew Dillon     bzero(buf, bytes * 2 + PAGE_SIZE);
5754e0874dSMatthew Dillon 
5854e0874dSMatthew Dillon     printf("tests one-way pipe using direct-write buffer\n");
5954e0874dSMatthew Dillon     if (pipe(fds)) {
6054e0874dSMatthew Dillon 	perror("pipe");
6154e0874dSMatthew Dillon 	exit(1);
6254e0874dSMatthew Dillon     }
6354e0874dSMatthew Dillon     if (fork() == 0) {
6454e0874dSMatthew Dillon 	/*
6554e0874dSMatthew Dillon 	 * child process
6654e0874dSMatthew Dillon 	 */
67da12ef25SMatthew Dillon 	int n;
68da12ef25SMatthew Dillon 	int i;
69da12ef25SMatthew Dillon 
7054e0874dSMatthew Dillon 	close(fds[0]);
7163929605SMatthew Dillon 	buf += (bytes + PAGE_MASK) & ~PAGE_MASK;
72da12ef25SMatthew Dillon 	i = 0;
73da12ef25SMatthew Dillon 	for (;;) {
74da12ef25SMatthew Dillon 	    n = read(fds[1], buf + i, bytes - i);
75da12ef25SMatthew Dillon 	    if (n <= 0)
76da12ef25SMatthew Dillon 		break;
77da12ef25SMatthew Dillon 	    if (n + i == bytes)
78da12ef25SMatthew Dillon 		i = 0;
79da12ef25SMatthew Dillon 	    else
80da12ef25SMatthew Dillon 		i += n;
81da12ef25SMatthew Dillon 	}
8254e0874dSMatthew Dillon 	_exit(0);
8354e0874dSMatthew Dillon     } else {
8454e0874dSMatthew Dillon 	/*
8554e0874dSMatthew Dillon 	 * parent process.
8654e0874dSMatthew Dillon 	 */
876f1cd508SMatthew Dillon 	if (ppri != 999) {
886f1cd508SMatthew Dillon 	    if (setpriority(PRIO_PROCESS, getpid(), ppri) < 0) {
896f1cd508SMatthew Dillon 		perror("setpriority");
906f1cd508SMatthew Dillon 		exit(1);
916f1cd508SMatthew Dillon 	    }
926f1cd508SMatthew Dillon 	}
9354e0874dSMatthew Dillon 	close(fds[1]);
9463929605SMatthew Dillon 
9563929605SMatthew Dillon 	/*
9663929605SMatthew Dillon 	 * Figure out how many loops it takes for 1 second's worth.
9763929605SMatthew Dillon 	 */
9854e0874dSMatthew Dillon 	start_timing();
9963929605SMatthew Dillon 	for (j = 0; ; ++j) {
10063929605SMatthew Dillon 	    if (write(fds[0], buf, bytes) != bytes) {
10163929605SMatthew Dillon 		perror("write");
10263929605SMatthew Dillon 		exit(1);
10363929605SMatthew Dillon 	    }
10463929605SMatthew Dillon 	    if ((j & 31) == 0 && stop_timing(0, NULL))
10563929605SMatthew Dillon 		break;
10663929605SMatthew Dillon 	}
107da12ef25SMatthew Dillon 	loops = j * 2 + 1;
10892d10ba7SMatthew Dillon 	loops *= 2;
10963929605SMatthew Dillon 	usleep(1000000 / 10);
11063929605SMatthew Dillon 	start_timing();
11163929605SMatthew Dillon 
11263929605SMatthew Dillon 	for (j = loops; j; --j) {
11354e0874dSMatthew Dillon 	    if (write(fds[0], buf, bytes) != bytes) {
11454e0874dSMatthew Dillon 		perror("write");
11554e0874dSMatthew Dillon 		exit(1);
11654e0874dSMatthew Dillon 	    }
11754e0874dSMatthew Dillon 	}
11854e0874dSMatthew Dillon 	close(fds[0]);
11963929605SMatthew Dillon 	while(wait(NULL) >= 0)
12063929605SMatthew Dillon 	    ;
12163929605SMatthew Dillon 	stop_timing(loops, "full duplex pipe / %dK bufs:", bytes / 1024);
12263929605SMatthew Dillon 	printf("%s: blkSize %d %5.2f MBytes/sec\n",
12363929605SMatthew Dillon 		msg,
12463929605SMatthew Dillon 		bytes,
12563929605SMatthew Dillon 		(double)loops * bytes * 1000000.0 /
12654e0874dSMatthew Dillon 		(1024.0 * 1024.0 * get_timing()));
12754e0874dSMatthew Dillon     }
12854e0874dSMatthew Dillon     return(0);
12954e0874dSMatthew Dillon }
13054e0874dSMatthew Dillon 
131