SYSPERF - update pipe1, pipe2
[dragonfly.git] / test / sysperf / pipe2.c
CommitLineData
54e0874d
MD
1/*
2 * pipe2.c
3 *
da12ef25 4 * $DragonFly: src/test/sysperf/pipe2.c,v 1.5 2004/04/29 16:05:21 dillon Exp $
54e0874d
MD
5 */
6
7#include "blib.h"
8#include <sys/resource.h>
9
63929605
MD
10#define PAGE_SIZE 4096
11#define PAGE_MASK (PAGE_SIZE - 1)
54e0874d
MD
12
13int
14main(int ac, char **av)
15{
16 long long count = 0;
17 long long max;
18 char c;
19 int j;
63929605 20 int loops;
54e0874d 21 int bytes;
6f1cd508 22 int ppri = 999;
54e0874d
MD
23 int fds[2];
24 char *buf;
25 char *ptr;
63929605 26 char *msg = "datarate";
54e0874d
MD
27
28 if (ac == 1) {
63929605 29 fprintf(stderr, "%s blocksize[k,m] [pipe_writer_pri] [msg]\n", av[0]);
54e0874d
MD
30 exit(1);
31 }
32 bytes = strtol(av[1], &ptr, 0);
33 if (*ptr == 'k' || *ptr == 'K') {
34 bytes *= 1024;
35 } else if (*ptr == 'm' || *ptr == 'M') {
36 bytes *= 1024 * 1024;
37 } else if (*ptr) {
38 fprintf(stderr, "Illegal numerical suffix: %s\n", ptr);
39 exit(1);
40 }
505204a8
MD
41 if (bytes <= 0) {
42 fprintf(stderr, "I can't handle %d sized buffers\n", bytes);
54e0874d
MD
43 exit(1);
44 }
6f1cd508
MD
45 if (ac >= 3)
46 ppri = strtol(av[2], NULL, 0);
63929605
MD
47 if (ac >= 4)
48 msg = av[3];
54e0874d 49
63929605
MD
50 buf = mmap(NULL, bytes * 2 + PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0);
51 if (buf == MAP_FAILED) {
52 perror("mmap/buffer");
54e0874d
MD
53 exit(1);
54 }
55
63929605 56 bzero(buf, bytes * 2 + PAGE_SIZE);
54e0874d
MD
57
58 printf("tests one-way pipe using direct-write buffer\n");
59 if (pipe(fds)) {
60 perror("pipe");
61 exit(1);
62 }
63 if (fork() == 0) {
64 /*
65 * child process
66 */
da12ef25
MD
67 int n;
68 int i;
69
54e0874d 70 close(fds[0]);
63929605 71 buf += (bytes + PAGE_MASK) & ~PAGE_MASK;
da12ef25
MD
72 i = 0;
73 for (;;) {
74 n = read(fds[1], buf + i, bytes - i);
75 if (n <= 0)
76 break;
77 if (n + i == bytes)
78 i = 0;
79 else
80 i += n;
81 }
54e0874d
MD
82 _exit(0);
83 } else {
84 /*
85 * parent process.
86 */
6f1cd508
MD
87 if (ppri != 999) {
88 if (setpriority(PRIO_PROCESS, getpid(), ppri) < 0) {
89 perror("setpriority");
90 exit(1);
91 }
92 }
54e0874d 93 close(fds[1]);
63929605
MD
94
95 /*
96 * Figure out how many loops it takes for 1 second's worth.
97 */
54e0874d 98 start_timing();
63929605
MD
99 for (j = 0; ; ++j) {
100 if (write(fds[0], buf, bytes) != bytes) {
101 perror("write");
102 exit(1);
103 }
104 if ((j & 31) == 0 && stop_timing(0, NULL))
105 break;
106 }
da12ef25 107 loops = j * 2 + 1;
92d10ba7 108 loops *= 2;
63929605
MD
109 usleep(1000000 / 10);
110 start_timing();
111
112 for (j = loops; j; --j) {
54e0874d
MD
113 if (write(fds[0], buf, bytes) != bytes) {
114 perror("write");
115 exit(1);
116 }
117 }
118 close(fds[0]);
63929605
MD
119 while(wait(NULL) >= 0)
120 ;
121 stop_timing(loops, "full duplex pipe / %dK bufs:", bytes / 1024);
122 printf("%s: blkSize %d %5.2f MBytes/sec\n",
123 msg,
124 bytes,
125 (double)loops * bytes * 1000000.0 /
54e0874d
MD
126 (1024.0 * 1024.0 * get_timing()));
127 }
128 return(0);
129}
130