Merge branch 'vendor/NCURSES'
[dragonfly.git] / test / syslink / test2.c
1 /*
2  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/test/syslink/test2.c,v 1.1 2007/06/29 17:18:42 dillon Exp $
35  */
36 #include <sys/types.h>
37 #include <sys/syslink.h>
38 #include <sys/syslink_msg.h>
39 #include <sys/mman.h>
40 #include <sys/uio.h>
41 #include <errno.h>
42 #include <string.h>
43 #include <stdio.h>
44
45 static void reader(int fd);
46 static void writer(int fd);
47
48 #define BUFSIZE 65536
49
50 int
51 main(int ac, char **av)
52 {
53         union syslink_info_all info;
54         int fd1;
55         int fd2;
56
57         bzero(&info, sizeof(info));
58         if (syslink(SYSLINK_CMD_NEW, &info.head, sizeof(info)) < 0) {
59                 perror("syslink");
60                 exit(1);
61         }
62         printf("fds %d %d\n", info.cmd_new.fds[0], info.cmd_new.fds[1]);
63         if (fork() == 0) {
64                 reader(info.cmd_new.fds[0]);
65         } else {
66                 writer(info.cmd_new.fds[1]);
67         }
68         while (wait(NULL) > 0)
69                 ;
70         return(0);
71 }
72
73 static
74 void
75 writer(int fd)
76 {
77         union syslink_small_msg cmd;
78         union syslink_small_msg rep;
79         int *dmabuf;
80         int n;
81         int waitforit = 10;
82         off_t bytes = 0;
83
84         dmabuf = mmap(NULL, BUFSIZE, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
85
86         bzero(&cmd, sizeof(cmd));
87         cmd.msg.sm_bytes = sizeof(struct syslink_msg);
88         cmd.msg.sm_proto = SMPROTO_BSDVFS;
89         cmd.msg.sm_msgid = 1;
90         cmd.msg.sm_head.se_cmd = 0 | SE_CMDF_DMAR | SE_CMDF_DMAW;
91         cmd.msg.sm_head.se_bytes = sizeof(cmd.msg.sm_head);
92         dmabuf[0] = 0;
93         dmabuf[4095] = 0;
94         for (;;) {
95                 struct iovec iov[2];
96
97                 iov[0].iov_base = (char *)&cmd;
98                 iov[0].iov_len = cmd.msg.sm_bytes;
99                 iov[1].iov_base = (char *)dmabuf;
100                 iov[1].iov_len = BUFSIZE;
101                 if (dmabuf[0] < 0)
102                         --dmabuf[0];
103                 else
104                         ++dmabuf[0];
105                 --dmabuf[4095];
106                 /*printf("write n = %d dmabuf[0] = %d\n", n, dmabuf[0]);*/
107                 n = writev(fd, iov, 2);
108                 bytes += BUFSIZE;
109                 if (bytes % (1024 * 1024 * 1024) == 0) {
110                         printf("%lld\r", bytes);
111                         fflush(stdout);
112                 }
113                 if (n < 0) {
114                         printf("write error %s\n", strerror(errno));
115                 } else if (waitforit) {
116                         /* fake up a pipeline */
117                         --waitforit;
118                 } else {
119                         n = read(fd, &rep, sizeof(rep));
120                         /*printf("read-reply %d %lld dmabuf[0] = %d\n",
121                                 n, rep.msg.sm_msgid, dmabuf[0]);*/
122                 }
123                 ++cmd.msg.sm_msgid;
124         }
125 }
126
127 static
128 void
129 reader(int fd)
130 {
131         union syslink_small_msg cmd;
132         union syslink_small_msg rep;
133         int *dmabuf;
134         int n;
135
136         dmabuf = mmap(NULL, BUFSIZE, PROT_READ|PROT_WRITE, MAP_ANON, -1, 0);
137
138         bzero(&rep, sizeof(rep));
139         rep.msg.sm_bytes = sizeof(struct syslink_msg);
140         rep.msg.sm_proto = SMPROTO_BSDVFS | SM_PROTO_REPLY;
141         rep.msg.sm_head.se_cmd = SE_CMDF_REPLY;
142         rep.msg.sm_head.se_bytes = sizeof(rep.msg.sm_head);
143
144         for (;;) {
145                 struct iovec iov[2];
146
147                 iov[0].iov_base = (char *)&cmd;
148                 iov[0].iov_len = sizeof(cmd);
149                 iov[1].iov_base = (char *)dmabuf;
150                 iov[1].iov_len = BUFSIZE;
151
152                 n = readv(fd, iov, 2);
153                 if (n < 0 && errno == ENOSPC) {
154                         printf("no space\n");
155                         exit(1);
156                 }
157                 if (n < 0) {
158                         printf("read error %s\n", strerror(errno));
159                 } else {
160                         /*printf("read  n = %d dmabuf[0] = %d [4095] = %d\n", 
161                                 n, dmabuf[0], dmabuf[4095]);*/
162                         rep.msg.sm_msgid = cmd.msg.sm_msgid;
163                         dmabuf[0] = -dmabuf[0];
164                         iov[0].iov_base = (char *)&rep;
165                         iov[0].iov_len = rep.msg.sm_bytes;
166                         /*n = write(fd, &rep, rep.msg.sm_bytes);*/
167                         n = writev(fd, iov, 2);
168                         if (n < 0) {
169                                 printf("reply error %s\n", strerror(errno));
170                         }
171                 }
172                 if (n < 0)
173                         break;
174                 /*sleep(1);*/
175         }
176 }
177