xref: /dflybsd-src/test/sysperf/pipe2.c (revision c5541aee854b0d32586182b733a9ea4d4c92168b)
1 /*
2  * pipe2.c
3  *
4  * $DragonFly: src/test/sysperf/pipe2.c,v 1.3 2004/04/01 01:47:44 dillon Exp $
5  */
6 
7 #include "blib.h"
8 #include <sys/resource.h>
9 
10 #define LOOPS	((int)(1000000LL * 16384 / bytes / divisor))
11 
12 int
13 main(int ac, char **av)
14 {
15     long long count = 0;
16     long long max;
17     char c;
18     int j;
19     int bytes;
20     int divisor;
21     int ppri = 999;
22     int fds[2];
23     char *buf;
24     char *ptr;
25 
26     if (ac == 1) {
27 	fprintf(stderr, "%s blocksize[k,m] [pipe_writer_pri]\n", av[0]);
28 	exit(1);
29     }
30     bytes = strtol(av[1], &ptr, 0);
31     if (*ptr == 'k' || *ptr == 'K') {
32 	bytes *= 1024;
33     } else if (*ptr == 'm' || *ptr == 'M') {
34 	bytes *= 1024 * 1024;
35     } else if (*ptr) {
36 	fprintf(stderr, "Illegal numerical suffix: %s\n", ptr);
37 	exit(1);
38     }
39     if (bytes <= 0) {
40 	fprintf(stderr, "I can't handle %d sized buffers\n", bytes);
41 	exit(1);
42     }
43     if (ac >= 3)
44 	ppri = strtol(av[2], NULL, 0);
45 
46     /*
47      * Tiny block sizes, try to take into account overhead.
48      */
49     if (bytes < 4096)
50 	divisor = 4096 / bytes;
51     else if (bytes > 1024 * 1024)
52 	divisor = 2;
53     else
54 	divisor = 1;
55 
56     if ((buf = malloc(bytes)) == NULL) {
57 	perror("malloc");
58 	exit(1);
59     }
60 
61     bzero(buf, bytes);
62 
63     printf("tests one-way pipe using direct-write buffer\n");
64     if (pipe(fds)) {
65 	perror("pipe");
66 	exit(1);
67     }
68     if (fork() == 0) {
69 	/*
70 	 * child process
71 	 */
72 	close(fds[0]);
73 	while (read(fds[1], buf, bytes) > 0)
74 		;
75 	_exit(0);
76     } else {
77 	/*
78 	 * parent process.
79 	 */
80 	if (ppri != 999) {
81 	    if (setpriority(PRIO_PROCESS, getpid(), ppri) < 0) {
82 		perror("setpriority");
83 		exit(1);
84 	    }
85 	}
86 	close(fds[1]);
87 	write(fds[0], buf, bytes);	/* prime the caches */
88 	start_timing();
89 	for (j = LOOPS; j; --j) {
90 	    if (write(fds[0], buf, bytes) != bytes) {
91 		perror("write");
92 		exit(1);
93 	    }
94 	}
95 	close(fds[0]);
96 	while(wait(NULL) >= 0);
97 	stop_timing(LOOPS, "full duplex pipe / %dK bufs:", bytes / 1024);
98 	printf("datarate: %5.2f MBytes/sec\n",
99 		(double)LOOPS * bytes * 1000000.0 /
100 		(1024.0 * 1024.0 * get_timing()));
101     }
102     return(0);
103 }
104 
105