splice: use fcntl(pfd[1], F_SETPIPE_SZ, 1048576); in splice2.c
[metze/misc/junkcode.git] / splice / splice2.c
1 #define _GNU_SOURCE
2 #include <unistd.h>
3 #include <fcntl.h>
4 #include <sys/uio.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <stdint.h>
10 #include <sys/socket.h>
11 #include <sys/sendfile.h>
12
13 int main(int argc, const char * const *argv)
14 {
15         int pfd[2];
16         int zfd;
17         int nfd;
18         size_t i;
19         size_t total_size;
20         size_t current_size = 0;
21         uint8_t *buffer = NULL;;
22         size_t buffer_size;
23
24         if (argc != 4) {
25                 printf("%s: usage %s <'splice'|'rw'> <total_size> <buffer_size>\n",
26                         argv[0], argv[0]);
27                 exit(1);
28         }
29
30         total_size = strtoull(argv[2], NULL, 0);
31         buffer_size = strtoull(argv[3], NULL, 0);
32
33         if (strcmp(argv[1], "splice") == 0) {
34                 pipe(pfd);
35                 fcntl(pfd[1], F_SETPIPE_SZ, 1048576);
36         } else {
37                 buffer = malloc(buffer_size);
38         }
39
40         zfd = open("/dev/zero", O_RDONLY);
41         nfd = open("/dev/null", O_WRONLY);
42
43         while (current_size < total_size) {
44                 ssize_t zret;
45                 ssize_t nret;
46
47                 errno = 0;
48                 if (buffer) {
49                         zret = read(zfd, buffer, buffer_size);
50                 } else {
51                         zret = splice(zfd, NULL, pfd[1], NULL,
52                                       buffer_size, SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
53                 }
54                 if (zret < 0) {
55                         printf("%d: zret[%lld] errno[%d/%s]\n", __LINE__,
56                                 (long long int)zret, errno, strerror(errno));
57                         break;
58                 }
59
60                 errno = 0;
61                 if (buffer) {
62                         nret = write(nfd, buffer, zret);
63                 } else {
64                         nret = splice(pfd[0], NULL, nfd, NULL,
65                                       zret, SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
66                 }
67                 if (nret < 0) {
68                         printf("%d: nret[%lld] errno[%d/%s]\n", __LINE__,
69                                 (long long int)nret, errno, strerror(errno));
70                         break;
71                 }
72                 if (nret != zret) {
73                         printf("%d: zret[%lld] nret[%lld]\n", __LINE__,
74                                 (long long int)zret,
75                                 (long long int)nret);
76                         break;
77                 }
78
79                 current_size += nret;
80                 i++;
81         }
82
83         printf("%s: i[%lld] total_size[%lld] buffer_size[%lld => %lld]\n",
84                 argv[1],
85                 (long long int)i,
86                 (long long int)total_size,
87                 (long long int)buffer_size,
88                 (long long int)(total_size/i));
89         return 0;
90 }